kvm.h 17 KB

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