kvm_host.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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 <linux/pvclock_gtod.h>
  23. #include <linux/clocksource.h>
  24. #include <linux/irqbypass.h>
  25. #include <linux/hyperv.h>
  26. #include <asm/apic.h>
  27. #include <asm/pvclock-abi.h>
  28. #include <asm/desc.h>
  29. #include <asm/mtrr.h>
  30. #include <asm/msr-index.h>
  31. #include <asm/asm.h>
  32. #include <asm/kvm_page_track.h>
  33. #include <asm/hyperv-tlfs.h>
  34. #define KVM_MAX_VCPUS 288
  35. #define KVM_SOFT_MAX_VCPUS 240
  36. #define KVM_MAX_VCPU_ID 1023
  37. #define KVM_USER_MEM_SLOTS 509
  38. /* memory slots that are not exposed to userspace */
  39. #define KVM_PRIVATE_MEM_SLOTS 3
  40. #define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
  41. #define KVM_HALT_POLL_NS_DEFAULT 200000
  42. #define KVM_IRQCHIP_NUM_PINS KVM_IOAPIC_NUM_PINS
  43. /* x86-specific vcpu->requests bit members */
  44. #define KVM_REQ_MIGRATE_TIMER KVM_ARCH_REQ(0)
  45. #define KVM_REQ_REPORT_TPR_ACCESS KVM_ARCH_REQ(1)
  46. #define KVM_REQ_TRIPLE_FAULT KVM_ARCH_REQ(2)
  47. #define KVM_REQ_MMU_SYNC KVM_ARCH_REQ(3)
  48. #define KVM_REQ_CLOCK_UPDATE KVM_ARCH_REQ(4)
  49. #define KVM_REQ_EVENT KVM_ARCH_REQ(6)
  50. #define KVM_REQ_APF_HALT KVM_ARCH_REQ(7)
  51. #define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(8)
  52. #define KVM_REQ_NMI KVM_ARCH_REQ(9)
  53. #define KVM_REQ_PMU KVM_ARCH_REQ(10)
  54. #define KVM_REQ_PMI KVM_ARCH_REQ(11)
  55. #define KVM_REQ_SMI KVM_ARCH_REQ(12)
  56. #define KVM_REQ_MASTERCLOCK_UPDATE KVM_ARCH_REQ(13)
  57. #define KVM_REQ_MCLOCK_INPROGRESS \
  58. KVM_ARCH_REQ_FLAGS(14, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  59. #define KVM_REQ_SCAN_IOAPIC \
  60. KVM_ARCH_REQ_FLAGS(15, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  61. #define KVM_REQ_GLOBAL_CLOCK_UPDATE KVM_ARCH_REQ(16)
  62. #define KVM_REQ_APIC_PAGE_RELOAD \
  63. KVM_ARCH_REQ_FLAGS(17, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  64. #define KVM_REQ_HV_CRASH KVM_ARCH_REQ(18)
  65. #define KVM_REQ_IOAPIC_EOI_EXIT KVM_ARCH_REQ(19)
  66. #define KVM_REQ_HV_RESET KVM_ARCH_REQ(20)
  67. #define KVM_REQ_HV_EXIT KVM_ARCH_REQ(21)
  68. #define KVM_REQ_HV_STIMER KVM_ARCH_REQ(22)
  69. #define KVM_REQ_LOAD_EOI_EXITMAP KVM_ARCH_REQ(23)
  70. #define CR0_RESERVED_BITS \
  71. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  72. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  73. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  74. #define CR3_PCID_INVD BIT_64(63)
  75. #define CR4_RESERVED_BITS \
  76. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  77. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  78. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
  79. | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
  80. | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \
  81. | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP))
  82. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  83. #define INVALID_PAGE (~(hpa_t)0)
  84. #define VALID_PAGE(x) ((x) != INVALID_PAGE)
  85. #define UNMAPPED_GVA (~(gpa_t)0)
  86. /* KVM Hugepage definitions for x86 */
  87. #define KVM_NR_PAGE_SIZES 3
  88. #define KVM_HPAGE_GFN_SHIFT(x) (((x) - 1) * 9)
  89. #define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
  90. #define KVM_HPAGE_SIZE(x) (1UL << KVM_HPAGE_SHIFT(x))
  91. #define KVM_HPAGE_MASK(x) (~(KVM_HPAGE_SIZE(x) - 1))
  92. #define KVM_PAGES_PER_HPAGE(x) (KVM_HPAGE_SIZE(x) / PAGE_SIZE)
  93. static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
  94. {
  95. /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
  96. return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
  97. (base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
  98. }
  99. #define KVM_PERMILLE_MMU_PAGES 20
  100. #define KVM_MIN_ALLOC_MMU_PAGES 64
  101. #define KVM_MMU_HASH_SHIFT 12
  102. #define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
  103. #define KVM_MIN_FREE_MMU_PAGES 5
  104. #define KVM_REFILL_PAGES 25
  105. #define KVM_MAX_CPUID_ENTRIES 80
  106. #define KVM_NR_FIXED_MTRR_REGION 88
  107. #define KVM_NR_VAR_MTRR 8
  108. #define ASYNC_PF_PER_VCPU 64
  109. enum kvm_reg {
  110. VCPU_REGS_RAX = 0,
  111. VCPU_REGS_RCX = 1,
  112. VCPU_REGS_RDX = 2,
  113. VCPU_REGS_RBX = 3,
  114. VCPU_REGS_RSP = 4,
  115. VCPU_REGS_RBP = 5,
  116. VCPU_REGS_RSI = 6,
  117. VCPU_REGS_RDI = 7,
  118. #ifdef CONFIG_X86_64
  119. VCPU_REGS_R8 = 8,
  120. VCPU_REGS_R9 = 9,
  121. VCPU_REGS_R10 = 10,
  122. VCPU_REGS_R11 = 11,
  123. VCPU_REGS_R12 = 12,
  124. VCPU_REGS_R13 = 13,
  125. VCPU_REGS_R14 = 14,
  126. VCPU_REGS_R15 = 15,
  127. #endif
  128. VCPU_REGS_RIP,
  129. NR_VCPU_REGS
  130. };
  131. enum kvm_reg_ex {
  132. VCPU_EXREG_PDPTR = NR_VCPU_REGS,
  133. VCPU_EXREG_CR3,
  134. VCPU_EXREG_RFLAGS,
  135. VCPU_EXREG_SEGMENTS,
  136. };
  137. enum {
  138. VCPU_SREG_ES,
  139. VCPU_SREG_CS,
  140. VCPU_SREG_SS,
  141. VCPU_SREG_DS,
  142. VCPU_SREG_FS,
  143. VCPU_SREG_GS,
  144. VCPU_SREG_TR,
  145. VCPU_SREG_LDTR,
  146. };
  147. #include <asm/kvm_emulate.h>
  148. #define KVM_NR_MEM_OBJS 40
  149. #define KVM_NR_DB_REGS 4
  150. #define DR6_BD (1 << 13)
  151. #define DR6_BS (1 << 14)
  152. #define DR6_RTM (1 << 16)
  153. #define DR6_FIXED_1 0xfffe0ff0
  154. #define DR6_INIT 0xffff0ff0
  155. #define DR6_VOLATILE 0x0001e00f
  156. #define DR7_BP_EN_MASK 0x000000ff
  157. #define DR7_GE (1 << 9)
  158. #define DR7_GD (1 << 13)
  159. #define DR7_FIXED_1 0x00000400
  160. #define DR7_VOLATILE 0xffff2bff
  161. #define PFERR_PRESENT_BIT 0
  162. #define PFERR_WRITE_BIT 1
  163. #define PFERR_USER_BIT 2
  164. #define PFERR_RSVD_BIT 3
  165. #define PFERR_FETCH_BIT 4
  166. #define PFERR_PK_BIT 5
  167. #define PFERR_GUEST_FINAL_BIT 32
  168. #define PFERR_GUEST_PAGE_BIT 33
  169. #define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT)
  170. #define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT)
  171. #define PFERR_USER_MASK (1U << PFERR_USER_BIT)
  172. #define PFERR_RSVD_MASK (1U << PFERR_RSVD_BIT)
  173. #define PFERR_FETCH_MASK (1U << PFERR_FETCH_BIT)
  174. #define PFERR_PK_MASK (1U << PFERR_PK_BIT)
  175. #define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT)
  176. #define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT)
  177. #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \
  178. PFERR_WRITE_MASK | \
  179. PFERR_PRESENT_MASK)
  180. /*
  181. * The mask used to denote special SPTEs, which can be either MMIO SPTEs or
  182. * Access Tracking SPTEs. We use bit 62 instead of bit 63 to avoid conflicting
  183. * with the SVE bit in EPT PTEs.
  184. */
  185. #define SPTE_SPECIAL_MASK (1ULL << 62)
  186. /* apic attention bits */
  187. #define KVM_APIC_CHECK_VAPIC 0
  188. /*
  189. * The following bit is set with PV-EOI, unset on EOI.
  190. * We detect PV-EOI changes by guest by comparing
  191. * this bit with PV-EOI in guest memory.
  192. * See the implementation in apic_update_pv_eoi.
  193. */
  194. #define KVM_APIC_PV_EOI_PENDING 1
  195. struct kvm_kernel_irq_routing_entry;
  196. /*
  197. * We don't want allocation failures within the mmu code, so we preallocate
  198. * enough memory for a single page fault in a cache.
  199. */
  200. struct kvm_mmu_memory_cache {
  201. int nobjs;
  202. void *objects[KVM_NR_MEM_OBJS];
  203. };
  204. /*
  205. * the pages used as guest page table on soft mmu are tracked by
  206. * kvm_memory_slot.arch.gfn_track which is 16 bits, so the role bits used
  207. * by indirect shadow page can not be more than 15 bits.
  208. *
  209. * Currently, we used 14 bits that are @level, @cr4_pae, @quadrant, @access,
  210. * @nxe, @cr0_wp, @smep_andnot_wp and @smap_andnot_wp.
  211. */
  212. union kvm_mmu_page_role {
  213. unsigned word;
  214. struct {
  215. unsigned level:4;
  216. unsigned cr4_pae:1;
  217. unsigned quadrant:2;
  218. unsigned direct:1;
  219. unsigned access:3;
  220. unsigned invalid:1;
  221. unsigned nxe:1;
  222. unsigned cr0_wp:1;
  223. unsigned smep_andnot_wp:1;
  224. unsigned smap_andnot_wp:1;
  225. unsigned ad_disabled:1;
  226. unsigned :7;
  227. /*
  228. * This is left at the top of the word so that
  229. * kvm_memslots_for_spte_role can extract it with a
  230. * simple shift. While there is room, give it a whole
  231. * byte so it is also faster to load it from memory.
  232. */
  233. unsigned smm:8;
  234. };
  235. };
  236. struct kvm_rmap_head {
  237. unsigned long val;
  238. };
  239. struct kvm_mmu_page {
  240. struct list_head link;
  241. struct hlist_node hash_link;
  242. /*
  243. * The following two entries are used to key the shadow page in the
  244. * hash table.
  245. */
  246. gfn_t gfn;
  247. union kvm_mmu_page_role role;
  248. u64 *spt;
  249. /* hold the gfn of each spte inside spt */
  250. gfn_t *gfns;
  251. bool unsync;
  252. int root_count; /* Currently serving as active root */
  253. unsigned int unsync_children;
  254. struct kvm_rmap_head parent_ptes; /* rmap pointers to parent sptes */
  255. /* The page is obsolete if mmu_valid_gen != kvm->arch.mmu_valid_gen. */
  256. unsigned long mmu_valid_gen;
  257. DECLARE_BITMAP(unsync_child_bitmap, 512);
  258. #ifdef CONFIG_X86_32
  259. /*
  260. * Used out of the mmu-lock to avoid reading spte values while an
  261. * update is in progress; see the comments in __get_spte_lockless().
  262. */
  263. int clear_spte_count;
  264. #endif
  265. /* Number of writes since the last time traversal visited this page. */
  266. atomic_t write_flooding_count;
  267. };
  268. struct kvm_pio_request {
  269. unsigned long count;
  270. int in;
  271. int port;
  272. int size;
  273. };
  274. #define PT64_ROOT_MAX_LEVEL 5
  275. struct rsvd_bits_validate {
  276. u64 rsvd_bits_mask[2][PT64_ROOT_MAX_LEVEL];
  277. u64 bad_mt_xwr;
  278. };
  279. /*
  280. * x86 supports 4 paging modes (5-level 64-bit, 4-level 64-bit, 3-level 32-bit,
  281. * and 2-level 32-bit). The kvm_mmu structure abstracts the details of the
  282. * current mmu mode.
  283. */
  284. struct kvm_mmu {
  285. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long root);
  286. unsigned long (*get_cr3)(struct kvm_vcpu *vcpu);
  287. u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index);
  288. int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err,
  289. bool prefault);
  290. void (*inject_page_fault)(struct kvm_vcpu *vcpu,
  291. struct x86_exception *fault);
  292. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva, u32 access,
  293. struct x86_exception *exception);
  294. gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
  295. struct x86_exception *exception);
  296. int (*sync_page)(struct kvm_vcpu *vcpu,
  297. struct kvm_mmu_page *sp);
  298. void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva);
  299. void (*update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  300. u64 *spte, const void *pte);
  301. hpa_t root_hpa;
  302. union kvm_mmu_page_role base_role;
  303. u8 root_level;
  304. u8 shadow_root_level;
  305. u8 ept_ad;
  306. bool direct_map;
  307. /*
  308. * Bitmap; bit set = permission fault
  309. * Byte index: page fault error code [4:1]
  310. * Bit index: pte permissions in ACC_* format
  311. */
  312. u8 permissions[16];
  313. /*
  314. * The pkru_mask indicates if protection key checks are needed. It
  315. * consists of 16 domains indexed by page fault error code bits [4:1],
  316. * with PFEC.RSVD replaced by ACC_USER_MASK from the page tables.
  317. * Each domain has 2 bits which are ANDed with AD and WD from PKRU.
  318. */
  319. u32 pkru_mask;
  320. u64 *pae_root;
  321. u64 *lm_root;
  322. /*
  323. * check zero bits on shadow page table entries, these
  324. * bits include not only hardware reserved bits but also
  325. * the bits spte never used.
  326. */
  327. struct rsvd_bits_validate shadow_zero_check;
  328. struct rsvd_bits_validate guest_rsvd_check;
  329. /* Can have large pages at levels 2..last_nonleaf_level-1. */
  330. u8 last_nonleaf_level;
  331. bool nx;
  332. u64 pdptrs[4]; /* pae */
  333. };
  334. enum pmc_type {
  335. KVM_PMC_GP = 0,
  336. KVM_PMC_FIXED,
  337. };
  338. struct kvm_pmc {
  339. enum pmc_type type;
  340. u8 idx;
  341. u64 counter;
  342. u64 eventsel;
  343. struct perf_event *perf_event;
  344. struct kvm_vcpu *vcpu;
  345. };
  346. struct kvm_pmu {
  347. unsigned nr_arch_gp_counters;
  348. unsigned nr_arch_fixed_counters;
  349. unsigned available_event_types;
  350. u64 fixed_ctr_ctrl;
  351. u64 global_ctrl;
  352. u64 global_status;
  353. u64 global_ovf_ctrl;
  354. u64 counter_bitmask[2];
  355. u64 global_ctrl_mask;
  356. u64 reserved_bits;
  357. u8 version;
  358. struct kvm_pmc gp_counters[INTEL_PMC_MAX_GENERIC];
  359. struct kvm_pmc fixed_counters[INTEL_PMC_MAX_FIXED];
  360. struct irq_work irq_work;
  361. u64 reprogram_pmi;
  362. };
  363. struct kvm_pmu_ops;
  364. enum {
  365. KVM_DEBUGREG_BP_ENABLED = 1,
  366. KVM_DEBUGREG_WONT_EXIT = 2,
  367. KVM_DEBUGREG_RELOAD = 4,
  368. };
  369. struct kvm_mtrr_range {
  370. u64 base;
  371. u64 mask;
  372. struct list_head node;
  373. };
  374. struct kvm_mtrr {
  375. struct kvm_mtrr_range var_ranges[KVM_NR_VAR_MTRR];
  376. mtrr_type fixed_ranges[KVM_NR_FIXED_MTRR_REGION];
  377. u64 deftype;
  378. struct list_head head;
  379. };
  380. /* Hyper-V SynIC timer */
  381. struct kvm_vcpu_hv_stimer {
  382. struct hrtimer timer;
  383. int index;
  384. u64 config;
  385. u64 count;
  386. u64 exp_time;
  387. struct hv_message msg;
  388. bool msg_pending;
  389. };
  390. /* Hyper-V synthetic interrupt controller (SynIC)*/
  391. struct kvm_vcpu_hv_synic {
  392. u64 version;
  393. u64 control;
  394. u64 msg_page;
  395. u64 evt_page;
  396. atomic64_t sint[HV_SYNIC_SINT_COUNT];
  397. atomic_t sint_to_gsi[HV_SYNIC_SINT_COUNT];
  398. DECLARE_BITMAP(auto_eoi_bitmap, 256);
  399. DECLARE_BITMAP(vec_bitmap, 256);
  400. bool active;
  401. bool dont_zero_synic_pages;
  402. };
  403. /* Hyper-V per vcpu emulation context */
  404. struct kvm_vcpu_hv {
  405. u32 vp_index;
  406. u64 hv_vapic;
  407. s64 runtime_offset;
  408. struct kvm_vcpu_hv_synic synic;
  409. struct kvm_hyperv_exit exit;
  410. struct kvm_vcpu_hv_stimer stimer[HV_SYNIC_STIMER_COUNT];
  411. DECLARE_BITMAP(stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
  412. };
  413. struct kvm_vcpu_arch {
  414. /*
  415. * rip and regs accesses must go through
  416. * kvm_{register,rip}_{read,write} functions.
  417. */
  418. unsigned long regs[NR_VCPU_REGS];
  419. u32 regs_avail;
  420. u32 regs_dirty;
  421. unsigned long cr0;
  422. unsigned long cr0_guest_owned_bits;
  423. unsigned long cr2;
  424. unsigned long cr3;
  425. unsigned long cr4;
  426. unsigned long cr4_guest_owned_bits;
  427. unsigned long cr8;
  428. u32 pkru;
  429. u32 hflags;
  430. u64 efer;
  431. u64 apic_base;
  432. struct kvm_lapic *apic; /* kernel irqchip context */
  433. bool apicv_active;
  434. bool load_eoi_exitmap_pending;
  435. DECLARE_BITMAP(ioapic_handled_vectors, 256);
  436. unsigned long apic_attention;
  437. int32_t apic_arb_prio;
  438. int mp_state;
  439. u64 ia32_misc_enable_msr;
  440. u64 smbase;
  441. u64 smi_count;
  442. bool tpr_access_reporting;
  443. u64 ia32_xss;
  444. u64 microcode_version;
  445. /*
  446. * Paging state of the vcpu
  447. *
  448. * If the vcpu runs in guest mode with two level paging this still saves
  449. * the paging mode of the l1 guest. This context is always used to
  450. * handle faults.
  451. */
  452. struct kvm_mmu mmu;
  453. /*
  454. * Paging state of an L2 guest (used for nested npt)
  455. *
  456. * This context will save all necessary information to walk page tables
  457. * of the an L2 guest. This context is only initialized for page table
  458. * walking and not for faulting since we never handle l2 page faults on
  459. * the host.
  460. */
  461. struct kvm_mmu nested_mmu;
  462. /*
  463. * Pointer to the mmu context currently used for
  464. * gva_to_gpa translations.
  465. */
  466. struct kvm_mmu *walk_mmu;
  467. struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;
  468. struct kvm_mmu_memory_cache mmu_page_cache;
  469. struct kvm_mmu_memory_cache mmu_page_header_cache;
  470. /*
  471. * QEMU userspace and the guest each have their own FPU state.
  472. * In vcpu_run, we switch between the user and guest FPU contexts.
  473. * While running a VCPU, the VCPU thread will have the guest FPU
  474. * context.
  475. *
  476. * Note that while the PKRU state lives inside the fpu registers,
  477. * it is switched out separately at VMENTER and VMEXIT time. The
  478. * "guest_fpu" state here contains the guest FPU context, with the
  479. * host PRKU bits.
  480. */
  481. struct fpu user_fpu;
  482. struct fpu guest_fpu;
  483. u64 xcr0;
  484. u64 guest_supported_xcr0;
  485. u32 guest_xstate_size;
  486. struct kvm_pio_request pio;
  487. void *pio_data;
  488. u8 event_exit_inst_len;
  489. struct kvm_queued_exception {
  490. bool pending;
  491. bool injected;
  492. bool has_error_code;
  493. u8 nr;
  494. u32 error_code;
  495. u8 nested_apf;
  496. } exception;
  497. struct kvm_queued_interrupt {
  498. bool injected;
  499. bool soft;
  500. u8 nr;
  501. } interrupt;
  502. int halt_request; /* real mode on Intel only */
  503. int cpuid_nent;
  504. struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
  505. int maxphyaddr;
  506. /* emulate context */
  507. struct x86_emulate_ctxt emulate_ctxt;
  508. bool emulate_regs_need_sync_to_vcpu;
  509. bool emulate_regs_need_sync_from_vcpu;
  510. int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
  511. gpa_t time;
  512. struct pvclock_vcpu_time_info hv_clock;
  513. unsigned int hw_tsc_khz;
  514. struct gfn_to_hva_cache pv_time;
  515. bool pv_time_enabled;
  516. /* set guest stopped flag in pvclock flags field */
  517. bool pvclock_set_guest_stopped_request;
  518. struct {
  519. u64 msr_val;
  520. u64 last_steal;
  521. struct gfn_to_hva_cache stime;
  522. struct kvm_steal_time steal;
  523. } st;
  524. u64 tsc_offset;
  525. u64 last_guest_tsc;
  526. u64 last_host_tsc;
  527. u64 tsc_offset_adjustment;
  528. u64 this_tsc_nsec;
  529. u64 this_tsc_write;
  530. u64 this_tsc_generation;
  531. bool tsc_catchup;
  532. bool tsc_always_catchup;
  533. s8 virtual_tsc_shift;
  534. u32 virtual_tsc_mult;
  535. u32 virtual_tsc_khz;
  536. s64 ia32_tsc_adjust_msr;
  537. u64 tsc_scaling_ratio;
  538. atomic_t nmi_queued; /* unprocessed asynchronous NMIs */
  539. unsigned nmi_pending; /* NMI queued after currently running handler */
  540. bool nmi_injected; /* Trying to inject an NMI this entry */
  541. bool smi_pending; /* SMI queued after currently running handler */
  542. struct kvm_mtrr mtrr_state;
  543. u64 pat;
  544. unsigned switch_db_regs;
  545. unsigned long db[KVM_NR_DB_REGS];
  546. unsigned long dr6;
  547. unsigned long dr7;
  548. unsigned long eff_db[KVM_NR_DB_REGS];
  549. unsigned long guest_debug_dr7;
  550. u64 msr_platform_info;
  551. u64 msr_misc_features_enables;
  552. u64 mcg_cap;
  553. u64 mcg_status;
  554. u64 mcg_ctl;
  555. u64 mcg_ext_ctl;
  556. u64 *mce_banks;
  557. /* Cache MMIO info */
  558. u64 mmio_gva;
  559. unsigned access;
  560. gfn_t mmio_gfn;
  561. u64 mmio_gen;
  562. struct kvm_pmu pmu;
  563. /* used for guest single stepping over the given code position */
  564. unsigned long singlestep_rip;
  565. struct kvm_vcpu_hv hyperv;
  566. cpumask_var_t wbinvd_dirty_mask;
  567. unsigned long last_retry_eip;
  568. unsigned long last_retry_addr;
  569. struct {
  570. bool halted;
  571. gfn_t gfns[roundup_pow_of_two(ASYNC_PF_PER_VCPU)];
  572. struct gfn_to_hva_cache data;
  573. u64 msr_val;
  574. u32 id;
  575. bool send_user_only;
  576. u32 host_apf_reason;
  577. unsigned long nested_apf_token;
  578. bool delivery_as_pf_vmexit;
  579. } apf;
  580. /* OSVW MSRs (AMD only) */
  581. struct {
  582. u64 length;
  583. u64 status;
  584. } osvw;
  585. struct {
  586. u64 msr_val;
  587. struct gfn_to_hva_cache data;
  588. } pv_eoi;
  589. /*
  590. * Indicate whether the access faults on its page table in guest
  591. * which is set when fix page fault and used to detect unhandeable
  592. * instruction.
  593. */
  594. bool write_fault_to_shadow_pgtable;
  595. /* set at EPT violation at this point */
  596. unsigned long exit_qualification;
  597. /* pv related host specific info */
  598. struct {
  599. bool pv_unhalted;
  600. } pv;
  601. int pending_ioapic_eoi;
  602. int pending_external_vector;
  603. /* GPA available */
  604. bool gpa_available;
  605. gpa_t gpa_val;
  606. /* be preempted when it's in kernel-mode(cpl=0) */
  607. bool preempted_in_kernel;
  608. };
  609. struct kvm_lpage_info {
  610. int disallow_lpage;
  611. };
  612. struct kvm_arch_memory_slot {
  613. struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
  614. struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
  615. unsigned short *gfn_track[KVM_PAGE_TRACK_MAX];
  616. };
  617. /*
  618. * We use as the mode the number of bits allocated in the LDR for the
  619. * logical processor ID. It happens that these are all powers of two.
  620. * This makes it is very easy to detect cases where the APICs are
  621. * configured for multiple modes; in that case, we cannot use the map and
  622. * hence cannot use kvm_irq_delivery_to_apic_fast either.
  623. */
  624. #define KVM_APIC_MODE_XAPIC_CLUSTER 4
  625. #define KVM_APIC_MODE_XAPIC_FLAT 8
  626. #define KVM_APIC_MODE_X2APIC 16
  627. struct kvm_apic_map {
  628. struct rcu_head rcu;
  629. u8 mode;
  630. u32 max_apic_id;
  631. union {
  632. struct kvm_lapic *xapic_flat_map[8];
  633. struct kvm_lapic *xapic_cluster_map[16][4];
  634. };
  635. struct kvm_lapic *phys_map[];
  636. };
  637. /* Hyper-V emulation context */
  638. struct kvm_hv {
  639. struct mutex hv_lock;
  640. u64 hv_guest_os_id;
  641. u64 hv_hypercall;
  642. u64 hv_tsc_page;
  643. /* Hyper-v based guest crash (NT kernel bugcheck) parameters */
  644. u64 hv_crash_param[HV_X64_MSR_CRASH_PARAMS];
  645. u64 hv_crash_ctl;
  646. HV_REFERENCE_TSC_PAGE tsc_ref;
  647. struct idr conn_to_evt;
  648. u64 hv_reenlightenment_control;
  649. u64 hv_tsc_emulation_control;
  650. u64 hv_tsc_emulation_status;
  651. };
  652. enum kvm_irqchip_mode {
  653. KVM_IRQCHIP_NONE,
  654. KVM_IRQCHIP_KERNEL, /* created with KVM_CREATE_IRQCHIP */
  655. KVM_IRQCHIP_SPLIT, /* created with KVM_CAP_SPLIT_IRQCHIP */
  656. };
  657. struct kvm_arch {
  658. unsigned int n_used_mmu_pages;
  659. unsigned int n_requested_mmu_pages;
  660. unsigned int n_max_mmu_pages;
  661. unsigned int indirect_shadow_pages;
  662. unsigned long mmu_valid_gen;
  663. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  664. /*
  665. * Hash table of struct kvm_mmu_page.
  666. */
  667. struct list_head active_mmu_pages;
  668. struct list_head zapped_obsolete_pages;
  669. struct kvm_page_track_notifier_node mmu_sp_tracker;
  670. struct kvm_page_track_notifier_head track_notifier_head;
  671. struct list_head assigned_dev_head;
  672. struct iommu_domain *iommu_domain;
  673. bool iommu_noncoherent;
  674. #define __KVM_HAVE_ARCH_NONCOHERENT_DMA
  675. atomic_t noncoherent_dma_count;
  676. #define __KVM_HAVE_ARCH_ASSIGNED_DEVICE
  677. atomic_t assigned_device_count;
  678. struct kvm_pic *vpic;
  679. struct kvm_ioapic *vioapic;
  680. struct kvm_pit *vpit;
  681. atomic_t vapics_in_nmi_mode;
  682. struct mutex apic_map_lock;
  683. struct kvm_apic_map *apic_map;
  684. bool apic_access_page_done;
  685. gpa_t wall_clock;
  686. bool mwait_in_guest;
  687. bool hlt_in_guest;
  688. bool pause_in_guest;
  689. unsigned long irq_sources_bitmap;
  690. s64 kvmclock_offset;
  691. raw_spinlock_t tsc_write_lock;
  692. u64 last_tsc_nsec;
  693. u64 last_tsc_write;
  694. u32 last_tsc_khz;
  695. u64 cur_tsc_nsec;
  696. u64 cur_tsc_write;
  697. u64 cur_tsc_offset;
  698. u64 cur_tsc_generation;
  699. int nr_vcpus_matched_tsc;
  700. spinlock_t pvclock_gtod_sync_lock;
  701. bool use_master_clock;
  702. u64 master_kernel_ns;
  703. u64 master_cycle_now;
  704. struct delayed_work kvmclock_update_work;
  705. struct delayed_work kvmclock_sync_work;
  706. struct kvm_xen_hvm_config xen_hvm_config;
  707. /* reads protected by irq_srcu, writes by irq_lock */
  708. struct hlist_head mask_notifier_list;
  709. struct kvm_hv hyperv;
  710. #ifdef CONFIG_KVM_MMU_AUDIT
  711. int audit_point;
  712. #endif
  713. bool backwards_tsc_observed;
  714. bool boot_vcpu_runs_old_kvmclock;
  715. u32 bsp_vcpu_id;
  716. u64 disabled_quirks;
  717. enum kvm_irqchip_mode irqchip_mode;
  718. u8 nr_reserved_ioapic_pins;
  719. bool disabled_lapic_found;
  720. bool x2apic_format;
  721. bool x2apic_broadcast_quirk_disabled;
  722. };
  723. struct kvm_vm_stat {
  724. ulong mmu_shadow_zapped;
  725. ulong mmu_pte_write;
  726. ulong mmu_pte_updated;
  727. ulong mmu_pde_zapped;
  728. ulong mmu_flooded;
  729. ulong mmu_recycled;
  730. ulong mmu_cache_miss;
  731. ulong mmu_unsync;
  732. ulong remote_tlb_flush;
  733. ulong lpages;
  734. ulong max_mmu_page_hash_collisions;
  735. };
  736. struct kvm_vcpu_stat {
  737. u64 pf_fixed;
  738. u64 pf_guest;
  739. u64 tlb_flush;
  740. u64 invlpg;
  741. u64 exits;
  742. u64 io_exits;
  743. u64 mmio_exits;
  744. u64 signal_exits;
  745. u64 irq_window_exits;
  746. u64 nmi_window_exits;
  747. u64 halt_exits;
  748. u64 halt_successful_poll;
  749. u64 halt_attempted_poll;
  750. u64 halt_poll_invalid;
  751. u64 halt_wakeup;
  752. u64 request_irq_exits;
  753. u64 irq_exits;
  754. u64 host_state_reload;
  755. u64 fpu_reload;
  756. u64 insn_emulation;
  757. u64 insn_emulation_fail;
  758. u64 hypercalls;
  759. u64 irq_injections;
  760. u64 nmi_injections;
  761. u64 req_event;
  762. };
  763. struct x86_instruction_info;
  764. struct msr_data {
  765. bool host_initiated;
  766. u32 index;
  767. u64 data;
  768. };
  769. struct kvm_lapic_irq {
  770. u32 vector;
  771. u16 delivery_mode;
  772. u16 dest_mode;
  773. bool level;
  774. u16 trig_mode;
  775. u32 shorthand;
  776. u32 dest_id;
  777. bool msi_redir_hint;
  778. };
  779. struct kvm_x86_ops {
  780. int (*cpu_has_kvm_support)(void); /* __init */
  781. int (*disabled_by_bios)(void); /* __init */
  782. int (*hardware_enable)(void);
  783. void (*hardware_disable)(void);
  784. void (*check_processor_compatibility)(void *rtn);
  785. int (*hardware_setup)(void); /* __init */
  786. void (*hardware_unsetup)(void); /* __exit */
  787. bool (*cpu_has_accelerated_tpr)(void);
  788. bool (*cpu_has_high_real_mode_segbase)(void);
  789. void (*cpuid_update)(struct kvm_vcpu *vcpu);
  790. struct kvm *(*vm_alloc)(void);
  791. void (*vm_free)(struct kvm *);
  792. int (*vm_init)(struct kvm *kvm);
  793. void (*vm_destroy)(struct kvm *kvm);
  794. /* Create, but do not attach this VCPU */
  795. struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
  796. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  797. void (*vcpu_reset)(struct kvm_vcpu *vcpu, bool init_event);
  798. void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
  799. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  800. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  801. void (*update_bp_intercept)(struct kvm_vcpu *vcpu);
  802. int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
  803. int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
  804. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  805. void (*get_segment)(struct kvm_vcpu *vcpu,
  806. struct kvm_segment *var, int seg);
  807. int (*get_cpl)(struct kvm_vcpu *vcpu);
  808. void (*set_segment)(struct kvm_vcpu *vcpu,
  809. struct kvm_segment *var, int seg);
  810. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  811. void (*decache_cr0_guest_bits)(struct kvm_vcpu *vcpu);
  812. void (*decache_cr3)(struct kvm_vcpu *vcpu);
  813. void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  814. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  815. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  816. int (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  817. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  818. void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  819. void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  820. void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  821. void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  822. u64 (*get_dr6)(struct kvm_vcpu *vcpu);
  823. void (*set_dr6)(struct kvm_vcpu *vcpu, unsigned long value);
  824. void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);
  825. void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
  826. void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
  827. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  828. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  829. void (*tlb_flush)(struct kvm_vcpu *vcpu, bool invalidate_gpa);
  830. void (*run)(struct kvm_vcpu *vcpu);
  831. int (*handle_exit)(struct kvm_vcpu *vcpu);
  832. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  833. void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
  834. u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu);
  835. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  836. unsigned char *hypercall_addr);
  837. void (*set_irq)(struct kvm_vcpu *vcpu);
  838. void (*set_nmi)(struct kvm_vcpu *vcpu);
  839. void (*queue_exception)(struct kvm_vcpu *vcpu);
  840. void (*cancel_injection)(struct kvm_vcpu *vcpu);
  841. int (*interrupt_allowed)(struct kvm_vcpu *vcpu);
  842. int (*nmi_allowed)(struct kvm_vcpu *vcpu);
  843. bool (*get_nmi_mask)(struct kvm_vcpu *vcpu);
  844. void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked);
  845. void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
  846. void (*enable_irq_window)(struct kvm_vcpu *vcpu);
  847. void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
  848. bool (*get_enable_apicv)(struct kvm_vcpu *vcpu);
  849. void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu);
  850. void (*hwapic_irr_update)(struct kvm_vcpu *vcpu, int max_irr);
  851. void (*hwapic_isr_update)(struct kvm_vcpu *vcpu, int isr);
  852. void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
  853. void (*set_virtual_x2apic_mode)(struct kvm_vcpu *vcpu, bool set);
  854. void (*set_apic_access_page_addr)(struct kvm_vcpu *vcpu, hpa_t hpa);
  855. void (*deliver_posted_interrupt)(struct kvm_vcpu *vcpu, int vector);
  856. int (*sync_pir_to_irr)(struct kvm_vcpu *vcpu);
  857. int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
  858. int (*set_identity_map_addr)(struct kvm *kvm, u64 ident_addr);
  859. int (*get_tdp_level)(struct kvm_vcpu *vcpu);
  860. u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
  861. int (*get_lpage_level)(void);
  862. bool (*rdtscp_supported)(void);
  863. bool (*invpcid_supported)(void);
  864. void (*set_tdp_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  865. void (*set_supported_cpuid)(u32 func, struct kvm_cpuid_entry2 *entry);
  866. bool (*has_wbinvd_exit)(void);
  867. void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
  868. void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2);
  869. int (*check_intercept)(struct kvm_vcpu *vcpu,
  870. struct x86_instruction_info *info,
  871. enum x86_intercept_stage stage);
  872. void (*handle_external_intr)(struct kvm_vcpu *vcpu);
  873. bool (*mpx_supported)(void);
  874. bool (*xsaves_supported)(void);
  875. bool (*umip_emulated)(void);
  876. int (*check_nested_events)(struct kvm_vcpu *vcpu, bool external_intr);
  877. void (*sched_in)(struct kvm_vcpu *kvm, int cpu);
  878. /*
  879. * Arch-specific dirty logging hooks. These hooks are only supposed to
  880. * be valid if the specific arch has hardware-accelerated dirty logging
  881. * mechanism. Currently only for PML on VMX.
  882. *
  883. * - slot_enable_log_dirty:
  884. * called when enabling log dirty mode for the slot.
  885. * - slot_disable_log_dirty:
  886. * called when disabling log dirty mode for the slot.
  887. * also called when slot is created with log dirty disabled.
  888. * - flush_log_dirty:
  889. * called before reporting dirty_bitmap to userspace.
  890. * - enable_log_dirty_pt_masked:
  891. * called when reenabling log dirty for the GFNs in the mask after
  892. * corresponding bits are cleared in slot->dirty_bitmap.
  893. */
  894. void (*slot_enable_log_dirty)(struct kvm *kvm,
  895. struct kvm_memory_slot *slot);
  896. void (*slot_disable_log_dirty)(struct kvm *kvm,
  897. struct kvm_memory_slot *slot);
  898. void (*flush_log_dirty)(struct kvm *kvm);
  899. void (*enable_log_dirty_pt_masked)(struct kvm *kvm,
  900. struct kvm_memory_slot *slot,
  901. gfn_t offset, unsigned long mask);
  902. int (*write_log_dirty)(struct kvm_vcpu *vcpu);
  903. /* pmu operations of sub-arch */
  904. const struct kvm_pmu_ops *pmu_ops;
  905. /*
  906. * Architecture specific hooks for vCPU blocking due to
  907. * HLT instruction.
  908. * Returns for .pre_block():
  909. * - 0 means continue to block the vCPU.
  910. * - 1 means we cannot block the vCPU since some event
  911. * happens during this period, such as, 'ON' bit in
  912. * posted-interrupts descriptor is set.
  913. */
  914. int (*pre_block)(struct kvm_vcpu *vcpu);
  915. void (*post_block)(struct kvm_vcpu *vcpu);
  916. void (*vcpu_blocking)(struct kvm_vcpu *vcpu);
  917. void (*vcpu_unblocking)(struct kvm_vcpu *vcpu);
  918. int (*update_pi_irte)(struct kvm *kvm, unsigned int host_irq,
  919. uint32_t guest_irq, bool set);
  920. void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);
  921. int (*set_hv_timer)(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc);
  922. void (*cancel_hv_timer)(struct kvm_vcpu *vcpu);
  923. void (*setup_mce)(struct kvm_vcpu *vcpu);
  924. int (*smi_allowed)(struct kvm_vcpu *vcpu);
  925. int (*pre_enter_smm)(struct kvm_vcpu *vcpu, char *smstate);
  926. int (*pre_leave_smm)(struct kvm_vcpu *vcpu, u64 smbase);
  927. int (*enable_smi_window)(struct kvm_vcpu *vcpu);
  928. int (*mem_enc_op)(struct kvm *kvm, void __user *argp);
  929. int (*mem_enc_reg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
  930. int (*mem_enc_unreg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
  931. int (*get_msr_feature)(struct kvm_msr_entry *entry);
  932. };
  933. struct kvm_arch_async_pf {
  934. u32 token;
  935. gfn_t gfn;
  936. unsigned long cr3;
  937. bool direct_map;
  938. };
  939. extern struct kvm_x86_ops *kvm_x86_ops;
  940. #define __KVM_HAVE_ARCH_VM_ALLOC
  941. static inline struct kvm *kvm_arch_alloc_vm(void)
  942. {
  943. return kvm_x86_ops->vm_alloc();
  944. }
  945. static inline void kvm_arch_free_vm(struct kvm *kvm)
  946. {
  947. return kvm_x86_ops->vm_free(kvm);
  948. }
  949. int kvm_mmu_module_init(void);
  950. void kvm_mmu_module_exit(void);
  951. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  952. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  953. void kvm_mmu_setup(struct kvm_vcpu *vcpu);
  954. void kvm_mmu_init_vm(struct kvm *kvm);
  955. void kvm_mmu_uninit_vm(struct kvm *kvm);
  956. void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
  957. u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 p_mask,
  958. u64 acc_track_mask, u64 me_mask);
  959. void kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  960. void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
  961. struct kvm_memory_slot *memslot);
  962. void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
  963. const struct kvm_memory_slot *memslot);
  964. void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
  965. struct kvm_memory_slot *memslot);
  966. void kvm_mmu_slot_largepage_remove_write_access(struct kvm *kvm,
  967. struct kvm_memory_slot *memslot);
  968. void kvm_mmu_slot_set_dirty(struct kvm *kvm,
  969. struct kvm_memory_slot *memslot);
  970. void kvm_mmu_clear_dirty_pt_masked(struct kvm *kvm,
  971. struct kvm_memory_slot *slot,
  972. gfn_t gfn_offset, unsigned long mask);
  973. void kvm_mmu_zap_all(struct kvm *kvm);
  974. void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, struct kvm_memslots *slots);
  975. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm);
  976. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
  977. int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3);
  978. bool pdptrs_changed(struct kvm_vcpu *vcpu);
  979. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  980. const void *val, int bytes);
  981. struct kvm_irq_mask_notifier {
  982. void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked);
  983. int irq;
  984. struct hlist_node link;
  985. };
  986. void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
  987. struct kvm_irq_mask_notifier *kimn);
  988. void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
  989. struct kvm_irq_mask_notifier *kimn);
  990. void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
  991. bool mask);
  992. extern bool tdp_enabled;
  993. u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu);
  994. /* control of guest tsc rate supported? */
  995. extern bool kvm_has_tsc_control;
  996. /* maximum supported tsc_khz for guests */
  997. extern u32 kvm_max_guest_tsc_khz;
  998. /* number of bits of the fractional part of the TSC scaling ratio */
  999. extern u8 kvm_tsc_scaling_ratio_frac_bits;
  1000. /* maximum allowed value of TSC scaling ratio */
  1001. extern u64 kvm_max_tsc_scaling_ratio;
  1002. /* 1ull << kvm_tsc_scaling_ratio_frac_bits */
  1003. extern u64 kvm_default_tsc_scaling_ratio;
  1004. extern u64 kvm_mce_cap_supported;
  1005. enum emulation_result {
  1006. EMULATE_DONE, /* no further processing */
  1007. EMULATE_USER_EXIT, /* kvm_run ready for userspace exit */
  1008. EMULATE_FAIL, /* can't emulate this instruction */
  1009. };
  1010. #define EMULTYPE_NO_DECODE (1 << 0)
  1011. #define EMULTYPE_TRAP_UD (1 << 1)
  1012. #define EMULTYPE_SKIP (1 << 2)
  1013. #define EMULTYPE_RETRY (1 << 3)
  1014. #define EMULTYPE_NO_REEXECUTE (1 << 4)
  1015. #define EMULTYPE_NO_UD_ON_FAIL (1 << 5)
  1016. #define EMULTYPE_VMWARE (1 << 6)
  1017. int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2,
  1018. int emulation_type, void *insn, int insn_len);
  1019. static inline int emulate_instruction(struct kvm_vcpu *vcpu,
  1020. int emulation_type)
  1021. {
  1022. return x86_emulate_instruction(vcpu, 0,
  1023. emulation_type | EMULTYPE_NO_REEXECUTE, NULL, 0);
  1024. }
  1025. void kvm_enable_efer_bits(u64);
  1026. bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
  1027. int kvm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
  1028. int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
  1029. struct x86_emulate_ctxt;
  1030. int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in);
  1031. int kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
  1032. int kvm_emulate_halt(struct kvm_vcpu *vcpu);
  1033. int kvm_vcpu_halt(struct kvm_vcpu *vcpu);
  1034. int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu);
  1035. void kvm_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
  1036. int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg);
  1037. void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
  1038. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
  1039. int reason, bool has_error_code, u32 error_code);
  1040. int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  1041. int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
  1042. int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  1043. int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8);
  1044. int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val);
  1045. int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val);
  1046. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);
  1047. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  1048. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
  1049. int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr);
  1050. int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr);
  1051. int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr);
  1052. unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu);
  1053. void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
  1054. bool kvm_rdpmc(struct kvm_vcpu *vcpu);
  1055. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
  1056. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
  1057. void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr);
  1058. void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
  1059. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
  1060. int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  1061. gfn_t gfn, void *data, int offset, int len,
  1062. u32 access);
  1063. bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl);
  1064. bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr);
  1065. static inline int __kvm_irq_line_state(unsigned long *irq_state,
  1066. int irq_source_id, int level)
  1067. {
  1068. /* Logical OR for level trig interrupt */
  1069. if (level)
  1070. __set_bit(irq_source_id, irq_state);
  1071. else
  1072. __clear_bit(irq_source_id, irq_state);
  1073. return !!(*irq_state);
  1074. }
  1075. int kvm_pic_set_irq(struct kvm_pic *pic, int irq, int irq_source_id, int level);
  1076. void kvm_pic_clear_all(struct kvm_pic *pic, int irq_source_id);
  1077. void kvm_inject_nmi(struct kvm_vcpu *vcpu);
  1078. int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn);
  1079. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  1080. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  1081. int kvm_mmu_load(struct kvm_vcpu *vcpu);
  1082. void kvm_mmu_unload(struct kvm_vcpu *vcpu);
  1083. void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
  1084. gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
  1085. struct x86_exception *exception);
  1086. gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
  1087. struct x86_exception *exception);
  1088. gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
  1089. struct x86_exception *exception);
  1090. gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
  1091. struct x86_exception *exception);
  1092. gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
  1093. struct x86_exception *exception);
  1094. void kvm_vcpu_deactivate_apicv(struct kvm_vcpu *vcpu);
  1095. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
  1096. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u64 error_code,
  1097. void *insn, int insn_len);
  1098. void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
  1099. void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu);
  1100. void kvm_enable_tdp(void);
  1101. void kvm_disable_tdp(void);
  1102. static inline gpa_t translate_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
  1103. struct x86_exception *exception)
  1104. {
  1105. return gpa;
  1106. }
  1107. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  1108. {
  1109. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  1110. return (struct kvm_mmu_page *)page_private(page);
  1111. }
  1112. static inline u16 kvm_read_ldt(void)
  1113. {
  1114. u16 ldt;
  1115. asm("sldt %0" : "=g"(ldt));
  1116. return ldt;
  1117. }
  1118. static inline void kvm_load_ldt(u16 sel)
  1119. {
  1120. asm("lldt %0" : : "rm"(sel));
  1121. }
  1122. #ifdef CONFIG_X86_64
  1123. static inline unsigned long read_msr(unsigned long msr)
  1124. {
  1125. u64 value;
  1126. rdmsrl(msr, value);
  1127. return value;
  1128. }
  1129. #endif
  1130. static inline u32 get_rdx_init_val(void)
  1131. {
  1132. return 0x600; /* P6 family */
  1133. }
  1134. static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
  1135. {
  1136. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  1137. }
  1138. #define TSS_IOPB_BASE_OFFSET 0x66
  1139. #define TSS_BASE_SIZE 0x68
  1140. #define TSS_IOPB_SIZE (65536 / 8)
  1141. #define TSS_REDIRECTION_SIZE (256 / 8)
  1142. #define RMODE_TSS_SIZE \
  1143. (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  1144. enum {
  1145. TASK_SWITCH_CALL = 0,
  1146. TASK_SWITCH_IRET = 1,
  1147. TASK_SWITCH_JMP = 2,
  1148. TASK_SWITCH_GATE = 3,
  1149. };
  1150. #define HF_GIF_MASK (1 << 0)
  1151. #define HF_HIF_MASK (1 << 1)
  1152. #define HF_VINTR_MASK (1 << 2)
  1153. #define HF_NMI_MASK (1 << 3)
  1154. #define HF_IRET_MASK (1 << 4)
  1155. #define HF_GUEST_MASK (1 << 5) /* VCPU is in guest-mode */
  1156. #define HF_SMM_MASK (1 << 6)
  1157. #define HF_SMM_INSIDE_NMI_MASK (1 << 7)
  1158. #define __KVM_VCPU_MULTIPLE_ADDRESS_SPACE
  1159. #define KVM_ADDRESS_SPACE_NUM 2
  1160. #define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)
  1161. #define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm)
  1162. /*
  1163. * Hardware virtualization extension instructions may fault if a
  1164. * reboot turns off virtualization while processes are running.
  1165. * Trap the fault and ignore the instruction if that happens.
  1166. */
  1167. asmlinkage void kvm_spurious_fault(void);
  1168. #define ____kvm_handle_fault_on_reboot(insn, cleanup_insn) \
  1169. "666: " insn "\n\t" \
  1170. "668: \n\t" \
  1171. ".pushsection .fixup, \"ax\" \n" \
  1172. "667: \n\t" \
  1173. cleanup_insn "\n\t" \
  1174. "cmpb $0, kvm_rebooting \n\t" \
  1175. "jne 668b \n\t" \
  1176. __ASM_SIZE(push) " $666b \n\t" \
  1177. "call kvm_spurious_fault \n\t" \
  1178. ".popsection \n\t" \
  1179. _ASM_EXTABLE(666b, 667b)
  1180. #define __kvm_handle_fault_on_reboot(insn) \
  1181. ____kvm_handle_fault_on_reboot(insn, "")
  1182. #define KVM_ARCH_WANT_MMU_NOTIFIER
  1183. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
  1184. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end);
  1185. int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end);
  1186. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
  1187. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
  1188. int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v);
  1189. int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu);
  1190. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu);
  1191. int kvm_cpu_get_interrupt(struct kvm_vcpu *v);
  1192. void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);
  1193. void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu);
  1194. void kvm_define_shared_msr(unsigned index, u32 msr);
  1195. int kvm_set_shared_msr(unsigned index, u64 val, u64 mask);
  1196. u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc);
  1197. u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc);
  1198. unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu);
  1199. bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip);
  1200. void kvm_make_mclock_inprogress_request(struct kvm *kvm);
  1201. void kvm_make_scan_ioapic_request(struct kvm *kvm);
  1202. void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
  1203. struct kvm_async_pf *work);
  1204. void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
  1205. struct kvm_async_pf *work);
  1206. void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
  1207. struct kvm_async_pf *work);
  1208. bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu);
  1209. extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);
  1210. int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu);
  1211. int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err);
  1212. int kvm_is_in_guest(void);
  1213. int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size);
  1214. int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size);
  1215. bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu);
  1216. bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu);
  1217. bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
  1218. struct kvm_vcpu **dest_vcpu);
  1219. void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
  1220. struct kvm_lapic_irq *irq);
  1221. static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
  1222. {
  1223. if (kvm_x86_ops->vcpu_blocking)
  1224. kvm_x86_ops->vcpu_blocking(vcpu);
  1225. }
  1226. static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
  1227. {
  1228. if (kvm_x86_ops->vcpu_unblocking)
  1229. kvm_x86_ops->vcpu_unblocking(vcpu);
  1230. }
  1231. static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
  1232. static inline int kvm_cpu_get_apicid(int mps_cpu)
  1233. {
  1234. #ifdef CONFIG_X86_LOCAL_APIC
  1235. return default_cpu_present_to_apicid(mps_cpu);
  1236. #else
  1237. WARN_ON_ONCE(1);
  1238. return BAD_APICID;
  1239. #endif
  1240. }
  1241. #define put_smstate(type, buf, offset, val) \
  1242. *(type *)((buf) + (offset) - 0x7e00) = val
  1243. #endif /* _ASM_X86_KVM_HOST_H */