kvm.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 8
  36. #define KVM_PERMILLE_MMU_PAGES 20
  37. #define KVM_MIN_ALLOC_MMU_PAGES 64
  38. #define KVM_NUM_MMU_PAGES 1024
  39. #define KVM_MIN_FREE_MMU_PAGES 5
  40. #define KVM_REFILL_PAGES 25
  41. #define KVM_MAX_CPUID_ENTRIES 40
  42. #define DE_VECTOR 0
  43. #define UD_VECTOR 6
  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. /* hold the gfn of each spte inside spt */
  111. gfn_t *gfns;
  112. unsigned long slot_bitmap; /* One bit set per slot which has memory
  113. * in this shadow page.
  114. */
  115. int multimapped; /* More than one parent_pte? */
  116. int root_count; /* Currently serving as active root */
  117. union {
  118. u64 *parent_pte; /* !multimapped */
  119. struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */
  120. };
  121. };
  122. struct kvm_vcpu;
  123. extern struct kmem_cache *kvm_vcpu_cache;
  124. /*
  125. * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
  126. * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
  127. * mode.
  128. */
  129. struct kvm_mmu {
  130. void (*new_cr3)(struct kvm_vcpu *vcpu);
  131. int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
  132. void (*free)(struct kvm_vcpu *vcpu);
  133. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
  134. void (*prefetch_page)(struct kvm_vcpu *vcpu,
  135. struct kvm_mmu_page *page);
  136. hpa_t root_hpa;
  137. int root_level;
  138. int shadow_root_level;
  139. u64 *pae_root;
  140. };
  141. #define KVM_NR_MEM_OBJS 40
  142. struct kvm_mmu_memory_cache {
  143. int nobjs;
  144. void *objects[KVM_NR_MEM_OBJS];
  145. };
  146. /*
  147. * We don't want allocation failures within the mmu code, so we preallocate
  148. * enough memory for a single page fault in a cache.
  149. */
  150. struct kvm_guest_debug {
  151. int enabled;
  152. unsigned long bp[4];
  153. int singlestep;
  154. };
  155. enum {
  156. VCPU_REGS_RAX = 0,
  157. VCPU_REGS_RCX = 1,
  158. VCPU_REGS_RDX = 2,
  159. VCPU_REGS_RBX = 3,
  160. VCPU_REGS_RSP = 4,
  161. VCPU_REGS_RBP = 5,
  162. VCPU_REGS_RSI = 6,
  163. VCPU_REGS_RDI = 7,
  164. #ifdef CONFIG_X86_64
  165. VCPU_REGS_R8 = 8,
  166. VCPU_REGS_R9 = 9,
  167. VCPU_REGS_R10 = 10,
  168. VCPU_REGS_R11 = 11,
  169. VCPU_REGS_R12 = 12,
  170. VCPU_REGS_R13 = 13,
  171. VCPU_REGS_R14 = 14,
  172. VCPU_REGS_R15 = 15,
  173. #endif
  174. NR_VCPU_REGS
  175. };
  176. enum {
  177. VCPU_SREG_CS,
  178. VCPU_SREG_DS,
  179. VCPU_SREG_ES,
  180. VCPU_SREG_FS,
  181. VCPU_SREG_GS,
  182. VCPU_SREG_SS,
  183. VCPU_SREG_TR,
  184. VCPU_SREG_LDTR,
  185. };
  186. #include "x86_emulate.h"
  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 halt_wakeup;
  211. u32 request_irq_exits;
  212. u32 irq_exits;
  213. u32 light_exits;
  214. u32 efer_reload;
  215. };
  216. struct kvm_io_device {
  217. void (*read)(struct kvm_io_device *this,
  218. gpa_t addr,
  219. int len,
  220. void *val);
  221. void (*write)(struct kvm_io_device *this,
  222. gpa_t addr,
  223. int len,
  224. const void *val);
  225. int (*in_range)(struct kvm_io_device *this, gpa_t addr);
  226. void (*destructor)(struct kvm_io_device *this);
  227. void *private;
  228. };
  229. static inline void kvm_iodevice_read(struct kvm_io_device *dev,
  230. gpa_t addr,
  231. int len,
  232. void *val)
  233. {
  234. dev->read(dev, addr, len, val);
  235. }
  236. static inline void kvm_iodevice_write(struct kvm_io_device *dev,
  237. gpa_t addr,
  238. int len,
  239. const void *val)
  240. {
  241. dev->write(dev, addr, len, val);
  242. }
  243. static inline int kvm_iodevice_inrange(struct kvm_io_device *dev, gpa_t addr)
  244. {
  245. return dev->in_range(dev, addr);
  246. }
  247. static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
  248. {
  249. if (dev->destructor)
  250. dev->destructor(dev);
  251. }
  252. /*
  253. * It would be nice to use something smarter than a linear search, TBD...
  254. * Thankfully we dont expect many devices to register (famous last words :),
  255. * so until then it will suffice. At least its abstracted so we can change
  256. * in one place.
  257. */
  258. struct kvm_io_bus {
  259. int dev_count;
  260. #define NR_IOBUS_DEVS 6
  261. struct kvm_io_device *devs[NR_IOBUS_DEVS];
  262. };
  263. void kvm_io_bus_init(struct kvm_io_bus *bus);
  264. void kvm_io_bus_destroy(struct kvm_io_bus *bus);
  265. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr);
  266. void kvm_io_bus_register_dev(struct kvm_io_bus *bus,
  267. struct kvm_io_device *dev);
  268. struct kvm_vcpu {
  269. struct kvm *kvm;
  270. struct preempt_notifier preempt_notifier;
  271. int vcpu_id;
  272. struct mutex mutex;
  273. int cpu;
  274. u64 host_tsc;
  275. struct kvm_run *run;
  276. int interrupt_window_open;
  277. int guest_mode;
  278. unsigned long requests;
  279. unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
  280. DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
  281. unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
  282. unsigned long rip; /* needs vcpu_load_rsp_rip() */
  283. unsigned long cr0;
  284. unsigned long cr2;
  285. unsigned long cr3;
  286. unsigned long cr4;
  287. unsigned long cr8;
  288. u64 pdptrs[4]; /* pae */
  289. u64 shadow_efer;
  290. u64 apic_base;
  291. struct kvm_lapic *apic; /* kernel irqchip context */
  292. #define VCPU_MP_STATE_RUNNABLE 0
  293. #define VCPU_MP_STATE_UNINITIALIZED 1
  294. #define VCPU_MP_STATE_INIT_RECEIVED 2
  295. #define VCPU_MP_STATE_SIPI_RECEIVED 3
  296. #define VCPU_MP_STATE_HALTED 4
  297. int mp_state;
  298. int sipi_vector;
  299. u64 ia32_misc_enable_msr;
  300. struct kvm_mmu mmu;
  301. struct kvm_mmu_memory_cache mmu_pte_chain_cache;
  302. struct kvm_mmu_memory_cache mmu_rmap_desc_cache;
  303. struct kvm_mmu_memory_cache mmu_page_cache;
  304. struct kvm_mmu_memory_cache mmu_page_header_cache;
  305. gfn_t last_pt_write_gfn;
  306. int last_pt_write_count;
  307. u64 *last_pte_updated;
  308. struct kvm_guest_debug guest_debug;
  309. struct i387_fxsave_struct host_fx_image;
  310. struct i387_fxsave_struct guest_fx_image;
  311. int fpu_active;
  312. int guest_fpu_loaded;
  313. int mmio_needed;
  314. int mmio_read_completed;
  315. int mmio_is_write;
  316. int mmio_size;
  317. unsigned char mmio_data[8];
  318. gpa_t mmio_phys_addr;
  319. gva_t mmio_fault_cr2;
  320. struct kvm_pio_request pio;
  321. void *pio_data;
  322. wait_queue_head_t wq;
  323. int sigset_active;
  324. sigset_t sigset;
  325. struct kvm_stat stat;
  326. struct {
  327. int active;
  328. u8 save_iopl;
  329. struct kvm_save_segment {
  330. u16 selector;
  331. unsigned long base;
  332. u32 limit;
  333. u32 ar;
  334. } tr, es, ds, fs, gs;
  335. } rmode;
  336. int halt_request; /* real mode on Intel only */
  337. int cpuid_nent;
  338. struct kvm_cpuid_entry cpuid_entries[KVM_MAX_CPUID_ENTRIES];
  339. /* emulate context */
  340. struct x86_emulate_ctxt emulate_ctxt;
  341. };
  342. struct kvm_mem_alias {
  343. gfn_t base_gfn;
  344. unsigned long npages;
  345. gfn_t target_gfn;
  346. };
  347. struct kvm_memory_slot {
  348. gfn_t base_gfn;
  349. unsigned long npages;
  350. unsigned long flags;
  351. struct page **phys_mem;
  352. unsigned long *rmap;
  353. unsigned long *dirty_bitmap;
  354. };
  355. struct kvm {
  356. struct mutex lock; /* protects everything except vcpus */
  357. int naliases;
  358. struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
  359. int nmemslots;
  360. struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS];
  361. /*
  362. * Hash table of struct kvm_mmu_page.
  363. */
  364. struct list_head active_mmu_pages;
  365. unsigned int n_free_mmu_pages;
  366. unsigned int n_requested_mmu_pages;
  367. unsigned int n_alloc_mmu_pages;
  368. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  369. struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  370. unsigned long rmap_overflow;
  371. struct list_head vm_list;
  372. struct file *filp;
  373. struct kvm_io_bus mmio_bus;
  374. struct kvm_io_bus pio_bus;
  375. struct kvm_pic *vpic;
  376. struct kvm_ioapic *vioapic;
  377. int round_robin_prev_vcpu;
  378. };
  379. static inline struct kvm_pic *pic_irqchip(struct kvm *kvm)
  380. {
  381. return kvm->vpic;
  382. }
  383. static inline struct kvm_ioapic *ioapic_irqchip(struct kvm *kvm)
  384. {
  385. return kvm->vioapic;
  386. }
  387. static inline int irqchip_in_kernel(struct kvm *kvm)
  388. {
  389. return pic_irqchip(kvm) != 0;
  390. }
  391. struct descriptor_table {
  392. u16 limit;
  393. unsigned long base;
  394. } __attribute__((packed));
  395. struct kvm_x86_ops {
  396. int (*cpu_has_kvm_support)(void); /* __init */
  397. int (*disabled_by_bios)(void); /* __init */
  398. void (*hardware_enable)(void *dummy); /* __init */
  399. void (*hardware_disable)(void *dummy);
  400. void (*check_processor_compatibility)(void *rtn);
  401. int (*hardware_setup)(void); /* __init */
  402. void (*hardware_unsetup)(void); /* __exit */
  403. /* Create, but do not attach this VCPU */
  404. struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
  405. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  406. void (*vcpu_reset)(struct kvm_vcpu *vcpu);
  407. void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
  408. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  409. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  410. void (*vcpu_decache)(struct kvm_vcpu *vcpu);
  411. int (*set_guest_debug)(struct kvm_vcpu *vcpu,
  412. struct kvm_debug_guest *dbg);
  413. void (*guest_debug_pre)(struct kvm_vcpu *vcpu);
  414. int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
  415. int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  416. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  417. void (*get_segment)(struct kvm_vcpu *vcpu,
  418. struct kvm_segment *var, int seg);
  419. void (*set_segment)(struct kvm_vcpu *vcpu,
  420. struct kvm_segment *var, int seg);
  421. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  422. void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  423. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  424. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  425. void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  426. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  427. void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  428. void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  429. void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  430. void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  431. unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
  432. void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  433. int *exception);
  434. void (*cache_regs)(struct kvm_vcpu *vcpu);
  435. void (*decache_regs)(struct kvm_vcpu *vcpu);
  436. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  437. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  438. void (*tlb_flush)(struct kvm_vcpu *vcpu);
  439. void (*inject_page_fault)(struct kvm_vcpu *vcpu,
  440. unsigned long addr, u32 err_code);
  441. void (*inject_gp)(struct kvm_vcpu *vcpu, unsigned err_code);
  442. void (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
  443. int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  444. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  445. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  446. unsigned char *hypercall_addr);
  447. int (*get_irq)(struct kvm_vcpu *vcpu);
  448. void (*set_irq)(struct kvm_vcpu *vcpu, int vec);
  449. void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
  450. void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
  451. struct kvm_run *run);
  452. };
  453. extern struct kvm_x86_ops *kvm_x86_ops;
  454. /* The guest did something we don't support. */
  455. #define pr_unimpl(vcpu, fmt, ...) \
  456. do { \
  457. if (printk_ratelimit()) \
  458. printk(KERN_ERR "kvm: %i: cpu%i " fmt, \
  459. current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \
  460. } while (0)
  461. #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
  462. #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
  463. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
  464. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
  465. int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
  466. struct module *module);
  467. void kvm_exit_x86(void);
  468. int kvm_mmu_module_init(void);
  469. void kvm_mmu_module_exit(void);
  470. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  471. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  472. int kvm_mmu_setup(struct kvm_vcpu *vcpu);
  473. void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
  474. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  475. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
  476. void kvm_mmu_zap_all(struct kvm *kvm);
  477. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
  478. hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa);
  479. #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
  480. #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
  481. static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
  482. hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva);
  483. struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
  484. extern hpa_t bad_page_address;
  485. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn);
  486. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
  487. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  488. int len);
  489. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
  490. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  491. int offset, int len);
  492. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  493. unsigned long len);
  494. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
  495. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
  496. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  497. void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
  498. enum emulation_result {
  499. EMULATE_DONE, /* no further processing */
  500. EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
  501. EMULATE_FAIL, /* can't emulate this instruction */
  502. };
  503. int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
  504. unsigned long cr2, u16 error_code, int no_decode);
  505. void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char *context);
  506. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  507. void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  508. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  509. unsigned long *rflags);
  510. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
  511. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
  512. unsigned long *rflags);
  513. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
  514. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  515. struct x86_emulate_ctxt;
  516. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  517. int size, unsigned port);
  518. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  519. int size, unsigned long count, int down,
  520. gva_t address, int rep, unsigned port);
  521. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
  522. int kvm_emulate_halt(struct kvm_vcpu *vcpu);
  523. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
  524. int emulate_clts(struct kvm_vcpu *vcpu);
  525. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
  526. unsigned long *dest);
  527. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
  528. unsigned long value);
  529. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  530. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr0);
  531. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr0);
  532. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr0);
  533. unsigned long get_cr8(struct kvm_vcpu *vcpu);
  534. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  535. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
  536. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  537. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  538. void fx_init(struct kvm_vcpu *vcpu);
  539. void kvm_resched(struct kvm_vcpu *vcpu);
  540. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
  541. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
  542. void kvm_flush_remote_tlbs(struct kvm *kvm);
  543. int emulator_read_std(unsigned long addr,
  544. void *val,
  545. unsigned int bytes,
  546. struct kvm_vcpu *vcpu);
  547. int emulator_write_emulated(unsigned long addr,
  548. const void *val,
  549. unsigned int bytes,
  550. struct kvm_vcpu *vcpu);
  551. unsigned long segment_base(u16 selector);
  552. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  553. const u8 *new, int bytes);
  554. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  555. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  556. int kvm_mmu_load(struct kvm_vcpu *vcpu);
  557. void kvm_mmu_unload(struct kvm_vcpu *vcpu);
  558. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
  559. int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
  560. static inline void kvm_guest_enter(void)
  561. {
  562. current->flags |= PF_VCPU;
  563. }
  564. static inline void kvm_guest_exit(void)
  565. {
  566. current->flags &= ~PF_VCPU;
  567. }
  568. static inline int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
  569. u32 error_code)
  570. {
  571. return vcpu->mmu.page_fault(vcpu, gva, error_code);
  572. }
  573. static inline void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
  574. {
  575. if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
  576. __kvm_mmu_free_some_pages(vcpu);
  577. }
  578. static inline int kvm_mmu_reload(struct kvm_vcpu *vcpu)
  579. {
  580. if (likely(vcpu->mmu.root_hpa != INVALID_PAGE))
  581. return 0;
  582. return kvm_mmu_load(vcpu);
  583. }
  584. static inline int is_long_mode(struct kvm_vcpu *vcpu)
  585. {
  586. #ifdef CONFIG_X86_64
  587. return vcpu->shadow_efer & EFER_LME;
  588. #else
  589. return 0;
  590. #endif
  591. }
  592. static inline int is_pae(struct kvm_vcpu *vcpu)
  593. {
  594. return vcpu->cr4 & X86_CR4_PAE;
  595. }
  596. static inline int is_pse(struct kvm_vcpu *vcpu)
  597. {
  598. return vcpu->cr4 & X86_CR4_PSE;
  599. }
  600. static inline int is_paging(struct kvm_vcpu *vcpu)
  601. {
  602. return vcpu->cr0 & X86_CR0_PG;
  603. }
  604. static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot)
  605. {
  606. return slot - kvm->memslots;
  607. }
  608. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  609. {
  610. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  611. return (struct kvm_mmu_page *)page_private(page);
  612. }
  613. static inline u16 read_fs(void)
  614. {
  615. u16 seg;
  616. asm("mov %%fs, %0" : "=g"(seg));
  617. return seg;
  618. }
  619. static inline u16 read_gs(void)
  620. {
  621. u16 seg;
  622. asm("mov %%gs, %0" : "=g"(seg));
  623. return seg;
  624. }
  625. static inline u16 read_ldt(void)
  626. {
  627. u16 ldt;
  628. asm("sldt %0" : "=g"(ldt));
  629. return ldt;
  630. }
  631. static inline void load_fs(u16 sel)
  632. {
  633. asm("mov %0, %%fs" : : "rm"(sel));
  634. }
  635. static inline void load_gs(u16 sel)
  636. {
  637. asm("mov %0, %%gs" : : "rm"(sel));
  638. }
  639. #ifndef load_ldt
  640. static inline void load_ldt(u16 sel)
  641. {
  642. asm("lldt %0" : : "rm"(sel));
  643. }
  644. #endif
  645. static inline void get_idt(struct descriptor_table *table)
  646. {
  647. asm("sidt %0" : "=m"(*table));
  648. }
  649. static inline void get_gdt(struct descriptor_table *table)
  650. {
  651. asm("sgdt %0" : "=m"(*table));
  652. }
  653. static inline unsigned long read_tr_base(void)
  654. {
  655. u16 tr;
  656. asm("str %0" : "=g"(tr));
  657. return segment_base(tr);
  658. }
  659. #ifdef CONFIG_X86_64
  660. static inline unsigned long read_msr(unsigned long msr)
  661. {
  662. u64 value;
  663. rdmsrl(msr, value);
  664. return value;
  665. }
  666. #endif
  667. static inline void fx_save(struct i387_fxsave_struct *image)
  668. {
  669. asm("fxsave (%0)":: "r" (image));
  670. }
  671. static inline void fx_restore(struct i387_fxsave_struct *image)
  672. {
  673. asm("fxrstor (%0)":: "r" (image));
  674. }
  675. static inline void fpu_init(void)
  676. {
  677. asm("finit");
  678. }
  679. static inline u32 get_rdx_init_val(void)
  680. {
  681. return 0x600; /* P6 family */
  682. }
  683. #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
  684. #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
  685. #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
  686. #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
  687. #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
  688. #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
  689. #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
  690. #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
  691. #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
  692. #define MSR_IA32_TIME_STAMP_COUNTER 0x010
  693. #define TSS_IOPB_BASE_OFFSET 0x66
  694. #define TSS_BASE_SIZE 0x68
  695. #define TSS_IOPB_SIZE (65536 / 8)
  696. #define TSS_REDIRECTION_SIZE (256 / 8)
  697. #define RMODE_TSS_SIZE (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  698. #endif