x86.h 14 KB

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