kvm.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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/hardirq.h>
  9. #include <linux/list.h>
  10. #include <linux/mutex.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/preempt.h>
  16. #include <asm/signal.h>
  17. #include <linux/kvm.h>
  18. #include <linux/kvm_para.h>
  19. #define CR3_PAE_RESERVED_BITS ((X86_CR3_PWT | X86_CR3_PCD) - 1)
  20. #define CR3_NONPAE_RESERVED_BITS ((PAGE_SIZE-1) & ~(X86_CR3_PWT | X86_CR3_PCD))
  21. #define CR3_L_MODE_RESERVED_BITS (CR3_NONPAE_RESERVED_BITS|0xFFFFFF0000000000ULL)
  22. #define KVM_GUEST_CR0_MASK \
  23. (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE \
  24. | X86_CR0_NW | X86_CR0_CD)
  25. #define KVM_VM_CR0_ALWAYS_ON \
  26. (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE | X86_CR0_TS \
  27. | X86_CR0_MP)
  28. #define KVM_GUEST_CR4_MASK \
  29. (X86_CR4_VME | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_VMXE)
  30. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  31. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  32. #define INVALID_PAGE (~(hpa_t)0)
  33. #define UNMAPPED_GVA (~(gpa_t)0)
  34. #define KVM_MAX_VCPUS 4
  35. #define KVM_ALIAS_SLOTS 4
  36. #define KVM_MEMORY_SLOTS 8
  37. #define KVM_PERMILLE_MMU_PAGES 20
  38. #define KVM_MIN_ALLOC_MMU_PAGES 64
  39. #define KVM_NUM_MMU_PAGES 1024
  40. #define KVM_MIN_FREE_MMU_PAGES 5
  41. #define KVM_REFILL_PAGES 25
  42. #define KVM_MAX_CPUID_ENTRIES 40
  43. #define DE_VECTOR 0
  44. #define UD_VECTOR 6
  45. #define NM_VECTOR 7
  46. #define DF_VECTOR 8
  47. #define TS_VECTOR 10
  48. #define NP_VECTOR 11
  49. #define SS_VECTOR 12
  50. #define GP_VECTOR 13
  51. #define PF_VECTOR 14
  52. #define SELECTOR_TI_MASK (1 << 2)
  53. #define SELECTOR_RPL_MASK 0x03
  54. #define IOPL_SHIFT 12
  55. #define KVM_PIO_PAGE_OFFSET 1
  56. /*
  57. * vcpu->requests bit members
  58. */
  59. #define KVM_REQ_TLB_FLUSH 0
  60. /*
  61. * Address types:
  62. *
  63. * gva - guest virtual address
  64. * gpa - guest physical address
  65. * gfn - guest frame number
  66. * hva - host virtual address
  67. * hpa - host physical address
  68. * hfn - host frame number
  69. */
  70. typedef unsigned long gva_t;
  71. typedef u64 gpa_t;
  72. typedef unsigned long gfn_t;
  73. typedef unsigned long hva_t;
  74. typedef u64 hpa_t;
  75. typedef unsigned long hfn_t;
  76. #define NR_PTE_CHAIN_ENTRIES 5
  77. struct kvm_pte_chain {
  78. u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES];
  79. struct hlist_node link;
  80. };
  81. /*
  82. * kvm_mmu_page_role, below, is defined as:
  83. *
  84. * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
  85. * bits 4:7 - page table level for this shadow (1-4)
  86. * bits 8:9 - page table quadrant for 2-level guests
  87. * bit 16 - "metaphysical" - gfn is not a real page (huge page/real mode)
  88. * bits 17:19 - "access" - the user, writable, and nx bits of a huge page pde
  89. */
  90. union kvm_mmu_page_role {
  91. unsigned word;
  92. struct {
  93. unsigned glevels : 4;
  94. unsigned level : 4;
  95. unsigned quadrant : 2;
  96. unsigned pad_for_nice_hex_output : 6;
  97. unsigned metaphysical : 1;
  98. unsigned hugepage_access : 3;
  99. };
  100. };
  101. struct kvm_mmu_page {
  102. struct list_head link;
  103. struct hlist_node hash_link;
  104. /*
  105. * The following two entries are used to key the shadow page in the
  106. * hash table.
  107. */
  108. gfn_t gfn;
  109. union kvm_mmu_page_role role;
  110. u64 *spt;
  111. /* hold the gfn of each spte inside spt */
  112. gfn_t *gfns;
  113. unsigned long slot_bitmap; /* One bit set per slot which has memory
  114. * in this shadow page.
  115. */
  116. int multimapped; /* More than one parent_pte? */
  117. int root_count; /* Currently serving as active root */
  118. union {
  119. u64 *parent_pte; /* !multimapped */
  120. struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */
  121. };
  122. };
  123. struct kvm_vcpu;
  124. extern struct kmem_cache *kvm_vcpu_cache;
  125. /*
  126. * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
  127. * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
  128. * mode.
  129. */
  130. struct kvm_mmu {
  131. void (*new_cr3)(struct kvm_vcpu *vcpu);
  132. int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
  133. void (*free)(struct kvm_vcpu *vcpu);
  134. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
  135. void (*prefetch_page)(struct kvm_vcpu *vcpu,
  136. struct kvm_mmu_page *page);
  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 40
  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. #include "x86_emulate.h"
  188. struct kvm_pio_request {
  189. unsigned long count;
  190. int cur_count;
  191. struct page *guest_pages[2];
  192. unsigned guest_page_offset;
  193. int in;
  194. int port;
  195. int size;
  196. int string;
  197. int down;
  198. int rep;
  199. };
  200. struct kvm_stat {
  201. u32 pf_fixed;
  202. u32 pf_guest;
  203. u32 tlb_flush;
  204. u32 invlpg;
  205. u32 exits;
  206. u32 io_exits;
  207. u32 mmio_exits;
  208. u32 signal_exits;
  209. u32 irq_window_exits;
  210. u32 halt_exits;
  211. u32 halt_wakeup;
  212. u32 request_irq_exits;
  213. u32 irq_exits;
  214. u32 light_exits;
  215. u32 efer_reload;
  216. };
  217. struct kvm_io_device {
  218. void (*read)(struct kvm_io_device *this,
  219. gpa_t addr,
  220. int len,
  221. void *val);
  222. void (*write)(struct kvm_io_device *this,
  223. gpa_t addr,
  224. int len,
  225. const void *val);
  226. int (*in_range)(struct kvm_io_device *this, gpa_t addr);
  227. void (*destructor)(struct kvm_io_device *this);
  228. void *private;
  229. };
  230. static inline void kvm_iodevice_read(struct kvm_io_device *dev,
  231. gpa_t addr,
  232. int len,
  233. void *val)
  234. {
  235. dev->read(dev, addr, len, val);
  236. }
  237. static inline void kvm_iodevice_write(struct kvm_io_device *dev,
  238. gpa_t addr,
  239. int len,
  240. const void *val)
  241. {
  242. dev->write(dev, addr, len, val);
  243. }
  244. static inline int kvm_iodevice_inrange(struct kvm_io_device *dev, gpa_t addr)
  245. {
  246. return dev->in_range(dev, addr);
  247. }
  248. static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
  249. {
  250. if (dev->destructor)
  251. dev->destructor(dev);
  252. }
  253. /*
  254. * It would be nice to use something smarter than a linear search, TBD...
  255. * Thankfully we dont expect many devices to register (famous last words :),
  256. * so until then it will suffice. At least its abstracted so we can change
  257. * in one place.
  258. */
  259. struct kvm_io_bus {
  260. int dev_count;
  261. #define NR_IOBUS_DEVS 6
  262. struct kvm_io_device *devs[NR_IOBUS_DEVS];
  263. };
  264. void kvm_io_bus_init(struct kvm_io_bus *bus);
  265. void kvm_io_bus_destroy(struct kvm_io_bus *bus);
  266. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr);
  267. void kvm_io_bus_register_dev(struct kvm_io_bus *bus,
  268. struct kvm_io_device *dev);
  269. #ifdef CONFIG_HAS_IOMEM
  270. #define KVM_VCPU_MMIO \
  271. int mmio_needed; \
  272. int mmio_read_completed; \
  273. int mmio_is_write; \
  274. int mmio_size; \
  275. unsigned char mmio_data[8]; \
  276. gpa_t mmio_phys_addr;
  277. #else
  278. #define KVM_VCPU_MMIO
  279. #endif
  280. #define KVM_VCPU_COMM \
  281. struct kvm *kvm; \
  282. struct preempt_notifier preempt_notifier; \
  283. int vcpu_id; \
  284. struct mutex mutex; \
  285. int cpu; \
  286. struct kvm_run *run; \
  287. int guest_mode; \
  288. unsigned long requests; \
  289. struct kvm_guest_debug guest_debug; \
  290. int fpu_active; \
  291. int guest_fpu_loaded; \
  292. wait_queue_head_t wq; \
  293. int sigset_active; \
  294. sigset_t sigset; \
  295. struct kvm_stat stat; \
  296. KVM_VCPU_MMIO
  297. struct kvm_mem_alias {
  298. gfn_t base_gfn;
  299. unsigned long npages;
  300. gfn_t target_gfn;
  301. };
  302. struct kvm_memory_slot {
  303. gfn_t base_gfn;
  304. unsigned long npages;
  305. unsigned long flags;
  306. unsigned long *rmap;
  307. unsigned long *dirty_bitmap;
  308. unsigned long userspace_addr;
  309. int user_alloc;
  310. };
  311. struct kvm {
  312. struct mutex lock; /* protects everything except vcpus */
  313. int naliases;
  314. struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
  315. int nmemslots;
  316. struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS];
  317. /*
  318. * Hash table of struct kvm_mmu_page.
  319. */
  320. struct list_head active_mmu_pages;
  321. unsigned int n_free_mmu_pages;
  322. unsigned int n_requested_mmu_pages;
  323. unsigned int n_alloc_mmu_pages;
  324. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  325. struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  326. unsigned long rmap_overflow;
  327. struct list_head vm_list;
  328. struct file *filp;
  329. struct kvm_io_bus mmio_bus;
  330. struct kvm_io_bus pio_bus;
  331. struct kvm_pic *vpic;
  332. struct kvm_ioapic *vioapic;
  333. int round_robin_prev_vcpu;
  334. };
  335. static inline struct kvm_pic *pic_irqchip(struct kvm *kvm)
  336. {
  337. return kvm->vpic;
  338. }
  339. static inline struct kvm_ioapic *ioapic_irqchip(struct kvm *kvm)
  340. {
  341. return kvm->vioapic;
  342. }
  343. static inline int irqchip_in_kernel(struct kvm *kvm)
  344. {
  345. return pic_irqchip(kvm) != 0;
  346. }
  347. struct descriptor_table {
  348. u16 limit;
  349. unsigned long base;
  350. } __attribute__((packed));
  351. struct kvm_x86_ops {
  352. int (*cpu_has_kvm_support)(void); /* __init */
  353. int (*disabled_by_bios)(void); /* __init */
  354. void (*hardware_enable)(void *dummy); /* __init */
  355. void (*hardware_disable)(void *dummy);
  356. void (*check_processor_compatibility)(void *rtn);
  357. int (*hardware_setup)(void); /* __init */
  358. void (*hardware_unsetup)(void); /* __exit */
  359. /* Create, but do not attach this VCPU */
  360. struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
  361. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  362. int (*vcpu_reset)(struct kvm_vcpu *vcpu);
  363. void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
  364. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  365. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  366. void (*vcpu_decache)(struct kvm_vcpu *vcpu);
  367. int (*set_guest_debug)(struct kvm_vcpu *vcpu,
  368. struct kvm_debug_guest *dbg);
  369. void (*guest_debug_pre)(struct kvm_vcpu *vcpu);
  370. int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
  371. int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  372. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  373. void (*get_segment)(struct kvm_vcpu *vcpu,
  374. struct kvm_segment *var, int seg);
  375. void (*set_segment)(struct kvm_vcpu *vcpu,
  376. struct kvm_segment *var, int seg);
  377. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  378. void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  379. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  380. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  381. void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  382. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  383. void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  384. void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  385. void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  386. void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  387. unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
  388. void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  389. int *exception);
  390. void (*cache_regs)(struct kvm_vcpu *vcpu);
  391. void (*decache_regs)(struct kvm_vcpu *vcpu);
  392. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  393. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  394. void (*tlb_flush)(struct kvm_vcpu *vcpu);
  395. void (*inject_page_fault)(struct kvm_vcpu *vcpu,
  396. unsigned long addr, u32 err_code);
  397. void (*inject_gp)(struct kvm_vcpu *vcpu, unsigned err_code);
  398. void (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
  399. int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  400. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  401. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  402. unsigned char *hypercall_addr);
  403. int (*get_irq)(struct kvm_vcpu *vcpu);
  404. void (*set_irq)(struct kvm_vcpu *vcpu, int vec);
  405. void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
  406. void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
  407. struct kvm_run *run);
  408. };
  409. extern struct kvm_x86_ops *kvm_x86_ops;
  410. /* The guest did something we don't support. */
  411. #define pr_unimpl(vcpu, fmt, ...) \
  412. do { \
  413. if (printk_ratelimit()) \
  414. printk(KERN_ERR "kvm: %i: cpu%i " fmt, \
  415. current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \
  416. } while (0)
  417. #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
  418. #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
  419. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
  420. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
  421. void vcpu_load(struct kvm_vcpu *vcpu);
  422. void vcpu_put(struct kvm_vcpu *vcpu);
  423. int kvm_init_x86(struct kvm_x86_ops *ops, unsigned int vcpu_size,
  424. struct module *module);
  425. void kvm_exit_x86(void);
  426. int kvm_mmu_module_init(void);
  427. void kvm_mmu_module_exit(void);
  428. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  429. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  430. int kvm_mmu_setup(struct kvm_vcpu *vcpu);
  431. void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
  432. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  433. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
  434. void kvm_mmu_zap_all(struct kvm *kvm);
  435. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
  436. hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa);
  437. #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
  438. #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
  439. static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
  440. hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva);
  441. struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
  442. extern struct page *bad_page;
  443. int is_error_page(struct page *page);
  444. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn);
  445. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
  446. void kvm_release_page(struct page *page);
  447. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  448. int len);
  449. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
  450. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  451. int offset, int len);
  452. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  453. unsigned long len);
  454. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
  455. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
  456. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  457. void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
  458. enum emulation_result {
  459. EMULATE_DONE, /* no further processing */
  460. EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
  461. EMULATE_FAIL, /* can't emulate this instruction */
  462. };
  463. int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
  464. unsigned long cr2, u16 error_code, int no_decode);
  465. void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char *context);
  466. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  467. void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  468. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  469. unsigned long *rflags);
  470. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
  471. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
  472. unsigned long *rflags);
  473. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
  474. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  475. struct x86_emulate_ctxt;
  476. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  477. int size, unsigned port);
  478. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  479. int size, unsigned long count, 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. unsigned long get_cr8(struct kvm_vcpu *vcpu);
  494. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  495. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
  496. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  497. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  498. void fx_init(struct kvm_vcpu *vcpu);
  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 emulator_read_std(unsigned long addr,
  504. void *val,
  505. unsigned int bytes,
  506. struct kvm_vcpu *vcpu);
  507. int emulator_write_emulated(unsigned long addr,
  508. const void *val,
  509. unsigned int bytes,
  510. struct kvm_vcpu *vcpu);
  511. unsigned long segment_base(u16 selector);
  512. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  513. 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_emulate_hypercall(struct kvm_vcpu *vcpu);
  519. int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
  520. long kvm_arch_dev_ioctl(struct file *filp,
  521. unsigned int ioctl, unsigned long arg);
  522. long kvm_arch_vcpu_ioctl(struct file *filp,
  523. unsigned int ioctl, unsigned long arg);
  524. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  525. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
  526. __init void kvm_arch_init(void);
  527. static inline void kvm_guest_enter(void)
  528. {
  529. account_system_vtime(current);
  530. current->flags |= PF_VCPU;
  531. }
  532. static inline void kvm_guest_exit(void)
  533. {
  534. account_system_vtime(current);
  535. current->flags &= ~PF_VCPU;
  536. }
  537. static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot)
  538. {
  539. return slot - kvm->memslots;
  540. }
  541. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  542. {
  543. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  544. return (struct kvm_mmu_page *)page_private(page);
  545. }
  546. static inline u16 read_fs(void)
  547. {
  548. u16 seg;
  549. asm("mov %%fs, %0" : "=g"(seg));
  550. return seg;
  551. }
  552. static inline u16 read_gs(void)
  553. {
  554. u16 seg;
  555. asm("mov %%gs, %0" : "=g"(seg));
  556. return seg;
  557. }
  558. static inline u16 read_ldt(void)
  559. {
  560. u16 ldt;
  561. asm("sldt %0" : "=g"(ldt));
  562. return ldt;
  563. }
  564. static inline void load_fs(u16 sel)
  565. {
  566. asm("mov %0, %%fs" : : "rm"(sel));
  567. }
  568. static inline void load_gs(u16 sel)
  569. {
  570. asm("mov %0, %%gs" : : "rm"(sel));
  571. }
  572. #ifndef load_ldt
  573. static inline void load_ldt(u16 sel)
  574. {
  575. asm("lldt %0" : : "rm"(sel));
  576. }
  577. #endif
  578. static inline void get_idt(struct descriptor_table *table)
  579. {
  580. asm("sidt %0" : "=m"(*table));
  581. }
  582. static inline void get_gdt(struct descriptor_table *table)
  583. {
  584. asm("sgdt %0" : "=m"(*table));
  585. }
  586. static inline unsigned long read_tr_base(void)
  587. {
  588. u16 tr;
  589. asm("str %0" : "=g"(tr));
  590. return segment_base(tr);
  591. }
  592. #ifdef CONFIG_X86_64
  593. static inline unsigned long read_msr(unsigned long msr)
  594. {
  595. u64 value;
  596. rdmsrl(msr, value);
  597. return value;
  598. }
  599. #endif
  600. static inline void fx_save(struct i387_fxsave_struct *image)
  601. {
  602. asm("fxsave (%0)":: "r" (image));
  603. }
  604. static inline void fx_restore(struct i387_fxsave_struct *image)
  605. {
  606. asm("fxrstor (%0)":: "r" (image));
  607. }
  608. static inline void fpu_init(void)
  609. {
  610. asm("finit");
  611. }
  612. static inline u32 get_rdx_init_val(void)
  613. {
  614. return 0x600; /* P6 family */
  615. }
  616. #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
  617. #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
  618. #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
  619. #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
  620. #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
  621. #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
  622. #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
  623. #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
  624. #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
  625. #define MSR_IA32_TIME_STAMP_COUNTER 0x010
  626. #define TSS_IOPB_BASE_OFFSET 0x66
  627. #define TSS_BASE_SIZE 0x68
  628. #define TSS_IOPB_SIZE (65536 / 8)
  629. #define TSS_REDIRECTION_SIZE (256 / 8)
  630. #define RMODE_TSS_SIZE (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  631. #endif