kvm.h 22 KB

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