2 * Kernel-based Virtual Machine driver for Linux
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
17 #include <linux/kvm_host.h>
21 #include "kvm_cache_regs.h"
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/vmalloc.h>
27 #include <linux/highmem.h>
28 #include <linux/sched.h>
29 #include <linux/ftrace_event.h>
30 #include <linux/slab.h>
32 #include <asm/tlbflush.h>
34 #include <asm/kvm_para.h>
36 #include <asm/virtext.h>
39 #define __ex(x) __kvm_handle_fault_on_reboot(x)
41 MODULE_AUTHOR("Qumranet");
42 MODULE_LICENSE("GPL");
44 #define IOPM_ALLOC_ORDER 2
45 #define MSRPM_ALLOC_ORDER 1
47 #define SEG_TYPE_LDT 2
48 #define SEG_TYPE_BUSY_TSS16 3
50 #define SVM_FEATURE_NPT (1 << 0)
51 #define SVM_FEATURE_LBRV (1 << 1)
52 #define SVM_FEATURE_SVML (1 << 2)
53 #define SVM_FEATURE_NRIP (1 << 3)
54 #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
56 #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
57 #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
58 #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
60 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
62 static bool erratum_383_found __read_mostly;
64 static const u32 host_save_user_msrs[] = {
66 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
69 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
72 #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
82 /* These are the merged vectors */
85 /* gpa pointers to the real vectors */
89 /* A VMEXIT is required but not yet emulated */
93 * If we vmexit during an instruction emulation we need this to restore
94 * the l1 guest rip after the emulation
96 unsigned long vmexit_rip;
97 unsigned long vmexit_rsp;
98 unsigned long vmexit_rax;
100 /* cache for intercepts of the guest */
101 u16 intercept_cr_read;
102 u16 intercept_cr_write;
103 u16 intercept_dr_read;
104 u16 intercept_dr_write;
105 u32 intercept_exceptions;
108 /* Nested Paging related state */
112 #define MSRPM_OFFSETS 16
113 static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
116 struct kvm_vcpu vcpu;
118 unsigned long vmcb_pa;
119 struct svm_cpu_data *svm_data;
120 uint64_t asid_generation;
121 uint64_t sysenter_esp;
122 uint64_t sysenter_eip;
126 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
133 struct nested_state nested;
137 unsigned int3_injected;
138 unsigned long int3_rip;
142 #define MSR_INVALID 0xffffffffU
144 static struct svm_direct_access_msrs {
145 u32 index; /* Index of the MSR */
146 bool always; /* True if intercept is always on */
147 } direct_access_msrs[] = {
148 { .index = MSR_STAR, .always = true },
149 { .index = MSR_IA32_SYSENTER_CS, .always = true },
151 { .index = MSR_GS_BASE, .always = true },
152 { .index = MSR_FS_BASE, .always = true },
153 { .index = MSR_KERNEL_GS_BASE, .always = true },
154 { .index = MSR_LSTAR, .always = true },
155 { .index = MSR_CSTAR, .always = true },
156 { .index = MSR_SYSCALL_MASK, .always = true },
158 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
159 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
160 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
161 { .index = MSR_IA32_LASTINTTOIP, .always = false },
162 { .index = MSR_INVALID, .always = false },
165 /* enable NPT for AMD64 and X86 with PAE */
166 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
167 static bool npt_enabled = true;
169 static bool npt_enabled;
173 module_param(npt, int, S_IRUGO);
175 static int nested = 1;
176 module_param(nested, int, S_IRUGO);
178 static void svm_flush_tlb(struct kvm_vcpu *vcpu);
179 static void svm_complete_interrupts(struct vcpu_svm *svm);
181 static int nested_svm_exit_handled(struct vcpu_svm *svm);
182 static int nested_svm_intercept(struct vcpu_svm *svm);
183 static int nested_svm_vmexit(struct vcpu_svm *svm);
184 static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
185 bool has_error_code, u32 error_code);
187 static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
189 return container_of(vcpu, struct vcpu_svm, vcpu);
192 static inline bool is_nested(struct vcpu_svm *svm)
194 return svm->nested.vmcb;
197 static inline void enable_gif(struct vcpu_svm *svm)
199 svm->vcpu.arch.hflags |= HF_GIF_MASK;
202 static inline void disable_gif(struct vcpu_svm *svm)
204 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
207 static inline bool gif_set(struct vcpu_svm *svm)
209 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
212 static unsigned long iopm_base;
214 struct kvm_ldttss_desc {
217 unsigned base1:8, type:5, dpl:2, p:1;
218 unsigned limit1:4, zero0:3, g:1, base2:8;
221 } __attribute__((packed));
223 struct svm_cpu_data {
229 struct kvm_ldttss_desc *tss_desc;
231 struct page *save_area;
234 static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
235 static uint32_t svm_features;
237 struct svm_init_data {
242 static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
244 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
245 #define MSRS_RANGE_SIZE 2048
246 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
248 static u32 svm_msrpm_offset(u32 msr)
253 for (i = 0; i < NUM_MSR_MAPS; i++) {
254 if (msr < msrpm_ranges[i] ||
255 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
258 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
259 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
261 /* Now we have the u8 offset - but need the u32 offset */
265 /* MSR not in any range */
269 #define MAX_INST_SIZE 15
271 static inline u32 svm_has(u32 feat)
273 return svm_features & feat;
276 static inline void clgi(void)
278 asm volatile (__ex(SVM_CLGI));
281 static inline void stgi(void)
283 asm volatile (__ex(SVM_STGI));
286 static inline void invlpga(unsigned long addr, u32 asid)
288 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
291 static inline void force_new_asid(struct kvm_vcpu *vcpu)
293 to_svm(vcpu)->asid_generation--;
296 static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
298 force_new_asid(vcpu);
301 static int get_npt_level(void)
304 return PT64_ROOT_LEVEL;
306 return PT32E_ROOT_LEVEL;
310 static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
312 vcpu->arch.efer = efer;
313 if (!npt_enabled && !(efer & EFER_LMA))
316 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
319 static int is_external_interrupt(u32 info)
321 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
322 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
325 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
327 struct vcpu_svm *svm = to_svm(vcpu);
330 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
331 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
335 static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
337 struct vcpu_svm *svm = to_svm(vcpu);
340 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
342 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
346 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
348 struct vcpu_svm *svm = to_svm(vcpu);
350 if (svm->vmcb->control.next_rip != 0)
351 svm->next_rip = svm->vmcb->control.next_rip;
353 if (!svm->next_rip) {
354 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
356 printk(KERN_DEBUG "%s: NOP\n", __func__);
359 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
360 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
361 __func__, kvm_rip_read(vcpu), svm->next_rip);
363 kvm_rip_write(vcpu, svm->next_rip);
364 svm_set_interrupt_shadow(vcpu, 0);
367 static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
368 bool has_error_code, u32 error_code,
371 struct vcpu_svm *svm = to_svm(vcpu);
374 * If we are within a nested VM we'd better #VMEXIT and let the guest
375 * handle the exception
378 nested_svm_check_exception(svm, nr, has_error_code, error_code))
381 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
382 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
385 * For guest debugging where we have to reinject #BP if some
386 * INT3 is guest-owned:
387 * Emulate nRIP by moving RIP forward. Will fail if injection
388 * raises a fault that is not intercepted. Still better than
389 * failing in all cases.
391 skip_emulated_instruction(&svm->vcpu);
392 rip = kvm_rip_read(&svm->vcpu);
393 svm->int3_rip = rip + svm->vmcb->save.cs.base;
394 svm->int3_injected = rip - old_rip;
397 svm->vmcb->control.event_inj = nr
399 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
400 | SVM_EVTINJ_TYPE_EXEPT;
401 svm->vmcb->control.event_inj_err = error_code;
404 static void svm_init_erratum_383(void)
410 if (!cpu_has_amd_erratum(amd_erratum_383))
413 /* Use _safe variants to not break nested virtualization */
414 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
420 low = lower_32_bits(val);
421 high = upper_32_bits(val);
423 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
425 erratum_383_found = true;
428 static int has_svm(void)
432 if (!cpu_has_svm(&msg)) {
433 printk(KERN_INFO "has_svm: %s\n", msg);
440 static void svm_hardware_disable(void *garbage)
445 static int svm_hardware_enable(void *garbage)
448 struct svm_cpu_data *sd;
450 struct desc_ptr gdt_descr;
451 struct desc_struct *gdt;
452 int me = raw_smp_processor_id();
454 rdmsrl(MSR_EFER, efer);
455 if (efer & EFER_SVME)
459 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
463 sd = per_cpu(svm_data, me);
466 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
471 sd->asid_generation = 1;
472 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
473 sd->next_asid = sd->max_asid + 1;
475 native_store_gdt(&gdt_descr);
476 gdt = (struct desc_struct *)gdt_descr.address;
477 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
479 wrmsrl(MSR_EFER, efer | EFER_SVME);
481 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
483 svm_init_erratum_383();
488 static void svm_cpu_uninit(int cpu)
490 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
495 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
496 __free_page(sd->save_area);
500 static int svm_cpu_init(int cpu)
502 struct svm_cpu_data *sd;
505 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
509 sd->save_area = alloc_page(GFP_KERNEL);
514 per_cpu(svm_data, cpu) = sd;
524 static bool valid_msr_intercept(u32 index)
528 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
529 if (direct_access_msrs[i].index == index)
535 static void set_msr_interception(u32 *msrpm, unsigned msr,
538 u8 bit_read, bit_write;
543 * If this warning triggers extend the direct_access_msrs list at the
544 * beginning of the file
546 WARN_ON(!valid_msr_intercept(msr));
548 offset = svm_msrpm_offset(msr);
549 bit_read = 2 * (msr & 0x0f);
550 bit_write = 2 * (msr & 0x0f) + 1;
553 BUG_ON(offset == MSR_INVALID);
555 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
556 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
561 static void svm_vcpu_init_msrpm(u32 *msrpm)
565 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
567 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
568 if (!direct_access_msrs[i].always)
571 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
575 static void add_msr_offset(u32 offset)
579 for (i = 0; i < MSRPM_OFFSETS; ++i) {
581 /* Offset already in list? */
582 if (msrpm_offsets[i] == offset)
585 /* Slot used by another offset? */
586 if (msrpm_offsets[i] != MSR_INVALID)
589 /* Add offset to list */
590 msrpm_offsets[i] = offset;
596 * If this BUG triggers the msrpm_offsets table has an overflow. Just
597 * increase MSRPM_OFFSETS in this case.
602 static void init_msrpm_offsets(void)
606 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
608 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
611 offset = svm_msrpm_offset(direct_access_msrs[i].index);
612 BUG_ON(offset == MSR_INVALID);
614 add_msr_offset(offset);
618 static void svm_enable_lbrv(struct vcpu_svm *svm)
620 u32 *msrpm = svm->msrpm;
622 svm->vmcb->control.lbr_ctl = 1;
623 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
624 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
625 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
626 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
629 static void svm_disable_lbrv(struct vcpu_svm *svm)
631 u32 *msrpm = svm->msrpm;
633 svm->vmcb->control.lbr_ctl = 0;
634 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
635 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
636 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
637 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
640 static __init int svm_hardware_setup(void)
643 struct page *iopm_pages;
647 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
652 iopm_va = page_address(iopm_pages);
653 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
654 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
656 init_msrpm_offsets();
658 if (boot_cpu_has(X86_FEATURE_NX))
659 kvm_enable_efer_bits(EFER_NX);
661 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
662 kvm_enable_efer_bits(EFER_FFXSR);
665 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
666 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
669 for_each_possible_cpu(cpu) {
670 r = svm_cpu_init(cpu);
675 svm_features = cpuid_edx(SVM_CPUID_FUNC);
677 if (!svm_has(SVM_FEATURE_NPT))
680 if (npt_enabled && !npt) {
681 printk(KERN_INFO "kvm: Nested Paging disabled\n");
686 printk(KERN_INFO "kvm: Nested Paging enabled\n");
694 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
699 static __exit void svm_hardware_unsetup(void)
703 for_each_possible_cpu(cpu)
706 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
710 static void init_seg(struct vmcb_seg *seg)
713 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
714 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
719 static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
722 seg->attrib = SVM_SELECTOR_P_MASK | type;
727 static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
729 struct vcpu_svm *svm = to_svm(vcpu);
730 u64 g_tsc_offset = 0;
732 if (is_nested(svm)) {
733 g_tsc_offset = svm->vmcb->control.tsc_offset -
734 svm->nested.hsave->control.tsc_offset;
735 svm->nested.hsave->control.tsc_offset = offset;
738 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
741 static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
743 struct vcpu_svm *svm = to_svm(vcpu);
745 svm->vmcb->control.tsc_offset += adjustment;
747 svm->nested.hsave->control.tsc_offset += adjustment;
750 static void init_vmcb(struct vcpu_svm *svm)
752 struct vmcb_control_area *control = &svm->vmcb->control;
753 struct vmcb_save_area *save = &svm->vmcb->save;
755 svm->vcpu.fpu_active = 1;
757 control->intercept_cr_read = INTERCEPT_CR0_MASK |
761 control->intercept_cr_write = INTERCEPT_CR0_MASK |
766 control->intercept_dr_read = INTERCEPT_DR0_MASK |
775 control->intercept_dr_write = INTERCEPT_DR0_MASK |
784 control->intercept_exceptions = (1 << PF_VECTOR) |
789 control->intercept = (1ULL << INTERCEPT_INTR) |
790 (1ULL << INTERCEPT_NMI) |
791 (1ULL << INTERCEPT_SMI) |
792 (1ULL << INTERCEPT_SELECTIVE_CR0) |
793 (1ULL << INTERCEPT_CPUID) |
794 (1ULL << INTERCEPT_INVD) |
795 (1ULL << INTERCEPT_HLT) |
796 (1ULL << INTERCEPT_INVLPG) |
797 (1ULL << INTERCEPT_INVLPGA) |
798 (1ULL << INTERCEPT_IOIO_PROT) |
799 (1ULL << INTERCEPT_MSR_PROT) |
800 (1ULL << INTERCEPT_TASK_SWITCH) |
801 (1ULL << INTERCEPT_SHUTDOWN) |
802 (1ULL << INTERCEPT_VMRUN) |
803 (1ULL << INTERCEPT_VMMCALL) |
804 (1ULL << INTERCEPT_VMLOAD) |
805 (1ULL << INTERCEPT_VMSAVE) |
806 (1ULL << INTERCEPT_STGI) |
807 (1ULL << INTERCEPT_CLGI) |
808 (1ULL << INTERCEPT_SKINIT) |
809 (1ULL << INTERCEPT_WBINVD) |
810 (1ULL << INTERCEPT_MONITOR) |
811 (1ULL << INTERCEPT_MWAIT);
813 control->iopm_base_pa = iopm_base;
814 control->msrpm_base_pa = __pa(svm->msrpm);
815 control->int_ctl = V_INTR_MASKING_MASK;
823 save->cs.selector = 0xf000;
824 /* Executable/Readable Code Segment */
825 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
826 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
827 save->cs.limit = 0xffff;
829 * cs.base should really be 0xffff0000, but vmx can't handle that, so
830 * be consistent with it.
832 * Replace when we have real mode working for vmx.
834 save->cs.base = 0xf0000;
836 save->gdtr.limit = 0xffff;
837 save->idtr.limit = 0xffff;
839 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
840 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
842 svm_set_efer(&svm->vcpu, 0);
843 save->dr6 = 0xffff0ff0;
846 save->rip = 0x0000fff0;
847 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
850 * This is the guest-visible cr0 value.
851 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
853 svm->vcpu.arch.cr0 = 0;
854 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
856 save->cr4 = X86_CR4_PAE;
860 /* Setup VMCB for Nested Paging */
861 control->nested_ctl = 1;
862 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
863 (1ULL << INTERCEPT_INVLPG));
864 control->intercept_exceptions &= ~(1 << PF_VECTOR);
865 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
866 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
867 save->g_pat = 0x0007040600070406ULL;
871 force_new_asid(&svm->vcpu);
873 svm->nested.vmcb = 0;
874 svm->vcpu.arch.hflags = 0;
876 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
877 control->pause_filter_count = 3000;
878 control->intercept |= (1ULL << INTERCEPT_PAUSE);
884 static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
886 struct vcpu_svm *svm = to_svm(vcpu);
890 if (!kvm_vcpu_is_bsp(vcpu)) {
891 kvm_rip_write(vcpu, 0);
892 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
893 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
895 vcpu->arch.regs_avail = ~0;
896 vcpu->arch.regs_dirty = ~0;
901 static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
903 struct vcpu_svm *svm;
905 struct page *msrpm_pages;
906 struct page *hsave_page;
907 struct page *nested_msrpm_pages;
910 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
916 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
921 page = alloc_page(GFP_KERNEL);
925 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
929 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
930 if (!nested_msrpm_pages)
933 hsave_page = alloc_page(GFP_KERNEL);
937 svm->nested.hsave = page_address(hsave_page);
939 svm->msrpm = page_address(msrpm_pages);
940 svm_vcpu_init_msrpm(svm->msrpm);
942 svm->nested.msrpm = page_address(nested_msrpm_pages);
943 svm_vcpu_init_msrpm(svm->nested.msrpm);
945 svm->vmcb = page_address(page);
946 clear_page(svm->vmcb);
947 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
948 svm->asid_generation = 0;
950 kvm_write_tsc(&svm->vcpu, 0);
952 err = fx_init(&svm->vcpu);
956 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
957 if (kvm_vcpu_is_bsp(&svm->vcpu))
958 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
963 __free_page(hsave_page);
965 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
967 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
971 kvm_vcpu_uninit(&svm->vcpu);
973 kmem_cache_free(kvm_vcpu_cache, svm);
978 static void svm_free_vcpu(struct kvm_vcpu *vcpu)
980 struct vcpu_svm *svm = to_svm(vcpu);
982 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
983 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
984 __free_page(virt_to_page(svm->nested.hsave));
985 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
986 kvm_vcpu_uninit(vcpu);
987 kmem_cache_free(kvm_vcpu_cache, svm);
990 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
992 struct vcpu_svm *svm = to_svm(vcpu);
995 if (unlikely(cpu != vcpu->cpu)) {
996 svm->asid_generation = 0;
999 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
1000 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
1003 static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1005 struct vcpu_svm *svm = to_svm(vcpu);
1008 ++vcpu->stat.host_state_reload;
1009 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
1010 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
1013 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1015 return to_svm(vcpu)->vmcb->save.rflags;
1018 static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1020 to_svm(vcpu)->vmcb->save.rflags = rflags;
1023 static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1026 case VCPU_EXREG_PDPTR:
1027 BUG_ON(!npt_enabled);
1028 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
1035 static void svm_set_vintr(struct vcpu_svm *svm)
1037 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
1040 static void svm_clear_vintr(struct vcpu_svm *svm)
1042 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1045 static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1047 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1050 case VCPU_SREG_CS: return &save->cs;
1051 case VCPU_SREG_DS: return &save->ds;
1052 case VCPU_SREG_ES: return &save->es;
1053 case VCPU_SREG_FS: return &save->fs;
1054 case VCPU_SREG_GS: return &save->gs;
1055 case VCPU_SREG_SS: return &save->ss;
1056 case VCPU_SREG_TR: return &save->tr;
1057 case VCPU_SREG_LDTR: return &save->ldtr;
1063 static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1065 struct vmcb_seg *s = svm_seg(vcpu, seg);
1070 static void svm_get_segment(struct kvm_vcpu *vcpu,
1071 struct kvm_segment *var, int seg)
1073 struct vmcb_seg *s = svm_seg(vcpu, seg);
1075 var->base = s->base;
1076 var->limit = s->limit;
1077 var->selector = s->selector;
1078 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1079 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1080 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1081 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1082 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1083 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1084 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1085 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
1088 * AMD's VMCB does not have an explicit unusable field, so emulate it
1089 * for cross vendor migration purposes by "not present"
1091 var->unusable = !var->present || (var->type == 0);
1096 * SVM always stores 0 for the 'G' bit in the CS selector in
1097 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1098 * Intel's VMENTRY has a check on the 'G' bit.
1100 var->g = s->limit > 0xfffff;
1104 * Work around a bug where the busy flag in the tr selector
1114 * The accessed bit must always be set in the segment
1115 * descriptor cache, although it can be cleared in the
1116 * descriptor, the cached bit always remains at 1. Since
1117 * Intel has a check on this, set it here to support
1118 * cross-vendor migration.
1125 * On AMD CPUs sometimes the DB bit in the segment
1126 * descriptor is left as 1, although the whole segment has
1127 * been made unusable. Clear it here to pass an Intel VMX
1128 * entry check when cross vendor migrating.
1136 static int svm_get_cpl(struct kvm_vcpu *vcpu)
1138 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1143 static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1145 struct vcpu_svm *svm = to_svm(vcpu);
1147 dt->size = svm->vmcb->save.idtr.limit;
1148 dt->address = svm->vmcb->save.idtr.base;
1151 static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1153 struct vcpu_svm *svm = to_svm(vcpu);
1155 svm->vmcb->save.idtr.limit = dt->size;
1156 svm->vmcb->save.idtr.base = dt->address ;
1159 static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1161 struct vcpu_svm *svm = to_svm(vcpu);
1163 dt->size = svm->vmcb->save.gdtr.limit;
1164 dt->address = svm->vmcb->save.gdtr.base;
1167 static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1169 struct vcpu_svm *svm = to_svm(vcpu);
1171 svm->vmcb->save.gdtr.limit = dt->size;
1172 svm->vmcb->save.gdtr.base = dt->address ;
1175 static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1179 static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
1183 static void update_cr0_intercept(struct vcpu_svm *svm)
1185 struct vmcb *vmcb = svm->vmcb;
1186 ulong gcr0 = svm->vcpu.arch.cr0;
1187 u64 *hcr0 = &svm->vmcb->save.cr0;
1189 if (!svm->vcpu.fpu_active)
1190 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1192 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1193 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1196 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
1197 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1198 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1199 if (is_nested(svm)) {
1200 struct vmcb *hsave = svm->nested.hsave;
1202 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1203 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1204 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1205 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1208 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1209 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1210 if (is_nested(svm)) {
1211 struct vmcb *hsave = svm->nested.hsave;
1213 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1214 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1219 static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1221 struct vcpu_svm *svm = to_svm(vcpu);
1223 if (is_nested(svm)) {
1225 * We are here because we run in nested mode, the host kvm
1226 * intercepts cr0 writes but the l1 hypervisor does not.
1227 * But the L1 hypervisor may intercept selective cr0 writes.
1228 * This needs to be checked here.
1230 unsigned long old, new;
1232 /* Remove bits that would trigger a real cr0 write intercept */
1233 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1234 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1237 /* cr0 write with ts and mp unchanged */
1238 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1239 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1240 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1241 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1242 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
1248 #ifdef CONFIG_X86_64
1249 if (vcpu->arch.efer & EFER_LME) {
1250 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
1251 vcpu->arch.efer |= EFER_LMA;
1252 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
1255 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
1256 vcpu->arch.efer &= ~EFER_LMA;
1257 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
1261 vcpu->arch.cr0 = cr0;
1264 cr0 |= X86_CR0_PG | X86_CR0_WP;
1266 if (!vcpu->fpu_active)
1269 * re-enable caching here because the QEMU bios
1270 * does not do it - this results in some delay at
1273 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1274 svm->vmcb->save.cr0 = cr0;
1275 update_cr0_intercept(svm);
1278 static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1280 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
1281 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1283 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1284 force_new_asid(vcpu);
1286 vcpu->arch.cr4 = cr4;
1289 cr4 |= host_cr4_mce;
1290 to_svm(vcpu)->vmcb->save.cr4 = cr4;
1293 static void svm_set_segment(struct kvm_vcpu *vcpu,
1294 struct kvm_segment *var, int seg)
1296 struct vcpu_svm *svm = to_svm(vcpu);
1297 struct vmcb_seg *s = svm_seg(vcpu, seg);
1299 s->base = var->base;
1300 s->limit = var->limit;
1301 s->selector = var->selector;
1305 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1306 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1307 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1308 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1309 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1310 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1311 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1312 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1314 if (seg == VCPU_SREG_CS)
1316 = (svm->vmcb->save.cs.attrib
1317 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1321 static void update_db_intercept(struct kvm_vcpu *vcpu)
1323 struct vcpu_svm *svm = to_svm(vcpu);
1325 svm->vmcb->control.intercept_exceptions &=
1326 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
1328 if (svm->nmi_singlestep)
1329 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1331 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1332 if (vcpu->guest_debug &
1333 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1334 svm->vmcb->control.intercept_exceptions |=
1336 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1337 svm->vmcb->control.intercept_exceptions |=
1340 vcpu->guest_debug = 0;
1343 static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
1345 struct vcpu_svm *svm = to_svm(vcpu);
1347 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1348 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1350 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1352 update_db_intercept(vcpu);
1355 static void load_host_msrs(struct kvm_vcpu *vcpu)
1357 #ifdef CONFIG_X86_64
1358 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1362 static void save_host_msrs(struct kvm_vcpu *vcpu)
1364 #ifdef CONFIG_X86_64
1365 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1369 static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
1371 if (sd->next_asid > sd->max_asid) {
1372 ++sd->asid_generation;
1374 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
1377 svm->asid_generation = sd->asid_generation;
1378 svm->vmcb->control.asid = sd->next_asid++;
1381 static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
1383 struct vcpu_svm *svm = to_svm(vcpu);
1385 svm->vmcb->save.dr7 = value;
1388 static int pf_interception(struct vcpu_svm *svm)
1390 u64 fault_address = svm->vmcb->control.exit_info_2;
1394 switch (svm->apf_reason) {
1396 error_code = svm->vmcb->control.exit_info_1;
1398 trace_kvm_page_fault(fault_address, error_code);
1399 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1400 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1401 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1403 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1404 svm->apf_reason = 0;
1405 local_irq_disable();
1406 kvm_async_pf_task_wait(fault_address);
1409 case KVM_PV_REASON_PAGE_READY:
1410 svm->apf_reason = 0;
1411 local_irq_disable();
1412 kvm_async_pf_task_wake(fault_address);
1419 static int db_interception(struct vcpu_svm *svm)
1421 struct kvm_run *kvm_run = svm->vcpu.run;
1423 if (!(svm->vcpu.guest_debug &
1424 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
1425 !svm->nmi_singlestep) {
1426 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1430 if (svm->nmi_singlestep) {
1431 svm->nmi_singlestep = false;
1432 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1433 svm->vmcb->save.rflags &=
1434 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1435 update_db_intercept(&svm->vcpu);
1438 if (svm->vcpu.guest_debug &
1439 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
1440 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1441 kvm_run->debug.arch.pc =
1442 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1443 kvm_run->debug.arch.exception = DB_VECTOR;
1450 static int bp_interception(struct vcpu_svm *svm)
1452 struct kvm_run *kvm_run = svm->vcpu.run;
1454 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1455 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1456 kvm_run->debug.arch.exception = BP_VECTOR;
1460 static int ud_interception(struct vcpu_svm *svm)
1464 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
1465 if (er != EMULATE_DONE)
1466 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1470 static void svm_fpu_activate(struct kvm_vcpu *vcpu)
1472 struct vcpu_svm *svm = to_svm(vcpu);
1475 if (is_nested(svm)) {
1478 h_excp = svm->nested.hsave->control.intercept_exceptions;
1479 n_excp = svm->nested.intercept_exceptions;
1480 h_excp &= ~(1 << NM_VECTOR);
1481 excp = h_excp | n_excp;
1483 excp = svm->vmcb->control.intercept_exceptions;
1484 excp &= ~(1 << NM_VECTOR);
1487 svm->vmcb->control.intercept_exceptions = excp;
1489 svm->vcpu.fpu_active = 1;
1490 update_cr0_intercept(svm);
1493 static int nm_interception(struct vcpu_svm *svm)
1495 svm_fpu_activate(&svm->vcpu);
1499 static bool is_erratum_383(void)
1504 if (!erratum_383_found)
1507 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1511 /* Bit 62 may or may not be set for this mce */
1512 value &= ~(1ULL << 62);
1514 if (value != 0xb600000000010015ULL)
1517 /* Clear MCi_STATUS registers */
1518 for (i = 0; i < 6; ++i)
1519 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1521 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1525 value &= ~(1ULL << 2);
1526 low = lower_32_bits(value);
1527 high = upper_32_bits(value);
1529 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1532 /* Flush tlb to evict multi-match entries */
1538 static void svm_handle_mce(struct vcpu_svm *svm)
1540 if (is_erratum_383()) {
1542 * Erratum 383 triggered. Guest state is corrupt so kill the
1545 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1547 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
1553 * On an #MC intercept the MCE handler is not called automatically in
1554 * the host. So do it by hand here.
1558 /* not sure if we ever come back to this point */
1563 static int mc_interception(struct vcpu_svm *svm)
1568 static int shutdown_interception(struct vcpu_svm *svm)
1570 struct kvm_run *kvm_run = svm->vcpu.run;
1573 * VMCB is undefined after a SHUTDOWN intercept
1574 * so reinitialize it.
1576 clear_page(svm->vmcb);
1579 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1583 static int io_interception(struct vcpu_svm *svm)
1585 struct kvm_vcpu *vcpu = &svm->vcpu;
1586 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
1587 int size, in, string;
1590 ++svm->vcpu.stat.io_exits;
1591 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1592 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1594 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
1596 port = io_info >> 16;
1597 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
1598 svm->next_rip = svm->vmcb->control.exit_info_2;
1599 skip_emulated_instruction(&svm->vcpu);
1601 return kvm_fast_pio_out(vcpu, size, port);
1604 static int nmi_interception(struct vcpu_svm *svm)
1609 static int intr_interception(struct vcpu_svm *svm)
1611 ++svm->vcpu.stat.irq_exits;
1615 static int nop_on_interception(struct vcpu_svm *svm)
1620 static int halt_interception(struct vcpu_svm *svm)
1622 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
1623 skip_emulated_instruction(&svm->vcpu);
1624 return kvm_emulate_halt(&svm->vcpu);
1627 static int vmmcall_interception(struct vcpu_svm *svm)
1629 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1630 skip_emulated_instruction(&svm->vcpu);
1631 kvm_emulate_hypercall(&svm->vcpu);
1635 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1637 struct vcpu_svm *svm = to_svm(vcpu);
1639 return svm->nested.nested_cr3;
1642 static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1645 struct vcpu_svm *svm = to_svm(vcpu);
1647 svm->vmcb->control.nested_cr3 = root;
1648 force_new_asid(vcpu);
1651 static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu)
1653 struct vcpu_svm *svm = to_svm(vcpu);
1655 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1656 svm->vmcb->control.exit_code_hi = 0;
1657 svm->vmcb->control.exit_info_1 = vcpu->arch.fault.error_code;
1658 svm->vmcb->control.exit_info_2 = vcpu->arch.fault.address;
1660 nested_svm_vmexit(svm);
1663 static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1667 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1669 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1670 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1671 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1672 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1673 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1678 static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1680 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1683 static int nested_svm_check_permissions(struct vcpu_svm *svm)
1685 if (!(svm->vcpu.arch.efer & EFER_SVME)
1686 || !is_paging(&svm->vcpu)) {
1687 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1691 if (svm->vmcb->save.cpl) {
1692 kvm_inject_gp(&svm->vcpu, 0);
1699 static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1700 bool has_error_code, u32 error_code)
1704 if (!is_nested(svm))
1707 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1708 svm->vmcb->control.exit_code_hi = 0;
1709 svm->vmcb->control.exit_info_1 = error_code;
1710 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1712 vmexit = nested_svm_intercept(svm);
1713 if (vmexit == NESTED_EXIT_DONE)
1714 svm->nested.exit_required = true;
1719 /* This function returns true if it is save to enable the irq window */
1720 static inline bool nested_svm_intr(struct vcpu_svm *svm)
1722 if (!is_nested(svm))
1725 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1728 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
1732 * if vmexit was already requested (by intercepted exception
1733 * for instance) do not overwrite it with "external interrupt"
1736 if (svm->nested.exit_required)
1739 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1740 svm->vmcb->control.exit_info_1 = 0;
1741 svm->vmcb->control.exit_info_2 = 0;
1743 if (svm->nested.intercept & 1ULL) {
1745 * The #vmexit can't be emulated here directly because this
1746 * code path runs with irqs and preemtion disabled. A
1747 * #vmexit emulation might sleep. Only signal request for
1750 svm->nested.exit_required = true;
1751 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
1758 /* This function returns true if it is save to enable the nmi window */
1759 static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1761 if (!is_nested(svm))
1764 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1767 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1768 svm->nested.exit_required = true;
1773 static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
1779 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
1780 if (is_error_page(page))
1788 kvm_release_page_clean(page);
1789 kvm_inject_gp(&svm->vcpu, 0);
1794 static void nested_svm_unmap(struct page *page)
1797 kvm_release_page_dirty(page);
1800 static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
1806 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1807 return NESTED_EXIT_HOST;
1809 port = svm->vmcb->control.exit_info_1 >> 16;
1810 gpa = svm->nested.vmcb_iopm + (port / 8);
1814 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1817 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1820 static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
1822 u32 offset, msr, value;
1825 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1826 return NESTED_EXIT_HOST;
1828 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1829 offset = svm_msrpm_offset(msr);
1830 write = svm->vmcb->control.exit_info_1 & 1;
1831 mask = 1 << ((2 * (msr & 0xf)) + write);
1833 if (offset == MSR_INVALID)
1834 return NESTED_EXIT_DONE;
1836 /* Offset is in 32 bit units but need in 8 bit units */
1839 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1840 return NESTED_EXIT_DONE;
1842 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1845 static int nested_svm_exit_special(struct vcpu_svm *svm)
1847 u32 exit_code = svm->vmcb->control.exit_code;
1849 switch (exit_code) {
1852 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
1853 return NESTED_EXIT_HOST;
1855 /* For now we are always handling NPFs when using them */
1857 return NESTED_EXIT_HOST;
1859 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
1860 /* When we're shadowing, trap PFs, but not async PF */
1861 if (!npt_enabled && svm->apf_reason == 0)
1862 return NESTED_EXIT_HOST;
1864 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1865 nm_interception(svm);
1871 return NESTED_EXIT_CONTINUE;
1875 * If this function returns true, this #vmexit was already handled
1877 static int nested_svm_intercept(struct vcpu_svm *svm)
1879 u32 exit_code = svm->vmcb->control.exit_code;
1880 int vmexit = NESTED_EXIT_HOST;
1882 switch (exit_code) {
1884 vmexit = nested_svm_exit_handled_msr(svm);
1887 vmexit = nested_svm_intercept_ioio(svm);
1889 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1890 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
1891 if (svm->nested.intercept_cr_read & cr_bits)
1892 vmexit = NESTED_EXIT_DONE;
1895 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1896 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
1897 if (svm->nested.intercept_cr_write & cr_bits)
1898 vmexit = NESTED_EXIT_DONE;
1901 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1902 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
1903 if (svm->nested.intercept_dr_read & dr_bits)
1904 vmexit = NESTED_EXIT_DONE;
1907 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1908 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
1909 if (svm->nested.intercept_dr_write & dr_bits)
1910 vmexit = NESTED_EXIT_DONE;
1913 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1914 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
1915 if (svm->nested.intercept_exceptions & excp_bits)
1916 vmexit = NESTED_EXIT_DONE;
1917 /* async page fault always cause vmexit */
1918 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
1919 svm->apf_reason != 0)
1920 vmexit = NESTED_EXIT_DONE;
1923 case SVM_EXIT_ERR: {
1924 vmexit = NESTED_EXIT_DONE;
1928 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
1929 if (svm->nested.intercept & exit_bits)
1930 vmexit = NESTED_EXIT_DONE;
1937 static int nested_svm_exit_handled(struct vcpu_svm *svm)
1941 vmexit = nested_svm_intercept(svm);
1943 if (vmexit == NESTED_EXIT_DONE)
1944 nested_svm_vmexit(svm);
1949 static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1951 struct vmcb_control_area *dst = &dst_vmcb->control;
1952 struct vmcb_control_area *from = &from_vmcb->control;
1954 dst->intercept_cr_read = from->intercept_cr_read;
1955 dst->intercept_cr_write = from->intercept_cr_write;
1956 dst->intercept_dr_read = from->intercept_dr_read;
1957 dst->intercept_dr_write = from->intercept_dr_write;
1958 dst->intercept_exceptions = from->intercept_exceptions;
1959 dst->intercept = from->intercept;
1960 dst->iopm_base_pa = from->iopm_base_pa;
1961 dst->msrpm_base_pa = from->msrpm_base_pa;
1962 dst->tsc_offset = from->tsc_offset;
1963 dst->asid = from->asid;
1964 dst->tlb_ctl = from->tlb_ctl;
1965 dst->int_ctl = from->int_ctl;
1966 dst->int_vector = from->int_vector;
1967 dst->int_state = from->int_state;
1968 dst->exit_code = from->exit_code;
1969 dst->exit_code_hi = from->exit_code_hi;
1970 dst->exit_info_1 = from->exit_info_1;
1971 dst->exit_info_2 = from->exit_info_2;
1972 dst->exit_int_info = from->exit_int_info;
1973 dst->exit_int_info_err = from->exit_int_info_err;
1974 dst->nested_ctl = from->nested_ctl;
1975 dst->event_inj = from->event_inj;
1976 dst->event_inj_err = from->event_inj_err;
1977 dst->nested_cr3 = from->nested_cr3;
1978 dst->lbr_ctl = from->lbr_ctl;
1981 static int nested_svm_vmexit(struct vcpu_svm *svm)
1983 struct vmcb *nested_vmcb;
1984 struct vmcb *hsave = svm->nested.hsave;
1985 struct vmcb *vmcb = svm->vmcb;
1988 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1989 vmcb->control.exit_info_1,
1990 vmcb->control.exit_info_2,
1991 vmcb->control.exit_int_info,
1992 vmcb->control.exit_int_info_err);
1994 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
1998 /* Exit nested SVM mode */
1999 svm->nested.vmcb = 0;
2001 /* Give the current vmcb to the guest */
2004 nested_vmcb->save.es = vmcb->save.es;
2005 nested_vmcb->save.cs = vmcb->save.cs;
2006 nested_vmcb->save.ss = vmcb->save.ss;
2007 nested_vmcb->save.ds = vmcb->save.ds;
2008 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2009 nested_vmcb->save.idtr = vmcb->save.idtr;
2010 nested_vmcb->save.efer = svm->vcpu.arch.efer;
2011 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
2012 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
2013 nested_vmcb->save.cr2 = vmcb->save.cr2;
2014 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
2015 nested_vmcb->save.rflags = vmcb->save.rflags;
2016 nested_vmcb->save.rip = vmcb->save.rip;
2017 nested_vmcb->save.rsp = vmcb->save.rsp;
2018 nested_vmcb->save.rax = vmcb->save.rax;
2019 nested_vmcb->save.dr7 = vmcb->save.dr7;
2020 nested_vmcb->save.dr6 = vmcb->save.dr6;
2021 nested_vmcb->save.cpl = vmcb->save.cpl;
2023 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2024 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2025 nested_vmcb->control.int_state = vmcb->control.int_state;
2026 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2027 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2028 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2029 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2030 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2031 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
2032 nested_vmcb->control.next_rip = vmcb->control.next_rip;
2035 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2036 * to make sure that we do not lose injected events. So check event_inj
2037 * here and copy it to exit_int_info if it is valid.
2038 * Exit_int_info and event_inj can't be both valid because the case
2039 * below only happens on a VMRUN instruction intercept which has
2040 * no valid exit_int_info set.
2042 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2043 struct vmcb_control_area *nc = &nested_vmcb->control;
2045 nc->exit_int_info = vmcb->control.event_inj;
2046 nc->exit_int_info_err = vmcb->control.event_inj_err;
2049 nested_vmcb->control.tlb_ctl = 0;
2050 nested_vmcb->control.event_inj = 0;
2051 nested_vmcb->control.event_inj_err = 0;
2053 /* We always set V_INTR_MASKING and remember the old value in hflags */
2054 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2055 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2057 /* Restore the original control entries */
2058 copy_vmcb_control_area(vmcb, hsave);
2060 kvm_clear_exception_queue(&svm->vcpu);
2061 kvm_clear_interrupt_queue(&svm->vcpu);
2063 svm->nested.nested_cr3 = 0;
2065 /* Restore selected save entries */
2066 svm->vmcb->save.es = hsave->save.es;
2067 svm->vmcb->save.cs = hsave->save.cs;
2068 svm->vmcb->save.ss = hsave->save.ss;
2069 svm->vmcb->save.ds = hsave->save.ds;
2070 svm->vmcb->save.gdtr = hsave->save.gdtr;
2071 svm->vmcb->save.idtr = hsave->save.idtr;
2072 svm->vmcb->save.rflags = hsave->save.rflags;
2073 svm_set_efer(&svm->vcpu, hsave->save.efer);
2074 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2075 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2077 svm->vmcb->save.cr3 = hsave->save.cr3;
2078 svm->vcpu.arch.cr3 = hsave->save.cr3;
2080 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
2082 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2083 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2084 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2085 svm->vmcb->save.dr7 = 0;
2086 svm->vmcb->save.cpl = 0;
2087 svm->vmcb->control.exit_int_info = 0;
2089 nested_svm_unmap(page);
2091 nested_svm_uninit_mmu_context(&svm->vcpu);
2092 kvm_mmu_reset_context(&svm->vcpu);
2093 kvm_mmu_load(&svm->vcpu);
2098 static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
2101 * This function merges the msr permission bitmaps of kvm and the
2102 * nested vmcb. It is omptimized in that it only merges the parts where
2103 * the kvm msr permission bitmap may contain zero bits
2107 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2110 for (i = 0; i < MSRPM_OFFSETS; i++) {
2114 if (msrpm_offsets[i] == 0xffffffff)
2117 p = msrpm_offsets[i];
2118 offset = svm->nested.vmcb_msrpm + (p * 4);
2120 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2123 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2126 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
2131 static bool nested_vmcb_checks(struct vmcb *vmcb)
2133 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2136 if (vmcb->control.asid == 0)
2139 if (vmcb->control.nested_ctl && !npt_enabled)
2145 static bool nested_svm_vmrun(struct vcpu_svm *svm)
2147 struct vmcb *nested_vmcb;
2148 struct vmcb *hsave = svm->nested.hsave;
2149 struct vmcb *vmcb = svm->vmcb;
2153 vmcb_gpa = svm->vmcb->save.rax;
2155 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
2159 if (!nested_vmcb_checks(nested_vmcb)) {
2160 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2161 nested_vmcb->control.exit_code_hi = 0;
2162 nested_vmcb->control.exit_info_1 = 0;
2163 nested_vmcb->control.exit_info_2 = 0;
2165 nested_svm_unmap(page);
2170 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
2171 nested_vmcb->save.rip,
2172 nested_vmcb->control.int_ctl,
2173 nested_vmcb->control.event_inj,
2174 nested_vmcb->control.nested_ctl);
2176 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2177 nested_vmcb->control.intercept_cr_write,
2178 nested_vmcb->control.intercept_exceptions,
2179 nested_vmcb->control.intercept);
2181 /* Clear internal status */
2182 kvm_clear_exception_queue(&svm->vcpu);
2183 kvm_clear_interrupt_queue(&svm->vcpu);
2186 * Save the old vmcb, so we don't need to pick what we save, but can
2187 * restore everything when a VMEXIT occurs
2189 hsave->save.es = vmcb->save.es;
2190 hsave->save.cs = vmcb->save.cs;
2191 hsave->save.ss = vmcb->save.ss;
2192 hsave->save.ds = vmcb->save.ds;
2193 hsave->save.gdtr = vmcb->save.gdtr;
2194 hsave->save.idtr = vmcb->save.idtr;
2195 hsave->save.efer = svm->vcpu.arch.efer;
2196 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
2197 hsave->save.cr4 = svm->vcpu.arch.cr4;
2198 hsave->save.rflags = vmcb->save.rflags;
2199 hsave->save.rip = kvm_rip_read(&svm->vcpu);
2200 hsave->save.rsp = vmcb->save.rsp;
2201 hsave->save.rax = vmcb->save.rax;
2203 hsave->save.cr3 = vmcb->save.cr3;
2205 hsave->save.cr3 = svm->vcpu.arch.cr3;
2207 copy_vmcb_control_area(hsave, vmcb);
2209 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2210 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2212 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2214 if (nested_vmcb->control.nested_ctl) {
2215 kvm_mmu_unload(&svm->vcpu);
2216 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2217 nested_svm_init_mmu_context(&svm->vcpu);
2220 /* Load the nested guest state */
2221 svm->vmcb->save.es = nested_vmcb->save.es;
2222 svm->vmcb->save.cs = nested_vmcb->save.cs;
2223 svm->vmcb->save.ss = nested_vmcb->save.ss;
2224 svm->vmcb->save.ds = nested_vmcb->save.ds;
2225 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2226 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2227 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2228 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2229 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2230 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2232 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2233 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
2235 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
2237 /* Guest paging mode is active - reset mmu */
2238 kvm_mmu_reset_context(&svm->vcpu);
2240 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
2241 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2242 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2243 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
2245 /* In case we don't even reach vcpu_run, the fields are not updated */
2246 svm->vmcb->save.rax = nested_vmcb->save.rax;
2247 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2248 svm->vmcb->save.rip = nested_vmcb->save.rip;
2249 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2250 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2251 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2253 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
2254 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
2256 /* cache intercepts */
2257 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2258 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2259 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2260 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2261 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2262 svm->nested.intercept = nested_vmcb->control.intercept;
2264 force_new_asid(&svm->vcpu);
2265 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
2266 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2267 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2269 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2271 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2272 /* We only want the cr8 intercept bits of the guest */
2273 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2274 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2277 /* We don't want to see VMMCALLs from a nested guest */
2278 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL);
2281 * We don't want a nested guest to be more powerful than the guest, so
2282 * all intercepts are ORed
2284 svm->vmcb->control.intercept_cr_read |=
2285 nested_vmcb->control.intercept_cr_read;
2286 svm->vmcb->control.intercept_cr_write |=
2287 nested_vmcb->control.intercept_cr_write;
2288 svm->vmcb->control.intercept_dr_read |=
2289 nested_vmcb->control.intercept_dr_read;
2290 svm->vmcb->control.intercept_dr_write |=
2291 nested_vmcb->control.intercept_dr_write;
2292 svm->vmcb->control.intercept_exceptions |=
2293 nested_vmcb->control.intercept_exceptions;
2295 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2297 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
2298 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2299 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2300 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
2301 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2302 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2304 nested_svm_unmap(page);
2306 /* nested_vmcb is our indicator if nested SVM is activated */
2307 svm->nested.vmcb = vmcb_gpa;
2314 static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
2316 to_vmcb->save.fs = from_vmcb->save.fs;
2317 to_vmcb->save.gs = from_vmcb->save.gs;
2318 to_vmcb->save.tr = from_vmcb->save.tr;
2319 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2320 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2321 to_vmcb->save.star = from_vmcb->save.star;
2322 to_vmcb->save.lstar = from_vmcb->save.lstar;
2323 to_vmcb->save.cstar = from_vmcb->save.cstar;
2324 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2325 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2326 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2327 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
2330 static int vmload_interception(struct vcpu_svm *svm)
2332 struct vmcb *nested_vmcb;
2335 if (nested_svm_check_permissions(svm))
2338 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2339 skip_emulated_instruction(&svm->vcpu);
2341 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
2345 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
2346 nested_svm_unmap(page);
2351 static int vmsave_interception(struct vcpu_svm *svm)
2353 struct vmcb *nested_vmcb;
2356 if (nested_svm_check_permissions(svm))
2359 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2360 skip_emulated_instruction(&svm->vcpu);
2362 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
2366 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
2367 nested_svm_unmap(page);
2372 static int vmrun_interception(struct vcpu_svm *svm)
2374 if (nested_svm_check_permissions(svm))
2377 /* Save rip after vmrun instruction */
2378 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
2380 if (!nested_svm_vmrun(svm))
2383 if (!nested_svm_vmrun_msrpm(svm))
2390 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2391 svm->vmcb->control.exit_code_hi = 0;
2392 svm->vmcb->control.exit_info_1 = 0;
2393 svm->vmcb->control.exit_info_2 = 0;
2395 nested_svm_vmexit(svm);
2400 static int stgi_interception(struct vcpu_svm *svm)
2402 if (nested_svm_check_permissions(svm))
2405 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2406 skip_emulated_instruction(&svm->vcpu);
2407 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2414 static int clgi_interception(struct vcpu_svm *svm)
2416 if (nested_svm_check_permissions(svm))
2419 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2420 skip_emulated_instruction(&svm->vcpu);
2424 /* After a CLGI no interrupts should come */
2425 svm_clear_vintr(svm);
2426 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2431 static int invlpga_interception(struct vcpu_svm *svm)
2433 struct kvm_vcpu *vcpu = &svm->vcpu;
2435 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2436 vcpu->arch.regs[VCPU_REGS_RAX]);
2438 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2439 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2441 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2442 skip_emulated_instruction(&svm->vcpu);
2446 static int skinit_interception(struct vcpu_svm *svm)
2448 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2450 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2454 static int invalid_op_interception(struct vcpu_svm *svm)
2456 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2460 static int task_switch_interception(struct vcpu_svm *svm)
2464 int int_type = svm->vmcb->control.exit_int_info &
2465 SVM_EXITINTINFO_TYPE_MASK;
2466 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
2468 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2470 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
2471 bool has_error_code = false;
2474 tss_selector = (u16)svm->vmcb->control.exit_info_1;
2476 if (svm->vmcb->control.exit_info_2 &
2477 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
2478 reason = TASK_SWITCH_IRET;
2479 else if (svm->vmcb->control.exit_info_2 &
2480 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2481 reason = TASK_SWITCH_JMP;
2483 reason = TASK_SWITCH_GATE;
2485 reason = TASK_SWITCH_CALL;
2487 if (reason == TASK_SWITCH_GATE) {
2489 case SVM_EXITINTINFO_TYPE_NMI:
2490 svm->vcpu.arch.nmi_injected = false;
2492 case SVM_EXITINTINFO_TYPE_EXEPT:
2493 if (svm->vmcb->control.exit_info_2 &
2494 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2495 has_error_code = true;
2497 (u32)svm->vmcb->control.exit_info_2;
2499 kvm_clear_exception_queue(&svm->vcpu);
2501 case SVM_EXITINTINFO_TYPE_INTR:
2502 kvm_clear_interrupt_queue(&svm->vcpu);
2509 if (reason != TASK_SWITCH_GATE ||
2510 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2511 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
2512 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2513 skip_emulated_instruction(&svm->vcpu);
2515 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2516 has_error_code, error_code) == EMULATE_FAIL) {
2517 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2518 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2519 svm->vcpu.run->internal.ndata = 0;
2525 static int cpuid_interception(struct vcpu_svm *svm)
2527 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
2528 kvm_emulate_cpuid(&svm->vcpu);
2532 static int iret_interception(struct vcpu_svm *svm)
2534 ++svm->vcpu.stat.nmi_window_exits;
2535 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
2536 svm->vcpu.arch.hflags |= HF_IRET_MASK;
2540 static int invlpg_interception(struct vcpu_svm *svm)
2542 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
2545 static int emulate_on_interception(struct vcpu_svm *svm)
2547 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
2550 static int cr0_write_interception(struct vcpu_svm *svm)
2552 struct kvm_vcpu *vcpu = &svm->vcpu;
2555 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2557 if (svm->nested.vmexit_rip) {
2558 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2559 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2560 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2561 svm->nested.vmexit_rip = 0;
2564 return r == EMULATE_DONE;
2567 static int cr8_write_interception(struct vcpu_svm *svm)
2569 struct kvm_run *kvm_run = svm->vcpu.run;
2571 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2572 /* instruction emulation calls kvm_set_cr8() */
2573 emulate_instruction(&svm->vcpu, 0, 0, 0);
2574 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2575 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2578 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2580 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2584 static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2586 struct vcpu_svm *svm = to_svm(vcpu);
2589 case MSR_IA32_TSC: {
2593 tsc_offset = svm->nested.hsave->control.tsc_offset;
2595 tsc_offset = svm->vmcb->control.tsc_offset;
2597 *data = tsc_offset + native_read_tsc();
2601 *data = svm->vmcb->save.star;
2603 #ifdef CONFIG_X86_64
2605 *data = svm->vmcb->save.lstar;
2608 *data = svm->vmcb->save.cstar;
2610 case MSR_KERNEL_GS_BASE:
2611 *data = svm->vmcb->save.kernel_gs_base;
2613 case MSR_SYSCALL_MASK:
2614 *data = svm->vmcb->save.sfmask;
2617 case MSR_IA32_SYSENTER_CS:
2618 *data = svm->vmcb->save.sysenter_cs;
2620 case MSR_IA32_SYSENTER_EIP:
2621 *data = svm->sysenter_eip;
2623 case MSR_IA32_SYSENTER_ESP:
2624 *data = svm->sysenter_esp;
2627 * Nobody will change the following 5 values in the VMCB so we can
2628 * safely return them on rdmsr. They will always be 0 until LBRV is
2631 case MSR_IA32_DEBUGCTLMSR:
2632 *data = svm->vmcb->save.dbgctl;
2634 case MSR_IA32_LASTBRANCHFROMIP:
2635 *data = svm->vmcb->save.br_from;
2637 case MSR_IA32_LASTBRANCHTOIP:
2638 *data = svm->vmcb->save.br_to;
2640 case MSR_IA32_LASTINTFROMIP:
2641 *data = svm->vmcb->save.last_excp_from;
2643 case MSR_IA32_LASTINTTOIP:
2644 *data = svm->vmcb->save.last_excp_to;
2646 case MSR_VM_HSAVE_PA:
2647 *data = svm->nested.hsave_msr;
2650 *data = svm->nested.vm_cr_msr;
2652 case MSR_IA32_UCODE_REV:
2656 return kvm_get_msr_common(vcpu, ecx, data);
2661 static int rdmsr_interception(struct vcpu_svm *svm)
2663 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2666 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2667 trace_kvm_msr_read_ex(ecx);
2668 kvm_inject_gp(&svm->vcpu, 0);
2670 trace_kvm_msr_read(ecx, data);
2672 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
2673 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
2674 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
2675 skip_emulated_instruction(&svm->vcpu);
2680 static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2682 struct vcpu_svm *svm = to_svm(vcpu);
2683 int svm_dis, chg_mask;
2685 if (data & ~SVM_VM_CR_VALID_MASK)
2688 chg_mask = SVM_VM_CR_VALID_MASK;
2690 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2691 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2693 svm->nested.vm_cr_msr &= ~chg_mask;
2694 svm->nested.vm_cr_msr |= (data & chg_mask);
2696 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2698 /* check for svm_disable while efer.svme is set */
2699 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2705 static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2707 struct vcpu_svm *svm = to_svm(vcpu);
2711 kvm_write_tsc(vcpu, data);
2714 svm->vmcb->save.star = data;
2716 #ifdef CONFIG_X86_64
2718 svm->vmcb->save.lstar = data;
2721 svm->vmcb->save.cstar = data;
2723 case MSR_KERNEL_GS_BASE:
2724 svm->vmcb->save.kernel_gs_base = data;
2726 case MSR_SYSCALL_MASK:
2727 svm->vmcb->save.sfmask = data;
2730 case MSR_IA32_SYSENTER_CS:
2731 svm->vmcb->save.sysenter_cs = data;
2733 case MSR_IA32_SYSENTER_EIP:
2734 svm->sysenter_eip = data;
2735 svm->vmcb->save.sysenter_eip = data;
2737 case MSR_IA32_SYSENTER_ESP:
2738 svm->sysenter_esp = data;
2739 svm->vmcb->save.sysenter_esp = data;
2741 case MSR_IA32_DEBUGCTLMSR:
2742 if (!svm_has(SVM_FEATURE_LBRV)) {
2743 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2747 if (data & DEBUGCTL_RESERVED_BITS)
2750 svm->vmcb->save.dbgctl = data;
2751 if (data & (1ULL<<0))
2752 svm_enable_lbrv(svm);
2754 svm_disable_lbrv(svm);
2756 case MSR_VM_HSAVE_PA:
2757 svm->nested.hsave_msr = data;
2760 return svm_set_vm_cr(vcpu, data);
2762 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2765 return kvm_set_msr_common(vcpu, ecx, data);
2770 static int wrmsr_interception(struct vcpu_svm *svm)
2772 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
2773 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
2774 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
2777 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
2778 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2779 trace_kvm_msr_write_ex(ecx, data);
2780 kvm_inject_gp(&svm->vcpu, 0);
2782 trace_kvm_msr_write(ecx, data);
2783 skip_emulated_instruction(&svm->vcpu);
2788 static int msr_interception(struct vcpu_svm *svm)
2790 if (svm->vmcb->control.exit_info_1)
2791 return wrmsr_interception(svm);
2793 return rdmsr_interception(svm);
2796 static int interrupt_window_interception(struct vcpu_svm *svm)
2798 struct kvm_run *kvm_run = svm->vcpu.run;
2800 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2801 svm_clear_vintr(svm);
2802 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2804 * If the user space waits to inject interrupts, exit as soon as
2807 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2808 kvm_run->request_interrupt_window &&
2809 !kvm_cpu_has_interrupt(&svm->vcpu)) {
2810 ++svm->vcpu.stat.irq_window_exits;
2811 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2818 static int pause_interception(struct vcpu_svm *svm)
2820 kvm_vcpu_on_spin(&(svm->vcpu));
2824 static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
2825 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2826 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2827 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2828 [SVM_EXIT_READ_CR8] = emulate_on_interception,
2829 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
2830 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
2831 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2832 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2833 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2834 [SVM_EXIT_READ_DR0] = emulate_on_interception,
2835 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2836 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2837 [SVM_EXIT_READ_DR3] = emulate_on_interception,
2838 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2839 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2840 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2841 [SVM_EXIT_READ_DR7] = emulate_on_interception,
2842 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2843 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2844 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2845 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
2846 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
2847 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
2848 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
2849 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
2850 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2851 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
2852 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
2853 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2854 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2855 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2856 [SVM_EXIT_INTR] = intr_interception,
2857 [SVM_EXIT_NMI] = nmi_interception,
2858 [SVM_EXIT_SMI] = nop_on_interception,
2859 [SVM_EXIT_INIT] = nop_on_interception,
2860 [SVM_EXIT_VINTR] = interrupt_window_interception,
2861 [SVM_EXIT_CPUID] = cpuid_interception,
2862 [SVM_EXIT_IRET] = iret_interception,
2863 [SVM_EXIT_INVD] = emulate_on_interception,
2864 [SVM_EXIT_PAUSE] = pause_interception,
2865 [SVM_EXIT_HLT] = halt_interception,
2866 [SVM_EXIT_INVLPG] = invlpg_interception,
2867 [SVM_EXIT_INVLPGA] = invlpga_interception,
2868 [SVM_EXIT_IOIO] = io_interception,
2869 [SVM_EXIT_MSR] = msr_interception,
2870 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
2871 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
2872 [SVM_EXIT_VMRUN] = vmrun_interception,
2873 [SVM_EXIT_VMMCALL] = vmmcall_interception,
2874 [SVM_EXIT_VMLOAD] = vmload_interception,
2875 [SVM_EXIT_VMSAVE] = vmsave_interception,
2876 [SVM_EXIT_STGI] = stgi_interception,
2877 [SVM_EXIT_CLGI] = clgi_interception,
2878 [SVM_EXIT_SKINIT] = skinit_interception,
2879 [SVM_EXIT_WBINVD] = emulate_on_interception,
2880 [SVM_EXIT_MONITOR] = invalid_op_interception,
2881 [SVM_EXIT_MWAIT] = invalid_op_interception,
2882 [SVM_EXIT_NPF] = pf_interception,
2885 void dump_vmcb(struct kvm_vcpu *vcpu)
2887 struct vcpu_svm *svm = to_svm(vcpu);
2888 struct vmcb_control_area *control = &svm->vmcb->control;
2889 struct vmcb_save_area *save = &svm->vmcb->save;
2891 pr_err("VMCB Control Area:\n");
2892 pr_err("cr_read: %04x\n", control->intercept_cr_read);
2893 pr_err("cr_write: %04x\n", control->intercept_cr_write);
2894 pr_err("dr_read: %04x\n", control->intercept_dr_read);
2895 pr_err("dr_write: %04x\n", control->intercept_dr_write);
2896 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2897 pr_err("intercepts: %016llx\n", control->intercept);
2898 pr_err("pause filter count: %d\n", control->pause_filter_count);
2899 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2900 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2901 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2902 pr_err("asid: %d\n", control->asid);
2903 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2904 pr_err("int_ctl: %08x\n", control->int_ctl);
2905 pr_err("int_vector: %08x\n", control->int_vector);
2906 pr_err("int_state: %08x\n", control->int_state);
2907 pr_err("exit_code: %08x\n", control->exit_code);
2908 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2909 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2910 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2911 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2912 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2913 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2914 pr_err("event_inj: %08x\n", control->event_inj);
2915 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2916 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2917 pr_err("next_rip: %016llx\n", control->next_rip);
2918 pr_err("VMCB State Save Area:\n");
2919 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2920 save->es.selector, save->es.attrib,
2921 save->es.limit, save->es.base);
2922 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2923 save->cs.selector, save->cs.attrib,
2924 save->cs.limit, save->cs.base);
2925 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2926 save->ss.selector, save->ss.attrib,
2927 save->ss.limit, save->ss.base);
2928 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2929 save->ds.selector, save->ds.attrib,
2930 save->ds.limit, save->ds.base);
2931 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2932 save->fs.selector, save->fs.attrib,
2933 save->fs.limit, save->fs.base);
2934 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2935 save->gs.selector, save->gs.attrib,
2936 save->gs.limit, save->gs.base);
2937 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2938 save->gdtr.selector, save->gdtr.attrib,
2939 save->gdtr.limit, save->gdtr.base);
2940 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2941 save->ldtr.selector, save->ldtr.attrib,
2942 save->ldtr.limit, save->ldtr.base);
2943 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2944 save->idtr.selector, save->idtr.attrib,
2945 save->idtr.limit, save->idtr.base);
2946 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2947 save->tr.selector, save->tr.attrib,
2948 save->tr.limit, save->tr.base);
2949 pr_err("cpl: %d efer: %016llx\n",
2950 save->cpl, save->efer);
2951 pr_err("cr0: %016llx cr2: %016llx\n",
2952 save->cr0, save->cr2);
2953 pr_err("cr3: %016llx cr4: %016llx\n",
2954 save->cr3, save->cr4);
2955 pr_err("dr6: %016llx dr7: %016llx\n",
2956 save->dr6, save->dr7);
2957 pr_err("rip: %016llx rflags: %016llx\n",
2958 save->rip, save->rflags);
2959 pr_err("rsp: %016llx rax: %016llx\n",
2960 save->rsp, save->rax);
2961 pr_err("star: %016llx lstar: %016llx\n",
2962 save->star, save->lstar);
2963 pr_err("cstar: %016llx sfmask: %016llx\n",
2964 save->cstar, save->sfmask);
2965 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2966 save->kernel_gs_base, save->sysenter_cs);
2967 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2968 save->sysenter_esp, save->sysenter_eip);
2969 pr_err("gpat: %016llx dbgctl: %016llx\n",
2970 save->g_pat, save->dbgctl);
2971 pr_err("br_from: %016llx br_to: %016llx\n",
2972 save->br_from, save->br_to);
2973 pr_err("excp_from: %016llx excp_to: %016llx\n",
2974 save->last_excp_from, save->last_excp_to);
2978 static int handle_exit(struct kvm_vcpu *vcpu)
2980 struct vcpu_svm *svm = to_svm(vcpu);
2981 struct kvm_run *kvm_run = vcpu->run;
2982 u32 exit_code = svm->vmcb->control.exit_code;
2984 trace_kvm_exit(exit_code, vcpu);
2986 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
2987 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2989 vcpu->arch.cr3 = svm->vmcb->save.cr3;
2991 if (unlikely(svm->nested.exit_required)) {
2992 nested_svm_vmexit(svm);
2993 svm->nested.exit_required = false;
2998 if (is_nested(svm)) {
3001 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3002 svm->vmcb->control.exit_info_1,
3003 svm->vmcb->control.exit_info_2,
3004 svm->vmcb->control.exit_int_info,
3005 svm->vmcb->control.exit_int_info_err);
3007 vmexit = nested_svm_exit_special(svm);
3009 if (vmexit == NESTED_EXIT_CONTINUE)
3010 vmexit = nested_svm_exit_handled(svm);
3012 if (vmexit == NESTED_EXIT_DONE)
3016 svm_complete_interrupts(svm);
3018 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3019 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3020 kvm_run->fail_entry.hardware_entry_failure_reason
3021 = svm->vmcb->control.exit_code;
3022 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3027 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
3028 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
3029 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3030 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
3031 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3033 __func__, svm->vmcb->control.exit_int_info,
3036 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
3037 || !svm_exit_handlers[exit_code]) {
3038 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
3039 kvm_run->hw.hardware_exit_reason = exit_code;
3043 return svm_exit_handlers[exit_code](svm);
3046 static void reload_tss(struct kvm_vcpu *vcpu)
3048 int cpu = raw_smp_processor_id();
3050 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3051 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
3055 static void pre_svm_run(struct vcpu_svm *svm)
3057 int cpu = raw_smp_processor_id();
3059 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3061 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3062 /* FIXME: handle wraparound of asid_generation */
3063 if (svm->asid_generation != sd->asid_generation)
3067 static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3069 struct vcpu_svm *svm = to_svm(vcpu);
3071 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3072 vcpu->arch.hflags |= HF_NMI_MASK;
3073 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
3074 ++vcpu->stat.nmi_injections;
3077 static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
3079 struct vmcb_control_area *control;
3081 control = &svm->vmcb->control;
3082 control->int_vector = irq;
3083 control->int_ctl &= ~V_INTR_PRIO_MASK;
3084 control->int_ctl |= V_IRQ_MASK |
3085 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
3088 static void svm_set_irq(struct kvm_vcpu *vcpu)
3090 struct vcpu_svm *svm = to_svm(vcpu);
3092 BUG_ON(!(gif_set(svm)));
3094 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3095 ++vcpu->stat.irq_injections;
3097 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3098 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
3101 static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3103 struct vcpu_svm *svm = to_svm(vcpu);
3105 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3112 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
3115 static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
3117 struct vcpu_svm *svm = to_svm(vcpu);
3118 struct vmcb *vmcb = svm->vmcb;
3120 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3121 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3122 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3127 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3129 struct vcpu_svm *svm = to_svm(vcpu);
3131 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3134 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3136 struct vcpu_svm *svm = to_svm(vcpu);
3139 svm->vcpu.arch.hflags |= HF_NMI_MASK;
3140 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
3142 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
3143 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
3147 static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3149 struct vcpu_svm *svm = to_svm(vcpu);
3150 struct vmcb *vmcb = svm->vmcb;
3153 if (!gif_set(svm) ||
3154 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3157 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3160 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3165 static void enable_irq_window(struct kvm_vcpu *vcpu)
3167 struct vcpu_svm *svm = to_svm(vcpu);
3170 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3171 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3172 * get that intercept, this function will be called again though and
3173 * we'll get the vintr intercept.
3175 if (gif_set(svm) && nested_svm_intr(svm)) {
3177 svm_inject_irq(svm, 0x0);
3181 static void enable_nmi_window(struct kvm_vcpu *vcpu)
3183 struct vcpu_svm *svm = to_svm(vcpu);
3185 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3187 return; /* IRET will cause a vm exit */
3190 * Something prevents NMI from been injected. Single step over possible
3191 * problem (IRET or exception injection or interrupt shadow)
3193 svm->nmi_singlestep = true;
3194 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3195 update_db_intercept(vcpu);
3198 static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3203 static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3205 force_new_asid(vcpu);
3208 static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3212 static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3214 struct vcpu_svm *svm = to_svm(vcpu);
3216 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3219 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
3220 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
3221 kvm_set_cr8(vcpu, cr8);
3225 static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3227 struct vcpu_svm *svm = to_svm(vcpu);
3230 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3233 cr8 = kvm_get_cr8(vcpu);
3234 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3235 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3238 static void svm_complete_interrupts(struct vcpu_svm *svm)
3242 u32 exitintinfo = svm->vmcb->control.exit_int_info;
3243 unsigned int3_injected = svm->int3_injected;
3245 svm->int3_injected = 0;
3247 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
3248 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3249 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3252 svm->vcpu.arch.nmi_injected = false;
3253 kvm_clear_exception_queue(&svm->vcpu);
3254 kvm_clear_interrupt_queue(&svm->vcpu);
3256 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3259 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3261 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3262 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3265 case SVM_EXITINTINFO_TYPE_NMI:
3266 svm->vcpu.arch.nmi_injected = true;
3268 case SVM_EXITINTINFO_TYPE_EXEPT:
3270 * In case of software exceptions, do not reinject the vector,
3271 * but re-execute the instruction instead. Rewind RIP first
3272 * if we emulated INT3 before.
3274 if (kvm_exception_is_soft(vector)) {
3275 if (vector == BP_VECTOR && int3_injected &&
3276 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3277 kvm_rip_write(&svm->vcpu,
3278 kvm_rip_read(&svm->vcpu) -
3282 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3283 u32 err = svm->vmcb->control.exit_int_info_err;
3284 kvm_requeue_exception_e(&svm->vcpu, vector, err);
3287 kvm_requeue_exception(&svm->vcpu, vector);
3289 case SVM_EXITINTINFO_TYPE_INTR:
3290 kvm_queue_interrupt(&svm->vcpu, vector, false);
3297 static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3299 struct vcpu_svm *svm = to_svm(vcpu);
3300 struct vmcb_control_area *control = &svm->vmcb->control;
3302 control->exit_int_info = control->event_inj;
3303 control->exit_int_info_err = control->event_inj_err;
3304 control->event_inj = 0;
3305 svm_complete_interrupts(svm);
3308 #ifdef CONFIG_X86_64
3314 static void svm_vcpu_run(struct kvm_vcpu *vcpu)
3316 struct vcpu_svm *svm = to_svm(vcpu);
3321 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3322 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3323 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3326 * A vmexit emulation is required before the vcpu can be executed
3329 if (unlikely(svm->nested.exit_required))
3334 sync_lapic_to_cr8(vcpu);
3336 save_host_msrs(vcpu);
3337 savesegment(fs, fs_selector);
3338 savesegment(gs, gs_selector);
3339 ldt_selector = kvm_read_ldt();
3340 svm->vmcb->save.cr2 = vcpu->arch.cr2;
3347 "push %%"R"bp; \n\t"
3348 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3349 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3350 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3351 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3352 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3353 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
3354 #ifdef CONFIG_X86_64
3355 "mov %c[r8](%[svm]), %%r8 \n\t"
3356 "mov %c[r9](%[svm]), %%r9 \n\t"
3357 "mov %c[r10](%[svm]), %%r10 \n\t"
3358 "mov %c[r11](%[svm]), %%r11 \n\t"
3359 "mov %c[r12](%[svm]), %%r12 \n\t"
3360 "mov %c[r13](%[svm]), %%r13 \n\t"
3361 "mov %c[r14](%[svm]), %%r14 \n\t"
3362 "mov %c[r15](%[svm]), %%r15 \n\t"
3365 /* Enter guest mode */
3367 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
3368 __ex(SVM_VMLOAD) "\n\t"
3369 __ex(SVM_VMRUN) "\n\t"
3370 __ex(SVM_VMSAVE) "\n\t"
3373 /* Save guest registers, load host registers */
3374 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3375 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3376 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3377 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3378 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3379 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
3380 #ifdef CONFIG_X86_64
3381 "mov %%r8, %c[r8](%[svm]) \n\t"
3382 "mov %%r9, %c[r9](%[svm]) \n\t"
3383 "mov %%r10, %c[r10](%[svm]) \n\t"
3384 "mov %%r11, %c[r11](%[svm]) \n\t"
3385 "mov %%r12, %c[r12](%[svm]) \n\t"
3386 "mov %%r13, %c[r13](%[svm]) \n\t"
3387 "mov %%r14, %c[r14](%[svm]) \n\t"
3388 "mov %%r15, %c[r15](%[svm]) \n\t"
3393 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
3394 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3395 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3396 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3397 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3398 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3399 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
3400 #ifdef CONFIG_X86_64
3401 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3402 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3403 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3404 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3405 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3406 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3407 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3408 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
3411 , R"bx", R"cx", R"dx", R"si", R"di"
3412 #ifdef CONFIG_X86_64
3413 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3417 load_host_msrs(vcpu);
3418 kvm_load_ldt(ldt_selector);
3419 loadsegment(fs, fs_selector);
3420 #ifdef CONFIG_X86_64
3421 load_gs_index(gs_selector);
3422 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
3424 loadsegment(gs, gs_selector);
3429 local_irq_disable();
3433 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3434 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3435 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3436 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3438 sync_cr8_to_lapic(vcpu);
3442 /* if exit due to PF check for async PF */
3443 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3444 svm->apf_reason = kvm_read_and_reset_pf_reason();
3447 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3448 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3452 * We need to handle MC intercepts here before the vcpu has a chance to
3453 * change the physical cpu
3455 if (unlikely(svm->vmcb->control.exit_code ==
3456 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3457 svm_handle_mce(svm);
3462 static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3464 struct vcpu_svm *svm = to_svm(vcpu);
3466 svm->vmcb->save.cr3 = root;
3467 force_new_asid(vcpu);
3470 static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3472 struct vcpu_svm *svm = to_svm(vcpu);
3474 svm->vmcb->control.nested_cr3 = root;
3476 /* Also sync guest cr3 here in case we live migrate */
3477 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3479 force_new_asid(vcpu);
3482 static int is_disabled(void)
3486 rdmsrl(MSR_VM_CR, vm_cr);
3487 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3494 svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3497 * Patch in the VMMCALL instruction:
3499 hypercall[0] = 0x0f;
3500 hypercall[1] = 0x01;
3501 hypercall[2] = 0xd9;
3504 static void svm_check_processor_compat(void *rtn)
3509 static bool svm_cpu_has_accelerated_tpr(void)
3514 static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
3519 static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3523 static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3527 /* Mask out xsave bit as long as it is not supported by SVM */
3528 entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
3532 entry->ecx |= (1 << 2); /* Set SVM bit */
3535 entry->eax = 1; /* SVM revision 1 */
3536 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3537 ASID emulation to nested SVM */
3538 entry->ecx = 0; /* Reserved */
3539 entry->edx = 0; /* Per default do not support any
3540 additional features */
3542 /* Support next_rip if host supports it */
3543 if (svm_has(SVM_FEATURE_NRIP))
3544 entry->edx |= SVM_FEATURE_NRIP;
3546 /* Support NPT for the guest if enabled */
3548 entry->edx |= SVM_FEATURE_NPT;
3554 static const struct trace_print_flags svm_exit_reasons_str[] = {
3555 { SVM_EXIT_READ_CR0, "read_cr0" },
3556 { SVM_EXIT_READ_CR3, "read_cr3" },
3557 { SVM_EXIT_READ_CR4, "read_cr4" },
3558 { SVM_EXIT_READ_CR8, "read_cr8" },
3559 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3560 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3561 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3562 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3563 { SVM_EXIT_READ_DR0, "read_dr0" },
3564 { SVM_EXIT_READ_DR1, "read_dr1" },
3565 { SVM_EXIT_READ_DR2, "read_dr2" },
3566 { SVM_EXIT_READ_DR3, "read_dr3" },
3567 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3568 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3569 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3570 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3571 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3572 { SVM_EXIT_WRITE_DR7, "write_dr7" },
3573 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3574 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3575 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3576 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3577 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3578 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3579 { SVM_EXIT_INTR, "interrupt" },
3580 { SVM_EXIT_NMI, "nmi" },
3581 { SVM_EXIT_SMI, "smi" },
3582 { SVM_EXIT_INIT, "init" },
3583 { SVM_EXIT_VINTR, "vintr" },
3584 { SVM_EXIT_CPUID, "cpuid" },
3585 { SVM_EXIT_INVD, "invd" },
3586 { SVM_EXIT_HLT, "hlt" },
3587 { SVM_EXIT_INVLPG, "invlpg" },
3588 { SVM_EXIT_INVLPGA, "invlpga" },
3589 { SVM_EXIT_IOIO, "io" },
3590 { SVM_EXIT_MSR, "msr" },
3591 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3592 { SVM_EXIT_SHUTDOWN, "shutdown" },
3593 { SVM_EXIT_VMRUN, "vmrun" },
3594 { SVM_EXIT_VMMCALL, "hypercall" },
3595 { SVM_EXIT_VMLOAD, "vmload" },
3596 { SVM_EXIT_VMSAVE, "vmsave" },
3597 { SVM_EXIT_STGI, "stgi" },
3598 { SVM_EXIT_CLGI, "clgi" },
3599 { SVM_EXIT_SKINIT, "skinit" },
3600 { SVM_EXIT_WBINVD, "wbinvd" },
3601 { SVM_EXIT_MONITOR, "monitor" },
3602 { SVM_EXIT_MWAIT, "mwait" },
3603 { SVM_EXIT_NPF, "npf" },
3607 static int svm_get_lpage_level(void)
3609 return PT_PDPE_LEVEL;
3612 static bool svm_rdtscp_supported(void)
3617 static bool svm_has_wbinvd_exit(void)
3622 static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3624 struct vcpu_svm *svm = to_svm(vcpu);
3626 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
3628 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3629 update_cr0_intercept(svm);
3632 static struct kvm_x86_ops svm_x86_ops = {
3633 .cpu_has_kvm_support = has_svm,
3634 .disabled_by_bios = is_disabled,
3635 .hardware_setup = svm_hardware_setup,
3636 .hardware_unsetup = svm_hardware_unsetup,
3637 .check_processor_compatibility = svm_check_processor_compat,
3638 .hardware_enable = svm_hardware_enable,
3639 .hardware_disable = svm_hardware_disable,
3640 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
3642 .vcpu_create = svm_create_vcpu,
3643 .vcpu_free = svm_free_vcpu,
3644 .vcpu_reset = svm_vcpu_reset,
3646 .prepare_guest_switch = svm_prepare_guest_switch,
3647 .vcpu_load = svm_vcpu_load,
3648 .vcpu_put = svm_vcpu_put,
3650 .set_guest_debug = svm_guest_debug,
3651 .get_msr = svm_get_msr,
3652 .set_msr = svm_set_msr,
3653 .get_segment_base = svm_get_segment_base,
3654 .get_segment = svm_get_segment,
3655 .set_segment = svm_set_segment,
3656 .get_cpl = svm_get_cpl,
3657 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
3658 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
3659 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
3660 .set_cr0 = svm_set_cr0,
3661 .set_cr3 = svm_set_cr3,
3662 .set_cr4 = svm_set_cr4,
3663 .set_efer = svm_set_efer,
3664 .get_idt = svm_get_idt,
3665 .set_idt = svm_set_idt,
3666 .get_gdt = svm_get_gdt,
3667 .set_gdt = svm_set_gdt,
3668 .set_dr7 = svm_set_dr7,
3669 .cache_reg = svm_cache_reg,
3670 .get_rflags = svm_get_rflags,
3671 .set_rflags = svm_set_rflags,
3672 .fpu_activate = svm_fpu_activate,
3673 .fpu_deactivate = svm_fpu_deactivate,
3675 .tlb_flush = svm_flush_tlb,
3677 .run = svm_vcpu_run,
3678 .handle_exit = handle_exit,
3679 .skip_emulated_instruction = skip_emulated_instruction,
3680 .set_interrupt_shadow = svm_set_interrupt_shadow,
3681 .get_interrupt_shadow = svm_get_interrupt_shadow,
3682 .patch_hypercall = svm_patch_hypercall,
3683 .set_irq = svm_set_irq,
3684 .set_nmi = svm_inject_nmi,
3685 .queue_exception = svm_queue_exception,
3686 .cancel_injection = svm_cancel_injection,
3687 .interrupt_allowed = svm_interrupt_allowed,
3688 .nmi_allowed = svm_nmi_allowed,
3689 .get_nmi_mask = svm_get_nmi_mask,
3690 .set_nmi_mask = svm_set_nmi_mask,
3691 .enable_nmi_window = enable_nmi_window,
3692 .enable_irq_window = enable_irq_window,
3693 .update_cr8_intercept = update_cr8_intercept,
3695 .set_tss_addr = svm_set_tss_addr,
3696 .get_tdp_level = get_npt_level,
3697 .get_mt_mask = svm_get_mt_mask,
3699 .exit_reasons_str = svm_exit_reasons_str,
3700 .get_lpage_level = svm_get_lpage_level,
3702 .cpuid_update = svm_cpuid_update,
3704 .rdtscp_supported = svm_rdtscp_supported,
3706 .set_supported_cpuid = svm_set_supported_cpuid,
3708 .has_wbinvd_exit = svm_has_wbinvd_exit,
3710 .write_tsc_offset = svm_write_tsc_offset,
3711 .adjust_tsc_offset = svm_adjust_tsc_offset,
3713 .set_tdp_cr3 = set_tdp_cr3,
3716 static int __init svm_init(void)
3718 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
3719 __alignof__(struct vcpu_svm), THIS_MODULE);
3722 static void __exit svm_exit(void)
3727 module_init(svm_init)
3728 module_exit(svm_exit)