kvm_host.h 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This header defines architecture specific interfaces, x86 version
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2. See
  7. * the COPYING file in the top-level directory.
  8. *
  9. */
  10. #ifndef _ASM_X86_KVM_HOST_H
  11. #define _ASM_X86_KVM_HOST_H
  12. #include <linux/types.h>
  13. #include <linux/mm.h>
  14. #include <linux/mmu_notifier.h>
  15. #include <linux/tracepoint.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/irq_work.h>
  18. #include <linux/kvm.h>
  19. #include <linux/kvm_para.h>
  20. #include <linux/kvm_types.h>
  21. #include <linux/perf_event.h>
  22. #include <asm/pvclock-abi.h>
  23. #include <asm/desc.h>
  24. #include <asm/mtrr.h>
  25. #include <asm/msr-index.h>
  26. #include <asm/asm.h>
  27. #define KVM_MAX_VCPUS 254
  28. #define KVM_SOFT_MAX_VCPUS 160
  29. #define KVM_MEMORY_SLOTS 32
  30. /* memory slots that does not exposed to userspace */
  31. #define KVM_PRIVATE_MEM_SLOTS 4
  32. #define KVM_MEM_SLOTS_NUM (KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
  33. #define KVM_MMIO_SIZE 16
  34. #define KVM_PIO_PAGE_OFFSET 1
  35. #define KVM_COALESCED_MMIO_PAGE_OFFSET 2
  36. #define CR0_RESERVED_BITS \
  37. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  38. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  39. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  40. #define CR3_PAE_RESERVED_BITS ((X86_CR3_PWT | X86_CR3_PCD) - 1)
  41. #define CR3_NONPAE_RESERVED_BITS ((PAGE_SIZE-1) & ~(X86_CR3_PWT | X86_CR3_PCD))
  42. #define CR3_PCID_ENABLED_RESERVED_BITS 0xFFFFFF0000000000ULL
  43. #define CR3_L_MODE_RESERVED_BITS (CR3_NONPAE_RESERVED_BITS | \
  44. 0xFFFFFF0000000000ULL)
  45. #define CR4_RESERVED_BITS \
  46. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  47. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  48. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
  49. | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_RDWRGSFS \
  50. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  51. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  52. #define INVALID_PAGE (~(hpa_t)0)
  53. #define VALID_PAGE(x) ((x) != INVALID_PAGE)
  54. #define UNMAPPED_GVA (~(gpa_t)0)
  55. /* KVM Hugepage definitions for x86 */
  56. #define KVM_NR_PAGE_SIZES 3
  57. #define KVM_HPAGE_GFN_SHIFT(x) (((x) - 1) * 9)
  58. #define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
  59. #define KVM_HPAGE_SIZE(x) (1UL << KVM_HPAGE_SHIFT(x))
  60. #define KVM_HPAGE_MASK(x) (~(KVM_HPAGE_SIZE(x) - 1))
  61. #define KVM_PAGES_PER_HPAGE(x) (KVM_HPAGE_SIZE(x) / PAGE_SIZE)
  62. #define DE_VECTOR 0
  63. #define DB_VECTOR 1
  64. #define BP_VECTOR 3
  65. #define OF_VECTOR 4
  66. #define BR_VECTOR 5
  67. #define UD_VECTOR 6
  68. #define NM_VECTOR 7
  69. #define DF_VECTOR 8
  70. #define TS_VECTOR 10
  71. #define NP_VECTOR 11
  72. #define SS_VECTOR 12
  73. #define GP_VECTOR 13
  74. #define PF_VECTOR 14
  75. #define MF_VECTOR 16
  76. #define MC_VECTOR 18
  77. #define SELECTOR_TI_MASK (1 << 2)
  78. #define SELECTOR_RPL_MASK 0x03
  79. #define IOPL_SHIFT 12
  80. #define KVM_PERMILLE_MMU_PAGES 20
  81. #define KVM_MIN_ALLOC_MMU_PAGES 64
  82. #define KVM_MMU_HASH_SHIFT 10
  83. #define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
  84. #define KVM_MIN_FREE_MMU_PAGES 5
  85. #define KVM_REFILL_PAGES 25
  86. #define KVM_MAX_CPUID_ENTRIES 80
  87. #define KVM_NR_FIXED_MTRR_REGION 88
  88. #define KVM_NR_VAR_MTRR 8
  89. #define ASYNC_PF_PER_VCPU 64
  90. extern raw_spinlock_t kvm_lock;
  91. extern struct list_head vm_list;
  92. struct kvm_vcpu;
  93. struct kvm;
  94. struct kvm_async_pf;
  95. enum kvm_reg {
  96. VCPU_REGS_RAX = 0,
  97. VCPU_REGS_RCX = 1,
  98. VCPU_REGS_RDX = 2,
  99. VCPU_REGS_RBX = 3,
  100. VCPU_REGS_RSP = 4,
  101. VCPU_REGS_RBP = 5,
  102. VCPU_REGS_RSI = 6,
  103. VCPU_REGS_RDI = 7,
  104. #ifdef CONFIG_X86_64
  105. VCPU_REGS_R8 = 8,
  106. VCPU_REGS_R9 = 9,
  107. VCPU_REGS_R10 = 10,
  108. VCPU_REGS_R11 = 11,
  109. VCPU_REGS_R12 = 12,
  110. VCPU_REGS_R13 = 13,
  111. VCPU_REGS_R14 = 14,
  112. VCPU_REGS_R15 = 15,
  113. #endif
  114. VCPU_REGS_RIP,
  115. NR_VCPU_REGS
  116. };
  117. enum kvm_reg_ex {
  118. VCPU_EXREG_PDPTR = NR_VCPU_REGS,
  119. VCPU_EXREG_CR3,
  120. VCPU_EXREG_RFLAGS,
  121. VCPU_EXREG_CPL,
  122. VCPU_EXREG_SEGMENTS,
  123. };
  124. enum {
  125. VCPU_SREG_ES,
  126. VCPU_SREG_CS,
  127. VCPU_SREG_SS,
  128. VCPU_SREG_DS,
  129. VCPU_SREG_FS,
  130. VCPU_SREG_GS,
  131. VCPU_SREG_TR,
  132. VCPU_SREG_LDTR,
  133. };
  134. #include <asm/kvm_emulate.h>
  135. #define KVM_NR_MEM_OBJS 40
  136. #define KVM_NR_DB_REGS 4
  137. #define DR6_BD (1 << 13)
  138. #define DR6_BS (1 << 14)
  139. #define DR6_FIXED_1 0xffff0ff0
  140. #define DR6_VOLATILE 0x0000e00f
  141. #define DR7_BP_EN_MASK 0x000000ff
  142. #define DR7_GE (1 << 9)
  143. #define DR7_GD (1 << 13)
  144. #define DR7_FIXED_1 0x00000400
  145. #define DR7_VOLATILE 0xffff23ff
  146. /* apic attention bits */
  147. #define KVM_APIC_CHECK_VAPIC 0
  148. /*
  149. * The following bit is set with PV-EOI, unset on EOI.
  150. * We detect PV-EOI changes by guest by comparing
  151. * this bit with PV-EOI in guest memory.
  152. * See the implementation in apic_update_pv_eoi.
  153. */
  154. #define KVM_APIC_PV_EOI_PENDING 1
  155. /*
  156. * We don't want allocation failures within the mmu code, so we preallocate
  157. * enough memory for a single page fault in a cache.
  158. */
  159. struct kvm_mmu_memory_cache {
  160. int nobjs;
  161. void *objects[KVM_NR_MEM_OBJS];
  162. };
  163. /*
  164. * kvm_mmu_page_role, below, is defined as:
  165. *
  166. * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
  167. * bits 4:7 - page table level for this shadow (1-4)
  168. * bits 8:9 - page table quadrant for 2-level guests
  169. * bit 16 - direct mapping of virtual to physical mapping at gfn
  170. * used for real mode and two-dimensional paging
  171. * bits 17:19 - common access permissions for all ptes in this shadow page
  172. */
  173. union kvm_mmu_page_role {
  174. unsigned word;
  175. struct {
  176. unsigned level:4;
  177. unsigned cr4_pae:1;
  178. unsigned quadrant:2;
  179. unsigned pad_for_nice_hex_output:6;
  180. unsigned direct:1;
  181. unsigned access:3;
  182. unsigned invalid:1;
  183. unsigned nxe:1;
  184. unsigned cr0_wp:1;
  185. unsigned smep_andnot_wp:1;
  186. };
  187. };
  188. struct kvm_mmu_page {
  189. struct list_head link;
  190. struct hlist_node hash_link;
  191. /*
  192. * The following two entries are used to key the shadow page in the
  193. * hash table.
  194. */
  195. gfn_t gfn;
  196. union kvm_mmu_page_role role;
  197. u64 *spt;
  198. /* hold the gfn of each spte inside spt */
  199. gfn_t *gfns;
  200. /*
  201. * One bit set per slot which has memory
  202. * in this shadow page.
  203. */
  204. DECLARE_BITMAP(slot_bitmap, KVM_MEM_SLOTS_NUM);
  205. bool unsync;
  206. int root_count; /* Currently serving as active root */
  207. unsigned int unsync_children;
  208. unsigned long parent_ptes; /* Reverse mapping for parent_pte */
  209. DECLARE_BITMAP(unsync_child_bitmap, 512);
  210. #ifdef CONFIG_X86_32
  211. int clear_spte_count;
  212. #endif
  213. int write_flooding_count;
  214. };
  215. struct kvm_pio_request {
  216. unsigned long count;
  217. int in;
  218. int port;
  219. int size;
  220. };
  221. /*
  222. * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
  223. * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
  224. * mode.
  225. */
  226. struct kvm_mmu {
  227. void (*new_cr3)(struct kvm_vcpu *vcpu);
  228. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long root);
  229. unsigned long (*get_cr3)(struct kvm_vcpu *vcpu);
  230. u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index);
  231. int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err,
  232. bool prefault);
  233. void (*inject_page_fault)(struct kvm_vcpu *vcpu,
  234. struct x86_exception *fault);
  235. void (*free)(struct kvm_vcpu *vcpu);
  236. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva, u32 access,
  237. struct x86_exception *exception);
  238. gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access);
  239. int (*sync_page)(struct kvm_vcpu *vcpu,
  240. struct kvm_mmu_page *sp);
  241. void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva);
  242. void (*update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  243. u64 *spte, const void *pte);
  244. hpa_t root_hpa;
  245. int root_level;
  246. int shadow_root_level;
  247. union kvm_mmu_page_role base_role;
  248. bool direct_map;
  249. /*
  250. * Bitmap; bit set = permission fault
  251. * Byte index: page fault error code [4:1]
  252. * Bit index: pte permissions in ACC_* format
  253. */
  254. u8 permissions[16];
  255. u64 *pae_root;
  256. u64 *lm_root;
  257. u64 rsvd_bits_mask[2][4];
  258. /*
  259. * Bitmap: bit set = last pte in walk
  260. * index[0:1]: level (zero-based)
  261. * index[2]: pte.ps
  262. */
  263. u8 last_pte_bitmap;
  264. bool nx;
  265. u64 pdptrs[4]; /* pae */
  266. };
  267. enum pmc_type {
  268. KVM_PMC_GP = 0,
  269. KVM_PMC_FIXED,
  270. };
  271. struct kvm_pmc {
  272. enum pmc_type type;
  273. u8 idx;
  274. u64 counter;
  275. u64 eventsel;
  276. struct perf_event *perf_event;
  277. struct kvm_vcpu *vcpu;
  278. };
  279. struct kvm_pmu {
  280. unsigned nr_arch_gp_counters;
  281. unsigned nr_arch_fixed_counters;
  282. unsigned available_event_types;
  283. u64 fixed_ctr_ctrl;
  284. u64 global_ctrl;
  285. u64 global_status;
  286. u64 global_ovf_ctrl;
  287. u64 counter_bitmask[2];
  288. u64 global_ctrl_mask;
  289. u8 version;
  290. struct kvm_pmc gp_counters[INTEL_PMC_MAX_GENERIC];
  291. struct kvm_pmc fixed_counters[INTEL_PMC_MAX_FIXED];
  292. struct irq_work irq_work;
  293. u64 reprogram_pmi;
  294. };
  295. struct kvm_vcpu_arch {
  296. /*
  297. * rip and regs accesses must go through
  298. * kvm_{register,rip}_{read,write} functions.
  299. */
  300. unsigned long regs[NR_VCPU_REGS];
  301. u32 regs_avail;
  302. u32 regs_dirty;
  303. unsigned long cr0;
  304. unsigned long cr0_guest_owned_bits;
  305. unsigned long cr2;
  306. unsigned long cr3;
  307. unsigned long cr4;
  308. unsigned long cr4_guest_owned_bits;
  309. unsigned long cr8;
  310. u32 hflags;
  311. u64 efer;
  312. u64 apic_base;
  313. struct kvm_lapic *apic; /* kernel irqchip context */
  314. unsigned long apic_attention;
  315. int32_t apic_arb_prio;
  316. int mp_state;
  317. int sipi_vector;
  318. u64 ia32_misc_enable_msr;
  319. bool tpr_access_reporting;
  320. /*
  321. * Paging state of the vcpu
  322. *
  323. * If the vcpu runs in guest mode with two level paging this still saves
  324. * the paging mode of the l1 guest. This context is always used to
  325. * handle faults.
  326. */
  327. struct kvm_mmu mmu;
  328. /*
  329. * Paging state of an L2 guest (used for nested npt)
  330. *
  331. * This context will save all necessary information to walk page tables
  332. * of the an L2 guest. This context is only initialized for page table
  333. * walking and not for faulting since we never handle l2 page faults on
  334. * the host.
  335. */
  336. struct kvm_mmu nested_mmu;
  337. /*
  338. * Pointer to the mmu context currently used for
  339. * gva_to_gpa translations.
  340. */
  341. struct kvm_mmu *walk_mmu;
  342. struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;
  343. struct kvm_mmu_memory_cache mmu_page_cache;
  344. struct kvm_mmu_memory_cache mmu_page_header_cache;
  345. struct fpu guest_fpu;
  346. u64 xcr0;
  347. struct kvm_pio_request pio;
  348. void *pio_data;
  349. u8 event_exit_inst_len;
  350. struct kvm_queued_exception {
  351. bool pending;
  352. bool has_error_code;
  353. bool reinject;
  354. u8 nr;
  355. u32 error_code;
  356. } exception;
  357. struct kvm_queued_interrupt {
  358. bool pending;
  359. bool soft;
  360. u8 nr;
  361. } interrupt;
  362. int halt_request; /* real mode on Intel only */
  363. int cpuid_nent;
  364. struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
  365. /* emulate context */
  366. struct x86_emulate_ctxt emulate_ctxt;
  367. bool emulate_regs_need_sync_to_vcpu;
  368. bool emulate_regs_need_sync_from_vcpu;
  369. int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
  370. gpa_t time;
  371. struct pvclock_vcpu_time_info hv_clock;
  372. unsigned int hw_tsc_khz;
  373. unsigned int time_offset;
  374. struct page *time_page;
  375. /* set guest stopped flag in pvclock flags field */
  376. bool pvclock_set_guest_stopped_request;
  377. struct {
  378. u64 msr_val;
  379. u64 last_steal;
  380. u64 accum_steal;
  381. struct gfn_to_hva_cache stime;
  382. struct kvm_steal_time steal;
  383. } st;
  384. u64 last_guest_tsc;
  385. u64 last_kernel_ns;
  386. u64 last_host_tsc;
  387. u64 tsc_offset_adjustment;
  388. u64 this_tsc_nsec;
  389. u64 this_tsc_write;
  390. u8 this_tsc_generation;
  391. bool tsc_catchup;
  392. bool tsc_always_catchup;
  393. s8 virtual_tsc_shift;
  394. u32 virtual_tsc_mult;
  395. u32 virtual_tsc_khz;
  396. atomic_t nmi_queued; /* unprocessed asynchronous NMIs */
  397. unsigned nmi_pending; /* NMI queued after currently running handler */
  398. bool nmi_injected; /* Trying to inject an NMI this entry */
  399. struct mtrr_state_type mtrr_state;
  400. u32 pat;
  401. int switch_db_regs;
  402. unsigned long db[KVM_NR_DB_REGS];
  403. unsigned long dr6;
  404. unsigned long dr7;
  405. unsigned long eff_db[KVM_NR_DB_REGS];
  406. u64 mcg_cap;
  407. u64 mcg_status;
  408. u64 mcg_ctl;
  409. u64 *mce_banks;
  410. /* Cache MMIO info */
  411. u64 mmio_gva;
  412. unsigned access;
  413. gfn_t mmio_gfn;
  414. struct kvm_pmu pmu;
  415. /* used for guest single stepping over the given code position */
  416. unsigned long singlestep_rip;
  417. /* fields used by HYPER-V emulation */
  418. u64 hv_vapic;
  419. cpumask_var_t wbinvd_dirty_mask;
  420. unsigned long last_retry_eip;
  421. unsigned long last_retry_addr;
  422. struct {
  423. bool halted;
  424. gfn_t gfns[roundup_pow_of_two(ASYNC_PF_PER_VCPU)];
  425. struct gfn_to_hva_cache data;
  426. u64 msr_val;
  427. u32 id;
  428. bool send_user_only;
  429. } apf;
  430. /* OSVW MSRs (AMD only) */
  431. struct {
  432. u64 length;
  433. u64 status;
  434. } osvw;
  435. struct {
  436. u64 msr_val;
  437. struct gfn_to_hva_cache data;
  438. } pv_eoi;
  439. };
  440. struct kvm_lpage_info {
  441. int write_count;
  442. };
  443. struct kvm_arch_memory_slot {
  444. unsigned long *rmap[KVM_NR_PAGE_SIZES];
  445. struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
  446. };
  447. struct kvm_arch {
  448. unsigned int n_used_mmu_pages;
  449. unsigned int n_requested_mmu_pages;
  450. unsigned int n_max_mmu_pages;
  451. unsigned int indirect_shadow_pages;
  452. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  453. /*
  454. * Hash table of struct kvm_mmu_page.
  455. */
  456. struct list_head active_mmu_pages;
  457. struct list_head assigned_dev_head;
  458. struct iommu_domain *iommu_domain;
  459. int iommu_flags;
  460. struct kvm_pic *vpic;
  461. struct kvm_ioapic *vioapic;
  462. struct kvm_pit *vpit;
  463. int vapics_in_nmi_mode;
  464. unsigned int tss_addr;
  465. struct page *apic_access_page;
  466. gpa_t wall_clock;
  467. struct page *ept_identity_pagetable;
  468. bool ept_identity_pagetable_done;
  469. gpa_t ept_identity_map_addr;
  470. unsigned long irq_sources_bitmap;
  471. s64 kvmclock_offset;
  472. raw_spinlock_t tsc_write_lock;
  473. u64 last_tsc_nsec;
  474. u64 last_tsc_write;
  475. u32 last_tsc_khz;
  476. u64 cur_tsc_nsec;
  477. u64 cur_tsc_write;
  478. u64 cur_tsc_offset;
  479. u8 cur_tsc_generation;
  480. struct kvm_xen_hvm_config xen_hvm_config;
  481. /* fields used by HYPER-V emulation */
  482. u64 hv_guest_os_id;
  483. u64 hv_hypercall;
  484. #ifdef CONFIG_KVM_MMU_AUDIT
  485. int audit_point;
  486. #endif
  487. };
  488. struct kvm_vm_stat {
  489. u32 mmu_shadow_zapped;
  490. u32 mmu_pte_write;
  491. u32 mmu_pte_updated;
  492. u32 mmu_pde_zapped;
  493. u32 mmu_flooded;
  494. u32 mmu_recycled;
  495. u32 mmu_cache_miss;
  496. u32 mmu_unsync;
  497. u32 remote_tlb_flush;
  498. u32 lpages;
  499. };
  500. struct kvm_vcpu_stat {
  501. u32 pf_fixed;
  502. u32 pf_guest;
  503. u32 tlb_flush;
  504. u32 invlpg;
  505. u32 exits;
  506. u32 io_exits;
  507. u32 mmio_exits;
  508. u32 signal_exits;
  509. u32 irq_window_exits;
  510. u32 nmi_window_exits;
  511. u32 halt_exits;
  512. u32 halt_wakeup;
  513. u32 request_irq_exits;
  514. u32 irq_exits;
  515. u32 host_state_reload;
  516. u32 efer_reload;
  517. u32 fpu_reload;
  518. u32 insn_emulation;
  519. u32 insn_emulation_fail;
  520. u32 hypercalls;
  521. u32 irq_injections;
  522. u32 nmi_injections;
  523. };
  524. struct x86_instruction_info;
  525. struct kvm_x86_ops {
  526. int (*cpu_has_kvm_support)(void); /* __init */
  527. int (*disabled_by_bios)(void); /* __init */
  528. int (*hardware_enable)(void *dummy);
  529. void (*hardware_disable)(void *dummy);
  530. void (*check_processor_compatibility)(void *rtn);
  531. int (*hardware_setup)(void); /* __init */
  532. void (*hardware_unsetup)(void); /* __exit */
  533. bool (*cpu_has_accelerated_tpr)(void);
  534. void (*cpuid_update)(struct kvm_vcpu *vcpu);
  535. /* Create, but do not attach this VCPU */
  536. struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
  537. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  538. int (*vcpu_reset)(struct kvm_vcpu *vcpu);
  539. void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
  540. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  541. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  542. void (*set_guest_debug)(struct kvm_vcpu *vcpu,
  543. struct kvm_guest_debug *dbg);
  544. int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
  545. int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  546. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  547. void (*get_segment)(struct kvm_vcpu *vcpu,
  548. struct kvm_segment *var, int seg);
  549. int (*get_cpl)(struct kvm_vcpu *vcpu);
  550. void (*set_segment)(struct kvm_vcpu *vcpu,
  551. struct kvm_segment *var, int seg);
  552. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  553. void (*decache_cr0_guest_bits)(struct kvm_vcpu *vcpu);
  554. void (*decache_cr3)(struct kvm_vcpu *vcpu);
  555. void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  556. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  557. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  558. int (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  559. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  560. void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  561. void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  562. void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  563. void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  564. void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
  565. void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
  566. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  567. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  568. void (*fpu_activate)(struct kvm_vcpu *vcpu);
  569. void (*fpu_deactivate)(struct kvm_vcpu *vcpu);
  570. void (*tlb_flush)(struct kvm_vcpu *vcpu);
  571. void (*run)(struct kvm_vcpu *vcpu);
  572. int (*handle_exit)(struct kvm_vcpu *vcpu);
  573. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  574. void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
  575. u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
  576. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  577. unsigned char *hypercall_addr);
  578. void (*set_irq)(struct kvm_vcpu *vcpu);
  579. void (*set_nmi)(struct kvm_vcpu *vcpu);
  580. void (*queue_exception)(struct kvm_vcpu *vcpu, unsigned nr,
  581. bool has_error_code, u32 error_code,
  582. bool reinject);
  583. void (*cancel_injection)(struct kvm_vcpu *vcpu);
  584. int (*interrupt_allowed)(struct kvm_vcpu *vcpu);
  585. int (*nmi_allowed)(struct kvm_vcpu *vcpu);
  586. bool (*get_nmi_mask)(struct kvm_vcpu *vcpu);
  587. void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked);
  588. void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
  589. void (*enable_irq_window)(struct kvm_vcpu *vcpu);
  590. void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
  591. int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
  592. int (*get_tdp_level)(void);
  593. u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
  594. int (*get_lpage_level)(void);
  595. bool (*rdtscp_supported)(void);
  596. bool (*invpcid_supported)(void);
  597. void (*adjust_tsc_offset)(struct kvm_vcpu *vcpu, s64 adjustment, bool host);
  598. void (*set_tdp_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  599. void (*set_supported_cpuid)(u32 func, struct kvm_cpuid_entry2 *entry);
  600. bool (*has_wbinvd_exit)(void);
  601. void (*set_tsc_khz)(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale);
  602. void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
  603. u64 (*compute_tsc_offset)(struct kvm_vcpu *vcpu, u64 target_tsc);
  604. u64 (*read_l1_tsc)(struct kvm_vcpu *vcpu);
  605. void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2);
  606. int (*check_intercept)(struct kvm_vcpu *vcpu,
  607. struct x86_instruction_info *info,
  608. enum x86_intercept_stage stage);
  609. };
  610. struct kvm_arch_async_pf {
  611. u32 token;
  612. gfn_t gfn;
  613. unsigned long cr3;
  614. bool direct_map;
  615. };
  616. extern struct kvm_x86_ops *kvm_x86_ops;
  617. static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
  618. s64 adjustment)
  619. {
  620. kvm_x86_ops->adjust_tsc_offset(vcpu, adjustment, false);
  621. }
  622. static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
  623. {
  624. kvm_x86_ops->adjust_tsc_offset(vcpu, adjustment, true);
  625. }
  626. int kvm_mmu_module_init(void);
  627. void kvm_mmu_module_exit(void);
  628. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  629. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  630. int kvm_mmu_setup(struct kvm_vcpu *vcpu);
  631. void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
  632. u64 dirty_mask, u64 nx_mask, u64 x_mask);
  633. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  634. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
  635. void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
  636. struct kvm_memory_slot *slot,
  637. gfn_t gfn_offset, unsigned long mask);
  638. void kvm_mmu_zap_all(struct kvm *kvm);
  639. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm);
  640. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
  641. int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3);
  642. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  643. const void *val, int bytes);
  644. u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
  645. extern bool tdp_enabled;
  646. u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu);
  647. /* control of guest tsc rate supported? */
  648. extern bool kvm_has_tsc_control;
  649. /* minimum supported tsc_khz for guests */
  650. extern u32 kvm_min_guest_tsc_khz;
  651. /* maximum supported tsc_khz for guests */
  652. extern u32 kvm_max_guest_tsc_khz;
  653. enum emulation_result {
  654. EMULATE_DONE, /* no further processing */
  655. EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
  656. EMULATE_FAIL, /* can't emulate this instruction */
  657. };
  658. #define EMULTYPE_NO_DECODE (1 << 0)
  659. #define EMULTYPE_TRAP_UD (1 << 1)
  660. #define EMULTYPE_SKIP (1 << 2)
  661. #define EMULTYPE_RETRY (1 << 3)
  662. int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2,
  663. int emulation_type, void *insn, int insn_len);
  664. static inline int emulate_instruction(struct kvm_vcpu *vcpu,
  665. int emulation_type)
  666. {
  667. return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
  668. }
  669. void kvm_enable_efer_bits(u64);
  670. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
  671. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  672. struct x86_emulate_ctxt;
  673. int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port);
  674. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
  675. int kvm_emulate_halt(struct kvm_vcpu *vcpu);
  676. int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu);
  677. void kvm_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
  678. int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg);
  679. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
  680. int reason, bool has_error_code, u32 error_code);
  681. int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  682. int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
  683. int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  684. int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8);
  685. int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val);
  686. int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val);
  687. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);
  688. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  689. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
  690. int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr);
  691. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  692. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  693. unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu);
  694. void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
  695. bool kvm_rdpmc(struct kvm_vcpu *vcpu);
  696. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
  697. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
  698. void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr);
  699. void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
  700. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
  701. int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  702. gfn_t gfn, void *data, int offset, int len,
  703. u32 access);
  704. void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
  705. bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl);
  706. static inline int __kvm_irq_line_state(unsigned long *irq_state,
  707. int irq_source_id, int level)
  708. {
  709. /* Logical OR for level trig interrupt */
  710. if (level)
  711. __set_bit(irq_source_id, irq_state);
  712. else
  713. __clear_bit(irq_source_id, irq_state);
  714. return !!(*irq_state);
  715. }
  716. int kvm_pic_set_irq(struct kvm_pic *pic, int irq, int irq_source_id, int level);
  717. void kvm_pic_clear_all(struct kvm_pic *pic, int irq_source_id);
  718. void kvm_inject_nmi(struct kvm_vcpu *vcpu);
  719. int fx_init(struct kvm_vcpu *vcpu);
  720. void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu);
  721. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  722. const u8 *new, int bytes);
  723. int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn);
  724. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  725. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  726. int kvm_mmu_load(struct kvm_vcpu *vcpu);
  727. void kvm_mmu_unload(struct kvm_vcpu *vcpu);
  728. void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
  729. gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access);
  730. gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
  731. struct x86_exception *exception);
  732. gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
  733. struct x86_exception *exception);
  734. gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
  735. struct x86_exception *exception);
  736. gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
  737. struct x86_exception *exception);
  738. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
  739. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code,
  740. void *insn, int insn_len);
  741. void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
  742. void kvm_enable_tdp(void);
  743. void kvm_disable_tdp(void);
  744. int complete_pio(struct kvm_vcpu *vcpu);
  745. bool kvm_check_iopl(struct kvm_vcpu *vcpu);
  746. static inline gpa_t translate_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access)
  747. {
  748. return gpa;
  749. }
  750. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  751. {
  752. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  753. return (struct kvm_mmu_page *)page_private(page);
  754. }
  755. static inline u16 kvm_read_ldt(void)
  756. {
  757. u16 ldt;
  758. asm("sldt %0" : "=g"(ldt));
  759. return ldt;
  760. }
  761. static inline void kvm_load_ldt(u16 sel)
  762. {
  763. asm("lldt %0" : : "rm"(sel));
  764. }
  765. #ifdef CONFIG_X86_64
  766. static inline unsigned long read_msr(unsigned long msr)
  767. {
  768. u64 value;
  769. rdmsrl(msr, value);
  770. return value;
  771. }
  772. #endif
  773. static inline u32 get_rdx_init_val(void)
  774. {
  775. return 0x600; /* P6 family */
  776. }
  777. static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
  778. {
  779. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  780. }
  781. #define TSS_IOPB_BASE_OFFSET 0x66
  782. #define TSS_BASE_SIZE 0x68
  783. #define TSS_IOPB_SIZE (65536 / 8)
  784. #define TSS_REDIRECTION_SIZE (256 / 8)
  785. #define RMODE_TSS_SIZE \
  786. (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  787. enum {
  788. TASK_SWITCH_CALL = 0,
  789. TASK_SWITCH_IRET = 1,
  790. TASK_SWITCH_JMP = 2,
  791. TASK_SWITCH_GATE = 3,
  792. };
  793. #define HF_GIF_MASK (1 << 0)
  794. #define HF_HIF_MASK (1 << 1)
  795. #define HF_VINTR_MASK (1 << 2)
  796. #define HF_NMI_MASK (1 << 3)
  797. #define HF_IRET_MASK (1 << 4)
  798. #define HF_GUEST_MASK (1 << 5) /* VCPU is in guest-mode */
  799. /*
  800. * Hardware virtualization extension instructions may fault if a
  801. * reboot turns off virtualization while processes are running.
  802. * Trap the fault and ignore the instruction if that happens.
  803. */
  804. asmlinkage void kvm_spurious_fault(void);
  805. extern bool kvm_rebooting;
  806. #define ____kvm_handle_fault_on_reboot(insn, cleanup_insn) \
  807. "666: " insn "\n\t" \
  808. "668: \n\t" \
  809. ".pushsection .fixup, \"ax\" \n" \
  810. "667: \n\t" \
  811. cleanup_insn "\n\t" \
  812. "cmpb $0, kvm_rebooting \n\t" \
  813. "jne 668b \n\t" \
  814. __ASM_SIZE(push) " $666b \n\t" \
  815. "call kvm_spurious_fault \n\t" \
  816. ".popsection \n\t" \
  817. _ASM_EXTABLE(666b, 667b)
  818. #define __kvm_handle_fault_on_reboot(insn) \
  819. ____kvm_handle_fault_on_reboot(insn, "")
  820. #define KVM_ARCH_WANT_MMU_NOTIFIER
  821. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
  822. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end);
  823. int kvm_age_hva(struct kvm *kvm, unsigned long hva);
  824. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
  825. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
  826. int cpuid_maxphyaddr(struct kvm_vcpu *vcpu);
  827. int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu);
  828. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu);
  829. int kvm_cpu_get_interrupt(struct kvm_vcpu *v);
  830. void kvm_define_shared_msr(unsigned index, u32 msr);
  831. void kvm_set_shared_msr(unsigned index, u64 val, u64 mask);
  832. bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip);
  833. void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
  834. struct kvm_async_pf *work);
  835. void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
  836. struct kvm_async_pf *work);
  837. void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
  838. struct kvm_async_pf *work);
  839. bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu);
  840. extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);
  841. void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err);
  842. int kvm_is_in_guest(void);
  843. void kvm_pmu_init(struct kvm_vcpu *vcpu);
  844. void kvm_pmu_destroy(struct kvm_vcpu *vcpu);
  845. void kvm_pmu_reset(struct kvm_vcpu *vcpu);
  846. void kvm_pmu_cpuid_update(struct kvm_vcpu *vcpu);
  847. bool kvm_pmu_msr(struct kvm_vcpu *vcpu, u32 msr);
  848. int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
  849. int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  850. int kvm_pmu_read_pmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
  851. void kvm_handle_pmu_event(struct kvm_vcpu *vcpu);
  852. void kvm_deliver_pmi(struct kvm_vcpu *vcpu);
  853. #endif /* _ASM_X86_KVM_HOST_H */