kvm.h 19 KB

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