kvm_host.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 ASM_KVM_HOST_H
  11. #define ASM_KVM_HOST_H
  12. #include <linux/types.h>
  13. #include <linux/mm.h>
  14. #include <linux/kvm.h>
  15. #include <linux/kvm_para.h>
  16. #include <linux/kvm_types.h>
  17. #include <asm/desc.h>
  18. #define CR3_PAE_RESERVED_BITS ((X86_CR3_PWT | X86_CR3_PCD) - 1)
  19. #define CR3_NONPAE_RESERVED_BITS ((PAGE_SIZE-1) & ~(X86_CR3_PWT | X86_CR3_PCD))
  20. #define CR3_L_MODE_RESERVED_BITS (CR3_NONPAE_RESERVED_BITS | \
  21. 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. #define KVM_ALIAS_SLOTS 4
  47. #define KVM_PERMILLE_MMU_PAGES 20
  48. #define KVM_MIN_ALLOC_MMU_PAGES 64
  49. #define KVM_MMU_HASH_SHIFT 10
  50. #define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
  51. #define KVM_MIN_FREE_MMU_PAGES 5
  52. #define KVM_REFILL_PAGES 25
  53. #define KVM_MAX_CPUID_ENTRIES 40
  54. extern spinlock_t kvm_lock;
  55. extern struct list_head vm_list;
  56. struct kvm_vcpu;
  57. struct kvm;
  58. enum {
  59. VCPU_REGS_RAX = 0,
  60. VCPU_REGS_RCX = 1,
  61. VCPU_REGS_RDX = 2,
  62. VCPU_REGS_RBX = 3,
  63. VCPU_REGS_RSP = 4,
  64. VCPU_REGS_RBP = 5,
  65. VCPU_REGS_RSI = 6,
  66. VCPU_REGS_RDI = 7,
  67. #ifdef CONFIG_X86_64
  68. VCPU_REGS_R8 = 8,
  69. VCPU_REGS_R9 = 9,
  70. VCPU_REGS_R10 = 10,
  71. VCPU_REGS_R11 = 11,
  72. VCPU_REGS_R12 = 12,
  73. VCPU_REGS_R13 = 13,
  74. VCPU_REGS_R14 = 14,
  75. VCPU_REGS_R15 = 15,
  76. #endif
  77. NR_VCPU_REGS
  78. };
  79. enum {
  80. VCPU_SREG_CS,
  81. VCPU_SREG_DS,
  82. VCPU_SREG_ES,
  83. VCPU_SREG_FS,
  84. VCPU_SREG_GS,
  85. VCPU_SREG_SS,
  86. VCPU_SREG_TR,
  87. VCPU_SREG_LDTR,
  88. };
  89. #include <asm/kvm_x86_emulate.h>
  90. #define KVM_NR_MEM_OBJS 40
  91. /*
  92. * We don't want allocation failures within the mmu code, so we preallocate
  93. * enough memory for a single page fault in a cache.
  94. */
  95. struct kvm_mmu_memory_cache {
  96. int nobjs;
  97. void *objects[KVM_NR_MEM_OBJS];
  98. };
  99. #define NR_PTE_CHAIN_ENTRIES 5
  100. struct kvm_pte_chain {
  101. u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES];
  102. struct hlist_node link;
  103. };
  104. /*
  105. * kvm_mmu_page_role, below, is defined as:
  106. *
  107. * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
  108. * bits 4:7 - page table level for this shadow (1-4)
  109. * bits 8:9 - page table quadrant for 2-level guests
  110. * bit 16 - "metaphysical" - gfn is not a real page (huge page/real mode)
  111. * bits 17:19 - common access permissions for all ptes in this shadow page
  112. */
  113. union kvm_mmu_page_role {
  114. unsigned word;
  115. struct {
  116. unsigned glevels:4;
  117. unsigned level:4;
  118. unsigned quadrant:2;
  119. unsigned pad_for_nice_hex_output:6;
  120. unsigned metaphysical:1;
  121. unsigned access:3;
  122. };
  123. };
  124. struct kvm_mmu_page {
  125. struct list_head link;
  126. struct hlist_node hash_link;
  127. /*
  128. * The following two entries are used to key the shadow page in the
  129. * hash table.
  130. */
  131. gfn_t gfn;
  132. union kvm_mmu_page_role role;
  133. u64 *spt;
  134. /* hold the gfn of each spte inside spt */
  135. gfn_t *gfns;
  136. unsigned long slot_bitmap; /* One bit set per slot which has memory
  137. * in this shadow page.
  138. */
  139. int multimapped; /* More than one parent_pte? */
  140. int root_count; /* Currently serving as active root */
  141. union {
  142. u64 *parent_pte; /* !multimapped */
  143. struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */
  144. };
  145. };
  146. /*
  147. * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
  148. * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
  149. * mode.
  150. */
  151. struct kvm_mmu {
  152. void (*new_cr3)(struct kvm_vcpu *vcpu);
  153. int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
  154. void (*free)(struct kvm_vcpu *vcpu);
  155. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
  156. void (*prefetch_page)(struct kvm_vcpu *vcpu,
  157. struct kvm_mmu_page *page);
  158. hpa_t root_hpa;
  159. int root_level;
  160. int shadow_root_level;
  161. u64 *pae_root;
  162. };
  163. struct kvm_vcpu_arch {
  164. u64 host_tsc;
  165. int interrupt_window_open;
  166. unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
  167. DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
  168. unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
  169. unsigned long rip; /* needs vcpu_load_rsp_rip() */
  170. unsigned long cr0;
  171. unsigned long cr2;
  172. unsigned long cr3;
  173. unsigned long cr4;
  174. unsigned long cr8;
  175. u64 pdptrs[4]; /* pae */
  176. u64 shadow_efer;
  177. u64 apic_base;
  178. struct kvm_lapic *apic; /* kernel irqchip context */
  179. #define VCPU_MP_STATE_RUNNABLE 0
  180. #define VCPU_MP_STATE_UNINITIALIZED 1
  181. #define VCPU_MP_STATE_INIT_RECEIVED 2
  182. #define VCPU_MP_STATE_SIPI_RECEIVED 3
  183. #define VCPU_MP_STATE_HALTED 4
  184. int mp_state;
  185. int sipi_vector;
  186. u64 ia32_misc_enable_msr;
  187. bool tpr_access_reporting;
  188. struct kvm_mmu mmu;
  189. struct kvm_mmu_memory_cache mmu_pte_chain_cache;
  190. struct kvm_mmu_memory_cache mmu_rmap_desc_cache;
  191. struct kvm_mmu_memory_cache mmu_page_cache;
  192. struct kvm_mmu_memory_cache mmu_page_header_cache;
  193. gfn_t last_pt_write_gfn;
  194. int last_pt_write_count;
  195. u64 *last_pte_updated;
  196. struct {
  197. gfn_t gfn; /* presumed gfn during guest pte update */
  198. struct page *page; /* page corresponding to that gfn */
  199. } update_pte;
  200. struct i387_fxsave_struct host_fx_image;
  201. struct i387_fxsave_struct guest_fx_image;
  202. gva_t mmio_fault_cr2;
  203. struct kvm_pio_request pio;
  204. void *pio_data;
  205. struct kvm_queued_exception {
  206. bool pending;
  207. bool has_error_code;
  208. u8 nr;
  209. u32 error_code;
  210. } exception;
  211. struct {
  212. int active;
  213. u8 save_iopl;
  214. struct kvm_save_segment {
  215. u16 selector;
  216. unsigned long base;
  217. u32 limit;
  218. u32 ar;
  219. } tr, es, ds, fs, gs;
  220. } rmode;
  221. int halt_request; /* real mode on Intel only */
  222. int cpuid_nent;
  223. struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
  224. /* emulate context */
  225. struct x86_emulate_ctxt emulate_ctxt;
  226. gpa_t time;
  227. struct kvm_vcpu_time_info hv_clock;
  228. unsigned int time_offset;
  229. struct page *time_page;
  230. };
  231. struct kvm_mem_alias {
  232. gfn_t base_gfn;
  233. unsigned long npages;
  234. gfn_t target_gfn;
  235. };
  236. struct kvm_arch{
  237. int naliases;
  238. struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
  239. unsigned int n_free_mmu_pages;
  240. unsigned int n_requested_mmu_pages;
  241. unsigned int n_alloc_mmu_pages;
  242. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  243. /*
  244. * Hash table of struct kvm_mmu_page.
  245. */
  246. struct list_head active_mmu_pages;
  247. struct kvm_pic *vpic;
  248. struct kvm_ioapic *vioapic;
  249. int round_robin_prev_vcpu;
  250. unsigned int tss_addr;
  251. struct page *apic_access_page;
  252. gpa_t wall_clock;
  253. };
  254. struct kvm_vm_stat {
  255. u32 mmu_shadow_zapped;
  256. u32 mmu_pte_write;
  257. u32 mmu_pte_updated;
  258. u32 mmu_pde_zapped;
  259. u32 mmu_flooded;
  260. u32 mmu_recycled;
  261. u32 mmu_cache_miss;
  262. u32 remote_tlb_flush;
  263. };
  264. struct kvm_vcpu_stat {
  265. u32 pf_fixed;
  266. u32 pf_guest;
  267. u32 tlb_flush;
  268. u32 invlpg;
  269. u32 exits;
  270. u32 io_exits;
  271. u32 mmio_exits;
  272. u32 signal_exits;
  273. u32 irq_window_exits;
  274. u32 halt_exits;
  275. u32 halt_wakeup;
  276. u32 request_irq_exits;
  277. u32 irq_exits;
  278. u32 host_state_reload;
  279. u32 efer_reload;
  280. u32 fpu_reload;
  281. u32 insn_emulation;
  282. u32 insn_emulation_fail;
  283. };
  284. struct descriptor_table {
  285. u16 limit;
  286. unsigned long base;
  287. } __attribute__((packed));
  288. struct kvm_x86_ops {
  289. int (*cpu_has_kvm_support)(void); /* __init */
  290. int (*disabled_by_bios)(void); /* __init */
  291. void (*hardware_enable)(void *dummy); /* __init */
  292. void (*hardware_disable)(void *dummy);
  293. void (*check_processor_compatibility)(void *rtn);
  294. int (*hardware_setup)(void); /* __init */
  295. void (*hardware_unsetup)(void); /* __exit */
  296. bool (*cpu_has_accelerated_tpr)(void);
  297. /* Create, but do not attach this VCPU */
  298. struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
  299. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  300. int (*vcpu_reset)(struct kvm_vcpu *vcpu);
  301. void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
  302. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  303. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  304. void (*vcpu_decache)(struct kvm_vcpu *vcpu);
  305. int (*set_guest_debug)(struct kvm_vcpu *vcpu,
  306. struct kvm_debug_guest *dbg);
  307. void (*guest_debug_pre)(struct kvm_vcpu *vcpu);
  308. int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
  309. int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  310. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  311. void (*get_segment)(struct kvm_vcpu *vcpu,
  312. struct kvm_segment *var, int seg);
  313. void (*set_segment)(struct kvm_vcpu *vcpu,
  314. struct kvm_segment *var, int seg);
  315. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  316. void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  317. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  318. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  319. void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  320. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  321. void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  322. void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  323. void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  324. void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  325. unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
  326. void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  327. int *exception);
  328. void (*cache_regs)(struct kvm_vcpu *vcpu);
  329. void (*decache_regs)(struct kvm_vcpu *vcpu);
  330. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  331. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  332. void (*tlb_flush)(struct kvm_vcpu *vcpu);
  333. void (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
  334. int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  335. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  336. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  337. unsigned char *hypercall_addr);
  338. int (*get_irq)(struct kvm_vcpu *vcpu);
  339. void (*set_irq)(struct kvm_vcpu *vcpu, int vec);
  340. void (*queue_exception)(struct kvm_vcpu *vcpu, unsigned nr,
  341. bool has_error_code, u32 error_code);
  342. bool (*exception_injected)(struct kvm_vcpu *vcpu);
  343. void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
  344. void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
  345. struct kvm_run *run);
  346. int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
  347. };
  348. extern struct kvm_x86_ops *kvm_x86_ops;
  349. int kvm_mmu_module_init(void);
  350. void kvm_mmu_module_exit(void);
  351. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  352. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  353. int kvm_mmu_setup(struct kvm_vcpu *vcpu);
  354. void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
  355. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  356. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
  357. void kvm_mmu_zap_all(struct kvm *kvm);
  358. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm);
  359. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
  360. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
  361. enum emulation_result {
  362. EMULATE_DONE, /* no further processing */
  363. EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
  364. EMULATE_FAIL, /* can't emulate this instruction */
  365. };
  366. #define EMULTYPE_NO_DECODE (1 << 0)
  367. #define EMULTYPE_TRAP_UD (1 << 1)
  368. int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
  369. unsigned long cr2, u16 error_code, int emulation_type);
  370. void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char *context);
  371. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  372. void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  373. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  374. unsigned long *rflags);
  375. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
  376. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
  377. unsigned long *rflags);
  378. void kvm_enable_efer_bits(u64);
  379. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
  380. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  381. struct x86_emulate_ctxt;
  382. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  383. int size, unsigned port);
  384. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  385. int size, unsigned long count, int down,
  386. gva_t address, int rep, unsigned port);
  387. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
  388. int kvm_emulate_halt(struct kvm_vcpu *vcpu);
  389. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
  390. int emulate_clts(struct kvm_vcpu *vcpu);
  391. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
  392. unsigned long *dest);
  393. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
  394. unsigned long value);
  395. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  396. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr0);
  397. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr0);
  398. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr0);
  399. unsigned long get_cr8(struct kvm_vcpu *vcpu);
  400. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  401. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
  402. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  403. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  404. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
  405. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
  406. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long cr2,
  407. u32 error_code);
  408. void fx_init(struct kvm_vcpu *vcpu);
  409. int emulator_read_std(unsigned long addr,
  410. void *val,
  411. unsigned int bytes,
  412. struct kvm_vcpu *vcpu);
  413. int emulator_write_emulated(unsigned long addr,
  414. const void *val,
  415. unsigned int bytes,
  416. struct kvm_vcpu *vcpu);
  417. unsigned long segment_base(u16 selector);
  418. void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu);
  419. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  420. const u8 *new, int bytes);
  421. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  422. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  423. int kvm_mmu_load(struct kvm_vcpu *vcpu);
  424. void kvm_mmu_unload(struct kvm_vcpu *vcpu);
  425. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
  426. int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
  427. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code);
  428. void kvm_enable_tdp(void);
  429. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
  430. int complete_pio(struct kvm_vcpu *vcpu);
  431. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  432. {
  433. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  434. return (struct kvm_mmu_page *)page_private(page);
  435. }
  436. static inline u16 read_fs(void)
  437. {
  438. u16 seg;
  439. asm("mov %%fs, %0" : "=g"(seg));
  440. return seg;
  441. }
  442. static inline u16 read_gs(void)
  443. {
  444. u16 seg;
  445. asm("mov %%gs, %0" : "=g"(seg));
  446. return seg;
  447. }
  448. static inline u16 read_ldt(void)
  449. {
  450. u16 ldt;
  451. asm("sldt %0" : "=g"(ldt));
  452. return ldt;
  453. }
  454. static inline void load_fs(u16 sel)
  455. {
  456. asm("mov %0, %%fs" : : "rm"(sel));
  457. }
  458. static inline void load_gs(u16 sel)
  459. {
  460. asm("mov %0, %%gs" : : "rm"(sel));
  461. }
  462. #ifndef load_ldt
  463. static inline void load_ldt(u16 sel)
  464. {
  465. asm("lldt %0" : : "rm"(sel));
  466. }
  467. #endif
  468. static inline void get_idt(struct descriptor_table *table)
  469. {
  470. asm("sidt %0" : "=m"(*table));
  471. }
  472. static inline void get_gdt(struct descriptor_table *table)
  473. {
  474. asm("sgdt %0" : "=m"(*table));
  475. }
  476. static inline unsigned long read_tr_base(void)
  477. {
  478. u16 tr;
  479. asm("str %0" : "=g"(tr));
  480. return segment_base(tr);
  481. }
  482. #ifdef CONFIG_X86_64
  483. static inline unsigned long read_msr(unsigned long msr)
  484. {
  485. u64 value;
  486. rdmsrl(msr, value);
  487. return value;
  488. }
  489. #endif
  490. static inline void fx_save(struct i387_fxsave_struct *image)
  491. {
  492. asm("fxsave (%0)":: "r" (image));
  493. }
  494. static inline void fx_restore(struct i387_fxsave_struct *image)
  495. {
  496. asm("fxrstor (%0)":: "r" (image));
  497. }
  498. static inline void fpu_init(void)
  499. {
  500. asm("finit");
  501. }
  502. static inline u32 get_rdx_init_val(void)
  503. {
  504. return 0x600; /* P6 family */
  505. }
  506. static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
  507. {
  508. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  509. }
  510. #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
  511. #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
  512. #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
  513. #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
  514. #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
  515. #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
  516. #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
  517. #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
  518. #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
  519. #define ASM_VMX_INVVPID ".byte 0x66, 0x0f, 0x38, 0x81, 0x08"
  520. #define MSR_IA32_TIME_STAMP_COUNTER 0x010
  521. #define TSS_IOPB_BASE_OFFSET 0x66
  522. #define TSS_BASE_SIZE 0x68
  523. #define TSS_IOPB_SIZE (65536 / 8)
  524. #define TSS_REDIRECTION_SIZE (256 / 8)
  525. #define RMODE_TSS_SIZE \
  526. (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  527. #endif