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 8 #ifndef _ASM_X86_KVM_HOST_H 9 #define _ASM_X86_KVM_HOST_H 10 11 #include <linux/types.h> 12 #include <linux/mm.h> 13 #include <linux/mmu_notifier.h> 14 #include <linux/tracepoint.h> 15 #include <linux/cpumask.h> 16 #include <linux/irq_work.h> 17 #include <linux/irq.h> 18 #include <linux/workqueue.h> 19 20 #include <linux/kvm.h> 21 #include <linux/kvm_para.h> 22 #include <linux/kvm_types.h> 23 #include <linux/perf_event.h> 24 #include <linux/pvclock_gtod.h> 25 #include <linux/clocksource.h> 26 #include <linux/irqbypass.h> 27 #include <linux/kfifo.h> 28 #include <linux/sched/vhost_task.h> 29 #include <linux/call_once.h> 30 #include <linux/atomic.h> 31 32 #include <asm/apic.h> 33 #include <asm/pvclock-abi.h> 34 #include <asm/debugreg.h> 35 #include <asm/desc.h> 36 #include <asm/mtrr.h> 37 #include <asm/msr-index.h> 38 #include <asm/msr.h> 39 #include <asm/asm.h> 40 #include <asm/irq_remapping.h> 41 #include <asm/kvm_page_track.h> 42 #include <asm/kvm_vcpu_regs.h> 43 #include <asm/virt.h> 44 45 #include <hyperv/hvhdk.h> 46 47 #define __KVM_HAVE_ARCH_VCPU_DEBUGFS 48 49 /* 50 * CONFIG_KVM_MAX_NR_VCPUS is defined iff CONFIG_KVM!=n, provide a dummy max if 51 * KVM is disabled (arbitrarily use the default from CONFIG_KVM_MAX_NR_VCPUS). 52 */ 53 #ifdef CONFIG_KVM_MAX_NR_VCPUS 54 #define KVM_MAX_VCPUS CONFIG_KVM_MAX_NR_VCPUS 55 #else 56 #define KVM_MAX_VCPUS 1024 57 #endif 58 59 /* 60 * In x86, the VCPU ID corresponds to the APIC ID, and APIC IDs 61 * might be larger than the actual number of VCPUs because the 62 * APIC ID encodes CPU topology information. 63 * 64 * In the worst case, we'll need less than one extra bit for the 65 * Core ID, and less than one extra bit for the Package (Die) ID, 66 * so ratio of 4 should be enough. 67 */ 68 #define KVM_VCPU_ID_RATIO 4 69 #define KVM_MAX_VCPU_IDS (KVM_MAX_VCPUS * KVM_VCPU_ID_RATIO) 70 71 /* memory slots that are not exposed to userspace */ 72 #define KVM_INTERNAL_MEM_SLOTS 3 73 74 #define KVM_HALT_POLL_NS_DEFAULT 200000 75 76 #define KVM_IRQCHIP_NUM_PINS KVM_IOAPIC_NUM_PINS 77 78 #define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ 79 KVM_DIRTY_LOG_INITIALLY_SET) 80 81 /* x86-specific vcpu->requests bit members */ 82 #define KVM_REQ_MIGRATE_TIMER KVM_ARCH_REQ(0) 83 #define KVM_REQ_REPORT_TPR_ACCESS KVM_ARCH_REQ(1) 84 #define KVM_REQ_TRIPLE_FAULT KVM_ARCH_REQ(2) 85 #define KVM_REQ_MMU_SYNC KVM_ARCH_REQ(3) 86 #define KVM_REQ_CLOCK_UPDATE KVM_ARCH_REQ(4) 87 #define KVM_REQ_LOAD_MMU_PGD KVM_ARCH_REQ(5) 88 #define KVM_REQ_EVENT KVM_ARCH_REQ(6) 89 #define KVM_REQ_APF_HALT KVM_ARCH_REQ(7) 90 #define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(8) 91 #define KVM_REQ_NMI KVM_ARCH_REQ(9) 92 #define KVM_REQ_PMU KVM_ARCH_REQ(10) 93 #define KVM_REQ_PMI KVM_ARCH_REQ(11) 94 #ifdef CONFIG_KVM_SMM 95 #define KVM_REQ_SMI KVM_ARCH_REQ(12) 96 #endif 97 #define KVM_REQ_MASTERCLOCK_UPDATE KVM_ARCH_REQ(13) 98 #define KVM_REQ_MCLOCK_INPROGRESS \ 99 KVM_ARCH_REQ_FLAGS(14, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 100 #define KVM_REQ_SCAN_IOAPIC \ 101 KVM_ARCH_REQ_FLAGS(15, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 102 #define KVM_REQ_GLOBAL_CLOCK_UPDATE KVM_ARCH_REQ(16) 103 #define KVM_REQ_APIC_PAGE_RELOAD \ 104 KVM_ARCH_REQ_FLAGS(17, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 105 #define KVM_REQ_HV_CRASH KVM_ARCH_REQ(18) 106 #define KVM_REQ_IOAPIC_EOI_EXIT KVM_ARCH_REQ(19) 107 #define KVM_REQ_HV_RESET KVM_ARCH_REQ(20) 108 #define KVM_REQ_HV_EXIT KVM_ARCH_REQ(21) 109 #define KVM_REQ_HV_STIMER KVM_ARCH_REQ(22) 110 #define KVM_REQ_LOAD_EOI_EXITMAP KVM_ARCH_REQ(23) 111 #define KVM_REQ_GET_NESTED_STATE_PAGES KVM_ARCH_REQ(24) 112 #define KVM_REQ_APICV_UPDATE \ 113 KVM_ARCH_REQ_FLAGS(25, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 114 #define KVM_REQ_TLB_FLUSH_CURRENT KVM_ARCH_REQ(26) 115 #define KVM_REQ_TLB_FLUSH_GUEST \ 116 KVM_ARCH_REQ_FLAGS(27, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 117 #define KVM_REQ_APF_READY KVM_ARCH_REQ(28) 118 #define KVM_REQ_RECALC_INTERCEPTS KVM_ARCH_REQ(29) 119 #define KVM_REQ_UPDATE_CPU_DIRTY_LOGGING \ 120 KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 121 #define KVM_REQ_MMU_FREE_OBSOLETE_ROOTS \ 122 KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 123 #define KVM_REQ_HV_TLB_FLUSH \ 124 KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 125 #define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE \ 126 KVM_ARCH_REQ_FLAGS(34, KVM_REQUEST_WAIT) 127 128 #define CR0_RESERVED_BITS \ 129 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ 130 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \ 131 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG)) 132 133 #define CR4_RESERVED_BITS \ 134 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\ 135 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \ 136 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \ 137 | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \ 138 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \ 139 | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP \ 140 | X86_CR4_LAM_SUP | X86_CR4_CET)) 141 142 #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR) 143 144 145 146 #define INVALID_PAGE (~(hpa_t)0) 147 #define VALID_PAGE(x) ((x) != INVALID_PAGE) 148 149 /* KVM Hugepage definitions for x86 */ 150 #define KVM_MAX_HUGEPAGE_LEVEL PG_LEVEL_1G 151 #define KVM_NR_PAGE_SIZES (KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1) 152 #define KVM_HPAGE_GFN_SHIFT(x) (((x) - 1) * 9) 153 #define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x)) 154 #define KVM_HPAGE_SIZE(x) (1UL << KVM_HPAGE_SHIFT(x)) 155 #define KVM_HPAGE_MASK(x) (~(KVM_HPAGE_SIZE(x) - 1)) 156 #define KVM_PAGES_PER_HPAGE(x) (KVM_HPAGE_SIZE(x) / PAGE_SIZE) 157 158 #define KVM_MAX_CPUID_ENTRIES 256 159 #define KVM_NR_VAR_MTRR 8 160 161 #define ASYNC_PF_PER_VCPU 64 162 163 enum kvm_reg { 164 VCPU_REGS_RAX = __VCPU_REGS_RAX, 165 VCPU_REGS_RCX = __VCPU_REGS_RCX, 166 VCPU_REGS_RDX = __VCPU_REGS_RDX, 167 VCPU_REGS_RBX = __VCPU_REGS_RBX, 168 VCPU_REGS_RSP = __VCPU_REGS_RSP, 169 VCPU_REGS_RBP = __VCPU_REGS_RBP, 170 VCPU_REGS_RSI = __VCPU_REGS_RSI, 171 VCPU_REGS_RDI = __VCPU_REGS_RDI, 172 #ifdef CONFIG_X86_64 173 VCPU_REGS_R8 = 8, 174 VCPU_REGS_R9, 175 VCPU_REGS_R10, 176 VCPU_REGS_R11, 177 VCPU_REGS_R12, 178 VCPU_REGS_R13, 179 VCPU_REGS_R14, 180 VCPU_REGS_R15, 181 #endif 182 NR_VCPU_GENERAL_PURPOSE_REGS, 183 184 VCPU_REG_RIP = NR_VCPU_GENERAL_PURPOSE_REGS, 185 186 VCPU_REG_PDPTR, 187 VCPU_REG_CR0, 188 /* 189 * Alias AMD's ERAPS (not a real register) to CR3 so that common code 190 * can trigger emulation of the RAP (Return Address Predictor) with 191 * minimal support required in common code. Piggyback CR3 as the RAP 192 * is cleared on writes to CR3, i.e. marking CR3 dirty will naturally 193 * mark ERAPS dirty as well. 194 */ 195 VCPU_REG_CR3, 196 VCPU_REG_ERAPS = VCPU_REG_CR3, 197 VCPU_REG_CR4, 198 VCPU_REG_RFLAGS, 199 VCPU_REG_SEGMENTS, 200 VCPU_REG_EXIT_INFO_1, 201 VCPU_REG_EXIT_INFO_2, 202 203 NR_VCPU_TOTAL_REGS, 204 }; 205 206 enum { 207 VCPU_SREG_ES, 208 VCPU_SREG_CS, 209 VCPU_SREG_SS, 210 VCPU_SREG_DS, 211 VCPU_SREG_FS, 212 VCPU_SREG_GS, 213 VCPU_SREG_TR, 214 VCPU_SREG_LDTR, 215 }; 216 217 enum exit_fastpath_completion { 218 EXIT_FASTPATH_NONE, 219 EXIT_FASTPATH_REENTER_GUEST, 220 EXIT_FASTPATH_EXIT_HANDLED, 221 EXIT_FASTPATH_EXIT_USERSPACE, 222 }; 223 typedef enum exit_fastpath_completion fastpath_t; 224 225 struct x86_emulate_ctxt; 226 struct x86_exception; 227 union kvm_smram; 228 enum x86_intercept; 229 enum x86_intercept_stage; 230 231 #define KVM_NR_DB_REGS 4 232 233 #define DR6_BUS_LOCK (1 << 11) 234 #define DR6_BD (1 << 13) 235 #define DR6_BS (1 << 14) 236 #define DR6_BT (1 << 15) 237 #define DR6_RTM (1 << 16) 238 /* 239 * DR6_ACTIVE_LOW combines fixed-1 and active-low bits. 240 * We can regard all the bits in DR6_FIXED_1 as active_low bits; 241 * they will never be 0 for now, but when they are defined 242 * in the future it will require no code change. 243 * 244 * DR6_ACTIVE_LOW is also used as the init/reset value for DR6. 245 */ 246 #define DR6_ACTIVE_LOW 0xffff0ff0 247 #define DR6_VOLATILE 0x0001e80f 248 #define DR6_FIXED_1 (DR6_ACTIVE_LOW & ~DR6_VOLATILE) 249 250 #define DR7_BP_EN_MASK 0x000000ff 251 #define DR7_GE (1 << 9) 252 #define DR7_GD (1 << 13) 253 #define DR7_VOLATILE 0xffff2bff 254 255 #define KVM_GUESTDBG_VALID_MASK \ 256 (KVM_GUESTDBG_ENABLE | \ 257 KVM_GUESTDBG_SINGLESTEP | \ 258 KVM_GUESTDBG_USE_HW_BP | \ 259 KVM_GUESTDBG_USE_SW_BP | \ 260 KVM_GUESTDBG_INJECT_BP | \ 261 KVM_GUESTDBG_INJECT_DB | \ 262 KVM_GUESTDBG_BLOCKIRQ) 263 264 #define PFERR_PRESENT_MASK BIT(0) 265 #define PFERR_WRITE_MASK BIT(1) 266 #define PFERR_USER_MASK BIT(2) 267 #define PFERR_RSVD_MASK BIT(3) 268 #define PFERR_FETCH_MASK BIT(4) 269 #define PFERR_PK_MASK BIT(5) 270 #define PFERR_SS_MASK BIT(6) 271 #define PFERR_SGX_MASK BIT(15) 272 #define PFERR_GUEST_RMP_MASK BIT_ULL(31) 273 #define PFERR_GUEST_FINAL_MASK BIT_ULL(32) 274 #define PFERR_GUEST_PAGE_MASK BIT_ULL(33) 275 #define PFERR_GUEST_FAULT_STAGE_MASK \ 276 (PFERR_GUEST_FINAL_MASK | PFERR_GUEST_PAGE_MASK) 277 #define PFERR_GUEST_ENC_MASK BIT_ULL(34) 278 #define PFERR_GUEST_SIZEM_MASK BIT_ULL(35) 279 #define PFERR_GUEST_VMPL_MASK BIT_ULL(36) 280 281 /* 282 * IMPLICIT_ACCESS is a KVM-defined flag used to correctly perform SMAP checks 283 * when emulating instructions that triggers implicit access. 284 */ 285 #define PFERR_IMPLICIT_ACCESS BIT_ULL(48) 286 /* 287 * PRIVATE_ACCESS is a KVM-defined flag us to indicate that a fault occurred 288 * when the guest was accessing private memory. 289 */ 290 #define PFERR_PRIVATE_ACCESS BIT_ULL(49) 291 #define PFERR_SYNTHETIC_MASK (PFERR_IMPLICIT_ACCESS | PFERR_PRIVATE_ACCESS) 292 293 /* apic attention bits */ 294 #define KVM_APIC_CHECK_VAPIC 0 295 /* 296 * The following bit is set with PV-EOI, unset on EOI. 297 * We detect PV-EOI changes by guest by comparing 298 * this bit with PV-EOI in guest memory. 299 * See the implementation in apic_update_pv_eoi. 300 */ 301 #define KVM_APIC_PV_EOI_PENDING 1 302 303 struct kvm_kernel_irqfd; 304 struct kvm_kernel_irq_routing_entry; 305 306 struct kvm_x86_msr_filter; 307 struct kvm_x86_pmu_event_filter; 308 309 struct kvm_caps { 310 /* control of guest tsc rate supported? */ 311 bool has_tsc_control; 312 /* maximum supported tsc_khz for guests */ 313 u32 max_guest_tsc_khz; 314 /* number of bits of the fractional part of the TSC scaling ratio */ 315 u8 tsc_scaling_ratio_frac_bits; 316 /* maximum allowed value of TSC scaling ratio */ 317 u64 max_tsc_scaling_ratio; 318 /* 1ull << kvm_caps.tsc_scaling_ratio_frac_bits */ 319 u64 default_tsc_scaling_ratio; 320 /* bus lock detection supported? */ 321 bool has_bus_lock_exit; 322 /* notify VM exit supported? */ 323 bool has_notify_vmexit; 324 /* bit mask of VM types */ 325 u32 supported_vm_types; 326 327 u64 supported_mce_cap; 328 u64 supported_xcr0; 329 u64 supported_xss; 330 u64 supported_perf_cap; 331 332 u64 supported_quirks; 333 u64 inapplicable_quirks; 334 }; 335 extern struct kvm_caps kvm_caps; 336 337 struct kvm_host_values { 338 /* 339 * The host's raw MAXPHYADDR, i.e. the number of non-reserved physical 340 * address bits irrespective of features that repurpose legal bits, 341 * e.g. MKTME. 342 */ 343 u8 maxphyaddr; 344 345 u64 efer; 346 u64 xcr0; 347 u64 xss; 348 u64 s_cet; 349 u64 arch_capabilities; 350 }; 351 extern struct kvm_host_values kvm_host; 352 353 /* 354 * kvm_mmu_page_role tracks the properties of a shadow page (where shadow page 355 * also includes TDP pages) to determine whether or not a page can be used in 356 * the given MMU context. This is a subset of the overall kvm_cpu_role to 357 * minimize the size of kvm_memory_slot.arch.gfn_write_track, i.e. allows 358 * allocating 2 bytes per gfn instead of 4 bytes per gfn. 359 * 360 * Upper-level shadow pages having gptes are tracked for write-protection via 361 * gfn_write_track. As above, gfn_write_track is a 16 bit counter, so KVM must 362 * not create more than 2^16-1 upper-level shadow pages at a single gfn, 363 * otherwise gfn_write_track will overflow and explosions will ensue. 364 * 365 * A unique shadow page (SP) for a gfn is created if and only if an existing SP 366 * cannot be reused. The ability to reuse a SP is tracked by its role, which 367 * incorporates various mode bits and properties of the SP. Roughly speaking, 368 * the number of unique SPs that can theoretically be created is 2^n, where n 369 * is the number of bits that are used to compute the role. 370 * 371 * But, even though there are 21 bits in the mask below, not all combinations 372 * of modes and flags are possible: 373 * 374 * - invalid shadow pages are not accounted, mirror pages are not shadowed, 375 * so the bits are effectively 19. 376 * 377 * - quadrant will only be used if has_4_byte_gpte=1 (non-PAE paging); 378 * execonly and ad_disabled are only used for nested EPT which has 379 * has_4_byte_gpte=0. Therefore, 2 bits are always unused. 380 * 381 * - the 4 bits of level are effectively limited to the values 2/3/4/5, 382 * as 4k SPs are not tracked (allowed to go unsync). In addition non-PAE 383 * paging has exactly one upper level, making level completely redundant 384 * when has_4_byte_gpte=1. 385 * 386 * - on top of this, smap_andnot_wp is only set if cr0_wp=0, 387 * therefore these two bits only give rise to 3 possibilities. 388 * 389 * Therefore, the maximum number of possible upper-level shadow pages for a 390 * single gfn is a bit less than 2^14. 391 */ 392 union kvm_mmu_page_role { 393 u32 word; 394 struct { 395 unsigned level:4; 396 unsigned has_4_byte_gpte:1; 397 unsigned quadrant:2; 398 unsigned direct:1; 399 unsigned access:4; 400 unsigned invalid:1; 401 unsigned efer_nx:1; 402 unsigned cr0_wp:1; 403 unsigned smap_andnot_wp:1; 404 unsigned ad_disabled:1; 405 unsigned guest_mode:1; 406 unsigned passthrough:1; 407 unsigned is_mirror:1; 408 409 /* 410 * cr4_smep is also set for EPT MBEC. Because it affects 411 * which pages are considered non-present (bit 10 additionally 412 * must be zero if MBEC is on) it has to be in the base role. 413 * It also has to be in the base role for AMD GMET because 414 * kernel-executable pages need to have U=0 with GMET enabled. 415 */ 416 unsigned cr4_smep:1; 417 418 unsigned:3; 419 420 /* 421 * This is left at the top of the word so that 422 * kvm_memslots_for_spte_role can extract it with a 423 * simple shift. While there is room, give it a whole 424 * byte so it is also faster to load it from memory. 425 */ 426 unsigned smm:8; 427 }; 428 }; 429 430 /* 431 * kvm_mmu_extended_role complements kvm_mmu_page_role, tracking properties 432 * relevant to the current MMU configuration. When loading CR0, CR4, or EFER, 433 * including on nested transitions, if nothing in the full role changes then 434 * MMU re-configuration can be skipped. @valid bit is set on first usage so we 435 * don't treat all-zero structure as valid data. 436 * 437 * The properties that are tracked in the extended role but not the page role 438 * are for things that either (a) do not affect the validity of the shadow page 439 * or (b) are indirectly reflected in the shadow page's role. For example, 440 * CR4.PKE only affects permission checks for software walks of the guest page 441 * tables (because KVM doesn't support Protection Keys with shadow paging), and 442 * CR0.PG, CR4.PAE, and CR4.PSE are indirectly reflected in role.level. 443 * 444 * Note, SMAP is not redundant with smap_andnot_wp in the page role. If 445 * CR0.WP=1, KVM can reuse shadow pages for the guest regardless of SMAP, 446 * but the MMU's permission checks for software walks need to be SMAP 447 * aware regardless of CR0.WP. 448 */ 449 union kvm_mmu_extended_role { 450 u32 word; 451 struct { 452 unsigned int valid:1; 453 unsigned int execonly:1; 454 unsigned int cr4_pse:1; 455 unsigned int cr4_pke:1; 456 unsigned int cr4_smap:1; 457 unsigned int cr4_la57:1; 458 unsigned int efer_lma:1; 459 460 /* 461 * True if either CR4.SMEP or EFER.NXE are set. For AMD NPT 462 * this is the "real" host CR4.SMEP whereas cr4_smep is 463 * actually GMET. 464 */ 465 unsigned int has_pferr_fetch:1; 466 }; 467 }; 468 469 union kvm_cpu_role { 470 u64 as_u64; 471 struct { 472 union kvm_mmu_page_role base; 473 union kvm_mmu_extended_role ext; 474 }; 475 }; 476 477 struct kvm_rmap_head { 478 atomic_long_t val; 479 }; 480 481 struct kvm_pio_request { 482 unsigned long count; 483 int in; 484 int port; 485 int size; 486 }; 487 488 #define PT64_ROOT_MAX_LEVEL 5 489 490 struct kvm_page_format { 491 u64 rsvd_bits_mask[2][PT64_ROOT_MAX_LEVEL]; 492 u64 bad_mt_xwr; 493 494 /* 495 * The pkru_mask indicates if protection key checks are needed. It 496 * consists of 16 domains indexed by page fault error code bits [4:1], 497 * with PFEC.RSVD replaced by ACC_USER_MASK from the page tables. 498 * Each domain has 2 bits which are ANDed with AD and WD from PKRU. 499 */ 500 u32 pkru_mask; 501 502 /* 503 * Bitmap; bit set = permission fault 504 * Array index: page fault error code [4:1] 505 * Bit index: pte permissions in ACC_* format 506 */ 507 u16 permissions[16]; 508 }; 509 510 struct kvm_mmu_root_info { 511 gpa_t pgd; 512 hpa_t hpa; 513 }; 514 515 #define KVM_MMU_ROOT_INFO_INVALID \ 516 ((struct kvm_mmu_root_info) { .pgd = INVALID_PAGE, .hpa = INVALID_PAGE }) 517 518 #define KVM_MMU_NUM_PREV_ROOTS 3 519 520 #define KVM_MMU_ROOT_CURRENT BIT(0) 521 #define KVM_MMU_ROOT_PREVIOUS(i) BIT(1+i) 522 #define KVM_MMU_ROOTS_ALL (BIT(1 + KVM_MMU_NUM_PREV_ROOTS) - 1) 523 524 #define KVM_HAVE_MMU_RWLOCK 525 526 struct kvm_mmu_page; 527 struct kvm_page_fault; 528 529 /* 530 * x86 supports 4 paging modes (5-level 64-bit, 4-level 64-bit, 3-level 32-bit, 531 * and 2-level 32-bit). The kvm_pagewalk structure abstracts the details of the 532 * current mmu mode. 533 */ 534 struct kvm_pagewalk { 535 unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu); 536 u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index); 537 void (*inject_page_fault)(struct kvm_vcpu *vcpu, 538 struct x86_exception *fault, 539 bool from_hardware); 540 gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w, 541 gpa_t gva_or_gpa, u64 access, 542 struct x86_exception *exception); 543 544 union kvm_cpu_role cpu_role; 545 struct kvm_page_format fmt; 546 }; 547 548 struct kvm_mmu { 549 int (*page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault); 550 int (*sync_spte)(struct kvm_vcpu *vcpu, 551 struct kvm_mmu_page *sp, int i); 552 struct kvm_pagewalk *w; 553 554 struct kvm_mmu_root_info root; 555 hpa_t mirror_root_hpa; 556 union kvm_mmu_page_role root_role; 557 558 struct kvm_mmu_root_info prev_roots[KVM_MMU_NUM_PREV_ROOTS]; 559 560 u64 *pae_root; 561 u64 *pml4_root; 562 u64 *pml5_root; 563 564 /* 565 * check zero bits on shadow page table entries, these 566 * bits include not only hardware reserved bits but also 567 * the bits spte never used. 568 */ 569 struct kvm_page_format fmt; 570 }; 571 572 enum pmc_type { 573 KVM_PMC_GP = 0, 574 KVM_PMC_FIXED, 575 }; 576 577 struct kvm_pmc { 578 enum pmc_type type; 579 u8 idx; 580 bool is_paused; 581 bool intr; 582 /* 583 * Base value of the PMC counter, relative to the *consumed* count in 584 * the associated perf_event. This value includes counter updates from 585 * the perf_event and emulated_count since the last time the counter 586 * was reprogrammed, but it is *not* the current value as seen by the 587 * guest or userspace. 588 * 589 * The count is relative to the associated perf_event so that KVM 590 * doesn't need to reprogram the perf_event every time the guest writes 591 * to the counter. 592 */ 593 u64 counter; 594 /* 595 * PMC events triggered by KVM emulation that haven't been fully 596 * processed, i.e. haven't undergone overflow detection. 597 */ 598 u64 emulated_counter; 599 u64 eventsel; 600 u64 eventsel_hw; 601 struct perf_event *perf_event; 602 struct kvm_vcpu *vcpu; 603 /* 604 * only for creating or reusing perf_event, 605 * eventsel value for general purpose counters, 606 * ctrl value for fixed counters. 607 */ 608 u64 current_config; 609 }; 610 611 /* More counters may conflict with other existing Architectural MSRs */ 612 #define KVM_MAX(a, b) ((a) >= (b) ? (a) : (b)) 613 #define KVM_MAX_NR_INTEL_GP_COUNTERS 8 614 #define KVM_MAX_NR_AMD_GP_COUNTERS 6 615 #define KVM_MAX_NR_GP_COUNTERS KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \ 616 KVM_MAX_NR_AMD_GP_COUNTERS) 617 618 #define KVM_MAX_NR_INTEL_FIXED_COUNTERS 3 619 #define KVM_MAX_NR_AMD_FIXED_COUNTERS 0 620 #define KVM_MAX_NR_FIXED_COUNTERS KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS, \ 621 KVM_MAX_NR_AMD_FIXED_COUNTERS) 622 623 struct kvm_pmu { 624 u8 version; 625 unsigned nr_arch_gp_counters; 626 unsigned nr_arch_fixed_counters; 627 unsigned available_event_types; 628 u64 fixed_ctr_ctrl; 629 u64 fixed_ctr_ctrl_hw; 630 u64 fixed_ctr_ctrl_rsvd; 631 u64 global_ctrl; 632 u64 global_status; 633 u64 counter_bitmask[2]; 634 u64 global_ctrl_rsvd; 635 u64 global_status_rsvd; 636 u64 reserved_bits; 637 u64 raw_event_mask; 638 struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS]; 639 struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS]; 640 641 /* 642 * Overlay the bitmap with a 64-bit atomic so that all bits can be 643 * set in a single access, e.g. to reprogram all counters when the PMU 644 * filter changes. 645 */ 646 union { 647 DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX); 648 atomic64_t __reprogram_pmi; 649 }; 650 DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX); 651 DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX); 652 653 DECLARE_BITMAP(pmc_counting_instructions, X86_PMC_IDX_MAX); 654 DECLARE_BITMAP(pmc_counting_branches, X86_PMC_IDX_MAX); 655 656 DECLARE_BITMAP(pmc_has_mode_specific_enables, X86_PMC_IDX_MAX); 657 658 u64 ds_area; 659 u64 pebs_enable; 660 u64 pebs_enable_rsvd; 661 u64 pebs_data_cfg; 662 u64 pebs_data_cfg_rsvd; 663 664 /* 665 * If a guest counter is cross-mapped to host counter with different 666 * index, its PEBS capability will be temporarily disabled. 667 * 668 * The user should make sure that this mask is updated 669 * after disabling interrupts and before perf_guest_get_msrs(); 670 */ 671 u64 host_cross_mapped_mask; 672 673 /* 674 * The gate to release perf_events not marked in 675 * pmc_in_use only once in a vcpu time slice. 676 */ 677 bool need_cleanup; 678 679 /* 680 * The total number of programmed perf_events and it helps to avoid 681 * redundant check before cleanup if guest don't use vPMU at all. 682 */ 683 u8 event_count; 684 }; 685 686 struct kvm_pmu_ops; 687 688 enum { 689 KVM_DEBUGREG_BP_ENABLED = BIT(0), 690 KVM_DEBUGREG_WONT_EXIT = BIT(1), 691 /* 692 * Guest debug registers (DR0-3, DR6 and DR7) are saved/restored by 693 * hardware on exit from or enter to guest. KVM needn't switch them. 694 * DR0-3, DR6 and DR7 are set to their architectural INIT value on VM 695 * exit, host values need to be restored. 696 */ 697 KVM_DEBUGREG_AUTO_SWITCH = BIT(2), 698 }; 699 700 struct kvm_mtrr { 701 u64 var[KVM_NR_VAR_MTRR * 2]; 702 u64 fixed_64k; 703 u64 fixed_16k[2]; 704 u64 fixed_4k[8]; 705 u64 deftype; 706 }; 707 708 /* Hyper-V SynIC timer */ 709 struct kvm_vcpu_hv_stimer { 710 struct hrtimer timer; 711 int index; 712 union hv_stimer_config config; 713 u64 count; 714 u64 exp_time; 715 struct hv_message msg; 716 bool msg_pending; 717 }; 718 719 /* Hyper-V synthetic interrupt controller (SynIC)*/ 720 struct kvm_vcpu_hv_synic { 721 u64 version; 722 u64 control; 723 u64 msg_page; 724 u64 evt_page; 725 atomic64_t sint[HV_SYNIC_SINT_COUNT]; 726 atomic_t sint_to_gsi[HV_SYNIC_SINT_COUNT]; 727 DECLARE_BITMAP(auto_eoi_bitmap, 256); 728 DECLARE_BITMAP(vec_bitmap, 256); 729 bool active; 730 bool dont_zero_synic_pages; 731 }; 732 733 /* The maximum number of entries on the TLB flush fifo. */ 734 #define KVM_HV_TLB_FLUSH_FIFO_SIZE (16) 735 /* 736 * Note: the following 'magic' entry is made up by KVM to avoid putting 737 * anything besides GVA on the TLB flush fifo. It is theoretically possible 738 * to observe a request to flush 4095 PFNs starting from 0xfffffffffffff000 739 * which will look identical. KVM's action to 'flush everything' instead of 740 * flushing these particular addresses is, however, fully legitimate as 741 * flushing more than requested is always OK. 742 */ 743 #define KVM_HV_TLB_FLUSHALL_ENTRY ((u64)-1) 744 745 enum hv_tlb_flush_fifos { 746 HV_L1_TLB_FLUSH_FIFO, 747 HV_L2_TLB_FLUSH_FIFO, 748 HV_NR_TLB_FLUSH_FIFOS, 749 }; 750 751 struct kvm_vcpu_hv_tlb_flush_fifo { 752 spinlock_t write_lock; 753 DECLARE_KFIFO(entries, u64, KVM_HV_TLB_FLUSH_FIFO_SIZE); 754 }; 755 756 /* Hyper-V per vcpu emulation context */ 757 struct kvm_vcpu_hv { 758 struct kvm_vcpu *vcpu; 759 u32 vp_index; 760 u64 hv_vapic; 761 s64 runtime_offset; 762 struct kvm_vcpu_hv_synic synic; 763 struct kvm_hyperv_exit exit; 764 struct kvm_vcpu_hv_stimer stimer[HV_SYNIC_STIMER_COUNT]; 765 DECLARE_BITMAP(stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT); 766 bool enforce_cpuid; 767 struct { 768 u32 features_eax; /* HYPERV_CPUID_FEATURES.EAX */ 769 u32 features_ebx; /* HYPERV_CPUID_FEATURES.EBX */ 770 u32 features_edx; /* HYPERV_CPUID_FEATURES.EDX */ 771 u32 enlightenments_eax; /* HYPERV_CPUID_ENLIGHTMENT_INFO.EAX */ 772 u32 enlightenments_ebx; /* HYPERV_CPUID_ENLIGHTMENT_INFO.EBX */ 773 u32 syndbg_cap_eax; /* HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES.EAX */ 774 u32 nested_eax; /* HYPERV_CPUID_NESTED_FEATURES.EAX */ 775 u32 nested_ebx; /* HYPERV_CPUID_NESTED_FEATURES.EBX */ 776 } cpuid_cache; 777 778 struct kvm_vcpu_hv_tlb_flush_fifo tlb_flush_fifo[HV_NR_TLB_FLUSH_FIFOS]; 779 780 /* 781 * Preallocated buffers for handling hypercalls that pass sparse vCPU 782 * sets (for high vCPU counts, they're too large to comfortably fit on 783 * the stack). 784 */ 785 u64 sparse_banks[HV_MAX_SPARSE_VCPU_BANKS]; 786 DECLARE_BITMAP(vcpu_mask, KVM_MAX_VCPUS); 787 788 struct hv_vp_assist_page vp_assist_page; 789 790 struct { 791 u64 pa_page_gpa; 792 u64 vm_id; 793 u32 vp_id; 794 } nested; 795 }; 796 797 struct kvm_hypervisor_cpuid { 798 u32 base; 799 u32 limit; 800 }; 801 802 #ifdef CONFIG_KVM_XEN 803 /* Xen HVM per vcpu emulation context */ 804 struct kvm_vcpu_xen { 805 u64 hypercall_rip; 806 u32 current_runstate; 807 u8 upcall_vector; 808 struct gfn_to_pfn_cache vcpu_info_cache; 809 struct gfn_to_pfn_cache vcpu_time_info_cache; 810 struct gfn_to_pfn_cache runstate_cache; 811 struct gfn_to_pfn_cache runstate2_cache; 812 u64 last_steal; 813 u64 runstate_entry_time; 814 u64 runstate_times[4]; 815 unsigned long evtchn_pending_sel; 816 u32 vcpu_id; /* The Xen / ACPI vCPU ID */ 817 u32 timer_virq; 818 u64 timer_expires; /* In guest epoch */ 819 atomic_t timer_pending; 820 struct hrtimer timer; 821 int poll_evtchn; 822 struct timer_list poll_timer; 823 struct kvm_hypervisor_cpuid cpuid; 824 }; 825 #endif 826 827 struct kvm_queued_exception { 828 bool pending; 829 bool injected; 830 bool has_error_code; 831 u8 vector; 832 u32 error_code; 833 unsigned long payload; 834 bool has_payload; 835 }; 836 837 /* 838 * Hardware-defined CPUID leafs that are either scattered by the kernel or are 839 * unknown to the kernel, but need to be directly used by KVM. Note, these 840 * word values conflict with the kernel's "bug" caps, but KVM doesn't use those. 841 */ 842 enum kvm_only_cpuid_leafs { 843 CPUID_12_EAX = NCAPINTS, 844 CPUID_7_1_EDX, 845 CPUID_8000_0007_EDX, 846 CPUID_8000_0022_EAX, 847 CPUID_7_2_EDX, 848 CPUID_24_0_EBX, 849 CPUID_8000_0021_ECX, 850 CPUID_7_1_ECX, 851 CPUID_1E_1_EAX, 852 CPUID_24_1_ECX, 853 NR_KVM_CPU_CAPS, 854 855 NKVMCAPINTS = NR_KVM_CPU_CAPS - NCAPINTS, 856 }; 857 858 struct kvm_vcpu_arch { 859 /* 860 * rip and regs accesses must go through 861 * kvm_{register,rip}_{read,write} functions. 862 */ 863 unsigned long regs[NR_VCPU_GENERAL_PURPOSE_REGS]; 864 unsigned long rip; 865 DECLARE_BITMAP(regs_avail, NR_VCPU_TOTAL_REGS); 866 DECLARE_BITMAP(regs_dirty, NR_VCPU_TOTAL_REGS); 867 868 unsigned long cr0; 869 unsigned long cr0_guest_owned_bits; 870 unsigned long cr2; 871 unsigned long cr3; 872 unsigned long cr4; 873 unsigned long cr4_guest_owned_bits; 874 unsigned long cr4_guest_rsvd_bits; 875 unsigned long cr8; 876 u32 host_pkru; 877 u32 pkru; 878 u32 hflags; 879 u64 efer; 880 u64 host_debugctl; 881 u64 apic_base; 882 struct kvm_lapic *apic; /* kernel irqchip context */ 883 bool load_eoi_exitmap_pending; 884 DECLARE_BITMAP(ioapic_handled_vectors, 256); 885 unsigned long apic_attention; 886 int32_t apic_arb_prio; 887 int mp_state; 888 u64 ia32_misc_enable_msr; 889 u64 smbase; 890 u64 smi_count; 891 bool at_instruction_boundary; 892 bool tpr_access_reporting; 893 bool xfd_no_write_intercept; 894 u64 microcode_version; 895 u64 arch_capabilities; 896 u64 perf_capabilities; 897 898 /* 899 * Paging state of the vcpu 900 * 901 * If the vcpu runs in guest mode with two level paging this still saves 902 * the paging mode of the l1 guest. This context is always used to 903 * handle faults. 904 */ 905 struct kvm_mmu *mmu; 906 907 /* Non-nested MMU for L1 */ 908 struct kvm_mmu root_mmu; 909 910 /* L1 TDP when running nested */ 911 struct kvm_mmu guest_mmu; 912 struct kvm_pagewalk ngpa_walk; 913 914 /* 915 * Pagewalk context used for gva_to_gpa translations. 916 */ 917 struct kvm_pagewalk gva_walk; 918 919 u64 pdptrs[4]; /* pae */ 920 921 struct kvm_mmu_memory_cache mmu_pte_list_desc_cache; 922 struct kvm_mmu_memory_cache mmu_shadow_page_cache; 923 struct kvm_mmu_memory_cache mmu_shadowed_info_cache; 924 struct kvm_mmu_memory_cache mmu_page_header_cache; 925 /* 926 * This cache is to allocate external page table. E.g. private EPT used 927 * by the TDX module. 928 */ 929 struct kvm_mmu_memory_cache mmu_external_spt_cache; 930 931 /* 932 * QEMU userspace and the guest each have their own FPU state. 933 * In vcpu_run, we switch between the user and guest FPU contexts. 934 * While running a VCPU, the VCPU thread will have the guest FPU 935 * context. 936 * 937 * Note that while the PKRU state lives inside the fpu registers, 938 * it is switched out separately at VMENTER and VMEXIT time. The 939 * "guest_fpstate" state here contains the guest FPU context, with the 940 * host PRKU bits. 941 */ 942 struct fpu_guest guest_fpu; 943 944 u64 xcr0; 945 u64 guest_supported_xcr0; 946 u64 ia32_xss; 947 u64 guest_supported_xss; 948 949 struct kvm_pio_request pio; 950 void *pio_data; 951 void *sev_pio_data; 952 unsigned sev_pio_count; 953 954 u8 event_exit_inst_len; 955 956 bool exception_from_userspace; 957 958 /* Exceptions to be injected to the guest. */ 959 struct kvm_queued_exception exception; 960 /* Exception VM-Exits to be synthesized to L1. */ 961 struct kvm_queued_exception exception_vmexit; 962 963 struct kvm_queued_interrupt { 964 bool injected; 965 bool soft; 966 u8 nr; 967 } interrupt; 968 969 int halt_request; /* real mode on Intel only */ 970 971 int cpuid_nent; 972 struct kvm_cpuid_entry2 *cpuid_entries; 973 bool cpuid_dynamic_bits_dirty; 974 bool is_amd_compatible; 975 976 /* 977 * cpu_caps holds the effective guest capabilities, i.e. the features 978 * the vCPU is allowed to use. Typically, but not always, features can 979 * be used by the guest if and only if both KVM and userspace want to 980 * expose the feature to the guest. 981 * 982 * A common exception is for virtualization holes, i.e. when KVM can't 983 * prevent the guest from using a feature, in which case the vCPU "has" 984 * the feature regardless of what KVM or userspace desires. 985 * 986 * Note, features that don't require KVM involvement in any way are 987 * NOT enforced/sanitized by KVM, i.e. are taken verbatim from the 988 * guest CPUID provided by userspace. 989 */ 990 u32 cpu_caps[NR_KVM_CPU_CAPS]; 991 992 u64 reserved_gpa_bits; 993 int maxphyaddr; 994 995 /* emulate context */ 996 997 struct x86_emulate_ctxt *emulate_ctxt; 998 bool emulate_regs_need_sync_to_vcpu; 999 bool emulate_regs_need_sync_from_vcpu; 1000 int (*complete_userspace_io)(struct kvm_vcpu *vcpu); 1001 unsigned long cui_linear_rip; 1002 int cui_rdmsr_imm_reg; 1003 1004 gpa_t time; 1005 s8 pvclock_tsc_shift; 1006 u32 pvclock_tsc_mul; 1007 unsigned int hw_tsc_khz; 1008 struct gfn_to_pfn_cache pv_time; 1009 /* set guest stopped flag in pvclock flags field */ 1010 bool pvclock_set_guest_stopped_request; 1011 1012 struct { 1013 u8 preempted; 1014 u64 msr_val; 1015 u64 last_steal; 1016 struct gfn_to_hva_cache cache; 1017 } st; 1018 1019 u64 l1_tsc_offset; 1020 u64 tsc_offset; /* current tsc offset */ 1021 u64 last_guest_tsc; 1022 u64 last_host_tsc; 1023 u64 tsc_offset_adjustment; 1024 u64 this_tsc_nsec; 1025 u64 this_tsc_write; 1026 u64 this_tsc_generation; 1027 bool tsc_catchup; 1028 bool tsc_always_catchup; 1029 s8 virtual_tsc_shift; 1030 u32 virtual_tsc_mult; 1031 u32 virtual_tsc_khz; 1032 s64 ia32_tsc_adjust_msr; 1033 u64 msr_ia32_power_ctl; 1034 u64 l1_tsc_scaling_ratio; 1035 u64 tsc_scaling_ratio; /* current scaling ratio */ 1036 1037 atomic_t nmi_queued; /* unprocessed asynchronous NMIs */ 1038 /* Number of NMIs pending injection, not including hardware vNMIs. */ 1039 unsigned int nmi_pending; 1040 bool nmi_injected; /* Trying to inject an NMI this entry */ 1041 bool smi_pending; /* SMI queued after currently running handler */ 1042 u8 handling_intr_from_guest; 1043 1044 struct kvm_mtrr mtrr_state; 1045 u64 pat; 1046 1047 unsigned switch_db_regs; 1048 unsigned long db[KVM_NR_DB_REGS]; 1049 unsigned long dr6; 1050 unsigned long dr7; 1051 unsigned long eff_db[KVM_NR_DB_REGS]; 1052 unsigned long guest_debug_dr7; 1053 u64 msr_platform_info; 1054 u64 msr_misc_features_enables; 1055 1056 u64 mcg_cap; 1057 u64 mcg_status; 1058 u64 mcg_ctl; 1059 u64 mcg_ext_ctl; 1060 u64 *mce_banks; 1061 u64 *mci_ctl2_banks; 1062 1063 /* Cache MMIO info */ 1064 u64 mmio_gva; 1065 unsigned mmio_access; 1066 gfn_t mmio_gfn; 1067 u64 mmio_gen; 1068 1069 struct kvm_pmu pmu; 1070 1071 /* used for guest single stepping over the given code position */ 1072 unsigned long singlestep_rip; 1073 1074 #ifdef CONFIG_KVM_HYPERV 1075 bool hyperv_enabled; 1076 struct kvm_vcpu_hv *hyperv; 1077 #endif 1078 #ifdef CONFIG_KVM_XEN 1079 struct kvm_vcpu_xen xen; 1080 #endif 1081 cpumask_var_t wbinvd_dirty_mask; 1082 1083 unsigned long last_retry_eip; 1084 unsigned long last_retry_addr; 1085 1086 struct { 1087 bool halted; 1088 gfn_t gfns[ASYNC_PF_PER_VCPU]; 1089 struct gfn_to_hva_cache data; 1090 u64 msr_en_val; /* MSR_KVM_ASYNC_PF_EN */ 1091 u64 msr_int_val; /* MSR_KVM_ASYNC_PF_INT */ 1092 u16 vec; 1093 u32 id; 1094 u32 host_apf_flags; 1095 bool pageready_pending; 1096 } apf; 1097 1098 /* OSVW MSRs (AMD only) */ 1099 struct { 1100 u64 length; 1101 u64 status; 1102 } osvw; 1103 1104 struct { 1105 u64 msr_val; 1106 struct gfn_to_hva_cache data; 1107 } pv_eoi; 1108 1109 u64 msr_kvm_poll_control; 1110 1111 /* pv related host specific info */ 1112 struct { 1113 bool pv_unhalted; 1114 } pv; 1115 1116 int pending_ioapic_eoi; 1117 int pending_external_vector; 1118 int highest_stale_pending_ioapic_eoi; 1119 1120 /* be preempted when it's in kernel-mode(cpl=0) */ 1121 bool preempted_in_kernel; 1122 1123 /* Host CPU on which VM-entry was most recently attempted */ 1124 int last_vmentry_cpu; 1125 1126 /* AMD MSRC001_0015 Hardware Configuration */ 1127 u64 msr_hwcr; 1128 1129 /* pv related cpuid info */ 1130 struct { 1131 /* 1132 * value of the eax register in the KVM_CPUID_FEATURES CPUID 1133 * leaf. 1134 */ 1135 u32 features; 1136 1137 /* 1138 * indicates whether pv emulation should be disabled if features 1139 * are not present in the guest's cpuid 1140 */ 1141 bool enforce; 1142 } pv_cpuid; 1143 1144 /* Protected Guests */ 1145 bool guest_state_protected; 1146 bool guest_tsc_protected; 1147 1148 /* 1149 * Set when PDPTS were loaded directly by the userspace without 1150 * reading the guest memory 1151 */ 1152 bool pdptrs_from_userspace; 1153 1154 /* 1155 * Set if an emulated nested VM-Enter to L2 is pending completion. KVM 1156 * must not synthesize a VM-Exit to L1 before entering L2, as VM-Exits 1157 * can only occur at instruction boundaries. The only exception is 1158 * VMX's "notify" exits, which exist in large part to break the CPU out 1159 * of infinite ucode loops, but can corrupt vCPU state in the process! 1160 * 1161 * For all intents and purposes, this is a boolean, but it's tracked as 1162 * a u8 so that KVM can detect when userspace may have stuffed vCPU 1163 * state and generated an architecturally-impossible VM-Exit. 1164 */ 1165 #define KVM_NESTED_RUN_PENDING 1 1166 #define KVM_NESTED_RUN_PENDING_UNTRUSTED 2 1167 u8 nested_run_pending; 1168 1169 #if IS_ENABLED(CONFIG_HYPERV) 1170 hpa_t hv_root_tdp; 1171 #endif 1172 }; 1173 1174 struct kvm_lpage_info { 1175 int disallow_lpage; 1176 }; 1177 1178 struct kvm_arch_memory_slot { 1179 struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES]; 1180 struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1]; 1181 unsigned short *gfn_write_track; 1182 }; 1183 1184 /* 1185 * Track the mode of the optimized logical map, as the rules for decoding the 1186 * destination vary per mode. Enabling the optimized logical map requires all 1187 * software-enabled local APIs to be in the same mode, each addressable APIC to 1188 * be mapped to only one MDA, and each MDA to map to at most one APIC. 1189 */ 1190 enum kvm_apic_logical_mode { 1191 /* All local APICs are software disabled. */ 1192 KVM_APIC_MODE_SW_DISABLED, 1193 /* All software enabled local APICs in xAPIC cluster addressing mode. */ 1194 KVM_APIC_MODE_XAPIC_CLUSTER, 1195 /* All software enabled local APICs in xAPIC flat addressing mode. */ 1196 KVM_APIC_MODE_XAPIC_FLAT, 1197 /* All software enabled local APICs in x2APIC mode. */ 1198 KVM_APIC_MODE_X2APIC, 1199 /* 1200 * Optimized map disabled, e.g. not all local APICs in the same logical 1201 * mode, same logical ID assigned to multiple APICs, etc. 1202 */ 1203 KVM_APIC_MODE_MAP_DISABLED, 1204 }; 1205 1206 struct kvm_apic_map { 1207 struct rcu_head rcu; 1208 enum kvm_apic_logical_mode logical_mode; 1209 u32 max_apic_id; 1210 union { 1211 struct kvm_lapic *xapic_flat_map[8]; 1212 struct kvm_lapic *xapic_cluster_map[16][4]; 1213 }; 1214 struct kvm_lapic *phys_map[]; 1215 }; 1216 1217 /* Hyper-V synthetic debugger (SynDbg)*/ 1218 struct kvm_hv_syndbg { 1219 struct { 1220 u64 control; 1221 u64 status; 1222 u64 send_page; 1223 u64 recv_page; 1224 u64 pending_page; 1225 } control; 1226 u64 options; 1227 }; 1228 1229 /* Current state of Hyper-V TSC page clocksource */ 1230 enum hv_tsc_page_status { 1231 /* TSC page was not set up or disabled */ 1232 HV_TSC_PAGE_UNSET = 0, 1233 /* TSC page MSR was written by the guest, update pending */ 1234 HV_TSC_PAGE_GUEST_CHANGED, 1235 /* TSC page update was triggered from the host side */ 1236 HV_TSC_PAGE_HOST_CHANGED, 1237 /* TSC page was properly set up and is currently active */ 1238 HV_TSC_PAGE_SET, 1239 /* TSC page was set up with an inaccessible GPA */ 1240 HV_TSC_PAGE_BROKEN, 1241 }; 1242 1243 #ifdef CONFIG_KVM_HYPERV 1244 /* Hyper-V emulation context */ 1245 struct kvm_hv { 1246 struct mutex hv_lock; 1247 u64 hv_guest_os_id; 1248 u64 hv_hypercall; 1249 u64 hv_tsc_page; 1250 enum hv_tsc_page_status hv_tsc_page_status; 1251 1252 /* Hyper-v based guest crash (NT kernel bugcheck) parameters */ 1253 u64 hv_crash_param[HV_X64_MSR_CRASH_PARAMS]; 1254 u64 hv_crash_ctl; 1255 1256 struct ms_hyperv_tsc_page tsc_ref; 1257 1258 struct idr conn_to_evt; 1259 1260 u64 hv_reenlightenment_control; 1261 u64 hv_tsc_emulation_control; 1262 u64 hv_tsc_emulation_status; 1263 u64 hv_invtsc_control; 1264 1265 /* How many vCPUs have VP index != vCPU index */ 1266 atomic_t num_mismatched_vp_indexes; 1267 1268 /* 1269 * How many SynICs use 'AutoEOI' feature 1270 * (protected by arch.apicv_update_lock) 1271 */ 1272 unsigned int synic_auto_eoi_used; 1273 1274 struct kvm_hv_syndbg hv_syndbg; 1275 1276 bool xsaves_xsavec_checked; 1277 }; 1278 #endif 1279 1280 #ifdef CONFIG_KVM_XEN 1281 /* Xen emulation context */ 1282 struct kvm_xen { 1283 struct mutex xen_lock; 1284 u32 xen_version; 1285 bool long_mode; 1286 bool runstate_update_flag; 1287 u8 upcall_vector; 1288 struct gfn_to_pfn_cache shinfo_cache; 1289 struct idr evtchn_ports; 1290 unsigned long poll_mask[BITS_TO_LONGS(KVM_MAX_VCPUS)]; 1291 1292 struct kvm_xen_hvm_config hvm_config; 1293 }; 1294 #endif 1295 1296 enum kvm_irqchip_mode { 1297 KVM_IRQCHIP_NONE, 1298 #ifdef CONFIG_KVM_IOAPIC 1299 KVM_IRQCHIP_KERNEL, /* created with KVM_CREATE_IRQCHIP */ 1300 #endif 1301 KVM_IRQCHIP_SPLIT, /* created with KVM_CAP_SPLIT_IRQCHIP */ 1302 }; 1303 1304 enum kvm_suppress_eoi_broadcast_mode { 1305 KVM_SUPPRESS_EOI_BROADCAST_QUIRKED, /* Legacy behavior */ 1306 KVM_SUPPRESS_EOI_BROADCAST_ENABLED, /* Enable Suppress EOI broadcast */ 1307 KVM_SUPPRESS_EOI_BROADCAST_DISABLED /* Disable Suppress EOI broadcast */ 1308 }; 1309 1310 struct kvm_possible_nx_huge_pages { 1311 /* 1312 * A list of kvm_mmu_page structs that, if zapped, could possibly be 1313 * replaced by an NX huge page. A shadow page is on this list if its 1314 * existence disallows an NX huge page (nx_huge_page_disallowed is set) 1315 * and there are no other conditions that prevent a huge page, e.g. 1316 * the backing host page is huge, dirtly logging is not enabled for its 1317 * memslot, etc... Note, zapping shadow pages on this list doesn't 1318 * guarantee an NX huge page will be created in its stead, e.g. if the 1319 * guest attempts to execute from the region then KVM obviously can't 1320 * create an NX huge page (without hanging the guest). 1321 */ 1322 struct list_head pages; 1323 u64 nr_pages; 1324 }; 1325 1326 enum kvm_mmu_type { 1327 KVM_SHADOW_MMU, 1328 #ifdef CONFIG_X86_64 1329 KVM_TDP_MMU, 1330 #endif 1331 KVM_NR_MMU_TYPES, 1332 }; 1333 1334 struct kvm_arch { 1335 unsigned long n_requested_mmu_pages; 1336 unsigned long n_max_mmu_pages; 1337 unsigned int indirect_shadow_pages; 1338 u8 mmu_valid_gen; 1339 u8 vm_type; 1340 bool has_private_mem; 1341 bool has_protected_state; 1342 bool has_protected_eoi; 1343 bool has_protected_pmu; 1344 bool pre_fault_allowed; 1345 struct hlist_head *mmu_page_hash; 1346 struct list_head active_mmu_pages; 1347 struct kvm_possible_nx_huge_pages possible_nx_huge_pages[KVM_NR_MMU_TYPES]; 1348 #ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING 1349 struct kvm_page_track_notifier_head track_notifier_head; 1350 #endif 1351 /* 1352 * Protects marking pages unsync during page faults, as TDP MMU page 1353 * faults only take mmu_lock for read. For simplicity, the unsync 1354 * pages lock is always taken when marking pages unsync regardless of 1355 * whether mmu_lock is held for read or write. 1356 */ 1357 spinlock_t mmu_unsync_pages_lock; 1358 1359 u64 shadow_mmio_value; 1360 1361 #define __KVM_HAVE_ARCH_NONCOHERENT_DMA 1362 atomic_t noncoherent_dma_count; 1363 unsigned long nr_possible_bypass_irqs; 1364 1365 #ifdef CONFIG_KVM_IOAPIC 1366 struct kvm_pic *vpic; 1367 struct kvm_ioapic *vioapic; 1368 struct kvm_pit *vpit; 1369 #endif 1370 atomic_t vapics_in_nmi_mode; 1371 1372 struct mutex apic_map_lock; 1373 struct kvm_apic_map __rcu *apic_map; 1374 atomic_t apic_map_dirty; 1375 1376 bool apic_access_memslot_enabled; 1377 bool apic_access_memslot_inhibited; 1378 1379 /* 1380 * Force apicv_update_lock and apicv_nr_irq_window_req to reside in a 1381 * dedicated cacheline. They are write-mostly, whereas most everything 1382 * else in kvm_arch is read-mostly. Note that apicv_inhibit_reasons is 1383 * read-mostly: toggling VM-wide inhibits is rare; _checking_ for 1384 * inhibits is common. 1385 */ 1386 ____cacheline_aligned 1387 /* 1388 * Protects apicv_inhibit_reasons and apicv_nr_irq_window_req (with an 1389 * asterisk, see kvm_inc_or_dec_irq_window_inhibit() for details). 1390 */ 1391 struct rw_semaphore apicv_update_lock; 1392 atomic_t apicv_nr_irq_window_req; 1393 ____cacheline_aligned 1394 1395 unsigned long apicv_inhibit_reasons; 1396 1397 gpa_t wall_clock; 1398 1399 u64 disabled_exits; 1400 1401 s64 kvmclock_offset; 1402 1403 /* 1404 * This also protects nr_vcpus_matched_tsc which is read from a 1405 * preemption-disabled region, so it must be a raw spinlock. 1406 */ 1407 raw_spinlock_t tsc_write_lock; 1408 u64 last_tsc_nsec; 1409 u64 last_tsc_write; 1410 u32 last_tsc_khz; 1411 u64 last_tsc_offset; 1412 u64 cur_tsc_nsec; 1413 u64 cur_tsc_write; 1414 u64 cur_tsc_offset; 1415 u64 cur_tsc_generation; 1416 int nr_vcpus_matched_tsc; 1417 1418 u32 default_tsc_khz; 1419 bool user_set_tsc; 1420 u64 apic_bus_cycle_ns; 1421 1422 seqcount_raw_spinlock_t pvclock_sc; 1423 bool use_master_clock; 1424 u64 master_kernel_ns; 1425 u64 master_cycle_now; 1426 struct ratelimit_state kvmclock_update_rs; 1427 1428 #ifdef CONFIG_KVM_HYPERV 1429 struct kvm_hv hyperv; 1430 #endif 1431 1432 #ifdef CONFIG_KVM_XEN 1433 struct kvm_xen xen; 1434 #endif 1435 1436 bool backwards_tsc_observed; 1437 bool boot_vcpu_runs_old_kvmclock; 1438 u32 bsp_vcpu_id; 1439 1440 u64 disabled_quirks; 1441 1442 enum kvm_irqchip_mode irqchip_mode; 1443 u8 nr_reserved_ioapic_pins; 1444 1445 bool disabled_lapic_found; 1446 1447 bool x2apic_format; 1448 bool x2apic_broadcast_quirk_disabled; 1449 enum kvm_suppress_eoi_broadcast_mode suppress_eoi_broadcast_mode; 1450 1451 bool has_mapped_host_mmio; 1452 bool guest_can_read_msr_platform_info; 1453 bool exception_payload_enabled; 1454 1455 bool triple_fault_event; 1456 1457 bool bus_lock_detection_enabled; 1458 bool enable_pmu; 1459 bool created_mediated_pmu; 1460 1461 u32 notify_window; 1462 u32 notify_vmexit_flags; 1463 /* 1464 * If exit_on_emulation_error is set, and the in-kernel instruction 1465 * emulator fails to emulate an instruction, allow userspace 1466 * the opportunity to look at it. 1467 */ 1468 bool exit_on_emulation_error; 1469 1470 /* Deflect RDMSR and WRMSR to user space when they trigger a #GP */ 1471 u32 user_space_msr_mask; 1472 struct kvm_x86_msr_filter __rcu *msr_filter; 1473 1474 u32 hypercall_exit_enabled; 1475 1476 /* Guest can access the SGX PROVISIONKEY. */ 1477 bool sgx_provisioning_allowed; 1478 1479 struct kvm_x86_pmu_event_filter __rcu *pmu_event_filter; 1480 struct vhost_task *nx_huge_page_recovery_thread; 1481 u64 nx_huge_page_last; 1482 struct once nx_once; 1483 1484 #ifdef CONFIG_X86_64 1485 #ifdef CONFIG_KVM_PROVE_MMU 1486 /* 1487 * The number of TDP MMU pages across all roots. Used only to sanity 1488 * check that KVM isn't leaking TDP MMU pages. 1489 */ 1490 atomic64_t tdp_mmu_pages; 1491 #endif 1492 1493 /* 1494 * List of struct kvm_mmu_pages being used as roots. 1495 * All struct kvm_mmu_pages in the list should have 1496 * tdp_mmu_page set. 1497 * 1498 * For reads, this list is protected by: 1499 * RCU alone or 1500 * the MMU lock in read mode + RCU or 1501 * the MMU lock in write mode 1502 * 1503 * For writes, this list is protected by tdp_mmu_pages_lock; see 1504 * below for the details. 1505 * 1506 * Roots will remain in the list until their tdp_mmu_root_count 1507 * drops to zero, at which point the thread that decremented the 1508 * count to zero should removed the root from the list and clean 1509 * it up, freeing the root after an RCU grace period. 1510 */ 1511 struct list_head tdp_mmu_roots; 1512 1513 /* 1514 * Protects accesses to the following fields when the MMU lock 1515 * is held in read mode: 1516 * - tdp_mmu_roots (above) 1517 * - the link field of kvm_mmu_page structs used by the TDP MMU 1518 * - possible_nx_huge_pages[KVM_TDP_MMU]; 1519 * - the possible_nx_huge_page_link field of kvm_mmu_page structs used 1520 * by the TDP MMU 1521 * Because the lock is only taken within the MMU lock, strictly 1522 * speaking it is redundant to acquire this lock when the thread 1523 * holds the MMU lock in write mode. However it often simplifies 1524 * the code to do so. 1525 */ 1526 spinlock_t tdp_mmu_pages_lock; 1527 #endif /* CONFIG_X86_64 */ 1528 1529 /* 1530 * If set, at least one shadow root has been allocated. This flag 1531 * is used as one input when determining whether certain memslot 1532 * related allocations are necessary. 1533 */ 1534 bool shadow_root_allocated; 1535 1536 #ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING 1537 /* 1538 * If set, the VM has (or had) an external write tracking user, and 1539 * thus all write tracking metadata has been allocated, even if KVM 1540 * itself isn't using write tracking. 1541 */ 1542 bool external_write_tracking_enabled; 1543 #endif 1544 1545 #if IS_ENABLED(CONFIG_HYPERV) 1546 hpa_t hv_root_tdp; 1547 spinlock_t hv_root_tdp_lock; 1548 struct hv_partition_assist_pg *hv_pa_pg; 1549 #endif 1550 /* 1551 * VM-scope maximum vCPU ID. Used to determine the size of structures 1552 * that increase along with the maximum vCPU ID, in which case, using 1553 * the global KVM_MAX_VCPU_IDS may lead to significant memory waste. 1554 */ 1555 u32 max_vcpu_ids; 1556 1557 bool disable_nx_huge_pages; 1558 1559 /* 1560 * Memory caches used to allocate shadow pages when performing eager 1561 * page splitting. No need for a shadowed_info_cache since eager page 1562 * splitting only allocates direct shadow pages. 1563 * 1564 * Protected by kvm->slots_lock. 1565 */ 1566 struct kvm_mmu_memory_cache split_shadow_page_cache; 1567 struct kvm_mmu_memory_cache split_page_header_cache; 1568 1569 /* 1570 * Memory cache used to allocate pte_list_desc structs while splitting 1571 * huge pages. In the worst case, to split one huge page, 512 1572 * pte_list_desc structs are needed to add each lower level leaf sptep 1573 * to the rmap plus 1 to extend the parent_ptes rmap of the lower level 1574 * page table. 1575 * 1576 * Protected by kvm->slots_lock. 1577 */ 1578 #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1) 1579 struct kvm_mmu_memory_cache split_desc_cache; 1580 1581 gfn_t gfn_direct_bits; 1582 1583 /* 1584 * Size of the CPU's dirty log buffer, i.e. VMX's PML buffer. A Zero 1585 * value indicates CPU dirty logging is unsupported or disabled in 1586 * current VM. 1587 */ 1588 int cpu_dirty_log_size; 1589 }; 1590 1591 struct kvm_vm_stat { 1592 struct kvm_vm_stat_generic generic; 1593 u64 mmu_shadow_zapped; 1594 u64 mmu_pte_write; 1595 u64 mmu_pde_zapped; 1596 u64 mmu_flooded; 1597 u64 mmu_recycled; 1598 u64 mmu_cache_miss; 1599 u64 mmu_unsync; 1600 u64 mmu_shadow_pages; 1601 union { 1602 struct { 1603 atomic64_t pages_4k; 1604 atomic64_t pages_2m; 1605 atomic64_t pages_1g; 1606 }; 1607 atomic64_t pages[KVM_NR_PAGE_SIZES]; 1608 }; 1609 u64 nx_lpage_splits; 1610 u64 max_mmu_page_hash_collisions; 1611 u64 max_mmu_rmap_size; 1612 }; 1613 1614 struct kvm_vcpu_stat { 1615 struct kvm_vcpu_stat_generic generic; 1616 u64 pf_taken; 1617 u64 pf_fixed; 1618 u64 pf_emulate; 1619 u64 pf_spurious; 1620 u64 pf_fast; 1621 u64 pf_mmio_spte_created; 1622 u64 pf_guest; 1623 u64 tlb_flush; 1624 u64 invlpg; 1625 1626 u64 exits; 1627 u64 io_exits; 1628 u64 mmio_exits; 1629 u64 signal_exits; 1630 u64 irq_window_exits; 1631 u64 nmi_window_exits; 1632 u64 l1d_flush; 1633 u64 halt_exits; 1634 u64 request_irq_exits; 1635 u64 irq_exits; 1636 u64 host_state_reload; 1637 u64 fpu_reload; 1638 u64 insn_emulation; 1639 u64 insn_emulation_fail; 1640 u64 hypercalls; 1641 u64 irq_injections; 1642 u64 nmi_injections; 1643 u64 req_event; 1644 u64 nested_run; 1645 u64 directed_yield_attempted; 1646 u64 directed_yield_successful; 1647 u64 preemption_reported; 1648 u64 preemption_other; 1649 u64 guest_mode; 1650 u64 notify_window_exits; 1651 }; 1652 1653 struct x86_instruction_info; 1654 1655 struct msr_data { 1656 bool host_initiated; 1657 u32 index; 1658 u64 data; 1659 }; 1660 1661 struct kvm_lapic_irq { 1662 u32 vector; 1663 u16 delivery_mode; 1664 u16 dest_mode; 1665 bool level; 1666 u16 trig_mode; 1667 u32 shorthand; 1668 u32 dest_id; 1669 bool msi_redir_hint; 1670 }; 1671 1672 enum kvm_x86_run_flags { 1673 KVM_RUN_FORCE_IMMEDIATE_EXIT = BIT(0), 1674 KVM_RUN_LOAD_GUEST_DR6 = BIT(1), 1675 KVM_RUN_LOAD_DEBUGCTL = BIT(2), 1676 }; 1677 1678 struct kvm_x86_ops { 1679 const char *name; 1680 1681 int (*check_processor_compatibility)(void); 1682 1683 int (*enable_virtualization_cpu)(void); 1684 void (*disable_virtualization_cpu)(void); 1685 cpu_emergency_virt_cb *emergency_disable_virtualization_cpu; 1686 1687 void (*hardware_unsetup)(void); 1688 bool (*has_emulated_msr)(struct kvm *kvm, u32 index); 1689 void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu); 1690 1691 unsigned int vm_size; 1692 int (*vm_init)(struct kvm *kvm); 1693 void (*vm_destroy)(struct kvm *kvm); 1694 void (*vm_pre_destroy)(struct kvm *kvm); 1695 1696 /* Create, but do not attach this VCPU */ 1697 int (*vcpu_precreate)(struct kvm *kvm); 1698 int (*vcpu_create)(struct kvm_vcpu *vcpu); 1699 void (*vcpu_free)(struct kvm_vcpu *vcpu); 1700 void (*vcpu_reset)(struct kvm_vcpu *vcpu, bool init_event); 1701 1702 void (*prepare_switch_to_guest)(struct kvm_vcpu *vcpu); 1703 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu); 1704 void (*vcpu_put)(struct kvm_vcpu *vcpu); 1705 1706 /* 1707 * Mask of DEBUGCTL bits that are owned by the host, i.e. that need to 1708 * match the host's value even while the guest is active. 1709 */ 1710 const u64 HOST_OWNED_DEBUGCTL; 1711 1712 void (*update_exception_bitmap)(struct kvm_vcpu *vcpu); 1713 int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr); 1714 int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr); 1715 u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg); 1716 void (*get_segment)(struct kvm_vcpu *vcpu, 1717 struct kvm_segment *var, int seg); 1718 int (*get_cpl)(struct kvm_vcpu *vcpu); 1719 int (*get_cpl_no_cache)(struct kvm_vcpu *vcpu); 1720 void (*set_segment)(struct kvm_vcpu *vcpu, 1721 struct kvm_segment *var, int seg); 1722 void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l); 1723 bool (*is_valid_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0); 1724 void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0); 1725 void (*post_set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3); 1726 bool (*is_valid_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4); 1727 void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4); 1728 int (*set_efer)(struct kvm_vcpu *vcpu, u64 efer); 1729 void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt); 1730 void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt); 1731 void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt); 1732 void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt); 1733 void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu); 1734 void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value); 1735 void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg); 1736 unsigned long (*get_rflags)(struct kvm_vcpu *vcpu); 1737 void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags); 1738 bool (*get_if_flag)(struct kvm_vcpu *vcpu); 1739 1740 void (*flush_tlb_all)(struct kvm_vcpu *vcpu); 1741 void (*flush_tlb_current)(struct kvm_vcpu *vcpu); 1742 #if IS_ENABLED(CONFIG_HYPERV) 1743 int (*flush_remote_tlbs)(struct kvm *kvm); 1744 int (*flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn, 1745 gfn_t nr_pages); 1746 #endif 1747 1748 /* 1749 * Flush any TLB entries associated with the given GVA. 1750 * Does not need to flush GPA->HPA mappings. 1751 * Can potentially get non-canonical addresses through INVLPGs, which 1752 * the implementation may choose to ignore if appropriate. 1753 */ 1754 void (*flush_tlb_gva)(struct kvm_vcpu *vcpu, gva_t addr, bool *full); 1755 1756 /* 1757 * Flush any TLB entries created by the guest. Like tlb_flush_gva(), 1758 * does not need to flush GPA->HPA mappings. 1759 */ 1760 void (*flush_tlb_guest)(struct kvm_vcpu *vcpu); 1761 1762 int (*vcpu_pre_run)(struct kvm_vcpu *vcpu); 1763 enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu, 1764 u64 run_flags); 1765 int (*handle_exit)(struct kvm_vcpu *vcpu, 1766 enum exit_fastpath_completion exit_fastpath); 1767 int (*skip_emulated_instruction)(struct kvm_vcpu *vcpu); 1768 void (*update_emulated_instruction)(struct kvm_vcpu *vcpu); 1769 void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask); 1770 u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu); 1771 void (*patch_hypercall)(struct kvm_vcpu *vcpu, 1772 unsigned char *hypercall_addr); 1773 void (*inject_irq)(struct kvm_vcpu *vcpu, bool reinjected); 1774 void (*inject_nmi)(struct kvm_vcpu *vcpu); 1775 void (*inject_exception)(struct kvm_vcpu *vcpu); 1776 void (*cancel_injection)(struct kvm_vcpu *vcpu); 1777 int (*interrupt_allowed)(struct kvm_vcpu *vcpu, bool for_injection); 1778 int (*nmi_allowed)(struct kvm_vcpu *vcpu, bool for_injection); 1779 bool (*get_nmi_mask)(struct kvm_vcpu *vcpu); 1780 void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked); 1781 /* Whether or not a virtual NMI is pending in hardware. */ 1782 bool (*is_vnmi_pending)(struct kvm_vcpu *vcpu); 1783 /* 1784 * Attempt to pend a virtual NMI in hardware. Returns %true on success 1785 * to allow using static_call_ret0 as the fallback. 1786 */ 1787 bool (*set_vnmi_pending)(struct kvm_vcpu *vcpu); 1788 void (*enable_nmi_window)(struct kvm_vcpu *vcpu); 1789 void (*enable_irq_window)(struct kvm_vcpu *vcpu); 1790 void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr); 1791 1792 const bool x2apic_icr_is_split; 1793 const unsigned long required_apicv_inhibits; 1794 bool allow_apicv_in_x2apic_without_x2apic_virtualization; 1795 void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu); 1796 void (*hwapic_isr_update)(struct kvm_vcpu *vcpu, int isr); 1797 void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap); 1798 void (*set_virtual_apic_mode)(struct kvm_vcpu *vcpu); 1799 void (*set_apic_access_page_addr)(struct kvm_vcpu *vcpu); 1800 void (*deliver_interrupt)(struct kvm_lapic *apic, int delivery_mode, 1801 int trig_mode, int vector); 1802 int (*sync_pir_to_irr)(struct kvm_vcpu *vcpu); 1803 int (*set_tss_addr)(struct kvm *kvm, unsigned int addr); 1804 int (*set_identity_map_addr)(struct kvm *kvm, u64 ident_addr); 1805 u8 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio); 1806 bool (*tdp_has_smep)(struct kvm *kvm); 1807 1808 void (*load_mmu_pgd)(struct kvm_vcpu *vcpu, hpa_t root_hpa, 1809 int root_level); 1810 1811 /* Update the external page table from spte getting set. */ 1812 int (*set_external_spte)(struct kvm *kvm, gfn_t gfn, u64 old_spte, 1813 u64 new_spte, enum pg_level level); 1814 1815 /* Update external page tables for page table about to be freed. */ 1816 void (*free_external_spt)(struct kvm *kvm, struct kvm_mmu_page *sp); 1817 1818 1819 bool (*has_wbinvd_exit)(void); 1820 1821 u64 (*get_l2_tsc_offset)(struct kvm_vcpu *vcpu); 1822 u64 (*get_l2_tsc_multiplier)(struct kvm_vcpu *vcpu); 1823 void (*write_tsc_offset)(struct kvm_vcpu *vcpu); 1824 void (*write_tsc_multiplier)(struct kvm_vcpu *vcpu); 1825 1826 /* 1827 * Retrieve somewhat arbitrary exit/entry information. Intended to 1828 * be used only from within tracepoints or error paths. 1829 */ 1830 void (*get_exit_info)(struct kvm_vcpu *vcpu, u32 *reason, 1831 u64 *info1, u64 *info2, 1832 u32 *intr_info, u32 *error_code); 1833 1834 void (*get_entry_info)(struct kvm_vcpu *vcpu, 1835 u32 *intr_info, u32 *error_code); 1836 1837 int (*check_intercept)(struct kvm_vcpu *vcpu, 1838 struct x86_instruction_info *info, 1839 enum x86_intercept_stage stage, 1840 struct x86_exception *exception); 1841 void (*handle_exit_irqoff)(struct kvm_vcpu *vcpu); 1842 1843 void (*update_cpu_dirty_logging)(struct kvm_vcpu *vcpu); 1844 1845 const struct kvm_x86_nested_ops *nested_ops; 1846 1847 void (*vcpu_blocking)(struct kvm_vcpu *vcpu); 1848 void (*vcpu_unblocking)(struct kvm_vcpu *vcpu); 1849 1850 int (*pi_update_irte)(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, 1851 unsigned int host_irq, uint32_t guest_irq, 1852 struct kvm_vcpu *vcpu, u32 vector); 1853 void (*pi_start_bypass)(struct kvm *kvm); 1854 void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu); 1855 void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu); 1856 bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu); 1857 bool (*protected_apic_has_interrupt)(struct kvm_vcpu *vcpu); 1858 1859 int (*set_hv_timer)(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc, 1860 bool *expired); 1861 void (*cancel_hv_timer)(struct kvm_vcpu *vcpu); 1862 1863 void (*setup_mce)(struct kvm_vcpu *vcpu); 1864 1865 #ifdef CONFIG_KVM_SMM 1866 int (*smi_allowed)(struct kvm_vcpu *vcpu, bool for_injection); 1867 int (*enter_smm)(struct kvm_vcpu *vcpu, union kvm_smram *smram); 1868 int (*leave_smm)(struct kvm_vcpu *vcpu, const union kvm_smram *smram); 1869 void (*enable_smi_window)(struct kvm_vcpu *vcpu); 1870 #endif 1871 1872 int (*dev_get_attr)(u32 group, u64 attr, u64 *val); 1873 int (*mem_enc_ioctl)(struct kvm *kvm, void __user *argp); 1874 int (*vcpu_mem_enc_ioctl)(struct kvm_vcpu *vcpu, void __user *argp); 1875 int (*vcpu_mem_enc_unlocked_ioctl)(struct kvm_vcpu *vcpu, void __user *argp); 1876 int (*mem_enc_register_region)(struct kvm *kvm, struct kvm_enc_region *argp); 1877 int (*mem_enc_unregister_region)(struct kvm *kvm, struct kvm_enc_region *argp); 1878 int (*vm_copy_enc_context_from)(struct kvm *kvm, unsigned int source_fd); 1879 int (*vm_move_enc_context_from)(struct kvm *kvm, unsigned int source_fd); 1880 void (*guest_memory_reclaimed)(struct kvm *kvm); 1881 1882 int (*get_feature_msr)(u32 msr, u64 *data); 1883 1884 int (*check_emulate_instruction)(struct kvm_vcpu *vcpu, int emul_type, 1885 void *insn, int insn_len); 1886 1887 bool (*apic_init_signal_blocked)(struct kvm_vcpu *vcpu); 1888 int (*enable_l2_tlb_flush)(struct kvm_vcpu *vcpu); 1889 1890 void (*migrate_timers)(struct kvm_vcpu *vcpu); 1891 void (*recalc_intercepts)(struct kvm_vcpu *vcpu); 1892 int (*complete_emulated_msr)(struct kvm_vcpu *vcpu, int err); 1893 1894 void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector); 1895 1896 /* 1897 * Returns vCPU specific APICv inhibit reasons 1898 */ 1899 unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu); 1900 1901 gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu, gva_t gva, unsigned int flags); 1902 void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); 1903 int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); 1904 void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end); 1905 int (*gmem_max_mapping_level)(struct kvm *kvm, kvm_pfn_t pfn, bool is_private); 1906 }; 1907 1908 struct kvm_x86_nested_ops { 1909 void (*leave_nested)(struct kvm_vcpu *vcpu); 1910 bool (*is_exception_vmexit)(struct kvm_vcpu *vcpu, u8 vector, 1911 u32 error_code); 1912 int (*check_events)(struct kvm_vcpu *vcpu); 1913 bool (*has_events)(struct kvm_vcpu *vcpu, bool for_injection); 1914 void (*triple_fault)(struct kvm_vcpu *vcpu); 1915 int (*get_state)(struct kvm_vcpu *vcpu, 1916 struct kvm_nested_state __user *user_kvm_nested_state, 1917 unsigned user_data_size); 1918 int (*set_state)(struct kvm_vcpu *vcpu, 1919 struct kvm_nested_state __user *user_kvm_nested_state, 1920 struct kvm_nested_state *kvm_state); 1921 bool (*get_nested_state_pages)(struct kvm_vcpu *vcpu); 1922 int (*write_log_dirty)(struct kvm_vcpu *vcpu, gpa_t l2_gpa); 1923 gpa_t (*translate_nested_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa, 1924 u64 access, 1925 struct x86_exception *exception, 1926 u64 pte_access); 1927 1928 int (*enable_evmcs)(struct kvm_vcpu *vcpu, 1929 uint16_t *vmcs_version); 1930 uint16_t (*get_evmcs_version)(struct kvm_vcpu *vcpu); 1931 void (*hv_inject_synthetic_vmexit_post_tlb_flush)(struct kvm_vcpu *vcpu); 1932 }; 1933 1934 struct kvm_x86_init_ops { 1935 int (*hardware_setup)(void); 1936 unsigned int (*handle_intel_pt_intr)(void); 1937 1938 struct kvm_x86_ops *runtime_ops; 1939 struct kvm_pmu_ops *pmu_ops; 1940 }; 1941 1942 struct kvm_arch_async_pf { 1943 u32 token; 1944 gfn_t gfn; 1945 unsigned long cr3; 1946 bool direct_map; 1947 u64 error_code; 1948 }; 1949 1950 extern bool __read_mostly allow_smaller_maxphyaddr; 1951 extern bool __read_mostly enable_apicv; 1952 extern bool __read_mostly enable_ipiv; 1953 extern bool __read_mostly enable_device_posted_irqs; 1954 extern struct kvm_x86_ops kvm_x86_ops; 1955 1956 #define kvm_x86_call(func) static_call(kvm_x86_##func) 1957 1958 #define KVM_X86_OP(func) \ 1959 DECLARE_STATIC_CALL(kvm_x86_##func, *(((struct kvm_x86_ops *)0)->func)); 1960 #define KVM_X86_OP_OPTIONAL KVM_X86_OP 1961 #define KVM_X86_OP_OPTIONAL_RET0 KVM_X86_OP 1962 #include <asm/kvm-x86-ops.h> 1963 1964 #define __KVM_HAVE_ARCH_VM_ALLOC 1965 static inline struct kvm *kvm_arch_alloc_vm(void) 1966 { 1967 return kvzalloc(kvm_x86_ops.vm_size, GFP_KERNEL_ACCOUNT); 1968 } 1969 1970 #define __KVM_HAVE_ARCH_VM_FREE 1971 void kvm_arch_free_vm(struct kvm *kvm); 1972 1973 #if IS_ENABLED(CONFIG_HYPERV) 1974 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS 1975 static inline int kvm_arch_flush_remote_tlbs(struct kvm *kvm) 1976 { 1977 if (kvm_x86_ops.flush_remote_tlbs && 1978 !kvm_x86_call(flush_remote_tlbs)(kvm)) 1979 return 0; 1980 else 1981 return -ENOTSUPP; 1982 } 1983 1984 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE 1985 static inline int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm, gfn_t gfn, 1986 u64 nr_pages) 1987 { 1988 if (!kvm_x86_ops.flush_remote_tlbs_range) 1989 return -EOPNOTSUPP; 1990 1991 return kvm_x86_call(flush_remote_tlbs_range)(kvm, gfn, nr_pages); 1992 } 1993 #endif /* CONFIG_HYPERV */ 1994 1995 enum kvm_intr_type { 1996 /* Values are arbitrary, but must be non-zero. */ 1997 KVM_HANDLING_IRQ = 1, 1998 KVM_HANDLING_NMI, 1999 }; 2000 2001 /* Enable perf NMI and timer modes to work, and minimise false positives. */ 2002 #define kvm_arch_pmi_in_guest(vcpu) \ 2003 ((vcpu) && (vcpu)->arch.handling_intr_from_guest && \ 2004 (!!in_nmi() == ((vcpu)->arch.handling_intr_from_guest == KVM_HANDLING_NMI))) 2005 2006 #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES 2007 #define kvm_arch_has_private_mem(kvm) ((kvm)->arch.has_private_mem) 2008 #endif 2009 2010 #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state) 2011 2012 #ifdef CONFIG_X86_64 2013 static inline unsigned long read_msr(unsigned long msr) 2014 { 2015 u64 value; 2016 2017 rdmsrq(msr, value); 2018 return value; 2019 } 2020 #endif 2021 2022 enum { 2023 TASK_SWITCH_CALL = 0, 2024 TASK_SWITCH_IRET = 1, 2025 TASK_SWITCH_JMP = 2, 2026 TASK_SWITCH_GATE = 3, 2027 }; 2028 2029 #define HF_GUEST_MASK (1 << 0) /* VCPU is in guest-mode */ 2030 2031 #ifdef CONFIG_KVM_SMM 2032 #define HF_SMM_MASK (1 << 1) 2033 #define HF_SMM_INSIDE_NMI_MASK (1 << 2) 2034 2035 # define KVM_MAX_NR_ADDRESS_SPACES 2 2036 /* SMM is currently unsupported for guests with private memory. */ 2037 # define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_has_private_mem(kvm) ? 1 : 2) 2038 # define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0) 2039 # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm) 2040 #else 2041 # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, 0) 2042 #endif 2043 2044 bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, 2045 struct kvm_async_pf *work); 2046 void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, 2047 struct kvm_async_pf *work); 2048 void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, 2049 struct kvm_async_pf *work); 2050 void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu); 2051 bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu); 2052 2053 static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) 2054 { 2055 kvm_x86_call(vcpu_blocking)(vcpu); 2056 } 2057 2058 static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) 2059 { 2060 kvm_x86_call(vcpu_unblocking)(vcpu); 2061 } 2062 2063 static inline bool kvm_arch_has_irq_bypass(void) 2064 { 2065 return enable_device_posted_irqs; 2066 } 2067 2068 #endif /* _ASM_X86_KVM_HOST_H */ 2069