x86.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #/*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This header defines architecture specific interfaces, x86 version
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2. See
  7. * the COPYING file in the top-level directory.
  8. *
  9. */
  10. #ifndef KVM_X86_H
  11. #define KVM_X86_H
  12. #include "kvm.h"
  13. #include <linux/types.h>
  14. #include <linux/mm.h>
  15. #include <linux/kvm.h>
  16. #include <linux/kvm_para.h>
  17. #define CR3_PAE_RESERVED_BITS ((X86_CR3_PWT | X86_CR3_PCD) - 1)
  18. #define CR3_NONPAE_RESERVED_BITS ((PAGE_SIZE-1) & ~(X86_CR3_PWT | X86_CR3_PCD))
  19. #define CR3_L_MODE_RESERVED_BITS (CR3_NONPAE_RESERVED_BITS|0xFFFFFF0000000000ULL)
  20. #define KVM_GUEST_CR0_MASK \
  21. (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE \
  22. | X86_CR0_NW | X86_CR0_CD)
  23. #define KVM_VM_CR0_ALWAYS_ON \
  24. (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE | X86_CR0_TS \
  25. | X86_CR0_MP)
  26. #define KVM_GUEST_CR4_MASK \
  27. (X86_CR4_VME | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_VMXE)
  28. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  29. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  30. #define INVALID_PAGE (~(hpa_t)0)
  31. #define UNMAPPED_GVA (~(gpa_t)0)
  32. #define DE_VECTOR 0
  33. #define UD_VECTOR 6
  34. #define NM_VECTOR 7
  35. #define DF_VECTOR 8
  36. #define TS_VECTOR 10
  37. #define NP_VECTOR 11
  38. #define SS_VECTOR 12
  39. #define GP_VECTOR 13
  40. #define PF_VECTOR 14
  41. #define SELECTOR_TI_MASK (1 << 2)
  42. #define SELECTOR_RPL_MASK 0x03
  43. #define IOPL_SHIFT 12
  44. extern spinlock_t kvm_lock;
  45. extern struct list_head vm_list;
  46. enum {
  47. VCPU_REGS_RAX = 0,
  48. VCPU_REGS_RCX = 1,
  49. VCPU_REGS_RDX = 2,
  50. VCPU_REGS_RBX = 3,
  51. VCPU_REGS_RSP = 4,
  52. VCPU_REGS_RBP = 5,
  53. VCPU_REGS_RSI = 6,
  54. VCPU_REGS_RDI = 7,
  55. #ifdef CONFIG_X86_64
  56. VCPU_REGS_R8 = 8,
  57. VCPU_REGS_R9 = 9,
  58. VCPU_REGS_R10 = 10,
  59. VCPU_REGS_R11 = 11,
  60. VCPU_REGS_R12 = 12,
  61. VCPU_REGS_R13 = 13,
  62. VCPU_REGS_R14 = 14,
  63. VCPU_REGS_R15 = 15,
  64. #endif
  65. NR_VCPU_REGS
  66. };
  67. enum {
  68. VCPU_SREG_CS,
  69. VCPU_SREG_DS,
  70. VCPU_SREG_ES,
  71. VCPU_SREG_FS,
  72. VCPU_SREG_GS,
  73. VCPU_SREG_SS,
  74. VCPU_SREG_TR,
  75. VCPU_SREG_LDTR,
  76. };
  77. #include "x86_emulate.h"
  78. struct kvm_vcpu {
  79. KVM_VCPU_COMM;
  80. u64 host_tsc;
  81. int interrupt_window_open;
  82. unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
  83. DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
  84. unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
  85. unsigned long rip; /* needs vcpu_load_rsp_rip() */
  86. unsigned long cr0;
  87. unsigned long cr2;
  88. unsigned long cr3;
  89. unsigned long cr4;
  90. unsigned long cr8;
  91. u64 pdptrs[4]; /* pae */
  92. u64 shadow_efer;
  93. u64 apic_base;
  94. struct kvm_lapic *apic; /* kernel irqchip context */
  95. #define VCPU_MP_STATE_RUNNABLE 0
  96. #define VCPU_MP_STATE_UNINITIALIZED 1
  97. #define VCPU_MP_STATE_INIT_RECEIVED 2
  98. #define VCPU_MP_STATE_SIPI_RECEIVED 3
  99. #define VCPU_MP_STATE_HALTED 4
  100. int mp_state;
  101. int sipi_vector;
  102. u64 ia32_misc_enable_msr;
  103. struct kvm_mmu mmu;
  104. struct kvm_mmu_memory_cache mmu_pte_chain_cache;
  105. struct kvm_mmu_memory_cache mmu_rmap_desc_cache;
  106. struct kvm_mmu_memory_cache mmu_page_cache;
  107. struct kvm_mmu_memory_cache mmu_page_header_cache;
  108. gfn_t last_pt_write_gfn;
  109. int last_pt_write_count;
  110. u64 *last_pte_updated;
  111. struct i387_fxsave_struct host_fx_image;
  112. struct i387_fxsave_struct guest_fx_image;
  113. gva_t mmio_fault_cr2;
  114. struct kvm_pio_request pio;
  115. void *pio_data;
  116. struct {
  117. int active;
  118. u8 save_iopl;
  119. struct kvm_save_segment {
  120. u16 selector;
  121. unsigned long base;
  122. u32 limit;
  123. u32 ar;
  124. } tr, es, ds, fs, gs;
  125. } rmode;
  126. int halt_request; /* real mode on Intel only */
  127. int cpuid_nent;
  128. struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
  129. /* emulate context */
  130. struct x86_emulate_ctxt emulate_ctxt;
  131. };
  132. struct kvm_x86_ops {
  133. int (*cpu_has_kvm_support)(void); /* __init */
  134. int (*disabled_by_bios)(void); /* __init */
  135. void (*hardware_enable)(void *dummy); /* __init */
  136. void (*hardware_disable)(void *dummy);
  137. void (*check_processor_compatibility)(void *rtn);
  138. int (*hardware_setup)(void); /* __init */
  139. void (*hardware_unsetup)(void); /* __exit */
  140. /* Create, but do not attach this VCPU */
  141. struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
  142. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  143. int (*vcpu_reset)(struct kvm_vcpu *vcpu);
  144. void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
  145. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  146. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  147. void (*vcpu_decache)(struct kvm_vcpu *vcpu);
  148. int (*set_guest_debug)(struct kvm_vcpu *vcpu,
  149. struct kvm_debug_guest *dbg);
  150. void (*guest_debug_pre)(struct kvm_vcpu *vcpu);
  151. int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
  152. int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  153. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  154. void (*get_segment)(struct kvm_vcpu *vcpu,
  155. struct kvm_segment *var, int seg);
  156. void (*set_segment)(struct kvm_vcpu *vcpu,
  157. struct kvm_segment *var, int seg);
  158. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  159. void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  160. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  161. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  162. void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  163. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  164. void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  165. void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  166. void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  167. void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  168. unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
  169. void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  170. int *exception);
  171. void (*cache_regs)(struct kvm_vcpu *vcpu);
  172. void (*decache_regs)(struct kvm_vcpu *vcpu);
  173. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  174. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  175. void (*tlb_flush)(struct kvm_vcpu *vcpu);
  176. void (*inject_page_fault)(struct kvm_vcpu *vcpu,
  177. unsigned long addr, u32 err_code);
  178. void (*inject_gp)(struct kvm_vcpu *vcpu, unsigned err_code);
  179. void (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
  180. int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  181. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  182. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  183. unsigned char *hypercall_addr);
  184. int (*get_irq)(struct kvm_vcpu *vcpu);
  185. void (*set_irq)(struct kvm_vcpu *vcpu, int vec);
  186. void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
  187. void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
  188. struct kvm_run *run);
  189. int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
  190. };
  191. extern struct kvm_x86_ops *kvm_x86_ops;
  192. int kvm_mmu_module_init(void);
  193. void kvm_mmu_module_exit(void);
  194. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  195. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  196. int kvm_mmu_setup(struct kvm_vcpu *vcpu);
  197. void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
  198. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  199. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
  200. void kvm_mmu_zap_all(struct kvm *kvm);
  201. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm);
  202. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
  203. enum emulation_result {
  204. EMULATE_DONE, /* no further processing */
  205. EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
  206. EMULATE_FAIL, /* can't emulate this instruction */
  207. };
  208. int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
  209. unsigned long cr2, u16 error_code, int no_decode);
  210. void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char *context);
  211. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  212. void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  213. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  214. unsigned long *rflags);
  215. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
  216. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
  217. unsigned long *rflags);
  218. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
  219. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  220. struct x86_emulate_ctxt;
  221. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  222. int size, unsigned port);
  223. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  224. int size, unsigned long count, int down,
  225. gva_t address, int rep, unsigned port);
  226. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
  227. int kvm_emulate_halt(struct kvm_vcpu *vcpu);
  228. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
  229. int emulate_clts(struct kvm_vcpu *vcpu);
  230. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
  231. unsigned long *dest);
  232. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
  233. unsigned long value);
  234. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  235. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr0);
  236. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr0);
  237. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr0);
  238. unsigned long get_cr8(struct kvm_vcpu *vcpu);
  239. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  240. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
  241. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  242. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  243. void fx_init(struct kvm_vcpu *vcpu);
  244. int emulator_read_std(unsigned long addr,
  245. void *val,
  246. unsigned int bytes,
  247. struct kvm_vcpu *vcpu);
  248. int emulator_write_emulated(unsigned long addr,
  249. const void *val,
  250. unsigned int bytes,
  251. struct kvm_vcpu *vcpu);
  252. unsigned long segment_base(u16 selector);
  253. void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu);
  254. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  255. const u8 *new, int bytes);
  256. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  257. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  258. int kvm_mmu_load(struct kvm_vcpu *vcpu);
  259. void kvm_mmu_unload(struct kvm_vcpu *vcpu);
  260. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
  261. int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
  262. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code);
  263. static inline void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
  264. {
  265. if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
  266. __kvm_mmu_free_some_pages(vcpu);
  267. }
  268. static inline int kvm_mmu_reload(struct kvm_vcpu *vcpu)
  269. {
  270. if (likely(vcpu->mmu.root_hpa != INVALID_PAGE))
  271. return 0;
  272. return kvm_mmu_load(vcpu);
  273. }
  274. static inline int is_long_mode(struct kvm_vcpu *vcpu)
  275. {
  276. #ifdef CONFIG_X86_64
  277. return vcpu->shadow_efer & EFER_LME;
  278. #else
  279. return 0;
  280. #endif
  281. }
  282. static inline int is_pae(struct kvm_vcpu *vcpu)
  283. {
  284. return vcpu->cr4 & X86_CR4_PAE;
  285. }
  286. static inline int is_pse(struct kvm_vcpu *vcpu)
  287. {
  288. return vcpu->cr4 & X86_CR4_PSE;
  289. }
  290. static inline int is_paging(struct kvm_vcpu *vcpu)
  291. {
  292. return vcpu->cr0 & X86_CR0_PG;
  293. }
  294. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
  295. int complete_pio(struct kvm_vcpu *vcpu);
  296. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  297. {
  298. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  299. return (struct kvm_mmu_page *)page_private(page);
  300. }
  301. static inline u16 read_fs(void)
  302. {
  303. u16 seg;
  304. asm("mov %%fs, %0" : "=g"(seg));
  305. return seg;
  306. }
  307. static inline u16 read_gs(void)
  308. {
  309. u16 seg;
  310. asm("mov %%gs, %0" : "=g"(seg));
  311. return seg;
  312. }
  313. static inline u16 read_ldt(void)
  314. {
  315. u16 ldt;
  316. asm("sldt %0" : "=g"(ldt));
  317. return ldt;
  318. }
  319. static inline void load_fs(u16 sel)
  320. {
  321. asm("mov %0, %%fs" : : "rm"(sel));
  322. }
  323. static inline void load_gs(u16 sel)
  324. {
  325. asm("mov %0, %%gs" : : "rm"(sel));
  326. }
  327. #ifndef load_ldt
  328. static inline void load_ldt(u16 sel)
  329. {
  330. asm("lldt %0" : : "rm"(sel));
  331. }
  332. #endif
  333. static inline void get_idt(struct descriptor_table *table)
  334. {
  335. asm("sidt %0" : "=m"(*table));
  336. }
  337. static inline void get_gdt(struct descriptor_table *table)
  338. {
  339. asm("sgdt %0" : "=m"(*table));
  340. }
  341. static inline unsigned long read_tr_base(void)
  342. {
  343. u16 tr;
  344. asm("str %0" : "=g"(tr));
  345. return segment_base(tr);
  346. }
  347. #ifdef CONFIG_X86_64
  348. static inline unsigned long read_msr(unsigned long msr)
  349. {
  350. u64 value;
  351. rdmsrl(msr, value);
  352. return value;
  353. }
  354. #endif
  355. static inline void fx_save(struct i387_fxsave_struct *image)
  356. {
  357. asm("fxsave (%0)":: "r" (image));
  358. }
  359. static inline void fx_restore(struct i387_fxsave_struct *image)
  360. {
  361. asm("fxrstor (%0)":: "r" (image));
  362. }
  363. static inline void fpu_init(void)
  364. {
  365. asm("finit");
  366. }
  367. static inline u32 get_rdx_init_val(void)
  368. {
  369. return 0x600; /* P6 family */
  370. }
  371. #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
  372. #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
  373. #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
  374. #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
  375. #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
  376. #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
  377. #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
  378. #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
  379. #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
  380. #define MSR_IA32_TIME_STAMP_COUNTER 0x010
  381. #define TSS_IOPB_BASE_OFFSET 0x66
  382. #define TSS_BASE_SIZE 0x68
  383. #define TSS_IOPB_SIZE (65536 / 8)
  384. #define TSS_REDIRECTION_SIZE (256 / 8)
  385. #define RMODE_TSS_SIZE (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  386. #endif