kvm_host.h 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Kernel-based Virtual Machine driver for Linux
  4. *
  5. * This header defines architecture specific interfaces, x86 version
  6. */
  7. #ifndef _ASM_X86_KVM_HOST_H
  8. #define _ASM_X86_KVM_HOST_H
  9. #include <linux/types.h>
  10. #include <linux/mm.h>
  11. #include <linux/mmu_notifier.h>
  12. #include <linux/tracepoint.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/irq_work.h>
  15. #include <linux/irq.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/kvm.h>
  18. #include <linux/kvm_para.h>
  19. #include <linux/kvm_types.h>
  20. #include <linux/perf_event.h>
  21. #include <linux/pvclock_gtod.h>
  22. #include <linux/clocksource.h>
  23. #include <linux/irqbypass.h>
  24. #include <linux/hyperv.h>
  25. #include <asm/apic.h>
  26. #include <asm/pvclock-abi.h>
  27. #include <asm/desc.h>
  28. #include <asm/mtrr.h>
  29. #include <asm/msr-index.h>
  30. #include <asm/asm.h>
  31. #include <asm/kvm_page_track.h>
  32. #include <asm/kvm_vcpu_regs.h>
  33. #include <asm/hyperv-tlfs.h>
  34. #define __KVM_HAVE_ARCH_VCPU_DEBUGFS
  35. #define KVM_MAX_VCPUS 1024
  36. /*
  37. * In x86, the VCPU ID corresponds to the APIC ID, and APIC IDs
  38. * might be larger than the actual number of VCPUs because the
  39. * APIC ID encodes CPU topology information.
  40. *
  41. * In the worst case, we'll need less than one extra bit for the
  42. * Core ID, and less than one extra bit for the Package (Die) ID,
  43. * so ratio of 4 should be enough.
  44. */
  45. #define KVM_VCPU_ID_RATIO 4
  46. #define KVM_MAX_VCPU_IDS (KVM_MAX_VCPUS * KVM_VCPU_ID_RATIO)
  47. /* memory slots that are not exposed to userspace */
  48. #define KVM_INTERNAL_MEM_SLOTS 3
  49. #define KVM_HALT_POLL_NS_DEFAULT 200000
  50. #define KVM_IRQCHIP_NUM_PINS KVM_IOAPIC_NUM_PINS
  51. #define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
  52. KVM_DIRTY_LOG_INITIALLY_SET)
  53. #define KVM_BUS_LOCK_DETECTION_VALID_MODE (KVM_BUS_LOCK_DETECTION_OFF | \
  54. KVM_BUS_LOCK_DETECTION_EXIT)
  55. #define KVM_X86_NOTIFY_VMEXIT_VALID_BITS (KVM_X86_NOTIFY_VMEXIT_ENABLED | \
  56. KVM_X86_NOTIFY_VMEXIT_USER)
  57. /* x86-specific vcpu->requests bit members */
  58. #define KVM_REQ_MIGRATE_TIMER KVM_ARCH_REQ(0)
  59. #define KVM_REQ_REPORT_TPR_ACCESS KVM_ARCH_REQ(1)
  60. #define KVM_REQ_TRIPLE_FAULT KVM_ARCH_REQ(2)
  61. #define KVM_REQ_MMU_SYNC KVM_ARCH_REQ(3)
  62. #define KVM_REQ_CLOCK_UPDATE KVM_ARCH_REQ(4)
  63. #define KVM_REQ_LOAD_MMU_PGD KVM_ARCH_REQ(5)
  64. #define KVM_REQ_EVENT KVM_ARCH_REQ(6)
  65. #define KVM_REQ_APF_HALT KVM_ARCH_REQ(7)
  66. #define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(8)
  67. #define KVM_REQ_NMI KVM_ARCH_REQ(9)
  68. #define KVM_REQ_PMU KVM_ARCH_REQ(10)
  69. #define KVM_REQ_PMI KVM_ARCH_REQ(11)
  70. #define KVM_REQ_SMI KVM_ARCH_REQ(12)
  71. #define KVM_REQ_MASTERCLOCK_UPDATE KVM_ARCH_REQ(13)
  72. #define KVM_REQ_MCLOCK_INPROGRESS \
  73. KVM_ARCH_REQ_FLAGS(14, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  74. #define KVM_REQ_SCAN_IOAPIC \
  75. KVM_ARCH_REQ_FLAGS(15, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  76. #define KVM_REQ_GLOBAL_CLOCK_UPDATE KVM_ARCH_REQ(16)
  77. #define KVM_REQ_APIC_PAGE_RELOAD \
  78. KVM_ARCH_REQ_FLAGS(17, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  79. #define KVM_REQ_HV_CRASH KVM_ARCH_REQ(18)
  80. #define KVM_REQ_IOAPIC_EOI_EXIT KVM_ARCH_REQ(19)
  81. #define KVM_REQ_HV_RESET KVM_ARCH_REQ(20)
  82. #define KVM_REQ_HV_EXIT KVM_ARCH_REQ(21)
  83. #define KVM_REQ_HV_STIMER KVM_ARCH_REQ(22)
  84. #define KVM_REQ_LOAD_EOI_EXITMAP KVM_ARCH_REQ(23)
  85. #define KVM_REQ_GET_NESTED_STATE_PAGES KVM_ARCH_REQ(24)
  86. #define KVM_REQ_APICV_UPDATE \
  87. KVM_ARCH_REQ_FLAGS(25, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  88. #define KVM_REQ_TLB_FLUSH_CURRENT KVM_ARCH_REQ(26)
  89. #define KVM_REQ_TLB_FLUSH_GUEST \
  90. KVM_ARCH_REQ_FLAGS(27, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  91. #define KVM_REQ_APF_READY KVM_ARCH_REQ(28)
  92. #define KVM_REQ_MSR_FILTER_CHANGED KVM_ARCH_REQ(29)
  93. #define KVM_REQ_UPDATE_CPU_DIRTY_LOGGING \
  94. KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  95. #define KVM_REQ_MMU_FREE_OBSOLETE_ROOTS \
  96. KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
  97. #define CR0_RESERVED_BITS \
  98. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  99. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  100. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  101. #define CR4_RESERVED_BITS \
  102. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  103. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  104. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
  105. | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
  106. | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \
  107. | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP))
  108. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  109. #define INVALID_PAGE (~(hpa_t)0)
  110. #define VALID_PAGE(x) ((x) != INVALID_PAGE)
  111. #define INVALID_GPA (~(gpa_t)0)
  112. /* KVM Hugepage definitions for x86 */
  113. #define KVM_MAX_HUGEPAGE_LEVEL PG_LEVEL_1G
  114. #define KVM_NR_PAGE_SIZES (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
  115. #define KVM_HPAGE_GFN_SHIFT(x) (((x) - 1) * 9)
  116. #define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
  117. #define KVM_HPAGE_SIZE(x) (1UL << KVM_HPAGE_SHIFT(x))
  118. #define KVM_HPAGE_MASK(x) (~(KVM_HPAGE_SIZE(x) - 1))
  119. #define KVM_PAGES_PER_HPAGE(x) (KVM_HPAGE_SIZE(x) / PAGE_SIZE)
  120. #define KVM_MEMSLOT_PAGES_TO_MMU_PAGES_RATIO 50
  121. #define KVM_MIN_ALLOC_MMU_PAGES 64UL
  122. #define KVM_MMU_HASH_SHIFT 12
  123. #define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
  124. #define KVM_MIN_FREE_MMU_PAGES 5
  125. #define KVM_REFILL_PAGES 25
  126. #define KVM_MAX_CPUID_ENTRIES 256
  127. #define KVM_NR_FIXED_MTRR_REGION 88
  128. #define KVM_NR_VAR_MTRR 8
  129. #define ASYNC_PF_PER_VCPU 64
  130. enum kvm_reg {
  131. VCPU_REGS_RAX = __VCPU_REGS_RAX,
  132. VCPU_REGS_RCX = __VCPU_REGS_RCX,
  133. VCPU_REGS_RDX = __VCPU_REGS_RDX,
  134. VCPU_REGS_RBX = __VCPU_REGS_RBX,
  135. VCPU_REGS_RSP = __VCPU_REGS_RSP,
  136. VCPU_REGS_RBP = __VCPU_REGS_RBP,
  137. VCPU_REGS_RSI = __VCPU_REGS_RSI,
  138. VCPU_REGS_RDI = __VCPU_REGS_RDI,
  139. #ifdef CONFIG_X86_64
  140. VCPU_REGS_R8 = __VCPU_REGS_R8,
  141. VCPU_REGS_R9 = __VCPU_REGS_R9,
  142. VCPU_REGS_R10 = __VCPU_REGS_R10,
  143. VCPU_REGS_R11 = __VCPU_REGS_R11,
  144. VCPU_REGS_R12 = __VCPU_REGS_R12,
  145. VCPU_REGS_R13 = __VCPU_REGS_R13,
  146. VCPU_REGS_R14 = __VCPU_REGS_R14,
  147. VCPU_REGS_R15 = __VCPU_REGS_R15,
  148. #endif
  149. VCPU_REGS_RIP,
  150. NR_VCPU_REGS,
  151. VCPU_EXREG_PDPTR = NR_VCPU_REGS,
  152. VCPU_EXREG_CR0,
  153. VCPU_EXREG_CR3,
  154. VCPU_EXREG_CR4,
  155. VCPU_EXREG_RFLAGS,
  156. VCPU_EXREG_SEGMENTS,
  157. VCPU_EXREG_EXIT_INFO_1,
  158. VCPU_EXREG_EXIT_INFO_2,
  159. };
  160. enum {
  161. VCPU_SREG_ES,
  162. VCPU_SREG_CS,
  163. VCPU_SREG_SS,
  164. VCPU_SREG_DS,
  165. VCPU_SREG_FS,
  166. VCPU_SREG_GS,
  167. VCPU_SREG_TR,
  168. VCPU_SREG_LDTR,
  169. };
  170. enum exit_fastpath_completion {
  171. EXIT_FASTPATH_NONE,
  172. EXIT_FASTPATH_REENTER_GUEST,
  173. EXIT_FASTPATH_EXIT_HANDLED,
  174. };
  175. typedef enum exit_fastpath_completion fastpath_t;
  176. struct x86_emulate_ctxt;
  177. struct x86_exception;
  178. enum x86_intercept;
  179. enum x86_intercept_stage;
  180. #define KVM_NR_DB_REGS 4
  181. #define DR6_BUS_LOCK (1 << 11)
  182. #define DR6_BD (1 << 13)
  183. #define DR6_BS (1 << 14)
  184. #define DR6_BT (1 << 15)
  185. #define DR6_RTM (1 << 16)
  186. /*
  187. * DR6_ACTIVE_LOW combines fixed-1 and active-low bits.
  188. * We can regard all the bits in DR6_FIXED_1 as active_low bits;
  189. * they will never be 0 for now, but when they are defined
  190. * in the future it will require no code change.
  191. *
  192. * DR6_ACTIVE_LOW is also used as the init/reset value for DR6.
  193. */
  194. #define DR6_ACTIVE_LOW 0xffff0ff0
  195. #define DR6_VOLATILE 0x0001e80f
  196. #define DR6_FIXED_1 (DR6_ACTIVE_LOW & ~DR6_VOLATILE)
  197. #define DR7_BP_EN_MASK 0x000000ff
  198. #define DR7_GE (1 << 9)
  199. #define DR7_GD (1 << 13)
  200. #define DR7_FIXED_1 0x00000400
  201. #define DR7_VOLATILE 0xffff2bff
  202. #define KVM_GUESTDBG_VALID_MASK \
  203. (KVM_GUESTDBG_ENABLE | \
  204. KVM_GUESTDBG_SINGLESTEP | \
  205. KVM_GUESTDBG_USE_HW_BP | \
  206. KVM_GUESTDBG_USE_SW_BP | \
  207. KVM_GUESTDBG_INJECT_BP | \
  208. KVM_GUESTDBG_INJECT_DB | \
  209. KVM_GUESTDBG_BLOCKIRQ)
  210. #define PFERR_PRESENT_BIT 0
  211. #define PFERR_WRITE_BIT 1
  212. #define PFERR_USER_BIT 2
  213. #define PFERR_RSVD_BIT 3
  214. #define PFERR_FETCH_BIT 4
  215. #define PFERR_PK_BIT 5
  216. #define PFERR_SGX_BIT 15
  217. #define PFERR_GUEST_FINAL_BIT 32
  218. #define PFERR_GUEST_PAGE_BIT 33
  219. #define PFERR_IMPLICIT_ACCESS_BIT 48
  220. #define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT)
  221. #define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT)
  222. #define PFERR_USER_MASK (1U << PFERR_USER_BIT)
  223. #define PFERR_RSVD_MASK (1U << PFERR_RSVD_BIT)
  224. #define PFERR_FETCH_MASK (1U << PFERR_FETCH_BIT)
  225. #define PFERR_PK_MASK (1U << PFERR_PK_BIT)
  226. #define PFERR_SGX_MASK (1U << PFERR_SGX_BIT)
  227. #define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT)
  228. #define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT)
  229. #define PFERR_IMPLICIT_ACCESS (1ULL << PFERR_IMPLICIT_ACCESS_BIT)
  230. #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \
  231. PFERR_WRITE_MASK | \
  232. PFERR_PRESENT_MASK)
  233. /* apic attention bits */
  234. #define KVM_APIC_CHECK_VAPIC 0
  235. /*
  236. * The following bit is set with PV-EOI, unset on EOI.
  237. * We detect PV-EOI changes by guest by comparing
  238. * this bit with PV-EOI in guest memory.
  239. * See the implementation in apic_update_pv_eoi.
  240. */
  241. #define KVM_APIC_PV_EOI_PENDING 1
  242. struct kvm_kernel_irq_routing_entry;
  243. /*
  244. * kvm_mmu_page_role tracks the properties of a shadow page (where shadow page
  245. * also includes TDP pages) to determine whether or not a page can be used in
  246. * the given MMU context. This is a subset of the overall kvm_cpu_role to
  247. * minimize the size of kvm_memory_slot.arch.gfn_track, i.e. allows allocating
  248. * 2 bytes per gfn instead of 4 bytes per gfn.
  249. *
  250. * Upper-level shadow pages having gptes are tracked for write-protection via
  251. * gfn_track. As above, gfn_track is a 16 bit counter, so KVM must not create
  252. * more than 2^16-1 upper-level shadow pages at a single gfn, otherwise
  253. * gfn_track will overflow and explosions will ensure.
  254. *
  255. * A unique shadow page (SP) for a gfn is created if and only if an existing SP
  256. * cannot be reused. The ability to reuse a SP is tracked by its role, which
  257. * incorporates various mode bits and properties of the SP. Roughly speaking,
  258. * the number of unique SPs that can theoretically be created is 2^n, where n
  259. * is the number of bits that are used to compute the role.
  260. *
  261. * But, even though there are 19 bits in the mask below, not all combinations
  262. * of modes and flags are possible:
  263. *
  264. * - invalid shadow pages are not accounted, so the bits are effectively 18
  265. *
  266. * - quadrant will only be used if has_4_byte_gpte=1 (non-PAE paging);
  267. * execonly and ad_disabled are only used for nested EPT which has
  268. * has_4_byte_gpte=0. Therefore, 2 bits are always unused.
  269. *
  270. * - the 4 bits of level are effectively limited to the values 2/3/4/5,
  271. * as 4k SPs are not tracked (allowed to go unsync). In addition non-PAE
  272. * paging has exactly one upper level, making level completely redundant
  273. * when has_4_byte_gpte=1.
  274. *
  275. * - on top of this, smep_andnot_wp and smap_andnot_wp are only set if
  276. * cr0_wp=0, therefore these three bits only give rise to 5 possibilities.
  277. *
  278. * Therefore, the maximum number of possible upper-level shadow pages for a
  279. * single gfn is a bit less than 2^13.
  280. */
  281. union kvm_mmu_page_role {
  282. u32 word;
  283. struct {
  284. unsigned level:4;
  285. unsigned has_4_byte_gpte:1;
  286. unsigned quadrant:2;
  287. unsigned direct:1;
  288. unsigned access:3;
  289. unsigned invalid:1;
  290. unsigned efer_nx:1;
  291. unsigned cr0_wp:1;
  292. unsigned smep_andnot_wp:1;
  293. unsigned smap_andnot_wp:1;
  294. unsigned ad_disabled:1;
  295. unsigned guest_mode:1;
  296. unsigned passthrough:1;
  297. unsigned :5;
  298. /*
  299. * This is left at the top of the word so that
  300. * kvm_memslots_for_spte_role can extract it with a
  301. * simple shift. While there is room, give it a whole
  302. * byte so it is also faster to load it from memory.
  303. */
  304. unsigned smm:8;
  305. };
  306. };
  307. /*
  308. * kvm_mmu_extended_role complements kvm_mmu_page_role, tracking properties
  309. * relevant to the current MMU configuration. When loading CR0, CR4, or EFER,
  310. * including on nested transitions, if nothing in the full role changes then
  311. * MMU re-configuration can be skipped. @valid bit is set on first usage so we
  312. * don't treat all-zero structure as valid data.
  313. *
  314. * The properties that are tracked in the extended role but not the page role
  315. * are for things that either (a) do not affect the validity of the shadow page
  316. * or (b) are indirectly reflected in the shadow page's role. For example,
  317. * CR4.PKE only affects permission checks for software walks of the guest page
  318. * tables (because KVM doesn't support Protection Keys with shadow paging), and
  319. * CR0.PG, CR4.PAE, and CR4.PSE are indirectly reflected in role.level.
  320. *
  321. * Note, SMEP and SMAP are not redundant with sm*p_andnot_wp in the page role.
  322. * If CR0.WP=1, KVM can reuse shadow pages for the guest regardless of SMEP and
  323. * SMAP, but the MMU's permission checks for software walks need to be SMEP and
  324. * SMAP aware regardless of CR0.WP.
  325. */
  326. union kvm_mmu_extended_role {
  327. u32 word;
  328. struct {
  329. unsigned int valid:1;
  330. unsigned int execonly:1;
  331. unsigned int cr4_pse:1;
  332. unsigned int cr4_pke:1;
  333. unsigned int cr4_smap:1;
  334. unsigned int cr4_smep:1;
  335. unsigned int cr4_la57:1;
  336. unsigned int efer_lma:1;
  337. };
  338. };
  339. union kvm_cpu_role {
  340. u64 as_u64;
  341. struct {
  342. union kvm_mmu_page_role base;
  343. union kvm_mmu_extended_role ext;
  344. };
  345. };
  346. struct kvm_rmap_head {
  347. unsigned long val;
  348. };
  349. struct kvm_pio_request {
  350. unsigned long linear_rip;
  351. unsigned long count;
  352. int in;
  353. int port;
  354. int size;
  355. };
  356. #define PT64_ROOT_MAX_LEVEL 5
  357. struct rsvd_bits_validate {
  358. u64 rsvd_bits_mask[2][PT64_ROOT_MAX_LEVEL];
  359. u64 bad_mt_xwr;
  360. };
  361. struct kvm_mmu_root_info {
  362. gpa_t pgd;
  363. hpa_t hpa;
  364. };
  365. #define KVM_MMU_ROOT_INFO_INVALID \
  366. ((struct kvm_mmu_root_info) { .pgd = INVALID_PAGE, .hpa = INVALID_PAGE })
  367. #define KVM_MMU_NUM_PREV_ROOTS 3
  368. #define KVM_HAVE_MMU_RWLOCK
  369. struct kvm_mmu_page;
  370. struct kvm_page_fault;
  371. /*
  372. * x86 supports 4 paging modes (5-level 64-bit, 4-level 64-bit, 3-level 32-bit,
  373. * and 2-level 32-bit). The kvm_mmu structure abstracts the details of the
  374. * current mmu mode.
  375. */
  376. struct kvm_mmu {
  377. unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu);
  378. u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index);
  379. int (*page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);
  380. void (*inject_page_fault)(struct kvm_vcpu *vcpu,
  381. struct x86_exception *fault);
  382. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  383. gpa_t gva_or_gpa, u64 access,
  384. struct x86_exception *exception);
  385. int (*sync_page)(struct kvm_vcpu *vcpu,
  386. struct kvm_mmu_page *sp);
  387. void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa);
  388. struct kvm_mmu_root_info root;
  389. union kvm_cpu_role cpu_role;
  390. union kvm_mmu_page_role root_role;
  391. /*
  392. * The pkru_mask indicates if protection key checks are needed. It
  393. * consists of 16 domains indexed by page fault error code bits [4:1],
  394. * with PFEC.RSVD replaced by ACC_USER_MASK from the page tables.
  395. * Each domain has 2 bits which are ANDed with AD and WD from PKRU.
  396. */
  397. u32 pkru_mask;
  398. struct kvm_mmu_root_info prev_roots[KVM_MMU_NUM_PREV_ROOTS];
  399. /*
  400. * Bitmap; bit set = permission fault
  401. * Byte index: page fault error code [4:1]
  402. * Bit index: pte permissions in ACC_* format
  403. */
  404. u8 permissions[16];
  405. u64 *pae_root;
  406. u64 *pml4_root;
  407. u64 *pml5_root;
  408. /*
  409. * check zero bits on shadow page table entries, these
  410. * bits include not only hardware reserved bits but also
  411. * the bits spte never used.
  412. */
  413. struct rsvd_bits_validate shadow_zero_check;
  414. struct rsvd_bits_validate guest_rsvd_check;
  415. u64 pdptrs[4]; /* pae */
  416. };
  417. struct kvm_tlb_range {
  418. u64 start_gfn;
  419. u64 pages;
  420. };
  421. enum pmc_type {
  422. KVM_PMC_GP = 0,
  423. KVM_PMC_FIXED,
  424. };
  425. struct kvm_pmc {
  426. enum pmc_type type;
  427. u8 idx;
  428. u64 counter;
  429. u64 eventsel;
  430. struct perf_event *perf_event;
  431. struct kvm_vcpu *vcpu;
  432. /*
  433. * eventsel value for general purpose counters,
  434. * ctrl value for fixed counters.
  435. */
  436. u64 current_config;
  437. bool is_paused;
  438. bool intr;
  439. };
  440. /* More counters may conflict with other existing Architectural MSRs */
  441. #define KVM_INTEL_PMC_MAX_GENERIC 8
  442. #define MSR_ARCH_PERFMON_PERFCTR_MAX (MSR_ARCH_PERFMON_PERFCTR0 + KVM_INTEL_PMC_MAX_GENERIC - 1)
  443. #define MSR_ARCH_PERFMON_EVENTSEL_MAX (MSR_ARCH_PERFMON_EVENTSEL0 + KVM_INTEL_PMC_MAX_GENERIC - 1)
  444. #define KVM_PMC_MAX_FIXED 3
  445. #define KVM_AMD_PMC_MAX_GENERIC 6
  446. struct kvm_pmu {
  447. unsigned nr_arch_gp_counters;
  448. unsigned nr_arch_fixed_counters;
  449. unsigned available_event_types;
  450. u64 fixed_ctr_ctrl;
  451. u64 fixed_ctr_ctrl_mask;
  452. u64 global_ctrl;
  453. u64 global_status;
  454. u64 counter_bitmask[2];
  455. u64 global_ctrl_mask;
  456. u64 global_ovf_ctrl_mask;
  457. u64 reserved_bits;
  458. u64 raw_event_mask;
  459. u8 version;
  460. struct kvm_pmc gp_counters[KVM_INTEL_PMC_MAX_GENERIC];
  461. struct kvm_pmc fixed_counters[KVM_PMC_MAX_FIXED];
  462. struct irq_work irq_work;
  463. DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
  464. DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
  465. DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
  466. u64 ds_area;
  467. u64 pebs_enable;
  468. u64 pebs_enable_mask;
  469. u64 pebs_data_cfg;
  470. u64 pebs_data_cfg_mask;
  471. /*
  472. * If a guest counter is cross-mapped to host counter with different
  473. * index, its PEBS capability will be temporarily disabled.
  474. *
  475. * The user should make sure that this mask is updated
  476. * after disabling interrupts and before perf_guest_get_msrs();
  477. */
  478. u64 host_cross_mapped_mask;
  479. /*
  480. * The gate to release perf_events not marked in
  481. * pmc_in_use only once in a vcpu time slice.
  482. */
  483. bool need_cleanup;
  484. /*
  485. * The total number of programmed perf_events and it helps to avoid
  486. * redundant check before cleanup if guest don't use vPMU at all.
  487. */
  488. u8 event_count;
  489. };
  490. struct kvm_pmu_ops;
  491. enum {
  492. KVM_DEBUGREG_BP_ENABLED = 1,
  493. KVM_DEBUGREG_WONT_EXIT = 2,
  494. };
  495. struct kvm_mtrr_range {
  496. u64 base;
  497. u64 mask;
  498. struct list_head node;
  499. };
  500. struct kvm_mtrr {
  501. struct kvm_mtrr_range var_ranges[KVM_NR_VAR_MTRR];
  502. mtrr_type fixed_ranges[KVM_NR_FIXED_MTRR_REGION];
  503. u64 deftype;
  504. struct list_head head;
  505. };
  506. /* Hyper-V SynIC timer */
  507. struct kvm_vcpu_hv_stimer {
  508. struct hrtimer timer;
  509. int index;
  510. union hv_stimer_config config;
  511. u64 count;
  512. u64 exp_time;
  513. struct hv_message msg;
  514. bool msg_pending;
  515. };
  516. /* Hyper-V synthetic interrupt controller (SynIC)*/
  517. struct kvm_vcpu_hv_synic {
  518. u64 version;
  519. u64 control;
  520. u64 msg_page;
  521. u64 evt_page;
  522. atomic64_t sint[HV_SYNIC_SINT_COUNT];
  523. atomic_t sint_to_gsi[HV_SYNIC_SINT_COUNT];
  524. DECLARE_BITMAP(auto_eoi_bitmap, 256);
  525. DECLARE_BITMAP(vec_bitmap, 256);
  526. bool active;
  527. bool dont_zero_synic_pages;
  528. };
  529. /* Hyper-V per vcpu emulation context */
  530. struct kvm_vcpu_hv {
  531. struct kvm_vcpu *vcpu;
  532. u32 vp_index;
  533. u64 hv_vapic;
  534. s64 runtime_offset;
  535. struct kvm_vcpu_hv_synic synic;
  536. struct kvm_hyperv_exit exit;
  537. struct kvm_vcpu_hv_stimer stimer[HV_SYNIC_STIMER_COUNT];
  538. DECLARE_BITMAP(stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
  539. bool enforce_cpuid;
  540. struct {
  541. u32 features_eax; /* HYPERV_CPUID_FEATURES.EAX */
  542. u32 features_ebx; /* HYPERV_CPUID_FEATURES.EBX */
  543. u32 features_edx; /* HYPERV_CPUID_FEATURES.EDX */
  544. u32 enlightenments_eax; /* HYPERV_CPUID_ENLIGHTMENT_INFO.EAX */
  545. u32 enlightenments_ebx; /* HYPERV_CPUID_ENLIGHTMENT_INFO.EBX */
  546. u32 syndbg_cap_eax; /* HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES.EAX */
  547. u32 nested_eax; /* HYPERV_CPUID_NESTED_FEATURES.EAX */
  548. u32 nested_ebx; /* HYPERV_CPUID_NESTED_FEATURES.EBX */
  549. } cpuid_cache;
  550. };
  551. /* Xen HVM per vcpu emulation context */
  552. struct kvm_vcpu_xen {
  553. u64 hypercall_rip;
  554. u32 current_runstate;
  555. u8 upcall_vector;
  556. struct gfn_to_pfn_cache vcpu_info_cache;
  557. struct gfn_to_pfn_cache vcpu_time_info_cache;
  558. struct gfn_to_pfn_cache runstate_cache;
  559. u64 last_steal;
  560. u64 runstate_entry_time;
  561. u64 runstate_times[4];
  562. unsigned long evtchn_pending_sel;
  563. u32 vcpu_id; /* The Xen / ACPI vCPU ID */
  564. u32 timer_virq;
  565. u64 timer_expires; /* In guest epoch */
  566. atomic_t timer_pending;
  567. struct hrtimer timer;
  568. int poll_evtchn;
  569. struct timer_list poll_timer;
  570. };
  571. struct kvm_queued_exception {
  572. bool pending;
  573. bool injected;
  574. bool has_error_code;
  575. u8 vector;
  576. u32 error_code;
  577. unsigned long payload;
  578. bool has_payload;
  579. };
  580. struct kvm_vcpu_arch {
  581. /*
  582. * rip and regs accesses must go through
  583. * kvm_{register,rip}_{read,write} functions.
  584. */
  585. unsigned long regs[NR_VCPU_REGS];
  586. u32 regs_avail;
  587. u32 regs_dirty;
  588. unsigned long cr0;
  589. unsigned long cr0_guest_owned_bits;
  590. unsigned long cr2;
  591. unsigned long cr3;
  592. unsigned long cr4;
  593. unsigned long cr4_guest_owned_bits;
  594. unsigned long cr4_guest_rsvd_bits;
  595. unsigned long cr8;
  596. u32 host_pkru;
  597. u32 pkru;
  598. u32 hflags;
  599. u64 efer;
  600. u64 apic_base;
  601. struct kvm_lapic *apic; /* kernel irqchip context */
  602. bool load_eoi_exitmap_pending;
  603. DECLARE_BITMAP(ioapic_handled_vectors, 256);
  604. unsigned long apic_attention;
  605. int32_t apic_arb_prio;
  606. int mp_state;
  607. u64 ia32_misc_enable_msr;
  608. u64 smbase;
  609. u64 smi_count;
  610. bool at_instruction_boundary;
  611. bool tpr_access_reporting;
  612. bool xsaves_enabled;
  613. bool xfd_no_write_intercept;
  614. u64 ia32_xss;
  615. u64 microcode_version;
  616. u64 arch_capabilities;
  617. u64 perf_capabilities;
  618. /*
  619. * Paging state of the vcpu
  620. *
  621. * If the vcpu runs in guest mode with two level paging this still saves
  622. * the paging mode of the l1 guest. This context is always used to
  623. * handle faults.
  624. */
  625. struct kvm_mmu *mmu;
  626. /* Non-nested MMU for L1 */
  627. struct kvm_mmu root_mmu;
  628. /* L1 MMU when running nested */
  629. struct kvm_mmu guest_mmu;
  630. /*
  631. * Paging state of an L2 guest (used for nested npt)
  632. *
  633. * This context will save all necessary information to walk page tables
  634. * of an L2 guest. This context is only initialized for page table
  635. * walking and not for faulting since we never handle l2 page faults on
  636. * the host.
  637. */
  638. struct kvm_mmu nested_mmu;
  639. /*
  640. * Pointer to the mmu context currently used for
  641. * gva_to_gpa translations.
  642. */
  643. struct kvm_mmu *walk_mmu;
  644. struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;
  645. struct kvm_mmu_memory_cache mmu_shadow_page_cache;
  646. struct kvm_mmu_memory_cache mmu_shadowed_info_cache;
  647. struct kvm_mmu_memory_cache mmu_page_header_cache;
  648. /*
  649. * QEMU userspace and the guest each have their own FPU state.
  650. * In vcpu_run, we switch between the user and guest FPU contexts.
  651. * While running a VCPU, the VCPU thread will have the guest FPU
  652. * context.
  653. *
  654. * Note that while the PKRU state lives inside the fpu registers,
  655. * it is switched out separately at VMENTER and VMEXIT time. The
  656. * "guest_fpstate" state here contains the guest FPU context, with the
  657. * host PRKU bits.
  658. */
  659. struct fpu_guest guest_fpu;
  660. u64 xcr0;
  661. u64 guest_supported_xcr0;
  662. struct kvm_pio_request pio;
  663. void *pio_data;
  664. void *sev_pio_data;
  665. unsigned sev_pio_count;
  666. u8 event_exit_inst_len;
  667. bool exception_from_userspace;
  668. /* Exceptions to be injected to the guest. */
  669. struct kvm_queued_exception exception;
  670. /* Exception VM-Exits to be synthesized to L1. */
  671. struct kvm_queued_exception exception_vmexit;
  672. struct kvm_queued_interrupt {
  673. bool injected;
  674. bool soft;
  675. u8 nr;
  676. } interrupt;
  677. int halt_request; /* real mode on Intel only */
  678. int cpuid_nent;
  679. struct kvm_cpuid_entry2 *cpuid_entries;
  680. u32 kvm_cpuid_base;
  681. u64 reserved_gpa_bits;
  682. int maxphyaddr;
  683. /* emulate context */
  684. struct x86_emulate_ctxt *emulate_ctxt;
  685. bool emulate_regs_need_sync_to_vcpu;
  686. bool emulate_regs_need_sync_from_vcpu;
  687. int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
  688. gpa_t time;
  689. struct pvclock_vcpu_time_info hv_clock;
  690. unsigned int hw_tsc_khz;
  691. struct gfn_to_pfn_cache pv_time;
  692. /* set guest stopped flag in pvclock flags field */
  693. bool pvclock_set_guest_stopped_request;
  694. struct {
  695. u8 preempted;
  696. u64 msr_val;
  697. u64 last_steal;
  698. struct gfn_to_hva_cache cache;
  699. } st;
  700. u64 l1_tsc_offset;
  701. u64 tsc_offset; /* current tsc offset */
  702. u64 last_guest_tsc;
  703. u64 last_host_tsc;
  704. u64 tsc_offset_adjustment;
  705. u64 this_tsc_nsec;
  706. u64 this_tsc_write;
  707. u64 this_tsc_generation;
  708. bool tsc_catchup;
  709. bool tsc_always_catchup;
  710. s8 virtual_tsc_shift;
  711. u32 virtual_tsc_mult;
  712. u32 virtual_tsc_khz;
  713. s64 ia32_tsc_adjust_msr;
  714. u64 msr_ia32_power_ctl;
  715. u64 l1_tsc_scaling_ratio;
  716. u64 tsc_scaling_ratio; /* current scaling ratio */
  717. atomic_t nmi_queued; /* unprocessed asynchronous NMIs */
  718. unsigned nmi_pending; /* NMI queued after currently running handler */
  719. bool nmi_injected; /* Trying to inject an NMI this entry */
  720. bool smi_pending; /* SMI queued after currently running handler */
  721. u8 handling_intr_from_guest;
  722. struct kvm_mtrr mtrr_state;
  723. u64 pat;
  724. unsigned switch_db_regs;
  725. unsigned long db[KVM_NR_DB_REGS];
  726. unsigned long dr6;
  727. unsigned long dr7;
  728. unsigned long eff_db[KVM_NR_DB_REGS];
  729. unsigned long guest_debug_dr7;
  730. u64 msr_platform_info;
  731. u64 msr_misc_features_enables;
  732. u64 mcg_cap;
  733. u64 mcg_status;
  734. u64 mcg_ctl;
  735. u64 mcg_ext_ctl;
  736. u64 *mce_banks;
  737. u64 *mci_ctl2_banks;
  738. /* Cache MMIO info */
  739. u64 mmio_gva;
  740. unsigned mmio_access;
  741. gfn_t mmio_gfn;
  742. u64 mmio_gen;
  743. struct kvm_pmu pmu;
  744. /* used for guest single stepping over the given code position */
  745. unsigned long singlestep_rip;
  746. bool hyperv_enabled;
  747. struct kvm_vcpu_hv *hyperv;
  748. struct kvm_vcpu_xen xen;
  749. cpumask_var_t wbinvd_dirty_mask;
  750. unsigned long last_retry_eip;
  751. unsigned long last_retry_addr;
  752. struct {
  753. bool halted;
  754. gfn_t gfns[ASYNC_PF_PER_VCPU];
  755. struct gfn_to_hva_cache data;
  756. u64 msr_en_val; /* MSR_KVM_ASYNC_PF_EN */
  757. u64 msr_int_val; /* MSR_KVM_ASYNC_PF_INT */
  758. u16 vec;
  759. u32 id;
  760. bool send_user_only;
  761. u32 host_apf_flags;
  762. bool delivery_as_pf_vmexit;
  763. bool pageready_pending;
  764. } apf;
  765. /* OSVW MSRs (AMD only) */
  766. struct {
  767. u64 length;
  768. u64 status;
  769. } osvw;
  770. struct {
  771. u64 msr_val;
  772. struct gfn_to_hva_cache data;
  773. } pv_eoi;
  774. u64 msr_kvm_poll_control;
  775. /*
  776. * Indicates the guest is trying to write a gfn that contains one or
  777. * more of the PTEs used to translate the write itself, i.e. the access
  778. * is changing its own translation in the guest page tables. KVM exits
  779. * to userspace if emulation of the faulting instruction fails and this
  780. * flag is set, as KVM cannot make forward progress.
  781. *
  782. * If emulation fails for a write to guest page tables, KVM unprotects
  783. * (zaps) the shadow page for the target gfn and resumes the guest to
  784. * retry the non-emulatable instruction (on hardware). Unprotecting the
  785. * gfn doesn't allow forward progress for a self-changing access because
  786. * doing so also zaps the translation for the gfn, i.e. retrying the
  787. * instruction will hit a !PRESENT fault, which results in a new shadow
  788. * page and sends KVM back to square one.
  789. */
  790. bool write_fault_to_shadow_pgtable;
  791. /* set at EPT violation at this point */
  792. unsigned long exit_qualification;
  793. /* pv related host specific info */
  794. struct {
  795. bool pv_unhalted;
  796. } pv;
  797. int pending_ioapic_eoi;
  798. int pending_external_vector;
  799. /* be preempted when it's in kernel-mode(cpl=0) */
  800. bool preempted_in_kernel;
  801. /* Flush the L1 Data cache for L1TF mitigation on VMENTER */
  802. bool l1tf_flush_l1d;
  803. /* Host CPU on which VM-entry was most recently attempted */
  804. int last_vmentry_cpu;
  805. /* AMD MSRC001_0015 Hardware Configuration */
  806. u64 msr_hwcr;
  807. /* pv related cpuid info */
  808. struct {
  809. /*
  810. * value of the eax register in the KVM_CPUID_FEATURES CPUID
  811. * leaf.
  812. */
  813. u32 features;
  814. /*
  815. * indicates whether pv emulation should be disabled if features
  816. * are not present in the guest's cpuid
  817. */
  818. bool enforce;
  819. } pv_cpuid;
  820. /* Protected Guests */
  821. bool guest_state_protected;
  822. /*
  823. * Set when PDPTS were loaded directly by the userspace without
  824. * reading the guest memory
  825. */
  826. bool pdptrs_from_userspace;
  827. #if IS_ENABLED(CONFIG_HYPERV)
  828. hpa_t hv_root_tdp;
  829. #endif
  830. };
  831. struct kvm_lpage_info {
  832. int disallow_lpage;
  833. };
  834. struct kvm_arch_memory_slot {
  835. struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
  836. struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
  837. unsigned short *gfn_track[KVM_PAGE_TRACK_MAX];
  838. };
  839. /*
  840. * We use as the mode the number of bits allocated in the LDR for the
  841. * logical processor ID. It happens that these are all powers of two.
  842. * This makes it is very easy to detect cases where the APICs are
  843. * configured for multiple modes; in that case, we cannot use the map and
  844. * hence cannot use kvm_irq_delivery_to_apic_fast either.
  845. */
  846. #define KVM_APIC_MODE_XAPIC_CLUSTER 4
  847. #define KVM_APIC_MODE_XAPIC_FLAT 8
  848. #define KVM_APIC_MODE_X2APIC 16
  849. struct kvm_apic_map {
  850. struct rcu_head rcu;
  851. u8 mode;
  852. u32 max_apic_id;
  853. union {
  854. struct kvm_lapic *xapic_flat_map[8];
  855. struct kvm_lapic *xapic_cluster_map[16][4];
  856. };
  857. struct kvm_lapic *phys_map[];
  858. };
  859. /* Hyper-V synthetic debugger (SynDbg)*/
  860. struct kvm_hv_syndbg {
  861. struct {
  862. u64 control;
  863. u64 status;
  864. u64 send_page;
  865. u64 recv_page;
  866. u64 pending_page;
  867. } control;
  868. u64 options;
  869. };
  870. /* Current state of Hyper-V TSC page clocksource */
  871. enum hv_tsc_page_status {
  872. /* TSC page was not set up or disabled */
  873. HV_TSC_PAGE_UNSET = 0,
  874. /* TSC page MSR was written by the guest, update pending */
  875. HV_TSC_PAGE_GUEST_CHANGED,
  876. /* TSC page update was triggered from the host side */
  877. HV_TSC_PAGE_HOST_CHANGED,
  878. /* TSC page was properly set up and is currently active */
  879. HV_TSC_PAGE_SET,
  880. /* TSC page was set up with an inaccessible GPA */
  881. HV_TSC_PAGE_BROKEN,
  882. };
  883. /* Hyper-V emulation context */
  884. struct kvm_hv {
  885. struct mutex hv_lock;
  886. u64 hv_guest_os_id;
  887. u64 hv_hypercall;
  888. u64 hv_tsc_page;
  889. enum hv_tsc_page_status hv_tsc_page_status;
  890. /* Hyper-v based guest crash (NT kernel bugcheck) parameters */
  891. u64 hv_crash_param[HV_X64_MSR_CRASH_PARAMS];
  892. u64 hv_crash_ctl;
  893. struct ms_hyperv_tsc_page tsc_ref;
  894. struct idr conn_to_evt;
  895. u64 hv_reenlightenment_control;
  896. u64 hv_tsc_emulation_control;
  897. u64 hv_tsc_emulation_status;
  898. /* How many vCPUs have VP index != vCPU index */
  899. atomic_t num_mismatched_vp_indexes;
  900. /*
  901. * How many SynICs use 'AutoEOI' feature
  902. * (protected by arch.apicv_update_lock)
  903. */
  904. unsigned int synic_auto_eoi_used;
  905. struct hv_partition_assist_pg *hv_pa_pg;
  906. struct kvm_hv_syndbg hv_syndbg;
  907. };
  908. struct msr_bitmap_range {
  909. u32 flags;
  910. u32 nmsrs;
  911. u32 base;
  912. unsigned long *bitmap;
  913. };
  914. /* Xen emulation context */
  915. struct kvm_xen {
  916. u32 xen_version;
  917. bool long_mode;
  918. u8 upcall_vector;
  919. struct gfn_to_pfn_cache shinfo_cache;
  920. struct idr evtchn_ports;
  921. unsigned long poll_mask[BITS_TO_LONGS(KVM_MAX_VCPUS)];
  922. };
  923. enum kvm_irqchip_mode {
  924. KVM_IRQCHIP_NONE,
  925. KVM_IRQCHIP_KERNEL, /* created with KVM_CREATE_IRQCHIP */
  926. KVM_IRQCHIP_SPLIT, /* created with KVM_CAP_SPLIT_IRQCHIP */
  927. };
  928. struct kvm_x86_msr_filter {
  929. u8 count;
  930. bool default_allow:1;
  931. struct msr_bitmap_range ranges[16];
  932. };
  933. enum kvm_apicv_inhibit {
  934. /********************************************************************/
  935. /* INHIBITs that are relevant to both Intel's APICv and AMD's AVIC. */
  936. /********************************************************************/
  937. /*
  938. * APIC acceleration is disabled by a module parameter
  939. * and/or not supported in hardware.
  940. */
  941. APICV_INHIBIT_REASON_DISABLE,
  942. /*
  943. * APIC acceleration is inhibited because AutoEOI feature is
  944. * being used by a HyperV guest.
  945. */
  946. APICV_INHIBIT_REASON_HYPERV,
  947. /*
  948. * APIC acceleration is inhibited because the userspace didn't yet
  949. * enable the kernel/split irqchip.
  950. */
  951. APICV_INHIBIT_REASON_ABSENT,
  952. /* APIC acceleration is inhibited because KVM_GUESTDBG_BLOCKIRQ
  953. * (out of band, debug measure of blocking all interrupts on this vCPU)
  954. * was enabled, to avoid AVIC/APICv bypassing it.
  955. */
  956. APICV_INHIBIT_REASON_BLOCKIRQ,
  957. /*
  958. * For simplicity, the APIC acceleration is inhibited
  959. * first time either APIC ID or APIC base are changed by the guest
  960. * from their reset values.
  961. */
  962. APICV_INHIBIT_REASON_APIC_ID_MODIFIED,
  963. APICV_INHIBIT_REASON_APIC_BASE_MODIFIED,
  964. /******************************************************/
  965. /* INHIBITs that are relevant only to the AMD's AVIC. */
  966. /******************************************************/
  967. /*
  968. * AVIC is inhibited on a vCPU because it runs a nested guest.
  969. *
  970. * This is needed because unlike APICv, the peers of this vCPU
  971. * cannot use the doorbell mechanism to signal interrupts via AVIC when
  972. * a vCPU runs nested.
  973. */
  974. APICV_INHIBIT_REASON_NESTED,
  975. /*
  976. * On SVM, the wait for the IRQ window is implemented with pending vIRQ,
  977. * which cannot be injected when the AVIC is enabled, thus AVIC
  978. * is inhibited while KVM waits for IRQ window.
  979. */
  980. APICV_INHIBIT_REASON_IRQWIN,
  981. /*
  982. * PIT (i8254) 're-inject' mode, relies on EOI intercept,
  983. * which AVIC doesn't support for edge triggered interrupts.
  984. */
  985. APICV_INHIBIT_REASON_PIT_REINJ,
  986. /*
  987. * AVIC is disabled because SEV doesn't support it.
  988. */
  989. APICV_INHIBIT_REASON_SEV,
  990. };
  991. struct kvm_arch {
  992. unsigned long n_used_mmu_pages;
  993. unsigned long n_requested_mmu_pages;
  994. unsigned long n_max_mmu_pages;
  995. unsigned int indirect_shadow_pages;
  996. u8 mmu_valid_gen;
  997. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  998. struct list_head active_mmu_pages;
  999. struct list_head zapped_obsolete_pages;
  1000. struct list_head lpage_disallowed_mmu_pages;
  1001. struct kvm_page_track_notifier_node mmu_sp_tracker;
  1002. struct kvm_page_track_notifier_head track_notifier_head;
  1003. /*
  1004. * Protects marking pages unsync during page faults, as TDP MMU page
  1005. * faults only take mmu_lock for read. For simplicity, the unsync
  1006. * pages lock is always taken when marking pages unsync regardless of
  1007. * whether mmu_lock is held for read or write.
  1008. */
  1009. spinlock_t mmu_unsync_pages_lock;
  1010. struct list_head assigned_dev_head;
  1011. struct iommu_domain *iommu_domain;
  1012. bool iommu_noncoherent;
  1013. #define __KVM_HAVE_ARCH_NONCOHERENT_DMA
  1014. atomic_t noncoherent_dma_count;
  1015. #define __KVM_HAVE_ARCH_ASSIGNED_DEVICE
  1016. atomic_t assigned_device_count;
  1017. struct kvm_pic *vpic;
  1018. struct kvm_ioapic *vioapic;
  1019. struct kvm_pit *vpit;
  1020. atomic_t vapics_in_nmi_mode;
  1021. struct mutex apic_map_lock;
  1022. struct kvm_apic_map __rcu *apic_map;
  1023. atomic_t apic_map_dirty;
  1024. /* Protects apic_access_memslot_enabled and apicv_inhibit_reasons */
  1025. struct rw_semaphore apicv_update_lock;
  1026. bool apic_access_memslot_enabled;
  1027. unsigned long apicv_inhibit_reasons;
  1028. gpa_t wall_clock;
  1029. bool mwait_in_guest;
  1030. bool hlt_in_guest;
  1031. bool pause_in_guest;
  1032. bool cstate_in_guest;
  1033. unsigned long irq_sources_bitmap;
  1034. s64 kvmclock_offset;
  1035. /*
  1036. * This also protects nr_vcpus_matched_tsc which is read from a
  1037. * preemption-disabled region, so it must be a raw spinlock.
  1038. */
  1039. raw_spinlock_t tsc_write_lock;
  1040. u64 last_tsc_nsec;
  1041. u64 last_tsc_write;
  1042. u32 last_tsc_khz;
  1043. u64 last_tsc_offset;
  1044. u64 cur_tsc_nsec;
  1045. u64 cur_tsc_write;
  1046. u64 cur_tsc_offset;
  1047. u64 cur_tsc_generation;
  1048. int nr_vcpus_matched_tsc;
  1049. u32 default_tsc_khz;
  1050. seqcount_raw_spinlock_t pvclock_sc;
  1051. bool use_master_clock;
  1052. u64 master_kernel_ns;
  1053. u64 master_cycle_now;
  1054. struct delayed_work kvmclock_update_work;
  1055. struct delayed_work kvmclock_sync_work;
  1056. struct kvm_xen_hvm_config xen_hvm_config;
  1057. /* reads protected by irq_srcu, writes by irq_lock */
  1058. struct hlist_head mask_notifier_list;
  1059. struct kvm_hv hyperv;
  1060. struct kvm_xen xen;
  1061. bool backwards_tsc_observed;
  1062. bool boot_vcpu_runs_old_kvmclock;
  1063. u32 bsp_vcpu_id;
  1064. u64 disabled_quirks;
  1065. int cpu_dirty_logging_count;
  1066. enum kvm_irqchip_mode irqchip_mode;
  1067. u8 nr_reserved_ioapic_pins;
  1068. bool disabled_lapic_found;
  1069. bool x2apic_format;
  1070. bool x2apic_broadcast_quirk_disabled;
  1071. bool guest_can_read_msr_platform_info;
  1072. bool exception_payload_enabled;
  1073. bool triple_fault_event;
  1074. bool bus_lock_detection_enabled;
  1075. bool enable_pmu;
  1076. u32 notify_window;
  1077. u32 notify_vmexit_flags;
  1078. /*
  1079. * If exit_on_emulation_error is set, and the in-kernel instruction
  1080. * emulator fails to emulate an instruction, allow userspace
  1081. * the opportunity to look at it.
  1082. */
  1083. bool exit_on_emulation_error;
  1084. /* Deflect RDMSR and WRMSR to user space when they trigger a #GP */
  1085. u32 user_space_msr_mask;
  1086. struct kvm_x86_msr_filter __rcu *msr_filter;
  1087. u32 hypercall_exit_enabled;
  1088. /* Guest can access the SGX PROVISIONKEY. */
  1089. bool sgx_provisioning_allowed;
  1090. struct kvm_pmu_event_filter __rcu *pmu_event_filter;
  1091. struct task_struct *nx_lpage_recovery_thread;
  1092. #ifdef CONFIG_X86_64
  1093. /*
  1094. * Whether the TDP MMU is enabled for this VM. This contains a
  1095. * snapshot of the TDP MMU module parameter from when the VM was
  1096. * created and remains unchanged for the life of the VM. If this is
  1097. * true, TDP MMU handler functions will run for various MMU
  1098. * operations.
  1099. */
  1100. bool tdp_mmu_enabled;
  1101. /*
  1102. * List of kvm_mmu_page structs being used as roots.
  1103. * All kvm_mmu_page structs in the list should have
  1104. * tdp_mmu_page set.
  1105. *
  1106. * For reads, this list is protected by:
  1107. * the MMU lock in read mode + RCU or
  1108. * the MMU lock in write mode
  1109. *
  1110. * For writes, this list is protected by:
  1111. * the MMU lock in read mode + the tdp_mmu_pages_lock or
  1112. * the MMU lock in write mode
  1113. *
  1114. * Roots will remain in the list until their tdp_mmu_root_count
  1115. * drops to zero, at which point the thread that decremented the
  1116. * count to zero should removed the root from the list and clean
  1117. * it up, freeing the root after an RCU grace period.
  1118. */
  1119. struct list_head tdp_mmu_roots;
  1120. /*
  1121. * List of kvm_mmu_page structs not being used as roots.
  1122. * All kvm_mmu_page structs in the list should have
  1123. * tdp_mmu_page set and a tdp_mmu_root_count of 0.
  1124. */
  1125. struct list_head tdp_mmu_pages;
  1126. /*
  1127. * Protects accesses to the following fields when the MMU lock
  1128. * is held in read mode:
  1129. * - tdp_mmu_roots (above)
  1130. * - tdp_mmu_pages (above)
  1131. * - the link field of kvm_mmu_page structs used by the TDP MMU
  1132. * - lpage_disallowed_mmu_pages
  1133. * - the lpage_disallowed_link field of kvm_mmu_page structs used
  1134. * by the TDP MMU
  1135. * It is acceptable, but not necessary, to acquire this lock when
  1136. * the thread holds the MMU lock in write mode.
  1137. */
  1138. spinlock_t tdp_mmu_pages_lock;
  1139. #endif /* CONFIG_X86_64 */
  1140. /*
  1141. * If set, at least one shadow root has been allocated. This flag
  1142. * is used as one input when determining whether certain memslot
  1143. * related allocations are necessary.
  1144. */
  1145. bool shadow_root_allocated;
  1146. #if IS_ENABLED(CONFIG_HYPERV)
  1147. hpa_t hv_root_tdp;
  1148. spinlock_t hv_root_tdp_lock;
  1149. #endif
  1150. /*
  1151. * VM-scope maximum vCPU ID. Used to determine the size of structures
  1152. * that increase along with the maximum vCPU ID, in which case, using
  1153. * the global KVM_MAX_VCPU_IDS may lead to significant memory waste.
  1154. */
  1155. u32 max_vcpu_ids;
  1156. bool disable_nx_huge_pages;
  1157. /*
  1158. * Memory caches used to allocate shadow pages when performing eager
  1159. * page splitting. No need for a shadowed_info_cache since eager page
  1160. * splitting only allocates direct shadow pages.
  1161. *
  1162. * Protected by kvm->slots_lock.
  1163. */
  1164. struct kvm_mmu_memory_cache split_shadow_page_cache;
  1165. struct kvm_mmu_memory_cache split_page_header_cache;
  1166. /*
  1167. * Memory cache used to allocate pte_list_desc structs while splitting
  1168. * huge pages. In the worst case, to split one huge page, 512
  1169. * pte_list_desc structs are needed to add each lower level leaf sptep
  1170. * to the rmap plus 1 to extend the parent_ptes rmap of the lower level
  1171. * page table.
  1172. *
  1173. * Protected by kvm->slots_lock.
  1174. */
  1175. #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)
  1176. struct kvm_mmu_memory_cache split_desc_cache;
  1177. };
  1178. struct kvm_vm_stat {
  1179. struct kvm_vm_stat_generic generic;
  1180. u64 mmu_shadow_zapped;
  1181. u64 mmu_pte_write;
  1182. u64 mmu_pde_zapped;
  1183. u64 mmu_flooded;
  1184. u64 mmu_recycled;
  1185. u64 mmu_cache_miss;
  1186. u64 mmu_unsync;
  1187. union {
  1188. struct {
  1189. atomic64_t pages_4k;
  1190. atomic64_t pages_2m;
  1191. atomic64_t pages_1g;
  1192. };
  1193. atomic64_t pages[KVM_NR_PAGE_SIZES];
  1194. };
  1195. u64 nx_lpage_splits;
  1196. u64 max_mmu_page_hash_collisions;
  1197. u64 max_mmu_rmap_size;
  1198. };
  1199. struct kvm_vcpu_stat {
  1200. struct kvm_vcpu_stat_generic generic;
  1201. u64 pf_taken;
  1202. u64 pf_fixed;
  1203. u64 pf_emulate;
  1204. u64 pf_spurious;
  1205. u64 pf_fast;
  1206. u64 pf_mmio_spte_created;
  1207. u64 pf_guest;
  1208. u64 tlb_flush;
  1209. u64 invlpg;
  1210. u64 exits;
  1211. u64 io_exits;
  1212. u64 mmio_exits;
  1213. u64 signal_exits;
  1214. u64 irq_window_exits;
  1215. u64 nmi_window_exits;
  1216. u64 l1d_flush;
  1217. u64 halt_exits;
  1218. u64 request_irq_exits;
  1219. u64 irq_exits;
  1220. u64 host_state_reload;
  1221. u64 fpu_reload;
  1222. u64 insn_emulation;
  1223. u64 insn_emulation_fail;
  1224. u64 hypercalls;
  1225. u64 irq_injections;
  1226. u64 nmi_injections;
  1227. u64 req_event;
  1228. u64 nested_run;
  1229. u64 directed_yield_attempted;
  1230. u64 directed_yield_successful;
  1231. u64 preemption_reported;
  1232. u64 preemption_other;
  1233. u64 guest_mode;
  1234. u64 notify_window_exits;
  1235. };
  1236. struct x86_instruction_info;
  1237. struct msr_data {
  1238. bool host_initiated;
  1239. u32 index;
  1240. u64 data;
  1241. };
  1242. struct kvm_lapic_irq {
  1243. u32 vector;
  1244. u16 delivery_mode;
  1245. u16 dest_mode;
  1246. bool level;
  1247. u16 trig_mode;
  1248. u32 shorthand;
  1249. u32 dest_id;
  1250. bool msi_redir_hint;
  1251. };
  1252. static inline u16 kvm_lapic_irq_dest_mode(bool dest_mode_logical)
  1253. {
  1254. return dest_mode_logical ? APIC_DEST_LOGICAL : APIC_DEST_PHYSICAL;
  1255. }
  1256. struct kvm_x86_ops {
  1257. const char *name;
  1258. int (*hardware_enable)(void);
  1259. void (*hardware_disable)(void);
  1260. void (*hardware_unsetup)(void);
  1261. bool (*has_emulated_msr)(struct kvm *kvm, u32 index);
  1262. void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);
  1263. unsigned int vm_size;
  1264. int (*vm_init)(struct kvm *kvm);
  1265. void (*vm_destroy)(struct kvm *kvm);
  1266. /* Create, but do not attach this VCPU */
  1267. int (*vcpu_precreate)(struct kvm *kvm);
  1268. int (*vcpu_create)(struct kvm_vcpu *vcpu);
  1269. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  1270. void (*vcpu_reset)(struct kvm_vcpu *vcpu, bool init_event);
  1271. void (*prepare_switch_to_guest)(struct kvm_vcpu *vcpu);
  1272. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  1273. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  1274. void (*update_exception_bitmap)(struct kvm_vcpu *vcpu);
  1275. int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
  1276. int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
  1277. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  1278. void (*get_segment)(struct kvm_vcpu *vcpu,
  1279. struct kvm_segment *var, int seg);
  1280. int (*get_cpl)(struct kvm_vcpu *vcpu);
  1281. void (*set_segment)(struct kvm_vcpu *vcpu,
  1282. struct kvm_segment *var, int seg);
  1283. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  1284. bool (*is_valid_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  1285. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  1286. void (*post_set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  1287. bool (*is_valid_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  1288. void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  1289. int (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  1290. void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  1291. void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  1292. void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  1293. void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
  1294. void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);
  1295. void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
  1296. void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
  1297. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  1298. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  1299. bool (*get_if_flag)(struct kvm_vcpu *vcpu);
  1300. void (*flush_tlb_all)(struct kvm_vcpu *vcpu);
  1301. void (*flush_tlb_current)(struct kvm_vcpu *vcpu);
  1302. int (*tlb_remote_flush)(struct kvm *kvm);
  1303. int (*tlb_remote_flush_with_range)(struct kvm *kvm,
  1304. struct kvm_tlb_range *range);
  1305. /*
  1306. * Flush any TLB entries associated with the given GVA.
  1307. * Does not need to flush GPA->HPA mappings.
  1308. * Can potentially get non-canonical addresses through INVLPGs, which
  1309. * the implementation may choose to ignore if appropriate.
  1310. */
  1311. void (*flush_tlb_gva)(struct kvm_vcpu *vcpu, gva_t addr);
  1312. /*
  1313. * Flush any TLB entries created by the guest. Like tlb_flush_gva(),
  1314. * does not need to flush GPA->HPA mappings.
  1315. */
  1316. void (*flush_tlb_guest)(struct kvm_vcpu *vcpu);
  1317. int (*vcpu_pre_run)(struct kvm_vcpu *vcpu);
  1318. enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu);
  1319. int (*handle_exit)(struct kvm_vcpu *vcpu,
  1320. enum exit_fastpath_completion exit_fastpath);
  1321. int (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  1322. void (*update_emulated_instruction)(struct kvm_vcpu *vcpu);
  1323. void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
  1324. u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu);
  1325. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  1326. unsigned char *hypercall_addr);
  1327. void (*inject_irq)(struct kvm_vcpu *vcpu, bool reinjected);
  1328. void (*inject_nmi)(struct kvm_vcpu *vcpu);
  1329. void (*inject_exception)(struct kvm_vcpu *vcpu);
  1330. void (*cancel_injection)(struct kvm_vcpu *vcpu);
  1331. int (*interrupt_allowed)(struct kvm_vcpu *vcpu, bool for_injection);
  1332. int (*nmi_allowed)(struct kvm_vcpu *vcpu, bool for_injection);
  1333. bool (*get_nmi_mask)(struct kvm_vcpu *vcpu);
  1334. void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked);
  1335. void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
  1336. void (*enable_irq_window)(struct kvm_vcpu *vcpu);
  1337. void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
  1338. bool (*check_apicv_inhibit_reasons)(enum kvm_apicv_inhibit reason);
  1339. void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu);
  1340. void (*hwapic_irr_update)(struct kvm_vcpu *vcpu, int max_irr);
  1341. void (*hwapic_isr_update)(int isr);
  1342. bool (*guest_apic_has_interrupt)(struct kvm_vcpu *vcpu);
  1343. void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
  1344. void (*set_virtual_apic_mode)(struct kvm_vcpu *vcpu);
  1345. void (*set_apic_access_page_addr)(struct kvm_vcpu *vcpu);
  1346. void (*deliver_interrupt)(struct kvm_lapic *apic, int delivery_mode,
  1347. int trig_mode, int vector);
  1348. int (*sync_pir_to_irr)(struct kvm_vcpu *vcpu);
  1349. int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
  1350. int (*set_identity_map_addr)(struct kvm *kvm, u64 ident_addr);
  1351. u8 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
  1352. void (*load_mmu_pgd)(struct kvm_vcpu *vcpu, hpa_t root_hpa,
  1353. int root_level);
  1354. bool (*has_wbinvd_exit)(void);
  1355. u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu);
  1356. u64 (*get_l2_tsc_multiplier)(struct kvm_vcpu *vcpu);
  1357. void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
  1358. void (*write_tsc_multiplier)(struct kvm_vcpu *vcpu, u64 multiplier);
  1359. /*
  1360. * Retrieve somewhat arbitrary exit information. Intended to
  1361. * be used only from within tracepoints or error paths.
  1362. */
  1363. void (*get_exit_info)(struct kvm_vcpu *vcpu, u32 *reason,
  1364. u64 *info1, u64 *info2,
  1365. u32 *exit_int_info, u32 *exit_int_info_err_code);
  1366. int (*check_intercept)(struct kvm_vcpu *vcpu,
  1367. struct x86_instruction_info *info,
  1368. enum x86_intercept_stage stage,
  1369. struct x86_exception *exception);
  1370. void (*handle_exit_irqoff)(struct kvm_vcpu *vcpu);
  1371. void (*request_immediate_exit)(struct kvm_vcpu *vcpu);
  1372. void (*sched_in)(struct kvm_vcpu *kvm, int cpu);
  1373. /*
  1374. * Size of the CPU's dirty log buffer, i.e. VMX's PML buffer. A zero
  1375. * value indicates CPU dirty logging is unsupported or disabled.
  1376. */
  1377. int cpu_dirty_log_size;
  1378. void (*update_cpu_dirty_logging)(struct kvm_vcpu *vcpu);
  1379. const struct kvm_x86_nested_ops *nested_ops;
  1380. void (*vcpu_blocking)(struct kvm_vcpu *vcpu);
  1381. void (*vcpu_unblocking)(struct kvm_vcpu *vcpu);
  1382. int (*pi_update_irte)(struct kvm *kvm, unsigned int host_irq,
  1383. uint32_t guest_irq, bool set);
  1384. void (*pi_start_assignment)(struct kvm *kvm);
  1385. void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu);
  1386. void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);
  1387. bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu);
  1388. int (*set_hv_timer)(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
  1389. bool *expired);
  1390. void (*cancel_hv_timer)(struct kvm_vcpu *vcpu);
  1391. void (*setup_mce)(struct kvm_vcpu *vcpu);
  1392. int (*smi_allowed)(struct kvm_vcpu *vcpu, bool for_injection);
  1393. int (*enter_smm)(struct kvm_vcpu *vcpu, char *smstate);
  1394. int (*leave_smm)(struct kvm_vcpu *vcpu, const char *smstate);
  1395. void (*enable_smi_window)(struct kvm_vcpu *vcpu);
  1396. int (*mem_enc_ioctl)(struct kvm *kvm, void __user *argp);
  1397. int (*mem_enc_register_region)(struct kvm *kvm, struct kvm_enc_region *argp);
  1398. int (*mem_enc_unregister_region)(struct kvm *kvm, struct kvm_enc_region *argp);
  1399. int (*vm_copy_enc_context_from)(struct kvm *kvm, unsigned int source_fd);
  1400. int (*vm_move_enc_context_from)(struct kvm *kvm, unsigned int source_fd);
  1401. void (*guest_memory_reclaimed)(struct kvm *kvm);
  1402. int (*get_msr_feature)(struct kvm_msr_entry *entry);
  1403. bool (*can_emulate_instruction)(struct kvm_vcpu *vcpu, int emul_type,
  1404. void *insn, int insn_len);
  1405. bool (*apic_init_signal_blocked)(struct kvm_vcpu *vcpu);
  1406. int (*enable_direct_tlbflush)(struct kvm_vcpu *vcpu);
  1407. void (*migrate_timers)(struct kvm_vcpu *vcpu);
  1408. void (*msr_filter_changed)(struct kvm_vcpu *vcpu);
  1409. int (*complete_emulated_msr)(struct kvm_vcpu *vcpu, int err);
  1410. void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector);
  1411. /*
  1412. * Returns vCPU specific APICv inhibit reasons
  1413. */
  1414. unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
  1415. };
  1416. struct kvm_x86_nested_ops {
  1417. void (*leave_nested)(struct kvm_vcpu *vcpu);
  1418. bool (*is_exception_vmexit)(struct kvm_vcpu *vcpu, u8 vector,
  1419. u32 error_code);
  1420. int (*check_events)(struct kvm_vcpu *vcpu);
  1421. bool (*has_events)(struct kvm_vcpu *vcpu);
  1422. void (*triple_fault)(struct kvm_vcpu *vcpu);
  1423. int (*get_state)(struct kvm_vcpu *vcpu,
  1424. struct kvm_nested_state __user *user_kvm_nested_state,
  1425. unsigned user_data_size);
  1426. int (*set_state)(struct kvm_vcpu *vcpu,
  1427. struct kvm_nested_state __user *user_kvm_nested_state,
  1428. struct kvm_nested_state *kvm_state);
  1429. bool (*get_nested_state_pages)(struct kvm_vcpu *vcpu);
  1430. int (*write_log_dirty)(struct kvm_vcpu *vcpu, gpa_t l2_gpa);
  1431. int (*enable_evmcs)(struct kvm_vcpu *vcpu,
  1432. uint16_t *vmcs_version);
  1433. uint16_t (*get_evmcs_version)(struct kvm_vcpu *vcpu);
  1434. };
  1435. struct kvm_x86_init_ops {
  1436. int (*cpu_has_kvm_support)(void);
  1437. int (*disabled_by_bios)(void);
  1438. int (*check_processor_compatibility)(void);
  1439. int (*hardware_setup)(void);
  1440. unsigned int (*handle_intel_pt_intr)(void);
  1441. struct kvm_x86_ops *runtime_ops;
  1442. struct kvm_pmu_ops *pmu_ops;
  1443. };
  1444. struct kvm_arch_async_pf {
  1445. u32 token;
  1446. gfn_t gfn;
  1447. unsigned long cr3;
  1448. bool direct_map;
  1449. };
  1450. extern u32 __read_mostly kvm_nr_uret_msrs;
  1451. extern u64 __read_mostly host_efer;
  1452. extern bool __read_mostly allow_smaller_maxphyaddr;
  1453. extern bool __read_mostly enable_apicv;
  1454. extern struct kvm_x86_ops kvm_x86_ops;
  1455. #define KVM_X86_OP(func) \
  1456. DECLARE_STATIC_CALL(kvm_x86_##func, *(((struct kvm_x86_ops *)0)->func));
  1457. #define KVM_X86_OP_OPTIONAL KVM_X86_OP
  1458. #define KVM_X86_OP_OPTIONAL_RET0 KVM_X86_OP
  1459. #include <asm/kvm-x86-ops.h>
  1460. int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops);
  1461. void kvm_x86_vendor_exit(void);
  1462. #define __KVM_HAVE_ARCH_VM_ALLOC
  1463. static inline struct kvm *kvm_arch_alloc_vm(void)
  1464. {
  1465. return __vmalloc(kvm_x86_ops.vm_size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
  1466. }
  1467. #define __KVM_HAVE_ARCH_VM_FREE
  1468. void kvm_arch_free_vm(struct kvm *kvm);
  1469. #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLB
  1470. static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
  1471. {
  1472. if (kvm_x86_ops.tlb_remote_flush &&
  1473. !static_call(kvm_x86_tlb_remote_flush)(kvm))
  1474. return 0;
  1475. else
  1476. return -ENOTSUPP;
  1477. }
  1478. #define kvm_arch_pmi_in_guest(vcpu) \
  1479. ((vcpu) && (vcpu)->arch.handling_intr_from_guest)
  1480. void __init kvm_mmu_x86_module_init(void);
  1481. int kvm_mmu_vendor_module_init(void);
  1482. void kvm_mmu_vendor_module_exit(void);
  1483. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  1484. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  1485. void kvm_mmu_init_vm(struct kvm *kvm);
  1486. void kvm_mmu_uninit_vm(struct kvm *kvm);
  1487. void kvm_mmu_after_set_cpuid(struct kvm_vcpu *vcpu);
  1488. void kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  1489. void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
  1490. const struct kvm_memory_slot *memslot,
  1491. int start_level);
  1492. void kvm_mmu_slot_try_split_huge_pages(struct kvm *kvm,
  1493. const struct kvm_memory_slot *memslot,
  1494. int target_level);
  1495. void kvm_mmu_try_split_huge_pages(struct kvm *kvm,
  1496. const struct kvm_memory_slot *memslot,
  1497. u64 start, u64 end,
  1498. int target_level);
  1499. void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
  1500. const struct kvm_memory_slot *memslot);
  1501. void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
  1502. const struct kvm_memory_slot *memslot);
  1503. void kvm_mmu_zap_all(struct kvm *kvm);
  1504. void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen);
  1505. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages);
  1506. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
  1507. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  1508. const void *val, int bytes);
  1509. struct kvm_irq_mask_notifier {
  1510. void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked);
  1511. int irq;
  1512. struct hlist_node link;
  1513. };
  1514. void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
  1515. struct kvm_irq_mask_notifier *kimn);
  1516. void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
  1517. struct kvm_irq_mask_notifier *kimn);
  1518. void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
  1519. bool mask);
  1520. extern bool tdp_enabled;
  1521. u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu);
  1522. /*
  1523. * EMULTYPE_NO_DECODE - Set when re-emulating an instruction (after completing
  1524. * userspace I/O) to indicate that the emulation context
  1525. * should be reused as is, i.e. skip initialization of
  1526. * emulation context, instruction fetch and decode.
  1527. *
  1528. * EMULTYPE_TRAP_UD - Set when emulating an intercepted #UD from hardware.
  1529. * Indicates that only select instructions (tagged with
  1530. * EmulateOnUD) should be emulated (to minimize the emulator
  1531. * attack surface). See also EMULTYPE_TRAP_UD_FORCED.
  1532. *
  1533. * EMULTYPE_SKIP - Set when emulating solely to skip an instruction, i.e. to
  1534. * decode the instruction length. For use *only* by
  1535. * kvm_x86_ops.skip_emulated_instruction() implementations if
  1536. * EMULTYPE_COMPLETE_USER_EXIT is not set.
  1537. *
  1538. * EMULTYPE_ALLOW_RETRY_PF - Set when the emulator should resume the guest to
  1539. * retry native execution under certain conditions,
  1540. * Can only be set in conjunction with EMULTYPE_PF.
  1541. *
  1542. * EMULTYPE_TRAP_UD_FORCED - Set when emulating an intercepted #UD that was
  1543. * triggered by KVM's magic "force emulation" prefix,
  1544. * which is opt in via module param (off by default).
  1545. * Bypasses EmulateOnUD restriction despite emulating
  1546. * due to an intercepted #UD (see EMULTYPE_TRAP_UD).
  1547. * Used to test the full emulator from userspace.
  1548. *
  1549. * EMULTYPE_VMWARE_GP - Set when emulating an intercepted #GP for VMware
  1550. * backdoor emulation, which is opt in via module param.
  1551. * VMware backdoor emulation handles select instructions
  1552. * and reinjects the #GP for all other cases.
  1553. *
  1554. * EMULTYPE_PF - Set when emulating MMIO by way of an intercepted #PF, in which
  1555. * case the CR2/GPA value pass on the stack is valid.
  1556. *
  1557. * EMULTYPE_COMPLETE_USER_EXIT - Set when the emulator should update interruptibility
  1558. * state and inject single-step #DBs after skipping
  1559. * an instruction (after completing userspace I/O).
  1560. */
  1561. #define EMULTYPE_NO_DECODE (1 << 0)
  1562. #define EMULTYPE_TRAP_UD (1 << 1)
  1563. #define EMULTYPE_SKIP (1 << 2)
  1564. #define EMULTYPE_ALLOW_RETRY_PF (1 << 3)
  1565. #define EMULTYPE_TRAP_UD_FORCED (1 << 4)
  1566. #define EMULTYPE_VMWARE_GP (1 << 5)
  1567. #define EMULTYPE_PF (1 << 6)
  1568. #define EMULTYPE_COMPLETE_USER_EXIT (1 << 7)
  1569. int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type);
  1570. int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
  1571. void *insn, int insn_len);
  1572. void __kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu,
  1573. u64 *data, u8 ndata);
  1574. void kvm_prepare_emulation_failure_exit(struct kvm_vcpu *vcpu);
  1575. void kvm_enable_efer_bits(u64);
  1576. bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
  1577. int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, bool host_initiated);
  1578. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data);
  1579. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data);
  1580. int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu);
  1581. int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu);
  1582. int kvm_emulate_as_nop(struct kvm_vcpu *vcpu);
  1583. int kvm_emulate_invd(struct kvm_vcpu *vcpu);
  1584. int kvm_emulate_mwait(struct kvm_vcpu *vcpu);
  1585. int kvm_handle_invalid_op(struct kvm_vcpu *vcpu);
  1586. int kvm_emulate_monitor(struct kvm_vcpu *vcpu);
  1587. int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in);
  1588. int kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
  1589. int kvm_emulate_halt(struct kvm_vcpu *vcpu);
  1590. int kvm_emulate_halt_noskip(struct kvm_vcpu *vcpu);
  1591. int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu);
  1592. int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu);
  1593. void kvm_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
  1594. int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg);
  1595. void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);
  1596. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
  1597. int reason, bool has_error_code, u32 error_code);
  1598. void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned long cr0);
  1599. void kvm_post_set_cr4(struct kvm_vcpu *vcpu, unsigned long old_cr4, unsigned long cr4);
  1600. int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  1601. int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
  1602. int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  1603. int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8);
  1604. int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val);
  1605. void kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val);
  1606. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);
  1607. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  1608. int kvm_emulate_xsetbv(struct kvm_vcpu *vcpu);
  1609. int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr);
  1610. int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr);
  1611. unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu);
  1612. void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
  1613. int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu);
  1614. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
  1615. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
  1616. void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, unsigned long payload);
  1617. void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr);
  1618. void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
  1619. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
  1620. void kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
  1621. struct x86_exception *fault);
  1622. bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl);
  1623. bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr);
  1624. static inline int __kvm_irq_line_state(unsigned long *irq_state,
  1625. int irq_source_id, int level)
  1626. {
  1627. /* Logical OR for level trig interrupt */
  1628. if (level)
  1629. __set_bit(irq_source_id, irq_state);
  1630. else
  1631. __clear_bit(irq_source_id, irq_state);
  1632. return !!(*irq_state);
  1633. }
  1634. #define KVM_MMU_ROOT_CURRENT BIT(0)
  1635. #define KVM_MMU_ROOT_PREVIOUS(i) BIT(1+i)
  1636. #define KVM_MMU_ROOTS_ALL (~0UL)
  1637. int kvm_pic_set_irq(struct kvm_pic *pic, int irq, int irq_source_id, int level);
  1638. void kvm_pic_clear_all(struct kvm_pic *pic, int irq_source_id);
  1639. void kvm_inject_nmi(struct kvm_vcpu *vcpu);
  1640. void kvm_update_dr7(struct kvm_vcpu *vcpu);
  1641. int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn);
  1642. void kvm_mmu_free_roots(struct kvm *kvm, struct kvm_mmu *mmu,
  1643. ulong roots_to_free);
  1644. void kvm_mmu_free_guest_mode_roots(struct kvm *kvm, struct kvm_mmu *mmu);
  1645. gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
  1646. struct x86_exception *exception);
  1647. gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
  1648. struct x86_exception *exception);
  1649. gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
  1650. struct x86_exception *exception);
  1651. gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
  1652. struct x86_exception *exception);
  1653. bool kvm_apicv_activated(struct kvm *kvm);
  1654. bool kvm_vcpu_apicv_activated(struct kvm_vcpu *vcpu);
  1655. void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu);
  1656. void __kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,
  1657. enum kvm_apicv_inhibit reason, bool set);
  1658. void kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,
  1659. enum kvm_apicv_inhibit reason, bool set);
  1660. static inline void kvm_set_apicv_inhibit(struct kvm *kvm,
  1661. enum kvm_apicv_inhibit reason)
  1662. {
  1663. kvm_set_or_clear_apicv_inhibit(kvm, reason, true);
  1664. }
  1665. static inline void kvm_clear_apicv_inhibit(struct kvm *kvm,
  1666. enum kvm_apicv_inhibit reason)
  1667. {
  1668. kvm_set_or_clear_apicv_inhibit(kvm, reason, false);
  1669. }
  1670. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
  1671. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
  1672. void *insn, int insn_len);
  1673. void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
  1674. void kvm_mmu_invalidate_gva(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
  1675. gva_t gva, hpa_t root_hpa);
  1676. void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid);
  1677. void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd);
  1678. void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level,
  1679. int tdp_max_root_level, int tdp_huge_page_level);
  1680. static inline u16 kvm_read_ldt(void)
  1681. {
  1682. u16 ldt;
  1683. asm("sldt %0" : "=g"(ldt));
  1684. return ldt;
  1685. }
  1686. static inline void kvm_load_ldt(u16 sel)
  1687. {
  1688. asm("lldt %0" : : "rm"(sel));
  1689. }
  1690. #ifdef CONFIG_X86_64
  1691. static inline unsigned long read_msr(unsigned long msr)
  1692. {
  1693. u64 value;
  1694. rdmsrl(msr, value);
  1695. return value;
  1696. }
  1697. #endif
  1698. static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
  1699. {
  1700. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  1701. }
  1702. #define TSS_IOPB_BASE_OFFSET 0x66
  1703. #define TSS_BASE_SIZE 0x68
  1704. #define TSS_IOPB_SIZE (65536 / 8)
  1705. #define TSS_REDIRECTION_SIZE (256 / 8)
  1706. #define RMODE_TSS_SIZE \
  1707. (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  1708. enum {
  1709. TASK_SWITCH_CALL = 0,
  1710. TASK_SWITCH_IRET = 1,
  1711. TASK_SWITCH_JMP = 2,
  1712. TASK_SWITCH_GATE = 3,
  1713. };
  1714. #define HF_GIF_MASK (1 << 0)
  1715. #define HF_NMI_MASK (1 << 3)
  1716. #define HF_IRET_MASK (1 << 4)
  1717. #define HF_GUEST_MASK (1 << 5) /* VCPU is in guest-mode */
  1718. #define HF_SMM_MASK (1 << 6)
  1719. #define HF_SMM_INSIDE_NMI_MASK (1 << 7)
  1720. #define __KVM_VCPU_MULTIPLE_ADDRESS_SPACE
  1721. #define KVM_ADDRESS_SPACE_NUM 2
  1722. #define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)
  1723. #define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm)
  1724. #define KVM_ARCH_WANT_MMU_NOTIFIER
  1725. int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v);
  1726. int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu);
  1727. int kvm_cpu_has_extint(struct kvm_vcpu *v);
  1728. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu);
  1729. int kvm_cpu_get_interrupt(struct kvm_vcpu *v);
  1730. void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event);
  1731. int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low,
  1732. unsigned long ipi_bitmap_high, u32 min,
  1733. unsigned long icr, int op_64_bit);
  1734. int kvm_add_user_return_msr(u32 msr);
  1735. int kvm_find_user_return_msr(u32 msr);
  1736. int kvm_set_user_return_msr(unsigned index, u64 val, u64 mask);
  1737. static inline bool kvm_is_supported_user_return_msr(u32 msr)
  1738. {
  1739. return kvm_find_user_return_msr(msr) >= 0;
  1740. }
  1741. u64 kvm_scale_tsc(u64 tsc, u64 ratio);
  1742. u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc);
  1743. u64 kvm_calc_nested_tsc_offset(u64 l1_offset, u64 l2_offset, u64 l2_multiplier);
  1744. u64 kvm_calc_nested_tsc_multiplier(u64 l1_multiplier, u64 l2_multiplier);
  1745. unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu);
  1746. bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip);
  1747. void kvm_make_scan_ioapic_request(struct kvm *kvm);
  1748. void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
  1749. unsigned long *vcpu_bitmap);
  1750. bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
  1751. struct kvm_async_pf *work);
  1752. void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
  1753. struct kvm_async_pf *work);
  1754. void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
  1755. struct kvm_async_pf *work);
  1756. void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu);
  1757. bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu);
  1758. extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);
  1759. int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu);
  1760. int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err);
  1761. void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu);
  1762. void __user *__x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
  1763. u32 size);
  1764. bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu);
  1765. bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu);
  1766. bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
  1767. struct kvm_vcpu **dest_vcpu);
  1768. void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
  1769. struct kvm_lapic_irq *irq);
  1770. static inline bool kvm_irq_is_postable(struct kvm_lapic_irq *irq)
  1771. {
  1772. /* We can only post Fixed and LowPrio IRQs */
  1773. return (irq->delivery_mode == APIC_DM_FIXED ||
  1774. irq->delivery_mode == APIC_DM_LOWEST);
  1775. }
  1776. static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu)
  1777. {
  1778. static_call_cond(kvm_x86_vcpu_blocking)(vcpu);
  1779. }
  1780. static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu)
  1781. {
  1782. static_call_cond(kvm_x86_vcpu_unblocking)(vcpu);
  1783. }
  1784. static inline int kvm_cpu_get_apicid(int mps_cpu)
  1785. {
  1786. #ifdef CONFIG_X86_LOCAL_APIC
  1787. return default_cpu_present_to_apicid(mps_cpu);
  1788. #else
  1789. WARN_ON_ONCE(1);
  1790. return BAD_APICID;
  1791. #endif
  1792. }
  1793. #define put_smstate(type, buf, offset, val) \
  1794. *(type *)((buf) + (offset) - 0x7e00) = val
  1795. #define GET_SMSTATE(type, buf, offset) \
  1796. (*(type *)((buf) + (offset) - 0x7e00))
  1797. int kvm_cpu_dirty_log_size(void);
  1798. int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages);
  1799. #define KVM_CLOCK_VALID_FLAGS \
  1800. (KVM_CLOCK_TSC_STABLE | KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC)
  1801. #define KVM_X86_VALID_QUIRKS \
  1802. (KVM_X86_QUIRK_LINT0_REENABLED | \
  1803. KVM_X86_QUIRK_CD_NW_CLEARED | \
  1804. KVM_X86_QUIRK_LAPIC_MMIO_HOLE | \
  1805. KVM_X86_QUIRK_OUT_7E_INC_RIP | \
  1806. KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT | \
  1807. KVM_X86_QUIRK_FIX_HYPERCALL_INSN | \
  1808. KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS)
  1809. #endif /* _ASM_X86_KVM_HOST_H */