kvm.h 20 KB

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