5 * This work is licensed under the terms of the GNU GPL, version 2. See
6 * the COPYING file in the top-level directory.
9 #include <linux/types.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/spinlock.h>
13 #include <linux/signal.h>
14 #include <linux/sched.h>
16 #include <asm/signal.h>
19 #include <linux/kvm.h>
20 #include <linux/kvm_para.h>
22 #define CR0_PE_MASK (1ULL << 0)
23 #define CR0_MP_MASK (1ULL << 1)
24 #define CR0_TS_MASK (1ULL << 3)
25 #define CR0_NE_MASK (1ULL << 5)
26 #define CR0_WP_MASK (1ULL << 16)
27 #define CR0_NW_MASK (1ULL << 29)
28 #define CR0_CD_MASK (1ULL << 30)
29 #define CR0_PG_MASK (1ULL << 31)
31 #define CR3_WPT_MASK (1ULL << 3)
32 #define CR3_PCD_MASK (1ULL << 4)
34 #define CR3_RESEVED_BITS 0x07ULL
35 #define CR3_L_MODE_RESEVED_BITS (~((1ULL << 40) - 1) | 0x0fe7ULL)
36 #define CR3_FLAGS_MASK ((1ULL << 5) - 1)
38 #define CR4_VME_MASK (1ULL << 0)
39 #define CR4_PSE_MASK (1ULL << 4)
40 #define CR4_PAE_MASK (1ULL << 5)
41 #define CR4_PGE_MASK (1ULL << 7)
42 #define CR4_VMXE_MASK (1ULL << 13)
44 #define KVM_GUEST_CR0_MASK \
45 (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK \
46 | CR0_NW_MASK | CR0_CD_MASK)
47 #define KVM_VM_CR0_ALWAYS_ON \
48 (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK | CR0_TS_MASK \
50 #define KVM_GUEST_CR4_MASK \
51 (CR4_PSE_MASK | CR4_PAE_MASK | CR4_PGE_MASK | CR4_VMXE_MASK | CR4_VME_MASK)
52 #define KVM_PMODE_VM_CR4_ALWAYS_ON (CR4_VMXE_MASK | CR4_PAE_MASK)
53 #define KVM_RMODE_VM_CR4_ALWAYS_ON (CR4_VMXE_MASK | CR4_PAE_MASK | CR4_VME_MASK)
55 #define INVALID_PAGE (~(hpa_t)0)
56 #define UNMAPPED_GVA (~(gpa_t)0)
58 #define KVM_MAX_VCPUS 4
59 #define KVM_ALIAS_SLOTS 4
60 #define KVM_MEMORY_SLOTS 4
61 #define KVM_NUM_MMU_PAGES 1024
62 #define KVM_MIN_FREE_MMU_PAGES 5
63 #define KVM_REFILL_PAGES 25
64 #define KVM_MAX_CPUID_ENTRIES 40
66 #define FX_IMAGE_SIZE 512
67 #define FX_IMAGE_ALIGN 16
68 #define FX_BUF_SIZE (2 * FX_IMAGE_SIZE + FX_IMAGE_ALIGN)
79 #define SELECTOR_TI_MASK (1 << 2)
80 #define SELECTOR_RPL_MASK 0x03
84 #define KVM_PIO_PAGE_OFFSET 1
87 * vcpu->requests bit members
89 #define KVM_TLB_FLUSH 0
94 * gva - guest virtual address
95 * gpa - guest physical address
96 * gfn - guest frame number
97 * hva - host virtual address
98 * hpa - host physical address
99 * hfn - host frame number
102 typedef unsigned long gva_t;
104 typedef unsigned long gfn_t;
106 typedef unsigned long hva_t;
108 typedef unsigned long hfn_t;
110 #define NR_PTE_CHAIN_ENTRIES 5
112 struct kvm_pte_chain {
113 u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES];
114 struct hlist_node link;
118 * kvm_mmu_page_role, below, is defined as:
120 * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
121 * bits 4:7 - page table level for this shadow (1-4)
122 * bits 8:9 - page table quadrant for 2-level guests
123 * bit 16 - "metaphysical" - gfn is not a real page (huge page/real mode)
124 * bits 17:18 - "access" - the user and writable bits of a huge page pde
126 union kvm_mmu_page_role {
129 unsigned glevels : 4;
131 unsigned quadrant : 2;
132 unsigned pad_for_nice_hex_output : 6;
133 unsigned metaphysical : 1;
134 unsigned hugepage_access : 2;
138 struct kvm_mmu_page {
139 struct list_head link;
140 struct hlist_node hash_link;
143 * The following two entries are used to key the shadow page in the
147 union kvm_mmu_page_role role;
150 unsigned long slot_bitmap; /* One bit set per slot which has memory
151 * in this shadow page.
153 int multimapped; /* More than one parent_pte? */
154 int root_count; /* Currently serving as active root */
156 u64 *parent_pte; /* !multimapped */
157 struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */
167 #define vmx_msr_entry kvm_msr_entry
172 * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
173 * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
177 void (*new_cr3)(struct kvm_vcpu *vcpu);
178 int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
179 void (*free)(struct kvm_vcpu *vcpu);
180 gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
183 int shadow_root_level;
188 #define KVM_NR_MEM_OBJS 20
190 struct kvm_mmu_memory_cache {
192 void *objects[KVM_NR_MEM_OBJS];
196 * We don't want allocation failures within the mmu code, so we preallocate
197 * enough memory for a single page fault in a cache.
199 struct kvm_guest_debug {
238 struct kvm_pio_request {
241 struct page *guest_pages[2];
242 unsigned guest_page_offset;
260 u32 irq_window_exits;
262 u32 request_irq_exits;
268 struct kvm_io_device {
269 void (*read)(struct kvm_io_device *this,
273 void (*write)(struct kvm_io_device *this,
277 int (*in_range)(struct kvm_io_device *this, gpa_t addr);
278 void (*destructor)(struct kvm_io_device *this);
283 static inline void kvm_iodevice_read(struct kvm_io_device *dev,
288 dev->read(dev, addr, len, val);
291 static inline void kvm_iodevice_write(struct kvm_io_device *dev,
296 dev->write(dev, addr, len, val);
299 static inline int kvm_iodevice_inrange(struct kvm_io_device *dev, gpa_t addr)
301 return dev->in_range(dev, addr);
304 static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
306 dev->destructor(dev);
310 * It would be nice to use something smarter than a linear search, TBD...
311 * Thankfully we dont expect many devices to register (famous last words :),
312 * so until then it will suffice. At least its abstracted so we can change
317 #define NR_IOBUS_DEVS 6
318 struct kvm_io_device *devs[NR_IOBUS_DEVS];
321 void kvm_io_bus_init(struct kvm_io_bus *bus);
322 void kvm_io_bus_destroy(struct kvm_io_bus *bus);
323 struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr);
324 void kvm_io_bus_register_dev(struct kvm_io_bus *bus,
325 struct kvm_io_device *dev);
331 struct vcpu_svm *svm;
338 int interrupt_window_open;
340 unsigned long requests;
341 unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
342 #define NR_IRQ_WORDS KVM_IRQ_BITMAP_SIZE(unsigned long)
343 unsigned long irq_pending[NR_IRQ_WORDS];
344 unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
345 unsigned long rip; /* needs vcpu_load_rsp_rip() */
350 gpa_t para_state_gpa;
351 struct page *para_state_page;
355 u64 pdptrs[4]; /* pae */
358 u64 ia32_misc_enable_msr;
363 int msr_offset_kernel_gs_base;
365 struct vmx_msr_entry *guest_msrs;
366 struct vmx_msr_entry *host_msrs;
370 struct kvm_mmu_memory_cache mmu_pte_chain_cache;
371 struct kvm_mmu_memory_cache mmu_rmap_desc_cache;
372 struct kvm_mmu_memory_cache mmu_page_cache;
373 struct kvm_mmu_memory_cache mmu_page_header_cache;
375 gfn_t last_pt_write_gfn;
376 int last_pt_write_count;
378 struct kvm_guest_debug guest_debug;
380 char fx_buf[FX_BUF_SIZE];
382 char *guest_fx_image;
384 int guest_fpu_loaded;
385 struct vmx_host_state {
387 u16 fs_sel, gs_sel, ldt_sel;
388 int fs_gs_ldt_reload_needed;
392 int mmio_read_completed;
395 unsigned char mmio_data[8];
396 gpa_t mmio_phys_addr;
397 gva_t mmio_fault_cr2;
398 struct kvm_pio_request pio;
404 struct kvm_stat stat;
409 struct kvm_save_segment {
414 } tr, es, ds, fs, gs;
416 int halt_request; /* real mode on Intel only */
419 struct kvm_cpuid_entry cpuid_entries[KVM_MAX_CPUID_ENTRIES];
422 struct kvm_mem_alias {
424 unsigned long npages;
428 struct kvm_memory_slot {
430 unsigned long npages;
432 struct page **phys_mem;
433 unsigned long *dirty_bitmap;
437 spinlock_t lock; /* protects everything except vcpus */
439 struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
441 struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS];
443 * Hash table of struct kvm_mmu_page.
445 struct list_head active_mmu_pages;
446 int n_free_mmu_pages;
447 struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
449 struct kvm_vcpu vcpus[KVM_MAX_VCPUS];
450 int memory_config_version;
452 unsigned long rmap_overflow;
453 struct list_head vm_list;
455 struct kvm_io_bus mmio_bus;
458 struct descriptor_table {
461 } __attribute__((packed));
463 struct kvm_arch_ops {
464 int (*cpu_has_kvm_support)(void); /* __init */
465 int (*disabled_by_bios)(void); /* __init */
466 void (*hardware_enable)(void *dummy); /* __init */
467 void (*hardware_disable)(void *dummy);
468 int (*hardware_setup)(void); /* __init */
469 void (*hardware_unsetup)(void); /* __exit */
471 int (*vcpu_create)(struct kvm_vcpu *vcpu);
472 void (*vcpu_free)(struct kvm_vcpu *vcpu);
474 void (*vcpu_load)(struct kvm_vcpu *vcpu);
475 void (*vcpu_put)(struct kvm_vcpu *vcpu);
476 void (*vcpu_decache)(struct kvm_vcpu *vcpu);
478 int (*set_guest_debug)(struct kvm_vcpu *vcpu,
479 struct kvm_debug_guest *dbg);
480 int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
481 int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
482 u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
483 void (*get_segment)(struct kvm_vcpu *vcpu,
484 struct kvm_segment *var, int seg);
485 void (*set_segment)(struct kvm_vcpu *vcpu,
486 struct kvm_segment *var, int seg);
487 void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
488 void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
489 void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
490 void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
491 void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
492 void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
493 void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
494 void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
495 void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
496 void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
497 unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
498 void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
500 void (*cache_regs)(struct kvm_vcpu *vcpu);
501 void (*decache_regs)(struct kvm_vcpu *vcpu);
502 unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
503 void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
505 void (*invlpg)(struct kvm_vcpu *vcpu, gva_t addr);
506 void (*tlb_flush)(struct kvm_vcpu *vcpu);
507 void (*inject_page_fault)(struct kvm_vcpu *vcpu,
508 unsigned long addr, u32 err_code);
510 void (*inject_gp)(struct kvm_vcpu *vcpu, unsigned err_code);
512 int (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
513 int (*vcpu_setup)(struct kvm_vcpu *vcpu);
514 void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
515 void (*patch_hypercall)(struct kvm_vcpu *vcpu,
516 unsigned char *hypercall_addr);
519 extern struct kvm_arch_ops *kvm_arch_ops;
521 #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
522 #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
524 int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module);
525 void kvm_exit_arch(void);
527 int kvm_mmu_module_init(void);
528 void kvm_mmu_module_exit(void);
530 void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
531 int kvm_mmu_create(struct kvm_vcpu *vcpu);
532 int kvm_mmu_setup(struct kvm_vcpu *vcpu);
534 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
535 void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot);
536 void kvm_mmu_zap_all(struct kvm_vcpu *vcpu);
538 hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa);
539 #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
540 #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
541 static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
542 hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva);
543 struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
545 void kvm_emulator_want_group7_invlpg(void);
547 extern hpa_t bad_page_address;
549 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
550 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
551 void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
553 enum emulation_result {
554 EMULATE_DONE, /* no further processing */
555 EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
556 EMULATE_FAIL, /* can't emulate this instruction */
559 int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
560 unsigned long cr2, u16 error_code);
561 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
562 void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
563 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
564 unsigned long *rflags);
566 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
567 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
568 unsigned long *rflags);
570 struct x86_emulate_ctxt;
572 int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
573 int size, unsigned long count, int string, int down,
574 gva_t address, int rep, unsigned port);
575 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
576 int kvm_emulate_halt(struct kvm_vcpu *vcpu);
577 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
578 int emulate_clts(struct kvm_vcpu *vcpu);
579 int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr,
580 unsigned long *dest);
581 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
582 unsigned long value);
584 void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
585 void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr0);
586 void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr0);
587 void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr0);
588 void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
590 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
591 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
593 void fx_init(struct kvm_vcpu *vcpu);
595 void load_msrs(struct vmx_msr_entry *e, int n);
596 void save_msrs(struct vmx_msr_entry *e, int n);
597 void kvm_resched(struct kvm_vcpu *vcpu);
598 void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
599 void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
600 void kvm_flush_remote_tlbs(struct kvm *kvm);
602 int kvm_read_guest(struct kvm_vcpu *vcpu,
607 int kvm_write_guest(struct kvm_vcpu *vcpu,
612 unsigned long segment_base(u16 selector);
614 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
615 const u8 *old, const u8 *new, int bytes);
616 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
617 void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
618 int kvm_mmu_load(struct kvm_vcpu *vcpu);
619 void kvm_mmu_unload(struct kvm_vcpu *vcpu);
621 int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run);
623 static inline int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
626 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
627 kvm_mmu_free_some_pages(vcpu);
628 return vcpu->mmu.page_fault(vcpu, gva, error_code);
631 static inline int kvm_mmu_reload(struct kvm_vcpu *vcpu)
633 if (likely(vcpu->mmu.root_hpa != INVALID_PAGE))
636 return kvm_mmu_load(vcpu);
639 static inline int is_long_mode(struct kvm_vcpu *vcpu)
642 return vcpu->shadow_efer & EFER_LME;
648 static inline int is_pae(struct kvm_vcpu *vcpu)
650 return vcpu->cr4 & CR4_PAE_MASK;
653 static inline int is_pse(struct kvm_vcpu *vcpu)
655 return vcpu->cr4 & CR4_PSE_MASK;
658 static inline int is_paging(struct kvm_vcpu *vcpu)
660 return vcpu->cr0 & CR0_PG_MASK;
663 static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot)
665 return slot - kvm->memslots;
668 static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
670 struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
672 return (struct kvm_mmu_page *)page_private(page);
675 static inline u16 read_fs(void)
678 asm ("mov %%fs, %0" : "=g"(seg));
682 static inline u16 read_gs(void)
685 asm ("mov %%gs, %0" : "=g"(seg));
689 static inline u16 read_ldt(void)
692 asm ("sldt %0" : "=g"(ldt));
696 static inline void load_fs(u16 sel)
698 asm ("mov %0, %%fs" : : "rm"(sel));
701 static inline void load_gs(u16 sel)
703 asm ("mov %0, %%gs" : : "rm"(sel));
707 static inline void load_ldt(u16 sel)
709 asm ("lldt %0" : : "rm"(sel));
713 static inline void get_idt(struct descriptor_table *table)
715 asm ("sidt %0" : "=m"(*table));
718 static inline void get_gdt(struct descriptor_table *table)
720 asm ("sgdt %0" : "=m"(*table));
723 static inline unsigned long read_tr_base(void)
726 asm ("str %0" : "=g"(tr));
727 return segment_base(tr);
731 static inline unsigned long read_msr(unsigned long msr)
740 static inline void fx_save(void *image)
742 asm ("fxsave (%0)":: "r" (image));
745 static inline void fx_restore(void *image)
747 asm ("fxrstor (%0)":: "r" (image));
750 static inline void fpu_init(void)
755 static inline u32 get_rdx_init_val(void)
757 return 0x600; /* P6 family */
760 #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
761 #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
762 #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
763 #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
764 #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
765 #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
766 #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
767 #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
768 #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
770 #define MSR_IA32_TIME_STAMP_COUNTER 0x010
772 #define TSS_IOPB_BASE_OFFSET 0x66
773 #define TSS_BASE_SIZE 0x68
774 #define TSS_IOPB_SIZE (65536 / 8)
775 #define TSS_REDIRECTION_SIZE (256 / 8)
776 #define RMODE_TSS_SIZE (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)