2 * Kernel-based Virtual Machine driver for Linux
6 * Copyright (C) 2006 Qumranet, Inc.
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
16 #include <linux/kvm_host.h>
21 #include "kvm_cache_regs.h"
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/vmalloc.h>
26 #include <linux/highmem.h>
27 #include <linux/sched.h>
31 #include <asm/virtext.h>
33 #define __ex(x) __kvm_handle_fault_on_reboot(x)
35 MODULE_AUTHOR("Qumranet");
36 MODULE_LICENSE("GPL");
38 #define IOPM_ALLOC_ORDER 2
39 #define MSRPM_ALLOC_ORDER 1
41 #define DR7_GD_MASK (1 << 13)
42 #define DR6_BD_MASK (1 << 13)
44 #define SEG_TYPE_LDT 2
45 #define SEG_TYPE_BUSY_TSS16 3
47 #define SVM_FEATURE_NPT (1 << 0)
48 #define SVM_FEATURE_LBRV (1 << 1)
49 #define SVM_FEATURE_SVML (1 << 2)
51 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
53 /* Turn on to get debugging output*/
54 /* #define NESTED_DEBUG */
57 #define nsvm_printk(fmt, args...) printk(KERN_INFO fmt, ## args)
59 #define nsvm_printk(fmt, args...) do {} while(0)
62 /* enable NPT for AMD64 and X86 with PAE */
63 #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
64 static bool npt_enabled = true;
66 static bool npt_enabled = false;
70 module_param(npt, int, S_IRUGO);
72 static void kvm_reput_irq(struct vcpu_svm *svm);
73 static void svm_flush_tlb(struct kvm_vcpu *vcpu);
75 static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
77 return container_of(vcpu, struct vcpu_svm, vcpu);
80 static inline bool is_nested(struct vcpu_svm *svm)
82 return svm->nested_vmcb;
85 static unsigned long iopm_base;
87 struct kvm_ldttss_desc {
90 unsigned base1 : 8, type : 5, dpl : 2, p : 1;
91 unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
94 } __attribute__((packed));
102 struct kvm_ldttss_desc *tss_desc;
104 struct page *save_area;
107 static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
108 static uint32_t svm_features;
110 struct svm_init_data {
115 static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
117 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
118 #define MSRS_RANGE_SIZE 2048
119 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
121 #define MAX_INST_SIZE 15
123 static inline u32 svm_has(u32 feat)
125 return svm_features & feat;
128 static inline u8 pop_irq(struct kvm_vcpu *vcpu)
130 int word_index = __ffs(vcpu->arch.irq_summary);
131 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
132 int irq = word_index * BITS_PER_LONG + bit_index;
134 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
135 if (!vcpu->arch.irq_pending[word_index])
136 clear_bit(word_index, &vcpu->arch.irq_summary);
140 static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
142 set_bit(irq, vcpu->arch.irq_pending);
143 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
146 static inline void clgi(void)
148 asm volatile (__ex(SVM_CLGI));
151 static inline void stgi(void)
153 asm volatile (__ex(SVM_STGI));
156 static inline void invlpga(unsigned long addr, u32 asid)
158 asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid));
161 static inline unsigned long kvm_read_cr2(void)
165 asm volatile ("mov %%cr2, %0" : "=r" (cr2));
169 static inline void kvm_write_cr2(unsigned long val)
171 asm volatile ("mov %0, %%cr2" :: "r" (val));
174 static inline unsigned long read_dr6(void)
178 asm volatile ("mov %%dr6, %0" : "=r" (dr6));
182 static inline void write_dr6(unsigned long val)
184 asm volatile ("mov %0, %%dr6" :: "r" (val));
187 static inline unsigned long read_dr7(void)
191 asm volatile ("mov %%dr7, %0" : "=r" (dr7));
195 static inline void write_dr7(unsigned long val)
197 asm volatile ("mov %0, %%dr7" :: "r" (val));
200 static inline void force_new_asid(struct kvm_vcpu *vcpu)
202 to_svm(vcpu)->asid_generation--;
205 static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
207 force_new_asid(vcpu);
210 static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
212 if (!npt_enabled && !(efer & EFER_LMA))
215 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
216 vcpu->arch.shadow_efer = efer;
219 static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
220 bool has_error_code, u32 error_code)
222 struct vcpu_svm *svm = to_svm(vcpu);
224 svm->vmcb->control.event_inj = nr
226 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
227 | SVM_EVTINJ_TYPE_EXEPT;
228 svm->vmcb->control.event_inj_err = error_code;
231 static bool svm_exception_injected(struct kvm_vcpu *vcpu)
233 struct vcpu_svm *svm = to_svm(vcpu);
235 return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID);
238 static int is_external_interrupt(u32 info)
240 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
241 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
244 static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
246 struct vcpu_svm *svm = to_svm(vcpu);
248 if (!svm->next_rip) {
249 printk(KERN_DEBUG "%s: NOP\n", __func__);
252 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
253 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
254 __func__, kvm_rip_read(vcpu), svm->next_rip);
256 kvm_rip_write(vcpu, svm->next_rip);
257 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
259 vcpu->arch.interrupt_window_open = (svm->vcpu.arch.hflags & HF_GIF_MASK);
262 static int has_svm(void)
266 if (!cpu_has_svm(&msg)) {
267 printk(KERN_INFO "has_svn: %s\n", msg);
274 static void svm_hardware_disable(void *garbage)
279 static void svm_hardware_enable(void *garbage)
282 struct svm_cpu_data *svm_data;
284 struct desc_ptr gdt_descr;
285 struct desc_struct *gdt;
286 int me = raw_smp_processor_id();
289 printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
292 svm_data = per_cpu(svm_data, me);
295 printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
300 svm_data->asid_generation = 1;
301 svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
302 svm_data->next_asid = svm_data->max_asid + 1;
304 asm volatile ("sgdt %0" : "=m"(gdt_descr));
305 gdt = (struct desc_struct *)gdt_descr.address;
306 svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
308 rdmsrl(MSR_EFER, efer);
309 wrmsrl(MSR_EFER, efer | EFER_SVME);
311 wrmsrl(MSR_VM_HSAVE_PA,
312 page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
315 static void svm_cpu_uninit(int cpu)
317 struct svm_cpu_data *svm_data
318 = per_cpu(svm_data, raw_smp_processor_id());
323 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
324 __free_page(svm_data->save_area);
328 static int svm_cpu_init(int cpu)
330 struct svm_cpu_data *svm_data;
333 svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
337 svm_data->save_area = alloc_page(GFP_KERNEL);
339 if (!svm_data->save_area)
342 per_cpu(svm_data, cpu) = svm_data;
352 static void set_msr_interception(u32 *msrpm, unsigned msr,
357 for (i = 0; i < NUM_MSR_MAPS; i++) {
358 if (msr >= msrpm_ranges[i] &&
359 msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
360 u32 msr_offset = (i * MSRS_IN_RANGE + msr -
361 msrpm_ranges[i]) * 2;
363 u32 *base = msrpm + (msr_offset / 32);
364 u32 msr_shift = msr_offset % 32;
365 u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
366 *base = (*base & ~(0x3 << msr_shift)) |
374 static void svm_vcpu_init_msrpm(u32 *msrpm)
376 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
379 set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
380 set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
381 set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
382 set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
383 set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
384 set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
386 set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
387 set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
388 set_msr_interception(msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
389 set_msr_interception(msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
392 static void svm_enable_lbrv(struct vcpu_svm *svm)
394 u32 *msrpm = svm->msrpm;
396 svm->vmcb->control.lbr_ctl = 1;
397 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
398 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
399 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
400 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
403 static void svm_disable_lbrv(struct vcpu_svm *svm)
405 u32 *msrpm = svm->msrpm;
407 svm->vmcb->control.lbr_ctl = 0;
408 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
409 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
410 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
411 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
414 static __init int svm_hardware_setup(void)
417 struct page *iopm_pages;
421 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
426 iopm_va = page_address(iopm_pages);
427 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
428 clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
429 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
431 if (boot_cpu_has(X86_FEATURE_NX))
432 kvm_enable_efer_bits(EFER_NX);
434 for_each_online_cpu(cpu) {
435 r = svm_cpu_init(cpu);
440 svm_features = cpuid_edx(SVM_CPUID_FUNC);
442 if (!svm_has(SVM_FEATURE_NPT))
445 if (npt_enabled && !npt) {
446 printk(KERN_INFO "kvm: Nested Paging disabled\n");
451 printk(KERN_INFO "kvm: Nested Paging enabled\n");
459 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
464 static __exit void svm_hardware_unsetup(void)
468 for_each_online_cpu(cpu)
471 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
475 static void init_seg(struct vmcb_seg *seg)
478 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
479 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
484 static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
487 seg->attrib = SVM_SELECTOR_P_MASK | type;
492 static void init_vmcb(struct vcpu_svm *svm)
494 struct vmcb_control_area *control = &svm->vmcb->control;
495 struct vmcb_save_area *save = &svm->vmcb->save;
497 control->intercept_cr_read = INTERCEPT_CR0_MASK |
501 control->intercept_cr_write = INTERCEPT_CR0_MASK |
506 control->intercept_dr_read = INTERCEPT_DR0_MASK |
511 control->intercept_dr_write = INTERCEPT_DR0_MASK |
518 control->intercept_exceptions = (1 << PF_VECTOR) |
523 control->intercept = (1ULL << INTERCEPT_INTR) |
524 (1ULL << INTERCEPT_NMI) |
525 (1ULL << INTERCEPT_SMI) |
526 (1ULL << INTERCEPT_CPUID) |
527 (1ULL << INTERCEPT_INVD) |
528 (1ULL << INTERCEPT_HLT) |
529 (1ULL << INTERCEPT_INVLPG) |
530 (1ULL << INTERCEPT_INVLPGA) |
531 (1ULL << INTERCEPT_IOIO_PROT) |
532 (1ULL << INTERCEPT_MSR_PROT) |
533 (1ULL << INTERCEPT_TASK_SWITCH) |
534 (1ULL << INTERCEPT_SHUTDOWN) |
535 (1ULL << INTERCEPT_VMRUN) |
536 (1ULL << INTERCEPT_VMMCALL) |
537 (1ULL << INTERCEPT_VMLOAD) |
538 (1ULL << INTERCEPT_VMSAVE) |
539 (1ULL << INTERCEPT_STGI) |
540 (1ULL << INTERCEPT_CLGI) |
541 (1ULL << INTERCEPT_SKINIT) |
542 (1ULL << INTERCEPT_WBINVD) |
543 (1ULL << INTERCEPT_MONITOR) |
544 (1ULL << INTERCEPT_MWAIT);
546 control->iopm_base_pa = iopm_base;
547 control->msrpm_base_pa = __pa(svm->msrpm);
548 control->tsc_offset = 0;
549 control->int_ctl = V_INTR_MASKING_MASK;
557 save->cs.selector = 0xf000;
558 /* Executable/Readable Code Segment */
559 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
560 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
561 save->cs.limit = 0xffff;
563 * cs.base should really be 0xffff0000, but vmx can't handle that, so
564 * be consistent with it.
566 * Replace when we have real mode working for vmx.
568 save->cs.base = 0xf0000;
570 save->gdtr.limit = 0xffff;
571 save->idtr.limit = 0xffff;
573 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
574 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
576 save->efer = EFER_SVME;
577 save->dr6 = 0xffff0ff0;
580 save->rip = 0x0000fff0;
581 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
584 * cr0 val on cpu init should be 0x60000010, we enable cpu
585 * cache by default. the orderly way is to enable cache in bios.
587 save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
588 save->cr4 = X86_CR4_PAE;
592 /* Setup VMCB for Nested Paging */
593 control->nested_ctl = 1;
594 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
595 (1ULL << INTERCEPT_INVLPG));
596 control->intercept_exceptions &= ~(1 << PF_VECTOR);
597 control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
599 control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
601 save->g_pat = 0x0007040600070406ULL;
602 /* enable caching because the QEMU Bios doesn't enable it */
603 save->cr0 = X86_CR0_ET;
607 force_new_asid(&svm->vcpu);
609 svm->nested_vmcb = 0;
610 svm->vcpu.arch.hflags = HF_GIF_MASK;
613 static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
615 struct vcpu_svm *svm = to_svm(vcpu);
619 if (vcpu->vcpu_id != 0) {
620 kvm_rip_write(vcpu, 0);
621 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
622 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
624 vcpu->arch.regs_avail = ~0;
625 vcpu->arch.regs_dirty = ~0;
630 static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
632 struct vcpu_svm *svm;
634 struct page *msrpm_pages;
635 struct page *hsave_page;
636 struct page *nested_msrpm_pages;
639 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
645 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
649 page = alloc_page(GFP_KERNEL);
656 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
660 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
661 if (!nested_msrpm_pages)
664 svm->msrpm = page_address(msrpm_pages);
665 svm_vcpu_init_msrpm(svm->msrpm);
667 hsave_page = alloc_page(GFP_KERNEL);
670 svm->hsave = page_address(hsave_page);
672 svm->nested_msrpm = page_address(nested_msrpm_pages);
674 svm->vmcb = page_address(page);
675 clear_page(svm->vmcb);
676 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
677 svm->asid_generation = 0;
678 memset(svm->db_regs, 0, sizeof(svm->db_regs));
682 svm->vcpu.fpu_active = 1;
683 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
684 if (svm->vcpu.vcpu_id == 0)
685 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
690 kvm_vcpu_uninit(&svm->vcpu);
692 kmem_cache_free(kvm_vcpu_cache, svm);
697 static void svm_free_vcpu(struct kvm_vcpu *vcpu)
699 struct vcpu_svm *svm = to_svm(vcpu);
701 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
702 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
703 __free_page(virt_to_page(svm->hsave));
704 __free_pages(virt_to_page(svm->nested_msrpm), MSRPM_ALLOC_ORDER);
705 kvm_vcpu_uninit(vcpu);
706 kmem_cache_free(kvm_vcpu_cache, svm);
709 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
711 struct vcpu_svm *svm = to_svm(vcpu);
714 if (unlikely(cpu != vcpu->cpu)) {
718 * Make sure that the guest sees a monotonically
722 delta = vcpu->arch.host_tsc - tsc_this;
723 svm->vmcb->control.tsc_offset += delta;
725 kvm_migrate_timers(vcpu);
728 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
729 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
732 static void svm_vcpu_put(struct kvm_vcpu *vcpu)
734 struct vcpu_svm *svm = to_svm(vcpu);
737 ++vcpu->stat.host_state_reload;
738 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
739 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
741 rdtscll(vcpu->arch.host_tsc);
744 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
746 return to_svm(vcpu)->vmcb->save.rflags;
749 static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
751 to_svm(vcpu)->vmcb->save.rflags = rflags;
754 static void svm_set_vintr(struct vcpu_svm *svm)
756 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
759 static void svm_clear_vintr(struct vcpu_svm *svm)
761 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
764 static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
766 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
769 case VCPU_SREG_CS: return &save->cs;
770 case VCPU_SREG_DS: return &save->ds;
771 case VCPU_SREG_ES: return &save->es;
772 case VCPU_SREG_FS: return &save->fs;
773 case VCPU_SREG_GS: return &save->gs;
774 case VCPU_SREG_SS: return &save->ss;
775 case VCPU_SREG_TR: return &save->tr;
776 case VCPU_SREG_LDTR: return &save->ldtr;
782 static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
784 struct vmcb_seg *s = svm_seg(vcpu, seg);
789 static void svm_get_segment(struct kvm_vcpu *vcpu,
790 struct kvm_segment *var, int seg)
792 struct vmcb_seg *s = svm_seg(vcpu, seg);
795 var->limit = s->limit;
796 var->selector = s->selector;
797 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
798 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
799 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
800 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
801 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
802 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
803 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
804 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
807 * SVM always stores 0 for the 'G' bit in the CS selector in
808 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
809 * Intel's VMENTRY has a check on the 'G' bit.
811 if (seg == VCPU_SREG_CS)
812 var->g = s->limit > 0xfffff;
815 * Work around a bug where the busy flag in the tr selector
818 if (seg == VCPU_SREG_TR)
821 var->unusable = !var->present;
824 static int svm_get_cpl(struct kvm_vcpu *vcpu)
826 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
831 static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
833 struct vcpu_svm *svm = to_svm(vcpu);
835 dt->limit = svm->vmcb->save.idtr.limit;
836 dt->base = svm->vmcb->save.idtr.base;
839 static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
841 struct vcpu_svm *svm = to_svm(vcpu);
843 svm->vmcb->save.idtr.limit = dt->limit;
844 svm->vmcb->save.idtr.base = dt->base ;
847 static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
849 struct vcpu_svm *svm = to_svm(vcpu);
851 dt->limit = svm->vmcb->save.gdtr.limit;
852 dt->base = svm->vmcb->save.gdtr.base;
855 static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
857 struct vcpu_svm *svm = to_svm(vcpu);
859 svm->vmcb->save.gdtr.limit = dt->limit;
860 svm->vmcb->save.gdtr.base = dt->base ;
863 static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
867 static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
869 struct vcpu_svm *svm = to_svm(vcpu);
872 if (vcpu->arch.shadow_efer & EFER_LME) {
873 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
874 vcpu->arch.shadow_efer |= EFER_LMA;
875 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
878 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
879 vcpu->arch.shadow_efer &= ~EFER_LMA;
880 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
887 if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
888 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
889 vcpu->fpu_active = 1;
892 vcpu->arch.cr0 = cr0;
893 cr0 |= X86_CR0_PG | X86_CR0_WP;
894 if (!vcpu->fpu_active) {
895 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
900 * re-enable caching here because the QEMU bios
901 * does not do it - this results in some delay at
904 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
905 svm->vmcb->save.cr0 = cr0;
908 static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
910 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
911 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
913 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
914 force_new_asid(vcpu);
916 vcpu->arch.cr4 = cr4;
920 to_svm(vcpu)->vmcb->save.cr4 = cr4;
923 static void svm_set_segment(struct kvm_vcpu *vcpu,
924 struct kvm_segment *var, int seg)
926 struct vcpu_svm *svm = to_svm(vcpu);
927 struct vmcb_seg *s = svm_seg(vcpu, seg);
930 s->limit = var->limit;
931 s->selector = var->selector;
935 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
936 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
937 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
938 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
939 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
940 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
941 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
942 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
944 if (seg == VCPU_SREG_CS)
946 = (svm->vmcb->save.cs.attrib
947 >> SVM_SELECTOR_DPL_SHIFT) & 3;
951 static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
956 static int svm_get_irq(struct kvm_vcpu *vcpu)
958 struct vcpu_svm *svm = to_svm(vcpu);
959 u32 exit_int_info = svm->vmcb->control.exit_int_info;
961 if (is_external_interrupt(exit_int_info))
962 return exit_int_info & SVM_EVTINJ_VEC_MASK;
966 static void load_host_msrs(struct kvm_vcpu *vcpu)
969 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
973 static void save_host_msrs(struct kvm_vcpu *vcpu)
976 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
980 static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
982 if (svm_data->next_asid > svm_data->max_asid) {
983 ++svm_data->asid_generation;
984 svm_data->next_asid = 1;
985 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
988 svm->vcpu.cpu = svm_data->cpu;
989 svm->asid_generation = svm_data->asid_generation;
990 svm->vmcb->control.asid = svm_data->next_asid++;
993 static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
995 unsigned long val = to_svm(vcpu)->db_regs[dr];
996 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
1000 static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
1003 struct vcpu_svm *svm = to_svm(vcpu);
1007 if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
1008 svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
1009 svm->vmcb->save.dr6 |= DR6_BD_MASK;
1010 *exception = DB_VECTOR;
1016 svm->db_regs[dr] = value;
1019 if (vcpu->arch.cr4 & X86_CR4_DE) {
1020 *exception = UD_VECTOR;
1024 if (value & ~((1ULL << 32) - 1)) {
1025 *exception = GP_VECTOR;
1028 svm->vmcb->save.dr7 = value;
1032 printk(KERN_DEBUG "%s: unexpected dr %u\n",
1034 *exception = UD_VECTOR;
1039 static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1041 u32 exit_int_info = svm->vmcb->control.exit_int_info;
1042 struct kvm *kvm = svm->vcpu.kvm;
1045 bool event_injection = false;
1047 if (!irqchip_in_kernel(kvm) &&
1048 is_external_interrupt(exit_int_info)) {
1049 event_injection = true;
1050 push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
1053 fault_address = svm->vmcb->control.exit_info_2;
1054 error_code = svm->vmcb->control.exit_info_1;
1057 KVMTRACE_3D(PAGE_FAULT, &svm->vcpu, error_code,
1058 (u32)fault_address, (u32)(fault_address >> 32),
1061 KVMTRACE_3D(TDP_FAULT, &svm->vcpu, error_code,
1062 (u32)fault_address, (u32)(fault_address >> 32),
1065 * FIXME: Tis shouldn't be necessary here, but there is a flush
1066 * missing in the MMU code. Until we find this bug, flush the
1067 * complete TLB here on an NPF
1070 svm_flush_tlb(&svm->vcpu);
1072 if (!npt_enabled && event_injection)
1073 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1074 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1077 static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1081 er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
1082 if (er != EMULATE_DONE)
1083 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1087 static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1089 svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
1090 if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
1091 svm->vmcb->save.cr0 &= ~X86_CR0_TS;
1092 svm->vcpu.fpu_active = 1;
1097 static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1100 * On an #MC intercept the MCE handler is not called automatically in
1101 * the host. So do it by hand here.
1105 /* not sure if we ever come back to this point */
1110 static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1113 * VMCB is undefined after a SHUTDOWN intercept
1114 * so reinitialize it.
1116 clear_page(svm->vmcb);
1119 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1123 static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1125 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
1126 int size, down, in, string, rep;
1129 ++svm->vcpu.stat.io_exits;
1131 svm->next_rip = svm->vmcb->control.exit_info_2;
1133 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1136 if (emulate_instruction(&svm->vcpu,
1137 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
1142 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1143 port = io_info >> 16;
1144 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
1145 rep = (io_info & SVM_IOIO_REP_MASK) != 0;
1146 down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
1148 skip_emulated_instruction(&svm->vcpu);
1149 return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
1152 static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1154 KVMTRACE_0D(NMI, &svm->vcpu, handler);
1158 static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1160 ++svm->vcpu.stat.irq_exits;
1161 KVMTRACE_0D(INTR, &svm->vcpu, handler);
1165 static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1170 static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1172 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
1173 skip_emulated_instruction(&svm->vcpu);
1174 return kvm_emulate_halt(&svm->vcpu);
1177 static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1179 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1180 skip_emulated_instruction(&svm->vcpu);
1181 kvm_emulate_hypercall(&svm->vcpu);
1185 static int nested_svm_check_permissions(struct vcpu_svm *svm)
1187 if (!(svm->vcpu.arch.shadow_efer & EFER_SVME)
1188 || !is_paging(&svm->vcpu)) {
1189 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1193 if (svm->vmcb->save.cpl) {
1194 kvm_inject_gp(&svm->vcpu, 0);
1201 static struct page *nested_svm_get_page(struct vcpu_svm *svm, u64 gpa)
1205 down_read(¤t->mm->mmap_sem);
1206 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
1207 up_read(¤t->mm->mmap_sem);
1209 if (is_error_page(page)) {
1210 printk(KERN_INFO "%s: could not find page at 0x%llx\n",
1212 kvm_release_page_clean(page);
1213 kvm_inject_gp(&svm->vcpu, 0);
1219 static int nested_svm_do(struct vcpu_svm *svm,
1220 u64 arg1_gpa, u64 arg2_gpa, void *opaque,
1221 int (*handler)(struct vcpu_svm *svm,
1226 struct page *arg1_page;
1227 struct page *arg2_page = NULL;
1232 arg1_page = nested_svm_get_page(svm, arg1_gpa);
1233 if(arg1_page == NULL)
1237 arg2_page = nested_svm_get_page(svm, arg2_gpa);
1238 if(arg2_page == NULL) {
1239 kvm_release_page_clean(arg1_page);
1244 arg1 = kmap_atomic(arg1_page, KM_USER0);
1246 arg2 = kmap_atomic(arg2_page, KM_USER1);
1248 retval = handler(svm, arg1, arg2, opaque);
1250 kunmap_atomic(arg1, KM_USER0);
1252 kunmap_atomic(arg2, KM_USER1);
1254 kvm_release_page_dirty(arg1_page);
1256 kvm_release_page_dirty(arg2_page);
1262 static int nested_svm_vmrun_msrpm(struct vcpu_svm *svm, void *arg1,
1263 void *arg2, void *opaque)
1266 u32 *nested_msrpm = (u32*)arg1;
1267 for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
1268 svm->nested_msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
1269 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested_msrpm);
1274 static int nested_svm_vmrun(struct vcpu_svm *svm, void *arg1,
1275 void *arg2, void *opaque)
1277 struct vmcb *nested_vmcb = (struct vmcb *)arg1;
1278 struct vmcb *hsave = svm->hsave;
1280 /* nested_vmcb is our indicator if nested SVM is activated */
1281 svm->nested_vmcb = svm->vmcb->save.rax;
1283 /* Clear internal status */
1284 svm->vcpu.arch.exception.pending = false;
1286 /* Save the old vmcb, so we don't need to pick what we save, but
1287 can restore everything when a VMEXIT occurs */
1288 memcpy(hsave, svm->vmcb, sizeof(struct vmcb));
1289 /* We need to remember the original CR3 in the SPT case */
1291 hsave->save.cr3 = svm->vcpu.arch.cr3;
1292 hsave->save.cr4 = svm->vcpu.arch.cr4;
1293 hsave->save.rip = svm->next_rip;
1295 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
1296 svm->vcpu.arch.hflags |= HF_HIF_MASK;
1298 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
1300 /* Load the nested guest state */
1301 svm->vmcb->save.es = nested_vmcb->save.es;
1302 svm->vmcb->save.cs = nested_vmcb->save.cs;
1303 svm->vmcb->save.ss = nested_vmcb->save.ss;
1304 svm->vmcb->save.ds = nested_vmcb->save.ds;
1305 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
1306 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
1307 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
1308 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
1309 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
1310 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
1312 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
1313 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
1315 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
1316 kvm_mmu_reset_context(&svm->vcpu);
1318 svm->vmcb->save.cr2 = nested_vmcb->save.cr2;
1319 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
1320 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
1321 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
1322 /* In case we don't even reach vcpu_run, the fields are not updated */
1323 svm->vmcb->save.rax = nested_vmcb->save.rax;
1324 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
1325 svm->vmcb->save.rip = nested_vmcb->save.rip;
1326 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
1327 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
1328 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
1330 /* We don't want a nested guest to be more powerful than the guest,
1331 so all intercepts are ORed */
1332 svm->vmcb->control.intercept_cr_read |=
1333 nested_vmcb->control.intercept_cr_read;
1334 svm->vmcb->control.intercept_cr_write |=
1335 nested_vmcb->control.intercept_cr_write;
1336 svm->vmcb->control.intercept_dr_read |=
1337 nested_vmcb->control.intercept_dr_read;
1338 svm->vmcb->control.intercept_dr_write |=
1339 nested_vmcb->control.intercept_dr_write;
1340 svm->vmcb->control.intercept_exceptions |=
1341 nested_vmcb->control.intercept_exceptions;
1343 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
1345 svm->nested_vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
1347 force_new_asid(&svm->vcpu);
1348 svm->vmcb->control.exit_int_info = nested_vmcb->control.exit_int_info;
1349 svm->vmcb->control.exit_int_info_err = nested_vmcb->control.exit_int_info_err;
1350 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
1351 if (nested_vmcb->control.int_ctl & V_IRQ_MASK) {
1352 nsvm_printk("nSVM Injecting Interrupt: 0x%x\n",
1353 nested_vmcb->control.int_ctl);
1355 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
1356 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
1358 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
1360 nsvm_printk("nSVM exit_int_info: 0x%x | int_state: 0x%x\n",
1361 nested_vmcb->control.exit_int_info,
1362 nested_vmcb->control.int_state);
1364 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
1365 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
1366 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
1367 if (nested_vmcb->control.event_inj & SVM_EVTINJ_VALID)
1368 nsvm_printk("Injecting Event: 0x%x\n",
1369 nested_vmcb->control.event_inj);
1370 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
1371 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
1373 svm->vcpu.arch.hflags |= HF_GIF_MASK;
1378 static int nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
1380 to_vmcb->save.fs = from_vmcb->save.fs;
1381 to_vmcb->save.gs = from_vmcb->save.gs;
1382 to_vmcb->save.tr = from_vmcb->save.tr;
1383 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
1384 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
1385 to_vmcb->save.star = from_vmcb->save.star;
1386 to_vmcb->save.lstar = from_vmcb->save.lstar;
1387 to_vmcb->save.cstar = from_vmcb->save.cstar;
1388 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
1389 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
1390 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
1391 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
1396 static int nested_svm_vmload(struct vcpu_svm *svm, void *nested_vmcb,
1397 void *arg2, void *opaque)
1399 return nested_svm_vmloadsave((struct vmcb *)nested_vmcb, svm->vmcb);
1402 static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb,
1403 void *arg2, void *opaque)
1405 return nested_svm_vmloadsave(svm->vmcb, (struct vmcb *)nested_vmcb);
1408 static int vmload_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1410 if (nested_svm_check_permissions(svm))
1413 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1414 skip_emulated_instruction(&svm->vcpu);
1416 nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmload);
1421 static int vmsave_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1423 if (nested_svm_check_permissions(svm))
1426 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1427 skip_emulated_instruction(&svm->vcpu);
1429 nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmsave);
1434 static int vmrun_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1436 nsvm_printk("VMrun\n");
1437 if (nested_svm_check_permissions(svm))
1440 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1441 skip_emulated_instruction(&svm->vcpu);
1443 if (nested_svm_do(svm, svm->vmcb->save.rax, 0,
1444 NULL, nested_svm_vmrun))
1447 if (nested_svm_do(svm, svm->nested_vmcb_msrpm, 0,
1448 NULL, nested_svm_vmrun_msrpm))
1454 static int stgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1456 if (nested_svm_check_permissions(svm))
1459 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1460 skip_emulated_instruction(&svm->vcpu);
1462 svm->vcpu.arch.hflags |= HF_GIF_MASK;
1467 static int clgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1469 if (nested_svm_check_permissions(svm))
1472 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
1473 skip_emulated_instruction(&svm->vcpu);
1475 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
1477 /* After a CLGI no interrupts should come */
1478 svm_clear_vintr(svm);
1479 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
1484 static int invalid_op_interception(struct vcpu_svm *svm,
1485 struct kvm_run *kvm_run)
1487 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1491 static int task_switch_interception(struct vcpu_svm *svm,
1492 struct kvm_run *kvm_run)
1496 tss_selector = (u16)svm->vmcb->control.exit_info_1;
1497 if (svm->vmcb->control.exit_info_2 &
1498 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
1499 return kvm_task_switch(&svm->vcpu, tss_selector,
1501 if (svm->vmcb->control.exit_info_2 &
1502 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
1503 return kvm_task_switch(&svm->vcpu, tss_selector,
1505 return kvm_task_switch(&svm->vcpu, tss_selector, TASK_SWITCH_CALL);
1508 static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1510 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
1511 kvm_emulate_cpuid(&svm->vcpu);
1515 static int invlpg_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1517 if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0) != EMULATE_DONE)
1518 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
1522 static int emulate_on_interception(struct vcpu_svm *svm,
1523 struct kvm_run *kvm_run)
1525 if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
1526 pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
1530 static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1532 emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
1533 if (irqchip_in_kernel(svm->vcpu.kvm))
1535 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
1539 static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
1541 struct vcpu_svm *svm = to_svm(vcpu);
1544 case MSR_IA32_TIME_STAMP_COUNTER: {
1548 *data = svm->vmcb->control.tsc_offset + tsc;
1552 *data = svm->vmcb->save.star;
1554 #ifdef CONFIG_X86_64
1556 *data = svm->vmcb->save.lstar;
1559 *data = svm->vmcb->save.cstar;
1561 case MSR_KERNEL_GS_BASE:
1562 *data = svm->vmcb->save.kernel_gs_base;
1564 case MSR_SYSCALL_MASK:
1565 *data = svm->vmcb->save.sfmask;
1568 case MSR_IA32_SYSENTER_CS:
1569 *data = svm->vmcb->save.sysenter_cs;
1571 case MSR_IA32_SYSENTER_EIP:
1572 *data = svm->vmcb->save.sysenter_eip;
1574 case MSR_IA32_SYSENTER_ESP:
1575 *data = svm->vmcb->save.sysenter_esp;
1577 /* Nobody will change the following 5 values in the VMCB so
1578 we can safely return them on rdmsr. They will always be 0
1579 until LBRV is implemented. */
1580 case MSR_IA32_DEBUGCTLMSR:
1581 *data = svm->vmcb->save.dbgctl;
1583 case MSR_IA32_LASTBRANCHFROMIP:
1584 *data = svm->vmcb->save.br_from;
1586 case MSR_IA32_LASTBRANCHTOIP:
1587 *data = svm->vmcb->save.br_to;
1589 case MSR_IA32_LASTINTFROMIP:
1590 *data = svm->vmcb->save.last_excp_from;
1592 case MSR_IA32_LASTINTTOIP:
1593 *data = svm->vmcb->save.last_excp_to;
1595 case MSR_VM_HSAVE_PA:
1596 *data = svm->hsave_msr;
1599 return kvm_get_msr_common(vcpu, ecx, data);
1604 static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1606 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1609 if (svm_get_msr(&svm->vcpu, ecx, &data))
1610 kvm_inject_gp(&svm->vcpu, 0);
1612 KVMTRACE_3D(MSR_READ, &svm->vcpu, ecx, (u32)data,
1613 (u32)(data >> 32), handler);
1615 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
1616 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
1617 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
1618 skip_emulated_instruction(&svm->vcpu);
1623 static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
1625 struct vcpu_svm *svm = to_svm(vcpu);
1628 case MSR_IA32_TIME_STAMP_COUNTER: {
1632 svm->vmcb->control.tsc_offset = data - tsc;
1636 svm->vmcb->save.star = data;
1638 #ifdef CONFIG_X86_64
1640 svm->vmcb->save.lstar = data;
1643 svm->vmcb->save.cstar = data;
1645 case MSR_KERNEL_GS_BASE:
1646 svm->vmcb->save.kernel_gs_base = data;
1648 case MSR_SYSCALL_MASK:
1649 svm->vmcb->save.sfmask = data;
1652 case MSR_IA32_SYSENTER_CS:
1653 svm->vmcb->save.sysenter_cs = data;
1655 case MSR_IA32_SYSENTER_EIP:
1656 svm->vmcb->save.sysenter_eip = data;
1658 case MSR_IA32_SYSENTER_ESP:
1659 svm->vmcb->save.sysenter_esp = data;
1661 case MSR_IA32_DEBUGCTLMSR:
1662 if (!svm_has(SVM_FEATURE_LBRV)) {
1663 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
1667 if (data & DEBUGCTL_RESERVED_BITS)
1670 svm->vmcb->save.dbgctl = data;
1671 if (data & (1ULL<<0))
1672 svm_enable_lbrv(svm);
1674 svm_disable_lbrv(svm);
1676 case MSR_K7_EVNTSEL0:
1677 case MSR_K7_EVNTSEL1:
1678 case MSR_K7_EVNTSEL2:
1679 case MSR_K7_EVNTSEL3:
1680 case MSR_K7_PERFCTR0:
1681 case MSR_K7_PERFCTR1:
1682 case MSR_K7_PERFCTR2:
1683 case MSR_K7_PERFCTR3:
1685 * Just discard all writes to the performance counters; this
1686 * should keep both older linux and windows 64-bit guests
1689 pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", ecx, data);
1692 case MSR_VM_HSAVE_PA:
1693 svm->hsave_msr = data;
1696 return kvm_set_msr_common(vcpu, ecx, data);
1701 static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1703 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1704 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
1705 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
1707 KVMTRACE_3D(MSR_WRITE, &svm->vcpu, ecx, (u32)data, (u32)(data >> 32),
1710 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
1711 if (svm_set_msr(&svm->vcpu, ecx, data))
1712 kvm_inject_gp(&svm->vcpu, 0);
1714 skip_emulated_instruction(&svm->vcpu);
1718 static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
1720 if (svm->vmcb->control.exit_info_1)
1721 return wrmsr_interception(svm, kvm_run);
1723 return rdmsr_interception(svm, kvm_run);
1726 static int interrupt_window_interception(struct vcpu_svm *svm,
1727 struct kvm_run *kvm_run)
1729 KVMTRACE_0D(PEND_INTR, &svm->vcpu, handler);
1731 svm_clear_vintr(svm);
1732 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
1734 * If the user space waits to inject interrupts, exit as soon as
1737 if (kvm_run->request_interrupt_window &&
1738 !svm->vcpu.arch.irq_summary) {
1739 ++svm->vcpu.stat.irq_window_exits;
1740 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
1747 static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
1748 struct kvm_run *kvm_run) = {
1749 [SVM_EXIT_READ_CR0] = emulate_on_interception,
1750 [SVM_EXIT_READ_CR3] = emulate_on_interception,
1751 [SVM_EXIT_READ_CR4] = emulate_on_interception,
1752 [SVM_EXIT_READ_CR8] = emulate_on_interception,
1754 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
1755 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
1756 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
1757 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
1758 [SVM_EXIT_READ_DR0] = emulate_on_interception,
1759 [SVM_EXIT_READ_DR1] = emulate_on_interception,
1760 [SVM_EXIT_READ_DR2] = emulate_on_interception,
1761 [SVM_EXIT_READ_DR3] = emulate_on_interception,
1762 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
1763 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
1764 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
1765 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
1766 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
1767 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
1768 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
1769 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
1770 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
1771 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
1772 [SVM_EXIT_INTR] = intr_interception,
1773 [SVM_EXIT_NMI] = nmi_interception,
1774 [SVM_EXIT_SMI] = nop_on_interception,
1775 [SVM_EXIT_INIT] = nop_on_interception,
1776 [SVM_EXIT_VINTR] = interrupt_window_interception,
1777 /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
1778 [SVM_EXIT_CPUID] = cpuid_interception,
1779 [SVM_EXIT_INVD] = emulate_on_interception,
1780 [SVM_EXIT_HLT] = halt_interception,
1781 [SVM_EXIT_INVLPG] = invlpg_interception,
1782 [SVM_EXIT_INVLPGA] = invalid_op_interception,
1783 [SVM_EXIT_IOIO] = io_interception,
1784 [SVM_EXIT_MSR] = msr_interception,
1785 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
1786 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
1787 [SVM_EXIT_VMRUN] = vmrun_interception,
1788 [SVM_EXIT_VMMCALL] = vmmcall_interception,
1789 [SVM_EXIT_VMLOAD] = vmload_interception,
1790 [SVM_EXIT_VMSAVE] = vmsave_interception,
1791 [SVM_EXIT_STGI] = stgi_interception,
1792 [SVM_EXIT_CLGI] = clgi_interception,
1793 [SVM_EXIT_SKINIT] = invalid_op_interception,
1794 [SVM_EXIT_WBINVD] = emulate_on_interception,
1795 [SVM_EXIT_MONITOR] = invalid_op_interception,
1796 [SVM_EXIT_MWAIT] = invalid_op_interception,
1797 [SVM_EXIT_NPF] = pf_interception,
1800 static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1802 struct vcpu_svm *svm = to_svm(vcpu);
1803 u32 exit_code = svm->vmcb->control.exit_code;
1805 KVMTRACE_3D(VMEXIT, vcpu, exit_code, (u32)svm->vmcb->save.rip,
1806 (u32)((u64)svm->vmcb->save.rip >> 32), entryexit);
1810 if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
1811 svm_set_cr0(vcpu, svm->vmcb->save.cr0);
1814 vcpu->arch.cr0 = svm->vmcb->save.cr0;
1815 vcpu->arch.cr3 = svm->vmcb->save.cr3;
1816 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1817 if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
1818 kvm_inject_gp(vcpu, 0);
1823 kvm_mmu_reset_context(vcpu);
1830 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
1831 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
1832 kvm_run->fail_entry.hardware_entry_failure_reason
1833 = svm->vmcb->control.exit_code;
1837 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
1838 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
1839 exit_code != SVM_EXIT_NPF)
1840 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
1842 __func__, svm->vmcb->control.exit_int_info,
1845 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
1846 || !svm_exit_handlers[exit_code]) {
1847 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1848 kvm_run->hw.hardware_exit_reason = exit_code;
1852 return svm_exit_handlers[exit_code](svm, kvm_run);
1855 static void reload_tss(struct kvm_vcpu *vcpu)
1857 int cpu = raw_smp_processor_id();
1859 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1860 svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
1864 static void pre_svm_run(struct vcpu_svm *svm)
1866 int cpu = raw_smp_processor_id();
1868 struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
1870 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
1871 if (svm->vcpu.cpu != cpu ||
1872 svm->asid_generation != svm_data->asid_generation)
1873 new_asid(svm, svm_data);
1877 static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
1879 struct vmcb_control_area *control;
1881 KVMTRACE_1D(INJ_VIRQ, &svm->vcpu, (u32)irq, handler);
1883 ++svm->vcpu.stat.irq_injections;
1884 control = &svm->vmcb->control;
1885 control->int_vector = irq;
1886 control->int_ctl &= ~V_INTR_PRIO_MASK;
1887 control->int_ctl |= V_IRQ_MASK |
1888 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1891 static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
1893 struct vcpu_svm *svm = to_svm(vcpu);
1895 svm_inject_irq(svm, irq);
1898 static void update_cr8_intercept(struct kvm_vcpu *vcpu)
1900 struct vcpu_svm *svm = to_svm(vcpu);
1901 struct vmcb *vmcb = svm->vmcb;
1904 if (!irqchip_in_kernel(vcpu->kvm) || vcpu->arch.apic->vapic_addr)
1907 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
1909 max_irr = kvm_lapic_find_highest_irr(vcpu);
1913 tpr = kvm_lapic_get_cr8(vcpu) << 4;
1915 if (tpr >= (max_irr & 0xf0))
1916 vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
1919 static void svm_intr_assist(struct kvm_vcpu *vcpu)
1921 struct vcpu_svm *svm = to_svm(vcpu);
1922 struct vmcb *vmcb = svm->vmcb;
1923 int intr_vector = -1;
1925 if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
1926 ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
1927 intr_vector = vmcb->control.exit_int_info &
1928 SVM_EVTINJ_VEC_MASK;
1929 vmcb->control.exit_int_info = 0;
1930 svm_inject_irq(svm, intr_vector);
1934 if (vmcb->control.int_ctl & V_IRQ_MASK)
1937 if (!kvm_cpu_has_interrupt(vcpu))
1940 if (!(svm->vcpu.arch.hflags & HF_GIF_MASK))
1943 if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
1944 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
1945 (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
1946 /* unable to deliver irq, set pending irq */
1948 svm_inject_irq(svm, 0x0);
1951 /* Okay, we can deliver the interrupt: grab it and update PIC state. */
1952 intr_vector = kvm_cpu_get_interrupt(vcpu);
1953 svm_inject_irq(svm, intr_vector);
1955 update_cr8_intercept(vcpu);
1958 static void kvm_reput_irq(struct vcpu_svm *svm)
1960 struct vmcb_control_area *control = &svm->vmcb->control;
1962 if ((control->int_ctl & V_IRQ_MASK)
1963 && !irqchip_in_kernel(svm->vcpu.kvm)) {
1964 control->int_ctl &= ~V_IRQ_MASK;
1965 push_irq(&svm->vcpu, control->int_vector);
1968 svm->vcpu.arch.interrupt_window_open =
1969 !(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
1970 (svm->vcpu.arch.hflags & HF_GIF_MASK);
1973 static void svm_do_inject_vector(struct vcpu_svm *svm)
1975 struct kvm_vcpu *vcpu = &svm->vcpu;
1976 int word_index = __ffs(vcpu->arch.irq_summary);
1977 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
1978 int irq = word_index * BITS_PER_LONG + bit_index;
1980 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
1981 if (!vcpu->arch.irq_pending[word_index])
1982 clear_bit(word_index, &vcpu->arch.irq_summary);
1983 svm_inject_irq(svm, irq);
1986 static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1987 struct kvm_run *kvm_run)
1989 struct vcpu_svm *svm = to_svm(vcpu);
1990 struct vmcb_control_area *control = &svm->vmcb->control;
1992 svm->vcpu.arch.interrupt_window_open =
1993 (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
1994 (svm->vmcb->save.rflags & X86_EFLAGS_IF) &&
1995 (svm->vcpu.arch.hflags & HF_GIF_MASK));
1997 if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
1999 * If interrupts enabled, and not blocked by sti or mov ss. Good.
2001 svm_do_inject_vector(svm);
2004 * Interrupts blocked. Wait for unblock.
2006 if (!svm->vcpu.arch.interrupt_window_open &&
2007 (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
2010 svm_clear_vintr(svm);
2013 static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
2018 static void save_db_regs(unsigned long *db_regs)
2020 asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
2021 asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
2022 asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
2023 asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
2026 static void load_db_regs(unsigned long *db_regs)
2028 asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
2029 asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
2030 asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
2031 asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
2034 static void svm_flush_tlb(struct kvm_vcpu *vcpu)
2036 force_new_asid(vcpu);
2039 static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
2043 static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
2045 struct vcpu_svm *svm = to_svm(vcpu);
2047 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
2048 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
2049 kvm_lapic_set_tpr(vcpu, cr8);
2053 static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
2055 struct vcpu_svm *svm = to_svm(vcpu);
2058 if (!irqchip_in_kernel(vcpu->kvm))
2061 cr8 = kvm_get_cr8(vcpu);
2062 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
2063 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
2066 #ifdef CONFIG_X86_64
2072 static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2074 struct vcpu_svm *svm = to_svm(vcpu);
2079 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
2080 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
2081 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
2085 sync_lapic_to_cr8(vcpu);
2087 save_host_msrs(vcpu);
2088 fs_selector = kvm_read_fs();
2089 gs_selector = kvm_read_gs();
2090 ldt_selector = kvm_read_ldt();
2091 svm->host_cr2 = kvm_read_cr2();
2092 svm->host_dr6 = read_dr6();
2093 svm->host_dr7 = read_dr7();
2094 if (!is_nested(svm))
2095 svm->vmcb->save.cr2 = vcpu->arch.cr2;
2096 /* required for live migration with NPT */
2098 svm->vmcb->save.cr3 = vcpu->arch.cr3;
2100 if (svm->vmcb->save.dr7 & 0xff) {
2102 save_db_regs(svm->host_db_regs);
2103 load_db_regs(svm->db_regs);
2111 "push %%"R"bp; \n\t"
2112 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
2113 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
2114 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
2115 "mov %c[rsi](%[svm]), %%"R"si \n\t"
2116 "mov %c[rdi](%[svm]), %%"R"di \n\t"
2117 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
2118 #ifdef CONFIG_X86_64
2119 "mov %c[r8](%[svm]), %%r8 \n\t"
2120 "mov %c[r9](%[svm]), %%r9 \n\t"
2121 "mov %c[r10](%[svm]), %%r10 \n\t"
2122 "mov %c[r11](%[svm]), %%r11 \n\t"
2123 "mov %c[r12](%[svm]), %%r12 \n\t"
2124 "mov %c[r13](%[svm]), %%r13 \n\t"
2125 "mov %c[r14](%[svm]), %%r14 \n\t"
2126 "mov %c[r15](%[svm]), %%r15 \n\t"
2129 /* Enter guest mode */
2131 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
2132 __ex(SVM_VMLOAD) "\n\t"
2133 __ex(SVM_VMRUN) "\n\t"
2134 __ex(SVM_VMSAVE) "\n\t"
2137 /* Save guest registers, load host registers */
2138 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
2139 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
2140 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
2141 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
2142 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
2143 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
2144 #ifdef CONFIG_X86_64
2145 "mov %%r8, %c[r8](%[svm]) \n\t"
2146 "mov %%r9, %c[r9](%[svm]) \n\t"
2147 "mov %%r10, %c[r10](%[svm]) \n\t"
2148 "mov %%r11, %c[r11](%[svm]) \n\t"
2149 "mov %%r12, %c[r12](%[svm]) \n\t"
2150 "mov %%r13, %c[r13](%[svm]) \n\t"
2151 "mov %%r14, %c[r14](%[svm]) \n\t"
2152 "mov %%r15, %c[r15](%[svm]) \n\t"
2157 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
2158 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
2159 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
2160 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
2161 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
2162 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
2163 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
2164 #ifdef CONFIG_X86_64
2165 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
2166 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
2167 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
2168 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
2169 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
2170 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
2171 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
2172 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
2175 , R"bx", R"cx", R"dx", R"si", R"di"
2176 #ifdef CONFIG_X86_64
2177 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
2181 if ((svm->vmcb->save.dr7 & 0xff))
2182 load_db_regs(svm->host_db_regs);
2184 vcpu->arch.cr2 = svm->vmcb->save.cr2;
2185 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
2186 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
2187 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
2189 write_dr6(svm->host_dr6);
2190 write_dr7(svm->host_dr7);
2191 kvm_write_cr2(svm->host_cr2);
2193 kvm_load_fs(fs_selector);
2194 kvm_load_gs(gs_selector);
2195 kvm_load_ldt(ldt_selector);
2196 load_host_msrs(vcpu);
2200 local_irq_disable();
2204 sync_cr8_to_lapic(vcpu);
2211 static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
2213 struct vcpu_svm *svm = to_svm(vcpu);
2216 svm->vmcb->control.nested_cr3 = root;
2217 force_new_asid(vcpu);
2221 svm->vmcb->save.cr3 = root;
2222 force_new_asid(vcpu);
2224 if (vcpu->fpu_active) {
2225 svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
2226 svm->vmcb->save.cr0 |= X86_CR0_TS;
2227 vcpu->fpu_active = 0;
2231 static int is_disabled(void)
2235 rdmsrl(MSR_VM_CR, vm_cr);
2236 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
2243 svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2246 * Patch in the VMMCALL instruction:
2248 hypercall[0] = 0x0f;
2249 hypercall[1] = 0x01;
2250 hypercall[2] = 0xd9;
2253 static void svm_check_processor_compat(void *rtn)
2258 static bool svm_cpu_has_accelerated_tpr(void)
2263 static int get_npt_level(void)
2265 #ifdef CONFIG_X86_64
2266 return PT64_ROOT_LEVEL;
2268 return PT32E_ROOT_LEVEL;
2272 static int svm_get_mt_mask_shift(void)
2277 static struct kvm_x86_ops svm_x86_ops = {
2278 .cpu_has_kvm_support = has_svm,
2279 .disabled_by_bios = is_disabled,
2280 .hardware_setup = svm_hardware_setup,
2281 .hardware_unsetup = svm_hardware_unsetup,
2282 .check_processor_compatibility = svm_check_processor_compat,
2283 .hardware_enable = svm_hardware_enable,
2284 .hardware_disable = svm_hardware_disable,
2285 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
2287 .vcpu_create = svm_create_vcpu,
2288 .vcpu_free = svm_free_vcpu,
2289 .vcpu_reset = svm_vcpu_reset,
2291 .prepare_guest_switch = svm_prepare_guest_switch,
2292 .vcpu_load = svm_vcpu_load,
2293 .vcpu_put = svm_vcpu_put,
2295 .set_guest_debug = svm_guest_debug,
2296 .get_msr = svm_get_msr,
2297 .set_msr = svm_set_msr,
2298 .get_segment_base = svm_get_segment_base,
2299 .get_segment = svm_get_segment,
2300 .set_segment = svm_set_segment,
2301 .get_cpl = svm_get_cpl,
2302 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
2303 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
2304 .set_cr0 = svm_set_cr0,
2305 .set_cr3 = svm_set_cr3,
2306 .set_cr4 = svm_set_cr4,
2307 .set_efer = svm_set_efer,
2308 .get_idt = svm_get_idt,
2309 .set_idt = svm_set_idt,
2310 .get_gdt = svm_get_gdt,
2311 .set_gdt = svm_set_gdt,
2312 .get_dr = svm_get_dr,
2313 .set_dr = svm_set_dr,
2314 .get_rflags = svm_get_rflags,
2315 .set_rflags = svm_set_rflags,
2317 .tlb_flush = svm_flush_tlb,
2319 .run = svm_vcpu_run,
2320 .handle_exit = handle_exit,
2321 .skip_emulated_instruction = skip_emulated_instruction,
2322 .patch_hypercall = svm_patch_hypercall,
2323 .get_irq = svm_get_irq,
2324 .set_irq = svm_set_irq,
2325 .queue_exception = svm_queue_exception,
2326 .exception_injected = svm_exception_injected,
2327 .inject_pending_irq = svm_intr_assist,
2328 .inject_pending_vectors = do_interrupt_requests,
2330 .set_tss_addr = svm_set_tss_addr,
2331 .get_tdp_level = get_npt_level,
2332 .get_mt_mask_shift = svm_get_mt_mask_shift,
2335 static int __init svm_init(void)
2337 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
2341 static void __exit svm_exit(void)
2346 module_init(svm_init)
2347 module_exit(svm_exit)