kvm.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. struct kvm_pio_request {
  132. unsigned long count;
  133. int cur_count;
  134. struct page *guest_pages[2];
  135. unsigned guest_page_offset;
  136. int in;
  137. int port;
  138. int size;
  139. int string;
  140. int down;
  141. int rep;
  142. };
  143. struct kvm_vcpu_stat {
  144. u32 pf_fixed;
  145. u32 pf_guest;
  146. u32 tlb_flush;
  147. u32 invlpg;
  148. u32 exits;
  149. u32 io_exits;
  150. u32 mmio_exits;
  151. u32 signal_exits;
  152. u32 irq_window_exits;
  153. u32 halt_exits;
  154. u32 halt_wakeup;
  155. u32 request_irq_exits;
  156. u32 irq_exits;
  157. u32 host_state_reload;
  158. u32 efer_reload;
  159. u32 fpu_reload;
  160. u32 insn_emulation;
  161. u32 insn_emulation_fail;
  162. };
  163. struct kvm_io_device {
  164. void (*read)(struct kvm_io_device *this,
  165. gpa_t addr,
  166. int len,
  167. void *val);
  168. void (*write)(struct kvm_io_device *this,
  169. gpa_t addr,
  170. int len,
  171. const void *val);
  172. int (*in_range)(struct kvm_io_device *this, gpa_t addr);
  173. void (*destructor)(struct kvm_io_device *this);
  174. void *private;
  175. };
  176. static inline void kvm_iodevice_read(struct kvm_io_device *dev,
  177. gpa_t addr,
  178. int len,
  179. void *val)
  180. {
  181. dev->read(dev, addr, len, val);
  182. }
  183. static inline void kvm_iodevice_write(struct kvm_io_device *dev,
  184. gpa_t addr,
  185. int len,
  186. const void *val)
  187. {
  188. dev->write(dev, addr, len, val);
  189. }
  190. static inline int kvm_iodevice_inrange(struct kvm_io_device *dev, gpa_t addr)
  191. {
  192. return dev->in_range(dev, addr);
  193. }
  194. static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
  195. {
  196. if (dev->destructor)
  197. dev->destructor(dev);
  198. }
  199. /*
  200. * It would be nice to use something smarter than a linear search, TBD...
  201. * Thankfully we dont expect many devices to register (famous last words :),
  202. * so until then it will suffice. At least its abstracted so we can change
  203. * in one place.
  204. */
  205. struct kvm_io_bus {
  206. int dev_count;
  207. #define NR_IOBUS_DEVS 6
  208. struct kvm_io_device *devs[NR_IOBUS_DEVS];
  209. };
  210. void kvm_io_bus_init(struct kvm_io_bus *bus);
  211. void kvm_io_bus_destroy(struct kvm_io_bus *bus);
  212. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr);
  213. void kvm_io_bus_register_dev(struct kvm_io_bus *bus,
  214. struct kvm_io_device *dev);
  215. #ifdef CONFIG_HAS_IOMEM
  216. #define KVM_VCPU_MMIO \
  217. int mmio_needed; \
  218. int mmio_read_completed; \
  219. int mmio_is_write; \
  220. int mmio_size; \
  221. unsigned char mmio_data[8]; \
  222. gpa_t mmio_phys_addr;
  223. #else
  224. #define KVM_VCPU_MMIO
  225. #endif
  226. #define KVM_VCPU_COMM \
  227. struct kvm *kvm; \
  228. struct preempt_notifier preempt_notifier; \
  229. int vcpu_id; \
  230. struct mutex mutex; \
  231. int cpu; \
  232. struct kvm_run *run; \
  233. int guest_mode; \
  234. unsigned long requests; \
  235. struct kvm_guest_debug guest_debug; \
  236. int fpu_active; \
  237. int guest_fpu_loaded; \
  238. wait_queue_head_t wq; \
  239. int sigset_active; \
  240. sigset_t sigset; \
  241. struct kvm_vcpu_stat stat; \
  242. KVM_VCPU_MMIO
  243. struct kvm_mem_alias {
  244. gfn_t base_gfn;
  245. unsigned long npages;
  246. gfn_t target_gfn;
  247. };
  248. struct kvm_memory_slot {
  249. gfn_t base_gfn;
  250. unsigned long npages;
  251. unsigned long flags;
  252. unsigned long *rmap;
  253. unsigned long *dirty_bitmap;
  254. unsigned long userspace_addr;
  255. int user_alloc;
  256. };
  257. struct kvm_vm_stat {
  258. u32 mmu_shadow_zapped;
  259. u32 mmu_pte_write;
  260. u32 mmu_pte_updated;
  261. u32 mmu_pde_zapped;
  262. u32 mmu_flooded;
  263. u32 mmu_recycled;
  264. };
  265. struct kvm {
  266. struct mutex lock; /* protects everything except vcpus */
  267. int naliases;
  268. struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
  269. int nmemslots;
  270. struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +
  271. KVM_PRIVATE_MEM_SLOTS];
  272. /*
  273. * Hash table of struct kvm_mmu_page.
  274. */
  275. struct list_head active_mmu_pages;
  276. unsigned int n_free_mmu_pages;
  277. unsigned int n_requested_mmu_pages;
  278. unsigned int n_alloc_mmu_pages;
  279. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  280. struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
  281. unsigned long rmap_overflow;
  282. struct list_head vm_list;
  283. struct file *filp;
  284. struct kvm_io_bus mmio_bus;
  285. struct kvm_io_bus pio_bus;
  286. struct kvm_pic *vpic;
  287. struct kvm_ioapic *vioapic;
  288. int round_robin_prev_vcpu;
  289. unsigned int tss_addr;
  290. struct page *apic_access_page;
  291. struct kvm_vm_stat stat;
  292. };
  293. static inline struct kvm_pic *pic_irqchip(struct kvm *kvm)
  294. {
  295. return kvm->vpic;
  296. }
  297. static inline struct kvm_ioapic *ioapic_irqchip(struct kvm *kvm)
  298. {
  299. return kvm->vioapic;
  300. }
  301. static inline int irqchip_in_kernel(struct kvm *kvm)
  302. {
  303. return pic_irqchip(kvm) != NULL;
  304. }
  305. struct descriptor_table {
  306. u16 limit;
  307. unsigned long base;
  308. } __attribute__((packed));
  309. /* The guest did something we don't support. */
  310. #define pr_unimpl(vcpu, fmt, ...) \
  311. do { \
  312. if (printk_ratelimit()) \
  313. printk(KERN_ERR "kvm: %i: cpu%i " fmt, \
  314. current->tgid, (vcpu)->vcpu_id , ## __VA_ARGS__); \
  315. } while (0)
  316. #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
  317. #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
  318. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
  319. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
  320. void vcpu_load(struct kvm_vcpu *vcpu);
  321. void vcpu_put(struct kvm_vcpu *vcpu);
  322. void decache_vcpus_on_cpu(int cpu);
  323. int kvm_init(void *opaque, unsigned int vcpu_size,
  324. struct module *module);
  325. void kvm_exit(void);
  326. int kvm_mmu_module_init(void);
  327. void kvm_mmu_module_exit(void);
  328. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  329. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  330. int kvm_mmu_setup(struct kvm_vcpu *vcpu);
  331. void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
  332. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  333. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
  334. void kvm_mmu_zap_all(struct kvm *kvm);
  335. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
  336. hpa_t gpa_to_hpa(struct kvm *kvm, gpa_t gpa);
  337. #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
  338. #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
  339. static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
  340. hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva);
  341. struct page *gva_to_page(struct kvm_vcpu *vcpu, gva_t gva);
  342. extern struct page *bad_page;
  343. int is_error_page(struct page *page);
  344. int kvm_is_error_hva(unsigned long addr);
  345. int kvm_set_memory_region(struct kvm *kvm,
  346. struct kvm_userspace_memory_region *mem,
  347. int user_alloc);
  348. int __kvm_set_memory_region(struct kvm *kvm,
  349. struct kvm_userspace_memory_region *mem,
  350. int user_alloc);
  351. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn);
  352. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
  353. void kvm_release_page(struct page *page);
  354. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  355. int len);
  356. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len);
  357. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  358. int offset, int len);
  359. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  360. unsigned long len);
  361. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len);
  362. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len);
  363. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  364. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn);
  365. void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
  366. enum emulation_result {
  367. EMULATE_DONE, /* no further processing */
  368. EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
  369. EMULATE_FAIL, /* can't emulate this instruction */
  370. };
  371. int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
  372. unsigned long cr2, u16 error_code, int no_decode);
  373. void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char *context);
  374. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  375. void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  376. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  377. unsigned long *rflags);
  378. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
  379. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
  380. unsigned long *rflags);
  381. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
  382. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  383. struct x86_emulate_ctxt;
  384. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  385. int size, unsigned port);
  386. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  387. int size, unsigned long count, int down,
  388. gva_t address, int rep, unsigned port);
  389. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
  390. int kvm_emulate_halt(struct kvm_vcpu *vcpu);
  391. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
  392. int emulate_clts(struct kvm_vcpu *vcpu);
  393. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
  394. unsigned long *dest);
  395. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
  396. unsigned long value);
  397. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  398. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr0);
  399. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr0);
  400. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr0);
  401. unsigned long get_cr8(struct kvm_vcpu *vcpu);
  402. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  403. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
  404. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  405. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  406. void fx_init(struct kvm_vcpu *vcpu);
  407. void kvm_vcpu_block(struct kvm_vcpu *vcpu);
  408. void kvm_resched(struct kvm_vcpu *vcpu);
  409. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu);
  410. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu);
  411. void kvm_flush_remote_tlbs(struct kvm *kvm);
  412. int emulator_read_std(unsigned long addr,
  413. void *val,
  414. unsigned int bytes,
  415. struct kvm_vcpu *vcpu);
  416. int emulator_write_emulated(unsigned long addr,
  417. const void *val,
  418. unsigned int bytes,
  419. struct kvm_vcpu *vcpu);
  420. unsigned long segment_base(u16 selector);
  421. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  422. const u8 *new, int bytes);
  423. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  424. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  425. int kvm_mmu_load(struct kvm_vcpu *vcpu);
  426. void kvm_mmu_unload(struct kvm_vcpu *vcpu);
  427. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
  428. int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
  429. long kvm_arch_dev_ioctl(struct file *filp,
  430. unsigned int ioctl, unsigned long arg);
  431. long kvm_arch_vcpu_ioctl(struct file *filp,
  432. unsigned int ioctl, unsigned long arg);
  433. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  434. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
  435. int kvm_dev_ioctl_check_extension(long ext);
  436. int kvm_get_dirty_log(struct kvm *kvm,
  437. struct kvm_dirty_log *log, int *is_dirty);
  438. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  439. struct kvm_dirty_log *log);
  440. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  441. struct
  442. kvm_userspace_memory_region *mem,
  443. int user_alloc);
  444. long kvm_arch_vm_ioctl(struct file *filp,
  445. unsigned int ioctl, unsigned long arg);
  446. void kvm_arch_destroy_vm(struct kvm *kvm);
  447. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  448. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu);
  449. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  450. struct kvm_translation *tr);
  451. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  452. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs);
  453. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  454. struct kvm_sregs *sregs);
  455. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  456. struct kvm_sregs *sregs);
  457. int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  458. struct kvm_debug_guest *dbg);
  459. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run);
  460. int kvm_arch_init(void *opaque);
  461. void kvm_arch_exit(void);
  462. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu);
  463. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu);
  464. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
  465. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
  466. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
  467. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
  468. void kvm_arch_vcpu_destory(struct kvm_vcpu *vcpu);
  469. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu);
  470. void kvm_arch_hardware_enable(void *garbage);
  471. void kvm_arch_hardware_disable(void *garbage);
  472. int kvm_arch_hardware_setup(void);
  473. void kvm_arch_hardware_unsetup(void);
  474. void kvm_arch_check_processor_compat(void *rtn);
  475. void kvm_free_physmem(struct kvm *kvm);
  476. struct kvm *kvm_arch_create_vm(void);
  477. void kvm_arch_destroy_vm(struct kvm *kvm);
  478. static inline void kvm_guest_enter(void)
  479. {
  480. account_system_vtime(current);
  481. current->flags |= PF_VCPU;
  482. }
  483. static inline void kvm_guest_exit(void)
  484. {
  485. account_system_vtime(current);
  486. current->flags &= ~PF_VCPU;
  487. }
  488. static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot)
  489. {
  490. return slot - kvm->memslots;
  491. }
  492. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  493. {
  494. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  495. return (struct kvm_mmu_page *)page_private(page);
  496. }
  497. static inline u16 read_fs(void)
  498. {
  499. u16 seg;
  500. asm("mov %%fs, %0" : "=g"(seg));
  501. return seg;
  502. }
  503. static inline u16 read_gs(void)
  504. {
  505. u16 seg;
  506. asm("mov %%gs, %0" : "=g"(seg));
  507. return seg;
  508. }
  509. static inline u16 read_ldt(void)
  510. {
  511. u16 ldt;
  512. asm("sldt %0" : "=g"(ldt));
  513. return ldt;
  514. }
  515. static inline void load_fs(u16 sel)
  516. {
  517. asm("mov %0, %%fs" : : "rm"(sel));
  518. }
  519. static inline void load_gs(u16 sel)
  520. {
  521. asm("mov %0, %%gs" : : "rm"(sel));
  522. }
  523. #ifndef load_ldt
  524. static inline void load_ldt(u16 sel)
  525. {
  526. asm("lldt %0" : : "rm"(sel));
  527. }
  528. #endif
  529. static inline void get_idt(struct descriptor_table *table)
  530. {
  531. asm("sidt %0" : "=m"(*table));
  532. }
  533. static inline void get_gdt(struct descriptor_table *table)
  534. {
  535. asm("sgdt %0" : "=m"(*table));
  536. }
  537. static inline unsigned long read_tr_base(void)
  538. {
  539. u16 tr;
  540. asm("str %0" : "=g"(tr));
  541. return segment_base(tr);
  542. }
  543. #ifdef CONFIG_X86_64
  544. static inline unsigned long read_msr(unsigned long msr)
  545. {
  546. u64 value;
  547. rdmsrl(msr, value);
  548. return value;
  549. }
  550. #endif
  551. static inline void fx_save(struct i387_fxsave_struct *image)
  552. {
  553. asm("fxsave (%0)":: "r" (image));
  554. }
  555. static inline void fx_restore(struct i387_fxsave_struct *image)
  556. {
  557. asm("fxrstor (%0)":: "r" (image));
  558. }
  559. static inline void fpu_init(void)
  560. {
  561. asm("finit");
  562. }
  563. static inline u32 get_rdx_init_val(void)
  564. {
  565. return 0x600; /* P6 family */
  566. }
  567. #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
  568. #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
  569. #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
  570. #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
  571. #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
  572. #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
  573. #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
  574. #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
  575. #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
  576. #define MSR_IA32_TIME_STAMP_COUNTER 0x010
  577. #define TSS_IOPB_BASE_OFFSET 0x66
  578. #define TSS_BASE_SIZE 0x68
  579. #define TSS_IOPB_SIZE (65536 / 8)
  580. #define TSS_REDIRECTION_SIZE (256 / 8)
  581. #define RMODE_TSS_SIZE (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  582. enum kvm_stat_kind {
  583. KVM_STAT_VM,
  584. KVM_STAT_VCPU,
  585. };
  586. struct kvm_stats_debugfs_item {
  587. const char *name;
  588. int offset;
  589. enum kvm_stat_kind kind;
  590. struct dentry *dentry;
  591. };
  592. extern struct kvm_stats_debugfs_item debugfs_entries[];
  593. #endif