kvm.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 KVM_MAX_VCPUS 4
  20. #define KVM_ALIAS_SLOTS 4
  21. #define KVM_MEMORY_SLOTS 8
  22. /* memory slots that does not exposed to userspace */
  23. #define KVM_PRIVATE_MEM_SLOTS 4
  24. #define KVM_PERMILLE_MMU_PAGES 20
  25. #define KVM_MIN_ALLOC_MMU_PAGES 64
  26. #define KVM_NUM_MMU_PAGES 1024
  27. #define KVM_MIN_FREE_MMU_PAGES 5
  28. #define KVM_REFILL_PAGES 25
  29. #define KVM_MAX_CPUID_ENTRIES 40
  30. #define KVM_PIO_PAGE_OFFSET 1
  31. /*
  32. * vcpu->requests bit members
  33. */
  34. #define KVM_REQ_TLB_FLUSH 0
  35. /*
  36. * Address types:
  37. *
  38. * gva - guest virtual address
  39. * gpa - guest physical address
  40. * gfn - guest frame number
  41. * hva - host virtual address
  42. * hpa - host physical address
  43. * hfn - host frame number
  44. */
  45. typedef unsigned long gva_t;
  46. typedef u64 gpa_t;
  47. typedef unsigned long gfn_t;
  48. typedef unsigned long hva_t;
  49. typedef u64 hpa_t;
  50. typedef unsigned long hfn_t;
  51. #define NR_PTE_CHAIN_ENTRIES 5
  52. struct kvm_pte_chain {
  53. u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES];
  54. struct hlist_node link;
  55. };
  56. /*
  57. * kvm_mmu_page_role, below, is defined as:
  58. *
  59. * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
  60. * bits 4:7 - page table level for this shadow (1-4)
  61. * bits 8:9 - page table quadrant for 2-level guests
  62. * bit 16 - "metaphysical" - gfn is not a real page (huge page/real mode)
  63. * bits 17:19 - "access" - the user, writable, and nx bits of a huge page pde
  64. */
  65. union kvm_mmu_page_role {
  66. unsigned word;
  67. struct {
  68. unsigned glevels : 4;
  69. unsigned level : 4;
  70. unsigned quadrant : 2;
  71. unsigned pad_for_nice_hex_output : 6;
  72. unsigned metaphysical : 1;
  73. unsigned hugepage_access : 3;
  74. };
  75. };
  76. struct kvm_mmu_page {
  77. struct list_head link;
  78. struct hlist_node hash_link;
  79. /*
  80. * The following two entries are used to key the shadow page in the
  81. * hash table.
  82. */
  83. gfn_t gfn;
  84. union kvm_mmu_page_role role;
  85. u64 *spt;
  86. /* hold the gfn of each spte inside spt */
  87. gfn_t *gfns;
  88. unsigned long slot_bitmap; /* One bit set per slot which has memory
  89. * in this shadow page.
  90. */
  91. int multimapped; /* More than one parent_pte? */
  92. int root_count; /* Currently serving as active root */
  93. union {
  94. u64 *parent_pte; /* !multimapped */
  95. struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */
  96. };
  97. };
  98. struct kvm_vcpu;
  99. extern struct kmem_cache *kvm_vcpu_cache;
  100. /*
  101. * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
  102. * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
  103. * mode.
  104. */
  105. struct kvm_mmu {
  106. void (*new_cr3)(struct kvm_vcpu *vcpu);
  107. int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
  108. void (*free)(struct kvm_vcpu *vcpu);
  109. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
  110. void (*prefetch_page)(struct kvm_vcpu *vcpu,
  111. struct kvm_mmu_page *page);
  112. hpa_t root_hpa;
  113. int root_level;
  114. int shadow_root_level;
  115. u64 *pae_root;
  116. };
  117. #define KVM_NR_MEM_OBJS 40
  118. struct kvm_mmu_memory_cache {
  119. int nobjs;
  120. void *objects[KVM_NR_MEM_OBJS];
  121. };
  122. /*
  123. * We don't want allocation failures within the mmu code, so we preallocate
  124. * enough memory for a single page fault in a cache.
  125. */
  126. struct kvm_guest_debug {
  127. int enabled;
  128. unsigned long bp[4];
  129. int singlestep;
  130. };
  131. enum {
  132. VCPU_REGS_RAX = 0,
  133. VCPU_REGS_RCX = 1,
  134. VCPU_REGS_RDX = 2,
  135. VCPU_REGS_RBX = 3,
  136. VCPU_REGS_RSP = 4,
  137. VCPU_REGS_RBP = 5,
  138. VCPU_REGS_RSI = 6,
  139. VCPU_REGS_RDI = 7,
  140. #ifdef CONFIG_X86_64
  141. VCPU_REGS_R8 = 8,
  142. VCPU_REGS_R9 = 9,
  143. VCPU_REGS_R10 = 10,
  144. VCPU_REGS_R11 = 11,
  145. VCPU_REGS_R12 = 12,
  146. VCPU_REGS_R13 = 13,
  147. VCPU_REGS_R14 = 14,
  148. VCPU_REGS_R15 = 15,
  149. #endif
  150. NR_VCPU_REGS
  151. };
  152. enum {
  153. VCPU_SREG_CS,
  154. VCPU_SREG_DS,
  155. VCPU_SREG_ES,
  156. VCPU_SREG_FS,
  157. VCPU_SREG_GS,
  158. VCPU_SREG_SS,
  159. VCPU_SREG_TR,
  160. VCPU_SREG_LDTR,
  161. };
  162. #include "x86_emulate.h"
  163. struct kvm_pio_request {
  164. unsigned long count;
  165. int cur_count;
  166. struct page *guest_pages[2];
  167. unsigned guest_page_offset;
  168. int in;
  169. int port;
  170. int size;
  171. int string;
  172. int down;
  173. int rep;
  174. };
  175. struct kvm_vcpu_stat {
  176. u32 pf_fixed;
  177. u32 pf_guest;
  178. u32 tlb_flush;
  179. u32 invlpg;
  180. u32 exits;
  181. u32 io_exits;
  182. u32 mmio_exits;
  183. u32 signal_exits;
  184. u32 irq_window_exits;
  185. u32 halt_exits;
  186. u32 halt_wakeup;
  187. u32 request_irq_exits;
  188. u32 irq_exits;
  189. u32 host_state_reload;
  190. u32 efer_reload;
  191. u32 fpu_reload;
  192. u32 insn_emulation;
  193. u32 insn_emulation_fail;
  194. };
  195. struct kvm_io_device {
  196. void (*read)(struct kvm_io_device *this,
  197. gpa_t addr,
  198. int len,
  199. void *val);
  200. void (*write)(struct kvm_io_device *this,
  201. gpa_t addr,
  202. int len,
  203. const void *val);
  204. int (*in_range)(struct kvm_io_device *this, gpa_t addr);
  205. void (*destructor)(struct kvm_io_device *this);
  206. void *private;
  207. };
  208. static inline void kvm_iodevice_read(struct kvm_io_device *dev,
  209. gpa_t addr,
  210. int len,
  211. void *val)
  212. {
  213. dev->read(dev, addr, len, val);
  214. }
  215. static inline void kvm_iodevice_write(struct kvm_io_device *dev,
  216. gpa_t addr,
  217. int len,
  218. const void *val)
  219. {
  220. dev->write(dev, addr, len, val);
  221. }
  222. static inline int kvm_iodevice_inrange(struct kvm_io_device *dev, gpa_t addr)
  223. {
  224. return dev->in_range(dev, addr);
  225. }
  226. static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
  227. {
  228. if (dev->destructor)
  229. dev->destructor(dev);
  230. }
  231. /*
  232. * It would be nice to use something smarter than a linear search, TBD...
  233. * Thankfully we dont expect many devices to register (famous last words :),
  234. * so until then it will suffice. At least its abstracted so we can change
  235. * in one place.
  236. */
  237. struct kvm_io_bus {
  238. int dev_count;
  239. #define NR_IOBUS_DEVS 6
  240. struct kvm_io_device *devs[NR_IOBUS_DEVS];
  241. };
  242. void kvm_io_bus_init(struct kvm_io_bus *bus);
  243. void kvm_io_bus_destroy(struct kvm_io_bus *bus);
  244. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr);
  245. void kvm_io_bus_register_dev(struct kvm_io_bus *bus,
  246. struct kvm_io_device *dev);
  247. #ifdef CONFIG_HAS_IOMEM
  248. #define KVM_VCPU_MMIO \
  249. int mmio_needed; \
  250. int mmio_read_completed; \
  251. int mmio_is_write; \
  252. int mmio_size; \
  253. unsigned char mmio_data[8]; \
  254. gpa_t mmio_phys_addr;
  255. #else
  256. #define KVM_VCPU_MMIO
  257. #endif
  258. #define KVM_VCPU_COMM \
  259. struct kvm *kvm; \
  260. struct preempt_notifier preempt_notifier; \
  261. int vcpu_id; \
  262. struct mutex mutex; \
  263. int cpu; \
  264. struct kvm_run *run; \
  265. int guest_mode; \
  266. unsigned long requests; \
  267. struct kvm_guest_debug guest_debug; \
  268. int fpu_active; \
  269. int guest_fpu_loaded; \
  270. wait_queue_head_t wq; \
  271. int sigset_active; \
  272. sigset_t sigset; \
  273. struct kvm_vcpu_stat stat; \
  274. KVM_VCPU_MMIO
  275. struct kvm_mem_alias {
  276. gfn_t base_gfn;
  277. unsigned long npages;
  278. gfn_t target_gfn;
  279. };
  280. struct kvm_memory_slot {
  281. gfn_t base_gfn;
  282. unsigned long npages;
  283. unsigned long flags;
  284. unsigned long *rmap;
  285. unsigned long *dirty_bitmap;
  286. unsigned long userspace_addr;
  287. int user_alloc;
  288. };
  289. struct kvm_vm_stat {
  290. u32 mmu_shadow_zapped;
  291. u32 mmu_pte_write;
  292. u32 mmu_pte_updated;
  293. u32 mmu_pde_zapped;
  294. u32 mmu_flooded;
  295. u32 mmu_recycled;
  296. };
  297. struct kvm {
  298. struct mutex lock; /* protects everything except vcpus */
  299. int naliases;
  300. struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
  301. int nmemslots;
  302. struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +
  303. KVM_PRIVATE_MEM_SLOTS];
  304. /*
  305. * Hash table of struct kvm_mmu_page.
  306. */
  307. struct list_head active_mmu_pages;
  308. unsigned int n_free_mmu_pages;
  309. unsigned int n_requested_mmu_pages;
  310. unsigned int n_alloc_mmu_pages;
  311. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  312. struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  313. unsigned long rmap_overflow;
  314. struct list_head vm_list;
  315. struct file *filp;
  316. struct kvm_io_bus mmio_bus;
  317. struct kvm_io_bus pio_bus;
  318. struct kvm_pic *vpic;
  319. struct kvm_ioapic *vioapic;
  320. int round_robin_prev_vcpu;
  321. unsigned int tss_addr;
  322. struct page *apic_access_page;
  323. struct kvm_vm_stat stat;
  324. };
  325. static inline struct kvm_pic *pic_irqchip(struct kvm *kvm)
  326. {
  327. return kvm->vpic;
  328. }
  329. static inline struct kvm_ioapic *ioapic_irqchip(struct kvm *kvm)
  330. {
  331. return kvm->vioapic;
  332. }
  333. static inline int irqchip_in_kernel(struct kvm *kvm)
  334. {
  335. return pic_irqchip(kvm) != NULL;
  336. }
  337. struct descriptor_table {
  338. u16 limit;
  339. unsigned long base;
  340. } __attribute__((packed));
  341. struct kvm_x86_ops {
  342. int (*cpu_has_kvm_support)(void); /* __init */
  343. int (*disabled_by_bios)(void); /* __init */
  344. void (*hardware_enable)(void *dummy); /* __init */
  345. void (*hardware_disable)(void *dummy);
  346. void (*check_processor_compatibility)(void *rtn);
  347. int (*hardware_setup)(void); /* __init */
  348. void (*hardware_unsetup)(void); /* __exit */
  349. /* Create, but do not attach this VCPU */
  350. struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
  351. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  352. int (*vcpu_reset)(struct kvm_vcpu *vcpu);
  353. void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
  354. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  355. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  356. void (*vcpu_decache)(struct kvm_vcpu *vcpu);
  357. int (*set_guest_debug)(struct kvm_vcpu *vcpu,
  358. struct kvm_debug_guest *dbg);
  359. void (*guest_debug_pre)(struct kvm_vcpu *vcpu);
  360. int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
  361. int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  362. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  363. void (*get_segment)(struct kvm_vcpu *vcpu,
  364. struct kvm_segment *var, int seg);
  365. void (*set_segment)(struct kvm_vcpu *vcpu,
  366. struct kvm_segment *var, int seg);
  367. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  368. void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  369. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  370. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  371. void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  372. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  373. void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  374. void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  375. void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  376. void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  377. unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
  378. void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  379. int *exception);
  380. void (*cache_regs)(struct kvm_vcpu *vcpu);
  381. void (*decache_regs)(struct kvm_vcpu *vcpu);
  382. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  383. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  384. void (*tlb_flush)(struct kvm_vcpu *vcpu);
  385. void (*inject_page_fault)(struct kvm_vcpu *vcpu,
  386. unsigned long addr, u32 err_code);
  387. void (*inject_gp)(struct kvm_vcpu *vcpu, unsigned err_code);
  388. void (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
  389. int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  390. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  391. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  392. unsigned char *hypercall_addr);
  393. int (*get_irq)(struct kvm_vcpu *vcpu);
  394. void (*set_irq)(struct kvm_vcpu *vcpu, int vec);
  395. void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
  396. void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
  397. struct kvm_run *run);
  398. int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
  399. };
  400. extern struct kvm_x86_ops *kvm_x86_ops;
  401. /* The guest did something we don't support. */
  402. #define pr_unimpl(vcpu, fmt, ...) \
  403. do { \
  404. if (printk_ratelimit()) \
  405. printk(KERN_ERR "kvm: %i: cpu%i " fmt, \
  406. current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \
  407. } while (0)
  408. #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
  409. #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
  410. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
  411. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
  412. void vcpu_load(struct kvm_vcpu *vcpu);
  413. void vcpu_put(struct kvm_vcpu *vcpu);
  414. void decache_vcpus_on_cpu(int cpu);
  415. int kvm_init(void *opaque, unsigned int vcpu_size,
  416. struct module *module);
  417. void kvm_exit(void);
  418. int kvm_mmu_module_init(void);
  419. void kvm_mmu_module_exit(void);
  420. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  421. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  422. int kvm_mmu_setup(struct kvm_vcpu *vcpu);
  423. void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
  424. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  425. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
  426. void kvm_mmu_zap_all(struct kvm *kvm);
  427. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
  428. hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa);
  429. #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
  430. #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
  431. static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
  432. hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva);
  433. struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
  434. extern struct page *bad_page;
  435. int is_error_page(struct page *page);
  436. int kvm_is_error_hva(unsigned long addr);
  437. int kvm_set_memory_region(struct kvm *kvm,
  438. struct kvm_userspace_memory_region *mem,
  439. int user_alloc);
  440. int __kvm_set_memory_region(struct kvm *kvm,
  441. struct kvm_userspace_memory_region *mem,
  442. int user_alloc);
  443. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn);
  444. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
  445. void kvm_release_page(struct page *page);
  446. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  447. int len);
  448. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
  449. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  450. int offset, int len);
  451. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  452. unsigned long len);
  453. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
  454. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
  455. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  456. int kvm_is_visible_gfn(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_vcpu_block(struct kvm_vcpu *vcpu);
  500. void kvm_resched(struct kvm_vcpu *vcpu);
  501. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
  502. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
  503. void kvm_flush_remote_tlbs(struct kvm *kvm);
  504. int emulator_read_std(unsigned long addr,
  505. void *val,
  506. unsigned int bytes,
  507. struct kvm_vcpu *vcpu);
  508. int emulator_write_emulated(unsigned long addr,
  509. const void *val,
  510. unsigned int bytes,
  511. struct kvm_vcpu *vcpu);
  512. unsigned long segment_base(u16 selector);
  513. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  514. const u8 *new, int bytes);
  515. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  516. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  517. int kvm_mmu_load(struct kvm_vcpu *vcpu);
  518. void kvm_mmu_unload(struct kvm_vcpu *vcpu);
  519. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
  520. int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
  521. long kvm_arch_dev_ioctl(struct file *filp,
  522. unsigned int ioctl, unsigned long arg);
  523. long kvm_arch_vcpu_ioctl(struct file *filp,
  524. unsigned int ioctl, unsigned long arg);
  525. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  526. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
  527. int kvm_dev_ioctl_check_extension(long ext);
  528. int kvm_get_dirty_log(struct kvm *kvm,
  529. struct kvm_dirty_log *log, int *is_dirty);
  530. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  531. struct kvm_dirty_log *log);
  532. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  533. struct
  534. kvm_userspace_memory_region *mem,
  535. int user_alloc);
  536. long kvm_arch_vm_ioctl(struct file *filp,
  537. unsigned int ioctl, unsigned long arg);
  538. void kvm_arch_destroy_vm(struct kvm *kvm);
  539. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  540. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  541. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  542. struct kvm_translation *tr);
  543. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  544. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  545. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  546. struct kvm_sregs *sregs);
  547. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  548. struct kvm_sregs *sregs);
  549. int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  550. struct kvm_debug_guest *dbg);
  551. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
  552. int kvm_arch_init(void *opaque);
  553. void kvm_arch_exit(void);
  554. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
  555. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
  556. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
  557. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  558. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
  559. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
  560. void kvm_arch_vcpu_destory(struct kvm_vcpu *vcpu);
  561. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu);
  562. void kvm_arch_hardware_enable(void *garbage);
  563. void kvm_arch_hardware_disable(void *garbage);
  564. int kvm_arch_hardware_setup(void);
  565. void kvm_arch_hardware_unsetup(void);
  566. void kvm_arch_check_processor_compat(void *rtn);
  567. void kvm_free_physmem(struct kvm *kvm);
  568. struct kvm *kvm_arch_create_vm(void);
  569. void kvm_arch_destroy_vm(struct kvm *kvm);
  570. static inline void kvm_guest_enter(void)
  571. {
  572. account_system_vtime(current);
  573. current->flags |= PF_VCPU;
  574. }
  575. static inline void kvm_guest_exit(void)
  576. {
  577. account_system_vtime(current);
  578. current->flags &= ~PF_VCPU;
  579. }
  580. static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot)
  581. {
  582. return slot - kvm->memslots;
  583. }
  584. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  585. {
  586. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  587. return (struct kvm_mmu_page *)page_private(page);
  588. }
  589. static inline u16 read_fs(void)
  590. {
  591. u16 seg;
  592. asm("mov %%fs, %0" : "=g"(seg));
  593. return seg;
  594. }
  595. static inline u16 read_gs(void)
  596. {
  597. u16 seg;
  598. asm("mov %%gs, %0" : "=g"(seg));
  599. return seg;
  600. }
  601. static inline u16 read_ldt(void)
  602. {
  603. u16 ldt;
  604. asm("sldt %0" : "=g"(ldt));
  605. return ldt;
  606. }
  607. static inline void load_fs(u16 sel)
  608. {
  609. asm("mov %0, %%fs" : : "rm"(sel));
  610. }
  611. static inline void load_gs(u16 sel)
  612. {
  613. asm("mov %0, %%gs" : : "rm"(sel));
  614. }
  615. #ifndef load_ldt
  616. static inline void load_ldt(u16 sel)
  617. {
  618. asm("lldt %0" : : "rm"(sel));
  619. }
  620. #endif
  621. static inline void get_idt(struct descriptor_table *table)
  622. {
  623. asm("sidt %0" : "=m"(*table));
  624. }
  625. static inline void get_gdt(struct descriptor_table *table)
  626. {
  627. asm("sgdt %0" : "=m"(*table));
  628. }
  629. static inline unsigned long read_tr_base(void)
  630. {
  631. u16 tr;
  632. asm("str %0" : "=g"(tr));
  633. return segment_base(tr);
  634. }
  635. #ifdef CONFIG_X86_64
  636. static inline unsigned long read_msr(unsigned long msr)
  637. {
  638. u64 value;
  639. rdmsrl(msr, value);
  640. return value;
  641. }
  642. #endif
  643. static inline void fx_save(struct i387_fxsave_struct *image)
  644. {
  645. asm("fxsave (%0)":: "r" (image));
  646. }
  647. static inline void fx_restore(struct i387_fxsave_struct *image)
  648. {
  649. asm("fxrstor (%0)":: "r" (image));
  650. }
  651. static inline void fpu_init(void)
  652. {
  653. asm("finit");
  654. }
  655. static inline u32 get_rdx_init_val(void)
  656. {
  657. return 0x600; /* P6 family */
  658. }
  659. #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
  660. #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
  661. #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
  662. #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
  663. #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
  664. #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
  665. #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
  666. #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
  667. #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
  668. #define MSR_IA32_TIME_STAMP_COUNTER 0x010
  669. #define TSS_IOPB_BASE_OFFSET 0x66
  670. #define TSS_BASE_SIZE 0x68
  671. #define TSS_IOPB_SIZE (65536 / 8)
  672. #define TSS_REDIRECTION_SIZE (256 / 8)
  673. #define RMODE_TSS_SIZE (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  674. enum kvm_stat_kind {
  675. KVM_STAT_VM,
  676. KVM_STAT_VCPU,
  677. };
  678. struct kvm_stats_debugfs_item {
  679. const char *name;
  680. int offset;
  681. enum kvm_stat_kind kind;
  682. struct dentry *dentry;
  683. };
  684. extern struct kvm_stats_debugfs_item debugfs_entries[];
  685. #endif