kvm.h 19 KB

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