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