kvm.h 22 KB

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