kvm.h 20 KB

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