kvm.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. #ifndef __KVM_H
  2. #define __KVM_H
  3. /*
  4. * This work is licensed under the terms of the GNU GPL, version 2. See
  5. * the COPYING file in the top-level directory.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/list.h>
  9. #include <linux/mutex.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/mm.h>
  12. #include <asm/signal.h>
  13. #include "vmx.h"
  14. #include <linux/kvm.h>
  15. #include <linux/kvm_para.h>
  16. #define CR0_PE_MASK (1ULL << 0)
  17. #define CR0_TS_MASK (1ULL << 3)
  18. #define CR0_NE_MASK (1ULL << 5)
  19. #define CR0_WP_MASK (1ULL << 16)
  20. #define CR0_NW_MASK (1ULL << 29)
  21. #define CR0_CD_MASK (1ULL << 30)
  22. #define CR0_PG_MASK (1ULL << 31)
  23. #define CR3_WPT_MASK (1ULL << 3)
  24. #define CR3_PCD_MASK (1ULL << 4)
  25. #define CR3_RESEVED_BITS 0x07ULL
  26. #define CR3_L_MODE_RESEVED_BITS (~((1ULL << 40) - 1) | 0x0fe7ULL)
  27. #define CR3_FLAGS_MASK ((1ULL << 5) - 1)
  28. #define CR4_VME_MASK (1ULL << 0)
  29. #define CR4_PSE_MASK (1ULL << 4)
  30. #define CR4_PAE_MASK (1ULL << 5)
  31. #define CR4_PGE_MASK (1ULL << 7)
  32. #define CR4_VMXE_MASK (1ULL << 13)
  33. #define KVM_GUEST_CR0_MASK \
  34. (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK \
  35. | CR0_NW_MASK | CR0_CD_MASK)
  36. #define KVM_VM_CR0_ALWAYS_ON \
  37. (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK | CR0_TS_MASK)
  38. #define KVM_GUEST_CR4_MASK \
  39. (CR4_PSE_MASK | CR4_PAE_MASK | CR4_PGE_MASK | CR4_VMXE_MASK | CR4_VME_MASK)
  40. #define KVM_PMODE_VM_CR4_ALWAYS_ON (CR4_VMXE_MASK | CR4_PAE_MASK)
  41. #define KVM_RMODE_VM_CR4_ALWAYS_ON (CR4_VMXE_MASK | CR4_PAE_MASK | CR4_VME_MASK)
  42. #define INVALID_PAGE (~(hpa_t)0)
  43. #define UNMAPPED_GVA (~(gpa_t)0)
  44. #define KVM_MAX_VCPUS 1
  45. #define KVM_ALIAS_SLOTS 4
  46. #define KVM_MEMORY_SLOTS 4
  47. #define KVM_NUM_MMU_PAGES 1024
  48. #define KVM_MIN_FREE_MMU_PAGES 5
  49. #define KVM_REFILL_PAGES 25
  50. #define KVM_MAX_CPUID_ENTRIES 40
  51. #define FX_IMAGE_SIZE 512
  52. #define FX_IMAGE_ALIGN 16
  53. #define FX_BUF_SIZE (2 * FX_IMAGE_SIZE + FX_IMAGE_ALIGN)
  54. #define DE_VECTOR 0
  55. #define NM_VECTOR 7
  56. #define DF_VECTOR 8
  57. #define TS_VECTOR 10
  58. #define NP_VECTOR 11
  59. #define SS_VECTOR 12
  60. #define GP_VECTOR 13
  61. #define PF_VECTOR 14
  62. #define SELECTOR_TI_MASK (1 << 2)
  63. #define SELECTOR_RPL_MASK 0x03
  64. #define IOPL_SHIFT 12
  65. #define KVM_PIO_PAGE_OFFSET 1
  66. /*
  67. * Address types:
  68. *
  69. * gva - guest virtual address
  70. * gpa - guest physical address
  71. * gfn - guest frame number
  72. * hva - host virtual address
  73. * hpa - host physical address
  74. * hfn - host frame number
  75. */
  76. typedef unsigned long gva_t;
  77. typedef u64 gpa_t;
  78. typedef unsigned long gfn_t;
  79. typedef unsigned long hva_t;
  80. typedef u64 hpa_t;
  81. typedef unsigned long hfn_t;
  82. #define NR_PTE_CHAIN_ENTRIES 5
  83. struct kvm_pte_chain {
  84. u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES];
  85. struct hlist_node link;
  86. };
  87. /*
  88. * kvm_mmu_page_role, below, is defined as:
  89. *
  90. * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
  91. * bits 4:7 - page table level for this shadow (1-4)
  92. * bits 8:9 - page table quadrant for 2-level guests
  93. * bit 16 - "metaphysical" - gfn is not a real page (huge page/real mode)
  94. * bits 17:18 - "access" - the user and writable bits of a huge page pde
  95. */
  96. union kvm_mmu_page_role {
  97. unsigned word;
  98. struct {
  99. unsigned glevels : 4;
  100. unsigned level : 4;
  101. unsigned quadrant : 2;
  102. unsigned pad_for_nice_hex_output : 6;
  103. unsigned metaphysical : 1;
  104. unsigned hugepage_access : 2;
  105. };
  106. };
  107. struct kvm_mmu_page {
  108. struct list_head link;
  109. struct hlist_node hash_link;
  110. /*
  111. * The following two entries are used to key the shadow page in the
  112. * hash table.
  113. */
  114. gfn_t gfn;
  115. union kvm_mmu_page_role role;
  116. hpa_t page_hpa;
  117. unsigned long slot_bitmap; /* One bit set per slot which has memory
  118. * in this shadow page.
  119. */
  120. int multimapped; /* More than one parent_pte? */
  121. int root_count; /* Currently serving as active root */
  122. union {
  123. u64 *parent_pte; /* !multimapped */
  124. struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */
  125. };
  126. };
  127. struct vmcs {
  128. u32 revision_id;
  129. u32 abort;
  130. char data[0];
  131. };
  132. #define vmx_msr_entry kvm_msr_entry
  133. struct kvm_vcpu;
  134. /*
  135. * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
  136. * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
  137. * mode.
  138. */
  139. struct kvm_mmu {
  140. void (*new_cr3)(struct kvm_vcpu *vcpu);
  141. int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
  142. void (*free)(struct kvm_vcpu *vcpu);
  143. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
  144. hpa_t root_hpa;
  145. int root_level;
  146. int shadow_root_level;
  147. u64 *pae_root;
  148. };
  149. #define KVM_NR_MEM_OBJS 20
  150. struct kvm_mmu_memory_cache {
  151. int nobjs;
  152. void *objects[KVM_NR_MEM_OBJS];
  153. };
  154. /*
  155. * We don't want allocation failures within the mmu code, so we preallocate
  156. * enough memory for a single page fault in a cache.
  157. */
  158. struct kvm_guest_debug {
  159. int enabled;
  160. unsigned long bp[4];
  161. int singlestep;
  162. };
  163. enum {
  164. VCPU_REGS_RAX = 0,
  165. VCPU_REGS_RCX = 1,
  166. VCPU_REGS_RDX = 2,
  167. VCPU_REGS_RBX = 3,
  168. VCPU_REGS_RSP = 4,
  169. VCPU_REGS_RBP = 5,
  170. VCPU_REGS_RSI = 6,
  171. VCPU_REGS_RDI = 7,
  172. #ifdef CONFIG_X86_64
  173. VCPU_REGS_R8 = 8,
  174. VCPU_REGS_R9 = 9,
  175. VCPU_REGS_R10 = 10,
  176. VCPU_REGS_R11 = 11,
  177. VCPU_REGS_R12 = 12,
  178. VCPU_REGS_R13 = 13,
  179. VCPU_REGS_R14 = 14,
  180. VCPU_REGS_R15 = 15,
  181. #endif
  182. NR_VCPU_REGS
  183. };
  184. enum {
  185. VCPU_SREG_CS,
  186. VCPU_SREG_DS,
  187. VCPU_SREG_ES,
  188. VCPU_SREG_FS,
  189. VCPU_SREG_GS,
  190. VCPU_SREG_SS,
  191. VCPU_SREG_TR,
  192. VCPU_SREG_LDTR,
  193. };
  194. struct kvm_pio_request {
  195. unsigned long count;
  196. int cur_count;
  197. struct page *guest_pages[2];
  198. unsigned guest_page_offset;
  199. int in;
  200. int size;
  201. int string;
  202. int down;
  203. int rep;
  204. };
  205. struct kvm_stat {
  206. u32 pf_fixed;
  207. u32 pf_guest;
  208. u32 tlb_flush;
  209. u32 invlpg;
  210. u32 exits;
  211. u32 io_exits;
  212. u32 mmio_exits;
  213. u32 signal_exits;
  214. u32 irq_window_exits;
  215. u32 halt_exits;
  216. u32 request_irq_exits;
  217. u32 irq_exits;
  218. u32 light_exits;
  219. };
  220. struct kvm_vcpu {
  221. struct kvm *kvm;
  222. union {
  223. struct vmcs *vmcs;
  224. struct vcpu_svm *svm;
  225. };
  226. struct mutex mutex;
  227. int cpu;
  228. int launched;
  229. u64 host_tsc;
  230. struct kvm_run *run;
  231. int interrupt_window_open;
  232. unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
  233. #define NR_IRQ_WORDS KVM_IRQ_BITMAP_SIZE(unsigned long)
  234. unsigned long irq_pending[NR_IRQ_WORDS];
  235. unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
  236. unsigned long rip; /* needs vcpu_load_rsp_rip() */
  237. unsigned long cr0;
  238. unsigned long cr2;
  239. unsigned long cr3;
  240. gpa_t para_state_gpa;
  241. struct page *para_state_page;
  242. gpa_t hypercall_gpa;
  243. unsigned long cr4;
  244. unsigned long cr8;
  245. u64 pdptrs[4]; /* pae */
  246. u64 shadow_efer;
  247. u64 apic_base;
  248. u64 ia32_misc_enable_msr;
  249. int nmsrs;
  250. struct vmx_msr_entry *guest_msrs;
  251. struct vmx_msr_entry *host_msrs;
  252. struct list_head free_pages;
  253. struct kvm_mmu_page page_header_buf[KVM_NUM_MMU_PAGES];
  254. struct kvm_mmu mmu;
  255. struct kvm_mmu_memory_cache mmu_pte_chain_cache;
  256. struct kvm_mmu_memory_cache mmu_rmap_desc_cache;
  257. gfn_t last_pt_write_gfn;
  258. int last_pt_write_count;
  259. struct kvm_guest_debug guest_debug;
  260. char fx_buf[FX_BUF_SIZE];
  261. char *host_fx_image;
  262. char *guest_fx_image;
  263. int fpu_active;
  264. int guest_fpu_loaded;
  265. struct vmx_host_state {
  266. int loaded;
  267. u16 fs_sel, gs_sel, ldt_sel;
  268. int fs_gs_ldt_reload_needed;
  269. } vmx_host_state;
  270. int mmio_needed;
  271. int mmio_read_completed;
  272. int mmio_is_write;
  273. int mmio_size;
  274. unsigned char mmio_data[8];
  275. gpa_t mmio_phys_addr;
  276. gva_t mmio_fault_cr2;
  277. struct kvm_pio_request pio;
  278. void *pio_data;
  279. int sigset_active;
  280. sigset_t sigset;
  281. struct kvm_stat stat;
  282. struct {
  283. int active;
  284. u8 save_iopl;
  285. struct kvm_save_segment {
  286. u16 selector;
  287. unsigned long base;
  288. u32 limit;
  289. u32 ar;
  290. } tr, es, ds, fs, gs;
  291. } rmode;
  292. int cpuid_nent;
  293. struct kvm_cpuid_entry cpuid_entries[KVM_MAX_CPUID_ENTRIES];
  294. };
  295. struct kvm_mem_alias {
  296. gfn_t base_gfn;
  297. unsigned long npages;
  298. gfn_t target_gfn;
  299. };
  300. struct kvm_memory_slot {
  301. gfn_t base_gfn;
  302. unsigned long npages;
  303. unsigned long flags;
  304. struct page **phys_mem;
  305. unsigned long *dirty_bitmap;
  306. };
  307. struct kvm {
  308. spinlock_t lock; /* protects everything except vcpus */
  309. int naliases;
  310. struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
  311. int nmemslots;
  312. struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS];
  313. /*
  314. * Hash table of struct kvm_mmu_page.
  315. */
  316. struct list_head active_mmu_pages;
  317. int n_free_mmu_pages;
  318. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  319. struct kvm_vcpu vcpus[KVM_MAX_VCPUS];
  320. int memory_config_version;
  321. int busy;
  322. unsigned long rmap_overflow;
  323. struct list_head vm_list;
  324. struct file *filp;
  325. };
  326. struct descriptor_table {
  327. u16 limit;
  328. unsigned long base;
  329. } __attribute__((packed));
  330. struct kvm_arch_ops {
  331. int (*cpu_has_kvm_support)(void); /* __init */
  332. int (*disabled_by_bios)(void); /* __init */
  333. void (*hardware_enable)(void *dummy); /* __init */
  334. void (*hardware_disable)(void *dummy);
  335. int (*hardware_setup)(void); /* __init */
  336. void (*hardware_unsetup)(void); /* __exit */
  337. int (*vcpu_create)(struct kvm_vcpu *vcpu);
  338. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  339. void (*vcpu_load)(struct kvm_vcpu *vcpu);
  340. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  341. void (*vcpu_decache)(struct kvm_vcpu *vcpu);
  342. int (*set_guest_debug)(struct kvm_vcpu *vcpu,
  343. struct kvm_debug_guest *dbg);
  344. int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
  345. int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  346. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  347. void (*get_segment)(struct kvm_vcpu *vcpu,
  348. struct kvm_segment *var, int seg);
  349. void (*set_segment)(struct kvm_vcpu *vcpu,
  350. struct kvm_segment *var, int seg);
  351. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  352. void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  353. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  354. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  355. void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  356. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  357. void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  358. void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  359. void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  360. void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  361. unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
  362. void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  363. int *exception);
  364. void (*cache_regs)(struct kvm_vcpu *vcpu);
  365. void (*decache_regs)(struct kvm_vcpu *vcpu);
  366. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  367. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  368. void (*invlpg)(struct kvm_vcpu *vcpu, gva_t addr);
  369. void (*tlb_flush)(struct kvm_vcpu *vcpu);
  370. void (*inject_page_fault)(struct kvm_vcpu *vcpu,
  371. unsigned long addr, u32 err_code);
  372. void (*inject_gp)(struct kvm_vcpu *vcpu, unsigned err_code);
  373. int (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
  374. int (*vcpu_setup)(struct kvm_vcpu *vcpu);
  375. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  376. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  377. unsigned char *hypercall_addr);
  378. };
  379. extern struct kvm_arch_ops *kvm_arch_ops;
  380. #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
  381. #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
  382. int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module);
  383. void kvm_exit_arch(void);
  384. int kvm_mmu_module_init(void);
  385. void kvm_mmu_module_exit(void);
  386. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  387. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  388. int kvm_mmu_setup(struct kvm_vcpu *vcpu);
  389. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  390. void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot);
  391. void kvm_mmu_zap_all(struct kvm_vcpu *vcpu);
  392. hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa);
  393. #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
  394. #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
  395. static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
  396. hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva);
  397. struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
  398. void kvm_emulator_want_group7_invlpg(void);
  399. extern hpa_t bad_page_address;
  400. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
  401. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  402. void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
  403. enum emulation_result {
  404. EMULATE_DONE, /* no further processing */
  405. EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
  406. EMULATE_FAIL, /* can't emulate this instruction */
  407. };
  408. int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
  409. unsigned long cr2, u16 error_code);
  410. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  411. void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  412. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  413. unsigned long *rflags);
  414. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
  415. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
  416. unsigned long *rflags);
  417. struct x86_emulate_ctxt;
  418. int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  419. int size, unsigned long count, int string, int down,
  420. gva_t address, int rep, unsigned port);
  421. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
  422. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
  423. int emulate_clts(struct kvm_vcpu *vcpu);
  424. int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr,
  425. unsigned long *dest);
  426. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
  427. unsigned long value);
  428. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  429. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr0);
  430. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr0);
  431. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr0);
  432. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  433. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  434. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  435. void fx_init(struct kvm_vcpu *vcpu);
  436. void load_msrs(struct vmx_msr_entry *e, int n);
  437. void save_msrs(struct vmx_msr_entry *e, int n);
  438. void kvm_resched(struct kvm_vcpu *vcpu);
  439. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
  440. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
  441. int kvm_read_guest(struct kvm_vcpu *vcpu,
  442. gva_t addr,
  443. unsigned long size,
  444. void *dest);
  445. int kvm_write_guest(struct kvm_vcpu *vcpu,
  446. gva_t addr,
  447. unsigned long size,
  448. void *data);
  449. unsigned long segment_base(u16 selector);
  450. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  451. const u8 *old, const u8 *new, int bytes);
  452. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  453. void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  454. int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run);
  455. static inline int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
  456. u32 error_code)
  457. {
  458. if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
  459. kvm_mmu_free_some_pages(vcpu);
  460. return vcpu->mmu.page_fault(vcpu, gva, error_code);
  461. }
  462. static inline int is_long_mode(struct kvm_vcpu *vcpu)
  463. {
  464. #ifdef CONFIG_X86_64
  465. return vcpu->shadow_efer & EFER_LME;
  466. #else
  467. return 0;
  468. #endif
  469. }
  470. static inline int is_pae(struct kvm_vcpu *vcpu)
  471. {
  472. return vcpu->cr4 & CR4_PAE_MASK;
  473. }
  474. static inline int is_pse(struct kvm_vcpu *vcpu)
  475. {
  476. return vcpu->cr4 & CR4_PSE_MASK;
  477. }
  478. static inline int is_paging(struct kvm_vcpu *vcpu)
  479. {
  480. return vcpu->cr0 & CR0_PG_MASK;
  481. }
  482. static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot)
  483. {
  484. return slot - kvm->memslots;
  485. }
  486. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  487. {
  488. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  489. return (struct kvm_mmu_page *)page_private(page);
  490. }
  491. static inline u16 read_fs(void)
  492. {
  493. u16 seg;
  494. asm ("mov %%fs, %0" : "=g"(seg));
  495. return seg;
  496. }
  497. static inline u16 read_gs(void)
  498. {
  499. u16 seg;
  500. asm ("mov %%gs, %0" : "=g"(seg));
  501. return seg;
  502. }
  503. static inline u16 read_ldt(void)
  504. {
  505. u16 ldt;
  506. asm ("sldt %0" : "=g"(ldt));
  507. return ldt;
  508. }
  509. static inline void load_fs(u16 sel)
  510. {
  511. asm ("mov %0, %%fs" : : "rm"(sel));
  512. }
  513. static inline void load_gs(u16 sel)
  514. {
  515. asm ("mov %0, %%gs" : : "rm"(sel));
  516. }
  517. #ifndef load_ldt
  518. static inline void load_ldt(u16 sel)
  519. {
  520. asm ("lldt %0" : : "rm"(sel));
  521. }
  522. #endif
  523. static inline void get_idt(struct descriptor_table *table)
  524. {
  525. asm ("sidt %0" : "=m"(*table));
  526. }
  527. static inline void get_gdt(struct descriptor_table *table)
  528. {
  529. asm ("sgdt %0" : "=m"(*table));
  530. }
  531. static inline unsigned long read_tr_base(void)
  532. {
  533. u16 tr;
  534. asm ("str %0" : "=g"(tr));
  535. return segment_base(tr);
  536. }
  537. #ifdef CONFIG_X86_64
  538. static inline unsigned long read_msr(unsigned long msr)
  539. {
  540. u64 value;
  541. rdmsrl(msr, value);
  542. return value;
  543. }
  544. #endif
  545. static inline void fx_save(void *image)
  546. {
  547. asm ("fxsave (%0)":: "r" (image));
  548. }
  549. static inline void fx_restore(void *image)
  550. {
  551. asm ("fxrstor (%0)":: "r" (image));
  552. }
  553. static inline void fpu_init(void)
  554. {
  555. asm ("finit");
  556. }
  557. static inline u32 get_rdx_init_val(void)
  558. {
  559. return 0x600; /* P6 family */
  560. }
  561. #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
  562. #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
  563. #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
  564. #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
  565. #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
  566. #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
  567. #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
  568. #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
  569. #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
  570. #define MSR_IA32_TIME_STAMP_COUNTER 0x010
  571. #define TSS_IOPB_BASE_OFFSET 0x66
  572. #define TSS_BASE_SIZE 0x68
  573. #define TSS_IOPB_SIZE (65536 / 8)
  574. #define TSS_REDIRECTION_SIZE (256 / 8)
  575. #define RMODE_TSS_SIZE (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  576. #endif