1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2012,2013 - ARM Ltd 4 * Author: Marc Zyngier <marc.zyngier@arm.com> 5 * 6 * Derived from arch/arm/include/asm/kvm_host.h: 7 * Copyright (C) 2012 - Virtual Open Systems and Columbia University 8 * Author: Christoffer Dall <c.dall@virtualopensystems.com> 9 */ 10 11 #ifndef __ARM64_KVM_HOST_H__ 12 #define __ARM64_KVM_HOST_H__ 13 14 #include <linux/arm-smccc.h> 15 #include <linux/bitmap.h> 16 #include <linux/types.h> 17 #include <linux/interval_tree.h> 18 #include <linux/jump_label.h> 19 #include <linux/kvm_types.h> 20 #include <linux/maple_tree.h> 21 #include <linux/percpu.h> 22 #include <linux/psci.h> 23 #include <asm/arch_gicv3.h> 24 #include <asm/barrier.h> 25 #include <asm/cpufeature.h> 26 #include <asm/cputype.h> 27 #include <asm/daifflags.h> 28 #include <asm/fpsimd.h> 29 #include <asm/kvm.h> 30 #include <asm/kvm_asm.h> 31 #include <asm/kvm_hcall.h> 32 #include <asm/vncr_mapping.h> 33 34 #define __KVM_HAVE_ARCH_INTC_INITIALIZED 35 #define __KVM_HAVE_MSI 36 #define __KVM_HAVE_READONLY_MEM 37 #define __KVM_HAVE_VCPU_RUN_PID_CHANGE 38 39 #define KVM_HALT_POLL_NS_DEFAULT 500000 40 41 #include <kvm/arm_vgic.h> 42 #include <kvm/arm_arch_timer.h> 43 #include <kvm/arm_pmu.h> 44 45 #define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS 46 47 #if ARM64_S390_COMMON == 1 48 #define KVM_VCPU_MAX_FEATURES 10 49 #define KVM_VCPU_VALID_FEATURES (BIT(KVM_VCPU_MAX_FEATURES) - 1) 50 51 #define KVM_REQ_SLEEP \ 52 KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) 53 #define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1) 54 #define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2) 55 #define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3) 56 #define KVM_REQ_RELOAD_GICv4 KVM_ARCH_REQ(4) 57 #define KVM_REQ_RELOAD_PMU KVM_ARCH_REQ(5) 58 #define KVM_REQ_SUSPEND KVM_ARCH_REQ(6) 59 #define KVM_REQ_RESYNC_PMU_EL0 KVM_ARCH_REQ(7) 60 #define KVM_REQ_NESTED_S2_UNMAP KVM_ARCH_REQ(8) 61 #define KVM_REQ_GUEST_HYP_IRQ_PENDING KVM_ARCH_REQ(9) 62 #define KVM_REQ_MAP_L1_VNCR_EL2 KVM_ARCH_REQ(10) 63 #define KVM_REQ_VGIC_PROCESS_UPDATE KVM_ARCH_REQ(11) 64 #define KVM_REQ_RELOAD_GICv5 KVM_ARCH_REQ(12) 65 66 /* Bit indices for kvm_arch::flags */ 67 enum { 68 /* 69 * If we encounter a data abort without valid instruction syndrome 70 * information, report this to user space. User space can (and 71 * should) opt in to this feature if KVM_CAP_ARM_NISV_TO_USER is 72 * supported. 73 */ 74 KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, 75 /* Memory Tagging Extension enabled for the guest */ 76 KVM_ARCH_FLAG_MTE_ENABLED, 77 /* At least one vCPU has ran in the VM */ 78 KVM_ARCH_FLAG_HAS_RAN_ONCE, 79 /* The vCPU feature set for the VM is configured */ 80 KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, 81 /* PSCI SYSTEM_SUSPEND enabled for the guest */ 82 KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, 83 /* VM counter offset */ 84 KVM_ARCH_FLAG_VM_COUNTER_OFFSET, 85 /* Timer PPIs made immutable */ 86 KVM_ARCH_FLAG_TIMER_PPIS_IMMUTABLE, 87 /* Initial ID reg values loaded */ 88 KVM_ARCH_FLAG_ID_REGS_INITIALIZED, 89 /* Fine-Grained UNDEF initialised */ 90 KVM_ARCH_FLAG_FGU_INITIALIZED, 91 /* SVE exposed to guest */ 92 KVM_ARCH_FLAG_GUEST_HAS_SVE, 93 /* MIDR_EL1, REVIDR_EL1, and AIDR_EL1 are writable from userspace */ 94 KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, 95 /* Unhandled SEAs are taken to userspace */ 96 KVM_ARCH_FLAG_EXIT_SEA, 97 /* No further ID register changes possible */ 98 KVM_ARCH_FLAG_ID_REGS_FINAL, 99 }; 100 101 struct vcpu_reset_state { 102 unsigned long pc; 103 unsigned long r0; 104 bool be; 105 bool reset; 106 }; 107 108 #endif /* ARM64_S390_COMMON == 1 */ 109 110 #define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ 111 KVM_DIRTY_LOG_INITIALLY_SET) 112 113 #define KVM_HAVE_MMU_RWLOCK 114 115 /* 116 * Mode of operation configurable with kvm-arm.mode early param. 117 * See Documentation/admin-guide/kernel-parameters.txt for more information. 118 */ 119 enum kvm_mode { 120 KVM_MODE_DEFAULT, 121 KVM_MODE_PROTECTED, 122 KVM_MODE_NV, 123 KVM_MODE_NONE, 124 }; 125 #ifdef CONFIG_KVM 126 enum kvm_mode kvm_get_mode(void); 127 void kvm_force_disabled(void); 128 #else 129 static inline enum kvm_mode kvm_get_mode(void) { return KVM_MODE_NONE; }; 130 static inline void kvm_force_disabled(void) { }; 131 #endif 132 133 extern unsigned int __ro_after_init kvm_sve_max_vl; 134 extern unsigned int __ro_after_init kvm_host_sve_max_vl; 135 int __init kvm_arm_init_sve(void); 136 137 u32 __attribute_const__ kvm_target_cpu(void); 138 void kvm_reset_vcpu(struct kvm_vcpu *vcpu); 139 void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu); 140 141 struct kvm_hyp_memcache { 142 phys_addr_t head; 143 unsigned long nr_pages; 144 struct pkvm_mapping *mapping; /* only used from EL1 */ 145 146 #define HYP_MEMCACHE_ACCOUNT_STAGE2 BIT(1) 147 #define HYP_MEMCACHE_ACCOUNT_KMEMCG BIT(2) 148 unsigned long flags; 149 }; 150 151 static inline void init_hyp_memcache(struct kvm_hyp_memcache *mc) 152 { 153 memset(mc, 0, sizeof(*mc)); 154 mc->mapping = ZERO_SIZE_PTR; /* Prevent allocation, solely useful for stage2 memcache */ 155 } 156 157 static inline void init_hyp_stage2_memcache(struct kvm_hyp_memcache *mc) 158 { 159 memset(mc, 0, sizeof(*mc)); 160 mc->flags = HYP_MEMCACHE_ACCOUNT_STAGE2 | HYP_MEMCACHE_ACCOUNT_KMEMCG; 161 } 162 163 static inline void push_hyp_memcache(struct kvm_hyp_memcache *mc, 164 phys_addr_t *p, 165 phys_addr_t (*to_pa)(void *virt)) 166 { 167 *p = mc->head; 168 mc->head = to_pa(p); 169 mc->nr_pages++; 170 } 171 172 static inline void *pop_hyp_memcache(struct kvm_hyp_memcache *mc, 173 void *(*to_va)(phys_addr_t phys)) 174 { 175 phys_addr_t *p = to_va(mc->head & PAGE_MASK); 176 177 if (!mc->nr_pages) 178 return NULL; 179 180 mc->head = *p; 181 mc->nr_pages--; 182 183 return p; 184 } 185 186 static inline int __topup_hyp_memcache(struct kvm_hyp_memcache *mc, 187 unsigned long min_pages, 188 void *(*alloc_fn)(void *arg), 189 phys_addr_t (*to_pa)(void *virt), 190 void *arg) 191 { 192 while (mc->nr_pages < min_pages) { 193 phys_addr_t *p = alloc_fn(arg); 194 195 if (!p) 196 return -ENOMEM; 197 push_hyp_memcache(mc, p, to_pa); 198 } 199 200 return 0; 201 } 202 203 static inline void __free_hyp_memcache(struct kvm_hyp_memcache *mc, 204 void (*free_fn)(void *virt, void *arg), 205 void *(*to_va)(phys_addr_t phys), 206 void *arg) 207 { 208 while (mc->nr_pages) 209 free_fn(pop_hyp_memcache(mc, to_va), arg); 210 } 211 212 void free_hyp_memcache(struct kvm_hyp_memcache *mc); 213 int topup_hyp_memcache(struct kvm_hyp_memcache *mc, unsigned long min_pages); 214 215 struct kvm_vmid { 216 atomic64_t id; 217 }; 218 219 /* 220 * Record of a guest stage-2 mapping, storing canonical and nested IPA 221 * ranges. Both ranges have the same size. The lower bits of nested.start 222 * store the index of the nested mmu this mapping belongs to. 223 */ 224 struct kvm_guest_s2_mapping { 225 struct interval_tree_node canonical; 226 struct interval_tree_node nested; 227 }; 228 229 struct kvm_vcpu_ops { 230 void (*vcpu_load)(struct kvm_vcpu *vcpu); 231 void (*vcpu_put)(struct kvm_vcpu *vcpu); 232 }; 233 234 struct kvm_gfn_range; 235 struct kvm_s2_fault_desc; 236 237 struct kvm_vm_s2_ops { 238 bool (*vm_age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range); 239 bool (*vm_test_age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range); 240 int (*vm_flush_remote_tlbs)(struct kvm *kvm); 241 int (*vm_flush_remote_tlbs_range)(struct kvm *kvm, gfn_t gfn, 242 u64 nr_pages); 243 void (*vm_stage2_unmap_range)(struct kvm_s2_mmu *mmu, 244 phys_addr_t start, u64 size, 245 bool may_block); 246 int (*vm_mem_abort)(const struct kvm_s2_fault_desc *s2fd); 247 }; 248 249 struct kvm_s2_mmu { 250 struct kvm_vmid vmid; 251 252 /* 253 * stage2 entry level table 254 * 255 * Two kvm_s2_mmu structures in the same VM can point to the same 256 * pgd here. This happens when running a guest using a 257 * translation regime that isn't affected by its own stage-2 258 * translation, such as a non-VHE hypervisor running at vEL2, or 259 * for vEL1/EL0 with vHCR_EL2.VM == 0. In that case, we use the 260 * canonical stage-2 page tables. 261 */ 262 phys_addr_t pgd_phys; 263 struct kvm_pgtable *pgt; 264 265 /* 266 * VTCR value used on the host. For a non-NV guest (or a NV 267 * guest that runs in a context where its own S2 doesn't 268 * apply), its T0SZ value reflects that of the IPA size. 269 * 270 * For a shadow S2 MMU, T0SZ reflects the PARange exposed to 271 * the guest. 272 */ 273 u64 vtcr; 274 275 /* The last vcpu id that ran on each physical CPU */ 276 int __percpu *last_vcpu_ran; 277 278 #define KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT 0 279 /* 280 * Memory cache used to split 281 * KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE worth of huge pages. It 282 * is used to allocate stage2 page tables while splitting huge 283 * pages. The choice of KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 284 * influences both the capacity of the split page cache, and 285 * how often KVM reschedules. Be wary of raising CHUNK_SIZE 286 * too high. 287 * 288 * Protected by kvm->slots_lock. 289 */ 290 struct kvm_mmu_memory_cache split_page_cache; 291 uint64_t split_page_chunk_size; 292 293 struct kvm_arch *arch; 294 295 /* 296 * For a shadow stage-2 MMU, the virtual vttbr used by the 297 * host to parse the guest S2. 298 * This either contains: 299 * - the virtual VTTBR programmed by the guest hypervisor with 300 * CnP cleared 301 * - The value 1 (VMID=0, BADDR=0, CnP=1) if invalid 302 * 303 * We also cache the full VTCR which gets used for TLB invalidation, 304 * taking the ARM ARM's "Any of the bits in VTCR_EL2 are permitted 305 * to be cached in a TLB" to the letter. 306 */ 307 u64 tlb_vttbr; 308 u64 tlb_vtcr; 309 310 /* Guest s2 mapping records indexed in this MMU's IPA space. */ 311 struct rb_root_cached guest_s2_mappings; 312 313 /* 314 * true when this represents a nested context where virtual 315 * HCR_EL2.VM == 1 316 */ 317 bool nested_stage2_enabled; 318 319 /* 320 * true when this MMU needs to be unmapped before being used for a new 321 * purpose. 322 */ 323 bool pending_unmap; 324 325 /* Index in the S2 MMU array, only valid for a shadow S2 */ 326 u16 s2_mmu_idx; 327 328 /* 329 * 0: Nobody is currently using this, check vttbr for validity 330 * >0: Somebody is actively using this. 331 */ 332 atomic_t refcnt; 333 }; 334 335 struct kvm_arch_memory_slot { 336 }; 337 338 /** 339 * struct kvm_smccc_features: Descriptor of the hypercall services exposed to the guests 340 * 341 * @std_bmap: Bitmap of standard secure service calls 342 * @std_hyp_bmap: Bitmap of standard hypervisor service calls 343 * @vendor_hyp_bmap: Bitmap of vendor specific hypervisor service calls 344 */ 345 struct kvm_smccc_features { 346 unsigned long std_bmap; 347 unsigned long std_hyp_bmap; 348 unsigned long vendor_hyp_bmap; /* Function numbers 0-63 */ 349 unsigned long vendor_hyp_bmap_2; /* Function numbers 64-127 */ 350 }; 351 352 struct kvm_protected_vm { 353 pkvm_handle_t handle; 354 struct kvm_hyp_memcache stage2_teardown_mc; 355 bool is_created; 356 357 /* 358 * True when the guest is being torn down. When in this state, the 359 * guest's vCPUs can't be loaded anymore, but its pages can be 360 * reclaimed by the host. 361 */ 362 bool is_dying; 363 }; 364 365 struct kvm_mpidr_data { 366 u64 mpidr_mask; 367 DECLARE_FLEX_ARRAY(u16, cmpidr_to_idx); 368 }; 369 370 static inline u16 kvm_mpidr_index(struct kvm_mpidr_data *data, u64 mpidr) 371 { 372 unsigned long index = 0, mask = data->mpidr_mask; 373 unsigned long aff = mpidr & MPIDR_HWID_BITMASK; 374 375 bitmap_gather(&index, &aff, &mask, fls(mask)); 376 377 return index; 378 } 379 380 struct kvm_sysreg_masks; 381 382 enum fgt_group_id { 383 __NO_FGT_GROUP__, 384 HFGRTR_GROUP, 385 HFGWTR_GROUP = HFGRTR_GROUP, 386 HDFGRTR_GROUP, 387 HDFGWTR_GROUP = HDFGRTR_GROUP, 388 HFGITR_GROUP, 389 HAFGRTR_GROUP, 390 HFGRTR2_GROUP, 391 HFGWTR2_GROUP = HFGRTR2_GROUP, 392 HDFGRTR2_GROUP, 393 HDFGWTR2_GROUP = HDFGRTR2_GROUP, 394 HFGITR2_GROUP, 395 ICH_HFGRTR_GROUP, 396 ICH_HFGWTR_GROUP = ICH_HFGRTR_GROUP, 397 ICH_HFGITR_GROUP, 398 399 /* Must be last */ 400 __NR_FGT_GROUP_IDS__ 401 }; 402 403 enum kvm_arm_vm_flavor { 404 VM_NVHE, 405 VM_VHE, 406 /* VMs running on a hyp that doesn't trust */ 407 MARKER(__VM_DISTRUSTING_HYP), 408 VM_PKVM, /* Normal guests on pKVM */ 409 MARKER(__VM_PROTECTED), 410 VM_PROTECTED_PKVM, /* Protected VM */ 411 VM_FLAVOR_MAX 412 }; 413 414 struct kvm_arch { 415 struct kvm_s2_mmu mmu; 416 const struct kvm_vm_s2_ops *vm_s2_ops; 417 418 enum kvm_arm_vm_flavor vm_flavor; 419 /* Mandated version of PSCI */ 420 u32 psci_version; 421 422 /* 423 * Fine-Grained UNDEF, mimicking the FGT layout defined by the 424 * architecture. We track them globally, as we present the 425 * same feature-set to all vcpus. 426 * 427 * Index 0 is currently spare. 428 */ 429 u64 fgu[__NR_FGT_GROUP_IDS__]; 430 431 /* 432 * Stage 2 paging state for VMs with nested S2 using a virtual 433 * VMID. 434 */ 435 struct kvm_s2_mmu **nested_mmus; 436 size_t nested_mmus_size; 437 int nested_mmus_next; 438 439 /* 440 * Serializes guest s2 tracking trees access when the mmu_lock 441 * is only held for read. 442 */ 443 spinlock_t guest_s2_tracking_lock; 444 445 /* Interrupt controller */ 446 struct vgic_dist vgic; 447 448 /* Timers */ 449 struct arch_timer_vm_data timer_data; 450 451 /* Protects VM-scoped configuration data */ 452 struct mutex config_lock; 453 454 unsigned long flags; 455 456 /* VM-wide vCPU feature set */ 457 DECLARE_BITMAP(vcpu_features, KVM_VCPU_MAX_FEATURES); 458 459 /* MPIDR to vcpu index mapping, optional */ 460 struct kvm_mpidr_data *mpidr_data; 461 462 /* 463 * VM-wide PMU filter, implemented as a bitmap and big enough for 464 * up to 2^10 events (ARMv8.0) or 2^16 events (ARMv8.1+). 465 */ 466 unsigned long *pmu_filter; 467 struct arm_pmu *arm_pmu; 468 469 cpumask_var_t supported_cpus; 470 471 /* Maximum number of counters for the guest */ 472 u8 nr_pmu_counters; 473 474 /* PMMIR_EL1.SLOTS value exposed to the guest. */ 475 u8 pmmir_slots; 476 477 /* Hypercall features firmware registers' descriptor */ 478 struct kvm_smccc_features smccc_feat; 479 struct maple_tree smccc_filter; 480 481 /* 482 * Emulated CPU ID registers per VM 483 * (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it 484 * is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8. 485 * 486 * These emulated idregs are VM-wide, but accessed from the context of a vCPU. 487 * Atomic access to multiple idregs are guarded by kvm_arch.config_lock. 488 */ 489 #define IDREG_IDX(id) (((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id)) 490 #define KVM_ARM_ID_REG_NUM (IDREG_IDX(sys_reg(3, 0, 0, 7, 7)) + 1) 491 u64 id_regs[KVM_ARM_ID_REG_NUM]; 492 493 u64 midr_el1; 494 u64 revidr_el1; 495 u64 aidr_el1; 496 u64 ctr_el0; 497 498 /* Masks for VNCR-backed and general EL2 sysregs */ 499 struct kvm_sysreg_masks *sysreg_masks; 500 501 /* Count the number of VNCR_EL2 TLBs */ 502 atomic_t vncr_tlb_count; 503 504 /* 505 * For an untrusted host VM, 'pkvm.handle' is used to lookup 506 * the associated pKVM instance in the hypervisor. 507 */ 508 struct kvm_protected_vm pkvm; 509 510 #ifdef CONFIG_PTDUMP_STAGE2_DEBUGFS 511 /* Nested virtualization info */ 512 struct dentry *debugfs_nv_dentry; 513 #endif 514 }; 515 516 struct kvm_vcpu_fault_info { 517 u64 esr_el2; /* Hyp Syndrom Register */ 518 u64 far_el2; /* Hyp Fault Address Register */ 519 u64 hpfar_el2; /* Hyp IPA Fault Address Register */ 520 u64 disr_el1; /* Deferred [SError] Status Register */ 521 }; 522 523 /* 524 * VNCR() just places the VNCR_capable registers in the enum after 525 * __VNCR_START__, and the value (after correction) to be an 8-byte offset 526 * from the VNCR base. As we don't require the enum to be otherwise ordered, 527 * we need the terrible hack below to ensure that we correctly size the 528 * sys_regs array, no matter what. 529 * 530 * The __MAX__ macro has been lifted from Sean Eron Anderson's wonderful 531 * treasure trove of bit hacks: 532 * https://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax 533 */ 534 #define __MAX__(x,y) ((x) ^ (((x) ^ (y)) & -((x) < (y)))) 535 #define VNCR(r) \ 536 __before_##r, \ 537 r = __VNCR_START__ + ((VNCR_ ## r) / 8), \ 538 __after_##r = __MAX__(__before_##r - 1, r) 539 540 enum vcpu_sysreg { 541 __INVALID_SYSREG__, /* 0 is reserved as an invalid value */ 542 MPIDR_EL1, /* MultiProcessor Affinity Register */ 543 CLIDR_EL1, /* Cache Level ID Register */ 544 CSSELR_EL1, /* Cache Size Selection Register */ 545 TPIDR_EL0, /* Thread ID, User R/W */ 546 TPIDRRO_EL0, /* Thread ID, User R/O */ 547 TPIDR_EL1, /* Thread ID, Privileged */ 548 CNTKCTL_EL1, /* Timer Control Register (EL1) */ 549 PAR_EL1, /* Physical Address Register */ 550 MDCCINT_EL1, /* Monitor Debug Comms Channel Interrupt Enable Reg */ 551 OSLSR_EL1, /* OS Lock Status Register */ 552 DISR_EL1, /* Deferred Interrupt Status Register */ 553 554 /* Performance Monitors Registers */ 555 PMCR_EL0, /* Control Register */ 556 PMSELR_EL0, /* Event Counter Selection Register */ 557 PMEVCNTR0_EL0, /* Event Counter Register (0-30) */ 558 PMEVCNTR30_EL0 = PMEVCNTR0_EL0 + 30, 559 PMCCNTR_EL0, /* Cycle Counter Register */ 560 PMEVTYPER0_EL0, /* Event Type Register (0-30) */ 561 PMEVTYPER30_EL0 = PMEVTYPER0_EL0 + 30, 562 PMCCFILTR_EL0, /* Cycle Count Filter Register */ 563 PMCNTENSET_EL0, /* Count Enable Set Register */ 564 PMINTENSET_EL1, /* Interrupt Enable Set Register */ 565 PMOVSSET_EL0, /* Overflow Flag Status Set Register */ 566 PMUSERENR_EL0, /* User Enable Register */ 567 568 /* Pointer Authentication Registers in a strict increasing order. */ 569 APIAKEYLO_EL1, 570 APIAKEYHI_EL1, 571 APIBKEYLO_EL1, 572 APIBKEYHI_EL1, 573 APDAKEYLO_EL1, 574 APDAKEYHI_EL1, 575 APDBKEYLO_EL1, 576 APDBKEYHI_EL1, 577 APGAKEYLO_EL1, 578 APGAKEYHI_EL1, 579 580 /* Memory Tagging Extension registers */ 581 RGSR_EL1, /* Random Allocation Tag Seed Register */ 582 GCR_EL1, /* Tag Control Register */ 583 TFSRE0_EL1, /* Tag Fault Status Register (EL0) */ 584 585 POR_EL0, /* Permission Overlay Register 0 (EL0) */ 586 587 /* FP/SIMD/SVE */ 588 SVCR, 589 FPMR, 590 591 /* 32bit specific registers. */ 592 DACR32_EL2, /* Domain Access Control Register */ 593 IFSR32_EL2, /* Instruction Fault Status Register */ 594 FPEXC32_EL2, /* Floating-Point Exception Control Register */ 595 DBGVCR32_EL2, /* Debug Vector Catch Register */ 596 597 /* EL2 registers */ 598 ACTLR_EL2, /* Auxiliary Control Register (EL2) */ 599 CPTR_EL2, /* Architectural Feature Trap Register (EL2) */ 600 HACR_EL2, /* Hypervisor Auxiliary Control Register */ 601 TTBR0_EL2, /* Translation Table Base Register 0 (EL2) */ 602 TTBR1_EL2, /* Translation Table Base Register 1 (EL2) */ 603 TCR_EL2, /* Translation Control Register (EL2) */ 604 PIRE0_EL2, /* Permission Indirection Register 0 (EL2) */ 605 PIR_EL2, /* Permission Indirection Register 1 (EL2) */ 606 POR_EL2, /* Permission Overlay Register 2 (EL2) */ 607 SPSR_EL2, /* EL2 saved program status register */ 608 ELR_EL2, /* EL2 exception link register */ 609 AFSR0_EL2, /* Auxiliary Fault Status Register 0 (EL2) */ 610 AFSR1_EL2, /* Auxiliary Fault Status Register 1 (EL2) */ 611 ESR_EL2, /* Exception Syndrome Register (EL2) */ 612 FAR_EL2, /* Fault Address Register (EL2) */ 613 HPFAR_EL2, /* Hypervisor IPA Fault Address Register */ 614 MAIR_EL2, /* Memory Attribute Indirection Register (EL2) */ 615 AMAIR_EL2, /* Auxiliary Memory Attribute Indirection Register (EL2) */ 616 VBAR_EL2, /* Vector Base Address Register (EL2) */ 617 RVBAR_EL2, /* Reset Vector Base Address Register */ 618 CONTEXTIDR_EL2, /* Context ID Register (EL2) */ 619 SP_EL2, /* EL2 Stack Pointer */ 620 CNTHP_CTL_EL2, 621 CNTHP_CVAL_EL2, 622 CNTHV_CTL_EL2, 623 CNTHV_CVAL_EL2, 624 625 /* Anything from this can be RES0/RES1 sanitised */ 626 MARKER(__SANITISED_REG_START__), 627 SCTLR_EL2, /* System Control Register (EL2) */ 628 TCR2_EL2, /* Extended Translation Control Register (EL2) */ 629 SCTLR2_EL2, /* System Control Register 2 (EL2) */ 630 MDCR_EL2, /* Monitor Debug Configuration Register (EL2) */ 631 CNTHCTL_EL2, /* Counter-timer Hypervisor Control register */ 632 ZCR_EL2, /* SVE Control Register (EL2) */ 633 HCR_EL2, /* Hypervisor Control Register */ 634 635 /* Any VNCR-capable reg goes after this point */ 636 MARKER(__VNCR_START__), 637 638 VNCR(SCTLR_EL1),/* System Control Register */ 639 VNCR(ACTLR_EL1),/* Auxiliary Control Register */ 640 VNCR(CPACR_EL1),/* Coprocessor Access Control */ 641 VNCR(ZCR_EL1), /* SVE Control */ 642 VNCR(TTBR0_EL1),/* Translation Table Base Register 0 */ 643 VNCR(TTBR1_EL1),/* Translation Table Base Register 1 */ 644 VNCR(TCR_EL1), /* Translation Control Register */ 645 VNCR(TCR2_EL1), /* Extended Translation Control Register */ 646 VNCR(SCTLR2_EL1), /* System Control Register 2 */ 647 VNCR(ESR_EL1), /* Exception Syndrome Register */ 648 VNCR(AFSR0_EL1),/* Auxiliary Fault Status Register 0 */ 649 VNCR(AFSR1_EL1),/* Auxiliary Fault Status Register 1 */ 650 VNCR(FAR_EL1), /* Fault Address Register */ 651 VNCR(MAIR_EL1), /* Memory Attribute Indirection Register */ 652 VNCR(VBAR_EL1), /* Vector Base Address Register */ 653 VNCR(CONTEXTIDR_EL1), /* Context ID Register */ 654 VNCR(AMAIR_EL1),/* Aux Memory Attribute Indirection Register */ 655 VNCR(MDSCR_EL1),/* Monitor Debug System Control Register */ 656 VNCR(ELR_EL1), 657 VNCR(SP_EL1), 658 VNCR(SPSR_EL1), 659 VNCR(TFSR_EL1), /* Tag Fault Status Register (EL1) */ 660 VNCR(VPIDR_EL2),/* Virtualization Processor ID Register */ 661 VNCR(VMPIDR_EL2),/* Virtualization Multiprocessor ID Register */ 662 VNCR(NVHCR_EL2),/* NV Hypervisor Configuration Register */ 663 VNCR(HSTR_EL2), /* Hypervisor System Trap Register */ 664 VNCR(VTTBR_EL2),/* Virtualization Translation Table Base Register */ 665 VNCR(VTCR_EL2), /* Virtualization Translation Control Register */ 666 VNCR(TPIDR_EL2),/* EL2 Software Thread ID Register */ 667 VNCR(HCRX_EL2), /* Extended Hypervisor Configuration Register */ 668 669 /* Permission Indirection Extension registers */ 670 VNCR(PIR_EL1), /* Permission Indirection Register 1 (EL1) */ 671 VNCR(PIRE0_EL1), /* Permission Indirection Register 0 (EL1) */ 672 673 VNCR(POR_EL1), /* Permission Overlay Register 1 (EL1) */ 674 675 /* FEAT_RAS registers */ 676 VNCR(VDISR_EL2), 677 VNCR(VSESR_EL2), 678 679 VNCR(HFGRTR_EL2), 680 VNCR(HFGWTR_EL2), 681 VNCR(HFGITR_EL2), 682 VNCR(HDFGRTR_EL2), 683 VNCR(HDFGWTR_EL2), 684 VNCR(HAFGRTR_EL2), 685 VNCR(HFGRTR2_EL2), 686 VNCR(HFGWTR2_EL2), 687 VNCR(HFGITR2_EL2), 688 VNCR(HDFGRTR2_EL2), 689 VNCR(HDFGWTR2_EL2), 690 691 VNCR(VNCR_EL2), 692 693 VNCR(CNTVOFF_EL2), 694 VNCR(CNTV_CVAL_EL0), 695 VNCR(CNTV_CTL_EL0), 696 VNCR(CNTP_CVAL_EL0), 697 VNCR(CNTP_CTL_EL0), 698 699 VNCR(ICH_LR0_EL2), 700 VNCR(ICH_LR1_EL2), 701 VNCR(ICH_LR2_EL2), 702 VNCR(ICH_LR3_EL2), 703 VNCR(ICH_LR4_EL2), 704 VNCR(ICH_LR5_EL2), 705 VNCR(ICH_LR6_EL2), 706 VNCR(ICH_LR7_EL2), 707 VNCR(ICH_LR8_EL2), 708 VNCR(ICH_LR9_EL2), 709 VNCR(ICH_LR10_EL2), 710 VNCR(ICH_LR11_EL2), 711 VNCR(ICH_LR12_EL2), 712 VNCR(ICH_LR13_EL2), 713 VNCR(ICH_LR14_EL2), 714 VNCR(ICH_LR15_EL2), 715 716 VNCR(ICH_AP0R0_EL2), 717 VNCR(ICH_AP0R1_EL2), 718 VNCR(ICH_AP0R2_EL2), 719 VNCR(ICH_AP0R3_EL2), 720 VNCR(ICH_AP1R0_EL2), 721 VNCR(ICH_AP1R1_EL2), 722 VNCR(ICH_AP1R2_EL2), 723 VNCR(ICH_AP1R3_EL2), 724 VNCR(ICH_HCR_EL2), 725 VNCR(ICH_VMCR_EL2), 726 727 VNCR(ICH_HFGRTR_EL2), 728 VNCR(ICH_HFGWTR_EL2), 729 VNCR(ICH_HFGITR_EL2), 730 731 NR_SYS_REGS /* Nothing after this line! */ 732 }; 733 734 struct resx { 735 u64 res0; 736 u64 res1; 737 }; 738 739 struct kvm_sysreg_masks { 740 struct resx mask[NR_SYS_REGS - __SANITISED_REG_START__]; 741 }; 742 743 static inline struct resx __kvm_get_sysreg_resx(struct kvm_arch *arch, 744 enum vcpu_sysreg sr) 745 { 746 struct kvm_sysreg_masks *masks; 747 748 masks = arch->sysreg_masks; 749 if (likely(masks && 750 sr >= __SANITISED_REG_START__ && sr < NR_SYS_REGS)) 751 return masks->mask[sr - __SANITISED_REG_START__]; 752 753 return (struct resx){}; 754 } 755 756 #define kvm_get_sysreg_resx(k, sr) __kvm_get_sysreg_resx(&(k)->arch, (sr)) 757 758 static inline void __kvm_set_sysreg_resx(struct kvm_arch *arch, 759 enum vcpu_sysreg sr, struct resx resx) 760 { 761 arch->sysreg_masks->mask[sr - __SANITISED_REG_START__] = resx; 762 } 763 764 #define kvm_set_sysreg_resx(k, sr, resx) \ 765 __kvm_set_sysreg_resx(&(k)->arch, (sr), (resx)) 766 767 struct fgt_masks { 768 const char *str; 769 u64 mask; 770 u64 nmask; 771 u64 res0; 772 u64 res1; 773 }; 774 775 extern struct fgt_masks hfgrtr_masks; 776 extern struct fgt_masks hfgwtr_masks; 777 extern struct fgt_masks hfgitr_masks; 778 extern struct fgt_masks hdfgrtr_masks; 779 extern struct fgt_masks hdfgwtr_masks; 780 extern struct fgt_masks hafgrtr_masks; 781 extern struct fgt_masks hfgrtr2_masks; 782 extern struct fgt_masks hfgwtr2_masks; 783 extern struct fgt_masks hfgitr2_masks; 784 extern struct fgt_masks hdfgrtr2_masks; 785 extern struct fgt_masks hdfgwtr2_masks; 786 extern struct fgt_masks ich_hfgrtr_masks; 787 extern struct fgt_masks ich_hfgwtr_masks; 788 extern struct fgt_masks ich_hfgitr_masks; 789 790 extern struct fgt_masks kvm_nvhe_sym(hfgrtr_masks); 791 extern struct fgt_masks kvm_nvhe_sym(hfgwtr_masks); 792 extern struct fgt_masks kvm_nvhe_sym(hfgitr_masks); 793 extern struct fgt_masks kvm_nvhe_sym(hdfgrtr_masks); 794 extern struct fgt_masks kvm_nvhe_sym(hdfgwtr_masks); 795 extern struct fgt_masks kvm_nvhe_sym(hafgrtr_masks); 796 extern struct fgt_masks kvm_nvhe_sym(hfgrtr2_masks); 797 extern struct fgt_masks kvm_nvhe_sym(hfgwtr2_masks); 798 extern struct fgt_masks kvm_nvhe_sym(hfgitr2_masks); 799 extern struct fgt_masks kvm_nvhe_sym(hdfgrtr2_masks); 800 extern struct fgt_masks kvm_nvhe_sym(hdfgwtr2_masks); 801 extern struct fgt_masks kvm_nvhe_sym(ich_hfgrtr_masks); 802 extern struct fgt_masks kvm_nvhe_sym(ich_hfgwtr_masks); 803 extern struct fgt_masks kvm_nvhe_sym(ich_hfgitr_masks); 804 805 struct kvm_cpu_context { 806 struct user_pt_regs regs; /* sp = sp_el0 */ 807 808 u64 spsr_abt; 809 u64 spsr_und; 810 u64 spsr_irq; 811 u64 spsr_fiq; 812 813 struct user_fpsimd_state fp_regs; 814 815 u64 sys_regs[NR_SYS_REGS]; 816 817 struct kvm_vcpu *__hyp_running_vcpu; 818 819 /* This pointer has to be 4kB aligned. */ 820 u64 *vncr_array; 821 }; 822 823 /* 824 * This structure is instantiated on a per-CPU basis, and contains 825 * data that is: 826 * 827 * - tied to a single physical CPU, and 828 * - either have a lifetime that does not extend past vcpu_put() 829 * - or is an invariant for the lifetime of the system 830 * 831 * Use host_data_ptr(field) as a way to access a pointer to such a 832 * field. 833 */ 834 struct kvm_host_data { 835 #define KVM_HOST_DATA_FLAG_HAS_SPE 0 836 #define KVM_HOST_DATA_FLAG_HAS_TRBE 1 837 #define KVM_HOST_DATA_FLAG_TRBE_ENABLED 2 838 #define KVM_HOST_DATA_FLAG_EL1_TRACING_CONFIGURED 3 839 #define KVM_HOST_DATA_FLAG_VCPU_IN_HYP_CONTEXT 4 840 #define KVM_HOST_DATA_FLAG_L1_VNCR_MAPPED 5 841 #define KVM_HOST_DATA_FLAG_HAS_BRBE 6 842 unsigned long flags; 843 844 struct kvm_cpu_context host_ctxt; 845 846 /* 847 * Hyp VA. 848 * sve_regs is only used in pKVM and if system_supports_sve(). 849 */ 850 struct arm64_sve_state *sve_regs; 851 852 /* Ownership of the FP regs */ 853 enum { 854 FP_STATE_FREE, 855 FP_STATE_HOST_OWNED, 856 FP_STATE_GUEST_OWNED, 857 } fp_owner; 858 859 /* 860 * host_debug_state contains the host registers which are 861 * saved and restored during world switches. 862 */ 863 struct { 864 /* {Break,watch}point registers */ 865 struct kvm_guest_debug_arch regs; 866 /* Statistical profiling extension */ 867 u64 pmscr_el1; 868 u64 pmblimitr_el1; 869 /* Self-hosted trace */ 870 u64 trfcr_el1; 871 u64 trblimitr_el1; 872 /* Values of trap registers for the host before guest entry. */ 873 u64 mdcr_el2; 874 u64 brbcr_el1; 875 } host_debug_state; 876 877 /* Guest trace filter value */ 878 u64 trfcr_while_in_guest; 879 880 /* Number of programmable event counters (PMCR_EL0.N) for this CPU */ 881 unsigned int nr_event_counters; 882 883 /* Number of debug breakpoints/watchpoints for this CPU (minus 1) */ 884 unsigned int debug_brps; 885 unsigned int debug_wrps; 886 887 /* Last vgic_irq part of the AP list recorded in an LR */ 888 struct vgic_irq *last_lr_irq; 889 890 /* PPI state tracking for GICv5-based guests */ 891 struct { 892 DECLARE_BITMAP(pendr, VGIC_V5_NR_PRIVATE_IRQS); 893 894 /* The saved state of the regs when leaving the guest */ 895 DECLARE_BITMAP(activer_exit, VGIC_V5_NR_PRIVATE_IRQS); 896 } vgic_v5_ppi_state; 897 }; 898 899 struct kvm_host_psci_config { 900 /* PSCI version used by host. */ 901 u32 version; 902 u32 smccc_version; 903 904 /* Function IDs used by host if version is v0.1. */ 905 struct psci_0_1_function_ids function_ids_0_1; 906 907 bool psci_0_1_cpu_suspend_implemented; 908 bool psci_0_1_cpu_on_implemented; 909 bool psci_0_1_cpu_off_implemented; 910 bool psci_0_1_migrate_implemented; 911 }; 912 913 extern struct kvm_host_psci_config kvm_nvhe_sym(kvm_host_psci_config); 914 #define kvm_host_psci_config CHOOSE_NVHE_SYM(kvm_host_psci_config) 915 916 extern s64 kvm_nvhe_sym(hyp_physvirt_offset); 917 #define hyp_physvirt_offset CHOOSE_NVHE_SYM(hyp_physvirt_offset) 918 919 extern u64 kvm_nvhe_sym(hyp_cpu_logical_map)[NR_CPUS]; 920 #define hyp_cpu_logical_map CHOOSE_NVHE_SYM(hyp_cpu_logical_map) 921 922 struct vncr_tlb; 923 924 struct kvm_vcpu_arch { 925 struct kvm_cpu_context ctxt; 926 const struct kvm_vcpu_ops *vcpu_ops; 927 928 /* 929 * Guest floating point state 930 * 931 * The architecture has two main floating point extensions, 932 * the original FPSIMD and SVE. These have overlapping 933 * register views, with the FPSIMD V registers occupying the 934 * low 128 bits of the SVE Z registers. When the core 935 * floating point code saves the register state of a task it 936 * records which view it saved in fp_type. 937 */ 938 struct arm64_sve_state *sve_state; 939 enum fp_type fp_type; 940 unsigned int sve_max_vl; 941 942 /* Stage 2 paging state used by the hardware on next switch */ 943 struct kvm_s2_mmu *hw_mmu; 944 945 /* Values of trap registers for the guest. */ 946 u64 hcr_el2; 947 u64 hcrx_el2; 948 u64 mdcr_el2; 949 950 struct { 951 u64 r; 952 u64 w; 953 } fgt[__NR_FGT_GROUP_IDS__]; 954 955 /* Exception Information */ 956 struct kvm_vcpu_fault_info fault; 957 958 /* Configuration flags, set once and for all before the vcpu can run */ 959 u8 cflags; 960 961 /* Input flags to the hypervisor code, potentially cleared after use */ 962 u8 iflags; 963 964 /* State flags for kernel bookkeeping, unused by the hypervisor code */ 965 u16 sflags; 966 967 /* 968 * Don't run the guest (internal implementation need). 969 * 970 * Contrary to the flags above, this is set/cleared outside of 971 * a vcpu context, and thus cannot be mixed with the flags 972 * themselves (or the flag accesses need to be made atomic). 973 */ 974 bool pause; 975 976 /* 977 * We maintain more than a single set of debug registers to support 978 * debugging the guest from the host and to maintain separate host and 979 * guest state during world switches. vcpu_debug_state are the debug 980 * registers of the vcpu as the guest sees them. 981 * 982 * external_debug_state contains the debug values we want to debug the 983 * guest. This is set via the KVM_SET_GUEST_DEBUG ioctl. 984 */ 985 struct kvm_guest_debug_arch vcpu_debug_state; 986 struct kvm_guest_debug_arch external_debug_state; 987 u64 external_mdscr_el1; 988 989 enum { 990 VCPU_DEBUG_FREE, 991 VCPU_DEBUG_HOST_OWNED, 992 VCPU_DEBUG_GUEST_OWNED, 993 } debug_owner; 994 995 /* VGIC state */ 996 struct vgic_cpu vgic_cpu; 997 struct arch_timer_cpu timer_cpu; 998 struct kvm_pmu pmu; 999 1000 /* vcpu power state */ 1001 struct kvm_mp_state mp_state; 1002 spinlock_t mp_state_lock; 1003 1004 /* Cache some mmu pages needed inside spinlock regions */ 1005 struct kvm_mmu_memory_cache mmu_page_cache; 1006 1007 /* Pages to top-up the pKVM/EL2 guest stage-2 pool */ 1008 struct kvm_hyp_memcache stage2_mc; 1009 1010 /* Virtual SError ESR to restore when HCR_EL2.VSE is set */ 1011 u64 vsesr_el2; 1012 1013 /* Additional reset state */ 1014 struct vcpu_reset_state reset_state; 1015 1016 /* Guest PV state */ 1017 struct { 1018 u64 last_steal; 1019 gpa_t base; 1020 } steal; 1021 1022 /* Per-vcpu CCSIDR override or NULL */ 1023 u32 *ccsidr; 1024 1025 /* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */ 1026 struct vncr_tlb *vncr_tlb; 1027 1028 /* Hyp-readable copy of kvm_vcpu::pid */ 1029 pid_t pid; 1030 }; 1031 1032 #if ARM64_S390_COMMON == 1 1033 /* 1034 * Each 'flag' is composed of a comma-separated triplet: 1035 * 1036 * - the flag-set it belongs to in the vcpu->arch structure 1037 * - the value for that flag 1038 * - the mask for that flag 1039 * 1040 * __vcpu_single_flag() builds such a triplet for a single-bit flag. 1041 * unpack_vcpu_flag() extract the flag value from the triplet for 1042 * direct use outside of the flag accessors. 1043 */ 1044 #define __vcpu_single_flag(_set, _f) _set, (_f), (_f) 1045 1046 #define __unpack_flag(_set, _f, _m) _f 1047 #define unpack_vcpu_flag(...) __unpack_flag(__VA_ARGS__) 1048 1049 #define __build_check_flag(v, flagset, f, m) \ 1050 do { \ 1051 typeof(v->arch.flagset) *_fset; \ 1052 \ 1053 /* Check that the flags fit in the mask */ \ 1054 BUILD_BUG_ON(HWEIGHT(m) != HWEIGHT((f) | (m))); \ 1055 /* Check that the flags fit in the type */ \ 1056 BUILD_BUG_ON((sizeof(*_fset) * 8) <= __fls(m)); \ 1057 } while (0) 1058 1059 #define __vcpu_get_flag(v, flagset, f, m) \ 1060 ({ \ 1061 __build_check_flag(v, flagset, f, m); \ 1062 \ 1063 READ_ONCE(v->arch.flagset) & (m); \ 1064 }) 1065 1066 /* 1067 * Note that the set/clear accessors must be preempt-safe in order to 1068 * avoid nesting them with load/put which also manipulate flags... 1069 */ 1070 #ifdef __KVM_NVHE_HYPERVISOR__ 1071 /* the nVHE hypervisor is always non-preemptible */ 1072 #define __vcpu_flags_preempt_disable() 1073 #define __vcpu_flags_preempt_enable() 1074 #else 1075 #define __vcpu_flags_preempt_disable() preempt_disable() 1076 #define __vcpu_flags_preempt_enable() preempt_enable() 1077 #endif 1078 1079 #define __vcpu_set_flag(v, flagset, f, m) \ 1080 do { \ 1081 typeof(v->arch.flagset) *fset; \ 1082 \ 1083 __build_check_flag(v, flagset, f, m); \ 1084 \ 1085 fset = &v->arch.flagset; \ 1086 __vcpu_flags_preempt_disable(); \ 1087 if (HWEIGHT(m) > 1) \ 1088 *fset &= ~(m); \ 1089 *fset |= (f); \ 1090 __vcpu_flags_preempt_enable(); \ 1091 } while (0) 1092 1093 #define __vcpu_clear_flag(v, flagset, f, m) \ 1094 do { \ 1095 typeof(v->arch.flagset) *fset; \ 1096 \ 1097 __build_check_flag(v, flagset, f, m); \ 1098 \ 1099 fset = &v->arch.flagset; \ 1100 __vcpu_flags_preempt_disable(); \ 1101 *fset &= ~(m); \ 1102 __vcpu_flags_preempt_enable(); \ 1103 } while (0) 1104 1105 #define __vcpu_test_and_clear_flag(v, flagset, f, m) \ 1106 ({ \ 1107 typeof(v->arch.flagset) set; \ 1108 \ 1109 set = __vcpu_get_flag(v, flagset, f, m); \ 1110 __vcpu_clear_flag(v, flagset, f, m); \ 1111 \ 1112 set; \ 1113 }) 1114 1115 #define __vcpu_copy_flag(vt, vs, flagset, f, m) \ 1116 do { \ 1117 typeof(vs->arch.flagset) tmp, val; \ 1118 \ 1119 __build_check_flag(vs, flagset, f, m); \ 1120 \ 1121 val = READ_ONCE(vs->arch.flagset); \ 1122 val &= (m); \ 1123 __vcpu_flags_preempt_disable(); \ 1124 tmp = READ_ONCE(vt->arch.flagset); \ 1125 tmp &= ~(m); \ 1126 tmp |= val; \ 1127 WRITE_ONCE(vt->arch.flagset, tmp); \ 1128 __vcpu_flags_preempt_enable(); \ 1129 } while (0) 1130 1131 #define vcpu_get_flag(v, ...) __vcpu_get_flag((v), __VA_ARGS__) 1132 #define vcpu_set_flag(v, ...) __vcpu_set_flag((v), __VA_ARGS__) 1133 #define vcpu_clear_flag(v, ...) __vcpu_clear_flag((v), __VA_ARGS__) 1134 #define vcpu_test_and_clear_flag(v, ...) \ 1135 __vcpu_test_and_clear_flag((v), __VA_ARGS__) 1136 #define vcpu_copy_flag(vt, vs, ...) __vcpu_copy_flag((vt), (vs), __VA_ARGS__) 1137 1138 /* KVM_ARM_VCPU_INIT completed */ 1139 #define VCPU_INITIALIZED __vcpu_single_flag(cflags, BIT(0)) 1140 /* SVE config completed */ 1141 #define VCPU_SVE_FINALIZED __vcpu_single_flag(cflags, BIT(1)) 1142 /* pKVM VCPU setup completed */ 1143 #define VCPU_PKVM_FINALIZED __vcpu_single_flag(cflags, BIT(2)) 1144 1145 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature); 1146 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu); 1147 1148 #define kvm_arm_vcpu_sve_finalized(vcpu) vcpu_get_flag(vcpu, VCPU_SVE_FINALIZED) 1149 1150 /* Exception pending */ 1151 #define PENDING_EXCEPTION __vcpu_single_flag(iflags, BIT(0)) 1152 /* 1153 * PC increment. Overlaps with EXCEPT_MASK on purpose so that it can't 1154 * be set together with an exception... 1155 */ 1156 #define INCREMENT_PC __vcpu_single_flag(iflags, BIT(1)) 1157 /* Target EL/MODE (not a single flag, but let's abuse the macro) */ 1158 #define EXCEPT_MASK __vcpu_single_flag(iflags, GENMASK(3, 1)) 1159 /* Cover both PENDING_EXCEPTION and EXCEPT_MASK for global operations */ 1160 #define PC_UPDATE_REQ __vcpu_single_flag(iflags, GENMASK(3, 0)) 1161 /* Host-set: the hyp flushes the non-protected vCPU state in on entry */ 1162 #define PKVM_HOST_STATE_DIRTY __vcpu_single_flag(iflags, BIT(4)) 1163 1164 /* Helpers to encode exceptions with minimum fuss */ 1165 #define __EXCEPT_MASK_VAL unpack_vcpu_flag(EXCEPT_MASK) 1166 #define __EXCEPT_SHIFT __builtin_ctzl(__EXCEPT_MASK_VAL) 1167 #define __vcpu_except_flags(_f) iflags, (_f << __EXCEPT_SHIFT), __EXCEPT_MASK_VAL 1168 1169 /* 1170 * When PENDING_EXCEPTION is set, EXCEPT_MASK can take the following 1171 * values: 1172 * 1173 * For AArch32 EL1: 1174 */ 1175 #define EXCEPT_AA32_UND __vcpu_except_flags(0) 1176 #define EXCEPT_AA32_IABT __vcpu_except_flags(1) 1177 #define EXCEPT_AA32_DABT __vcpu_except_flags(2) 1178 /* For AArch64: */ 1179 #define EXCEPT_AA64_EL1_SYNC __vcpu_except_flags(0) 1180 #define EXCEPT_AA64_EL1_IRQ __vcpu_except_flags(1) 1181 #define EXCEPT_AA64_EL1_FIQ __vcpu_except_flags(2) 1182 #define EXCEPT_AA64_EL1_SERR __vcpu_except_flags(3) 1183 /* For AArch64 with NV: */ 1184 #define EXCEPT_AA64_EL2_SYNC __vcpu_except_flags(4) 1185 #define EXCEPT_AA64_EL2_IRQ __vcpu_except_flags(5) 1186 #define EXCEPT_AA64_EL2_FIQ __vcpu_except_flags(6) 1187 #define EXCEPT_AA64_EL2_SERR __vcpu_except_flags(7) 1188 1189 /* Physical CPU not in supported_cpus */ 1190 #define ON_UNSUPPORTED_CPU __vcpu_single_flag(sflags, BIT(0)) 1191 /* WFIT instruction trapped */ 1192 #define IN_WFIT __vcpu_single_flag(sflags, BIT(1)) 1193 /* vcpu system registers loaded on physical CPU */ 1194 #define SYSREGS_ON_CPU __vcpu_single_flag(sflags, BIT(2)) 1195 /* Software step state is Active-pending for external debug */ 1196 #define HOST_SS_ACTIVE_PENDING __vcpu_single_flag(sflags, BIT(3)) 1197 /* Software step state is Active pending for guest debug */ 1198 #define GUEST_SS_ACTIVE_PENDING __vcpu_single_flag(sflags, BIT(4)) 1199 /* PMUSERENR for the guest EL0 is on physical CPU */ 1200 #define PMUSERENR_ON_CPU __vcpu_single_flag(sflags, BIT(5)) 1201 /* WFI instruction trapped */ 1202 #define IN_WFI __vcpu_single_flag(sflags, BIT(6)) 1203 /* KVM is currently emulating a nested ERET */ 1204 #define IN_NESTED_ERET __vcpu_single_flag(sflags, BIT(7)) 1205 /* SError pending for nested guest */ 1206 #define NESTED_SERROR_PENDING __vcpu_single_flag(sflags, BIT(8)) 1207 /* KVM is currently emulating an L2 to L1 exception */ 1208 #define IN_NESTED_EXCEPTION __vcpu_single_flag(sflags, BIT(9)) 1209 1210 #endif /* ARM64_S390_COMMON == 1 */ 1211 1212 #define vcpu_sve_max_vq(vcpu) sve_vq_from_vl((vcpu)->arch.sve_max_vl) 1213 1214 #define vcpu_sve_zcr_elx(vcpu) \ 1215 (unlikely(is_hyp_ctxt(vcpu)) ? ZCR_EL2 : ZCR_EL1) 1216 1217 #define sve_state_size_from_vl(sve_max_vl) ({ \ 1218 size_t __size_ret; \ 1219 unsigned int __vq; \ 1220 \ 1221 if (WARN_ON(!sve_vl_valid(sve_max_vl))) { \ 1222 __size_ret = 0; \ 1223 } else { \ 1224 __vq = sve_vq_from_vl(sve_max_vl); \ 1225 __size_ret = SVE_SIG_REGS_SIZE(__vq); \ 1226 } \ 1227 \ 1228 __size_ret; \ 1229 }) 1230 1231 #define vcpu_sve_state_size(vcpu) sve_state_size_from_vl((vcpu)->arch.sve_max_vl) 1232 1233 #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \ 1234 KVM_GUESTDBG_USE_SW_BP | \ 1235 KVM_GUESTDBG_USE_HW | \ 1236 KVM_GUESTDBG_SINGLESTEP) 1237 1238 #define kvm_has_sve(kvm) (system_supports_sve() && \ 1239 test_bit(KVM_ARCH_FLAG_GUEST_HAS_SVE, &(kvm)->arch.flags)) 1240 1241 #ifdef __KVM_NVHE_HYPERVISOR__ 1242 #define vcpu_has_sve(vcpu) kvm_has_sve(kern_hyp_va((vcpu)->kvm)) 1243 #else 1244 #define vcpu_has_sve(vcpu) kvm_has_sve((vcpu)->kvm) 1245 #endif 1246 1247 #ifdef CONFIG_ARM64_PTR_AUTH 1248 #define vcpu_has_ptrauth(vcpu) \ 1249 ((cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH) || \ 1250 cpus_have_final_cap(ARM64_HAS_GENERIC_AUTH)) && \ 1251 (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_ADDRESS) || \ 1252 vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_GENERIC))) 1253 #else 1254 #define vcpu_has_ptrauth(vcpu) false 1255 #endif 1256 1257 #define kvm_id_regs_final(kvm) \ 1258 test_bit(KVM_ARCH_FLAG_ID_REGS_FINAL, &(kvm)->arch.flags) 1259 1260 #define vcpu_on_unsupported_cpu(vcpu) \ 1261 vcpu_get_flag(vcpu, ON_UNSUPPORTED_CPU) 1262 1263 #define vcpu_set_on_unsupported_cpu(vcpu) \ 1264 vcpu_set_flag(vcpu, ON_UNSUPPORTED_CPU) 1265 1266 #define vcpu_clear_on_unsupported_cpu(vcpu) \ 1267 vcpu_clear_flag(vcpu, ON_UNSUPPORTED_CPU) 1268 1269 #define vcpu_gp_regs(v) ((v)->arch.ctxt.regs.regs) 1270 1271 /* 1272 * Only use __vcpu_sys_reg/ctxt_sys_reg if you know you want the 1273 * memory backed version of a register, and not the one most recently 1274 * accessed by a running VCPU. For example, for userspace access or 1275 * for system registers that are never context switched, but only 1276 * emulated. 1277 * 1278 * Don't bother with VNCR-based accesses in the nVHE code, it has no 1279 * business dealing with NV. 1280 */ 1281 static inline u64 *___ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r) 1282 { 1283 #if !defined (__KVM_NVHE_HYPERVISOR__) 1284 if (unlikely(cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) && 1285 r >= __VNCR_START__ && ctxt->vncr_array)) 1286 return &ctxt->vncr_array[r - __VNCR_START__]; 1287 #endif 1288 return (u64 *)&ctxt->sys_regs[r]; 1289 } 1290 1291 #define __ctxt_sys_reg(c,r) \ 1292 ({ \ 1293 BUILD_BUG_ON(__builtin_constant_p(r) && \ 1294 (r) >= NR_SYS_REGS); \ 1295 ___ctxt_sys_reg(c, r); \ 1296 }) 1297 1298 #define ctxt_sys_reg(c,r) (*__ctxt_sys_reg(c,r)) 1299 1300 u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *, enum vcpu_sysreg, u64); 1301 1302 #define __vcpu_assign_sys_reg(v, r, val) \ 1303 do { \ 1304 const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \ 1305 u64 __v = (val); \ 1306 if (vcpu_has_nv((v)) && (r) >= __SANITISED_REG_START__) \ 1307 __v = kvm_vcpu_apply_reg_masks((v), (r), __v); \ 1308 \ 1309 ctxt_sys_reg(ctxt, (r)) = __v; \ 1310 } while (0) 1311 1312 #define __vcpu_rmw_sys_reg(v, r, op, val) \ 1313 do { \ 1314 const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \ 1315 u64 __v = ctxt_sys_reg(ctxt, (r)); \ 1316 __v op (val); \ 1317 if (vcpu_has_nv((v)) && (r) >= __SANITISED_REG_START__) \ 1318 __v = kvm_vcpu_apply_reg_masks((v), (r), __v); \ 1319 \ 1320 ctxt_sys_reg(ctxt, (r)) = __v; \ 1321 } while (0) 1322 1323 #define __vcpu_sys_reg(v,r) \ 1324 ({ \ 1325 const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \ 1326 u64 __v = ctxt_sys_reg(ctxt, (r)); \ 1327 if (vcpu_has_nv((v)) && (r) >= __SANITISED_REG_START__) \ 1328 __v = kvm_vcpu_apply_reg_masks((v), (r), __v); \ 1329 __v; \ 1330 }) 1331 1332 u64 vcpu_read_sys_reg(const struct kvm_vcpu *, enum vcpu_sysreg); 1333 void vcpu_write_sys_reg(struct kvm_vcpu *, u64, enum vcpu_sysreg); 1334 1335 struct kvm_vm_stat { 1336 struct kvm_vm_stat_generic generic; 1337 }; 1338 1339 struct kvm_vcpu_stat { 1340 struct kvm_vcpu_stat_generic generic; 1341 u64 hvc_exit_stat; 1342 u64 wfe_exit_stat; 1343 u64 wfi_exit_stat; 1344 u64 mmio_exit_user; 1345 u64 mmio_exit_kernel; 1346 u64 signal_exits; 1347 u64 exits; 1348 }; 1349 1350 int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, 1351 struct kvm_vcpu_events *events); 1352 1353 int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, 1354 struct kvm_vcpu_events *events); 1355 1356 void kvm_arm_halt_guest(struct kvm *kvm); 1357 void kvm_arm_resume_guest(struct kvm *kvm); 1358 1359 #define vcpu_has_run_once(vcpu) (!!READ_ONCE((vcpu)->pid)) 1360 1361 int handle_exit(struct kvm_vcpu *vcpu, int exception_index); 1362 void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index); 1363 1364 int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu); 1365 int kvm_handle_cp14_32(struct kvm_vcpu *vcpu); 1366 int kvm_handle_cp14_64(struct kvm_vcpu *vcpu); 1367 int kvm_handle_cp15_32(struct kvm_vcpu *vcpu); 1368 int kvm_handle_cp15_64(struct kvm_vcpu *vcpu); 1369 int kvm_handle_sys_reg(struct kvm_vcpu *vcpu); 1370 int kvm_handle_cp10_id(struct kvm_vcpu *vcpu); 1371 1372 void kvm_sys_regs_create_debugfs(struct kvm *kvm); 1373 void kvm_reset_sys_regs(struct kvm_vcpu *vcpu); 1374 1375 int __init kvm_sys_reg_table_init(void); 1376 struct sys_reg_desc; 1377 int __init populate_sysreg_config(const struct sys_reg_desc *sr, 1378 unsigned int idx); 1379 int __init populate_nv_trap_config(void); 1380 1381 void kvm_calculate_traps(struct kvm_vcpu *vcpu); 1382 1383 #if ARM64_S390_COMMON == 1 1384 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu); 1385 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices); 1386 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 1387 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 1388 1389 unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu); 1390 int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices); 1391 1392 /* MMIO helpers */ 1393 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data); 1394 unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len); 1395 1396 int kvm_handle_mmio_return(struct kvm_vcpu *vcpu); 1397 int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa); 1398 1399 #endif /* ARM64_S390_COMMON == 1 */ 1400 1401 /* 1402 * Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event, 1403 * arrived in guest context. For arm64, any event that arrives while a vCPU is 1404 * loaded is considered to be "in guest". 1405 */ 1406 static inline bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu) 1407 { 1408 return IS_ENABLED(CONFIG_GUEST_PERF_EVENTS) && !!vcpu; 1409 } 1410 1411 long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu); 1412 gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu); 1413 void kvm_update_stolen_time(struct kvm_vcpu *vcpu); 1414 1415 bool kvm_arm_pvtime_supported(struct kvm *kvm); 1416 int kvm_arm_pvtime_set_attr(struct kvm_vcpu *vcpu, 1417 struct kvm_device_attr *attr); 1418 int kvm_arm_pvtime_get_attr(struct kvm_vcpu *vcpu, 1419 struct kvm_device_attr *attr); 1420 int kvm_arm_pvtime_has_attr(struct kvm_vcpu *vcpu, 1421 struct kvm_device_attr *attr); 1422 1423 extern unsigned int __ro_after_init kvm_arm_vmid_bits; 1424 int __init kvm_arm_vmid_alloc_init(void); 1425 void __init kvm_arm_vmid_alloc_free(void); 1426 void kvm_arm_vmid_update(struct kvm_vmid *kvm_vmid); 1427 void kvm_arm_vmid_clear_active(void); 1428 1429 static inline void kvm_arm_pvtime_vcpu_init(struct kvm_vcpu_arch *vcpu_arch) 1430 { 1431 vcpu_arch->steal.base = INVALID_GPA; 1432 } 1433 1434 static inline bool kvm_arm_is_pvtime_enabled(struct kvm_vcpu_arch *vcpu_arch) 1435 { 1436 return (vcpu_arch->steal.base != INVALID_GPA); 1437 } 1438 1439 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr); 1440 1441 DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data); 1442 1443 /* 1444 * How we access per-CPU host data depends on the where we access it from, 1445 * and the mode we're in: 1446 * 1447 * - VHE and nVHE hypervisor bits use their locally defined instance 1448 * 1449 * - the rest of the kernel use either the VHE or nVHE one, depending on 1450 * the mode we're running in. 1451 * 1452 * Unless we're in protected mode, fully deprivileged, and the nVHE 1453 * per-CPU stuff is exclusively accessible to the protected EL2 code. 1454 * In this case, the EL1 code uses the *VHE* data as its private state 1455 * (which makes sense in a way as there shouldn't be any shared state 1456 * between the host and the hypervisor). 1457 * 1458 * Yes, this is all totally trivial. Shoot me now. 1459 */ 1460 #if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__) 1461 #define host_data_ptr(f) (&this_cpu_ptr(&kvm_host_data)->f) 1462 #else 1463 #define host_data_ptr(f) \ 1464 (static_branch_unlikely(&kvm_protected_mode_initialized) ? \ 1465 &this_cpu_ptr(&kvm_host_data)->f : \ 1466 &this_cpu_ptr_hyp_sym(kvm_host_data)->f) 1467 #endif 1468 1469 #define host_data_test_flag(flag) \ 1470 (test_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags))) 1471 #define host_data_set_flag(flag) \ 1472 set_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags)) 1473 #define host_data_clear_flag(flag) \ 1474 clear_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags)) 1475 1476 /* Check whether the FP regs are owned by the guest */ 1477 static inline bool guest_owns_fp_regs(void) 1478 { 1479 return *host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED; 1480 } 1481 1482 /* Check whether the FP regs are owned by the host */ 1483 static inline bool host_owns_fp_regs(void) 1484 { 1485 return *host_data_ptr(fp_owner) == FP_STATE_HOST_OWNED; 1486 } 1487 1488 static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt) 1489 { 1490 /* The host's MPIDR is immutable, so let's set it up at boot time */ 1491 ctxt_sys_reg(cpu_ctxt, MPIDR_EL1) = read_cpuid_mpidr(); 1492 } 1493 1494 static inline bool kvm_system_needs_idmapped_vectors(void) 1495 { 1496 return cpus_have_final_cap(ARM64_SPECTRE_V3A); 1497 } 1498 1499 void kvm_init_host_debug_data(void); 1500 void kvm_debug_init_vhe(void); 1501 void kvm_vcpu_load_debug(struct kvm_vcpu *vcpu); 1502 void kvm_vcpu_put_debug(struct kvm_vcpu *vcpu); 1503 void kvm_debug_set_guest_ownership(struct kvm_vcpu *vcpu); 1504 void kvm_debug_handle_oslar(struct kvm_vcpu *vcpu, u64 val); 1505 1506 #define kvm_vcpu_os_lock_enabled(vcpu) \ 1507 (!!(__vcpu_sys_reg(vcpu, OSLSR_EL1) & OSLSR_EL1_OSLK)) 1508 1509 #define kvm_debug_regs_in_use(vcpu) \ 1510 ((vcpu)->arch.debug_owner != VCPU_DEBUG_FREE) 1511 #define kvm_host_owns_debug_regs(vcpu) \ 1512 ((vcpu)->arch.debug_owner == VCPU_DEBUG_HOST_OWNED) 1513 #define kvm_guest_owns_debug_regs(vcpu) \ 1514 ((vcpu)->arch.debug_owner == VCPU_DEBUG_GUEST_OWNED) 1515 1516 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, 1517 struct kvm_device_attr *attr); 1518 int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu, 1519 struct kvm_device_attr *attr); 1520 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu, 1521 struct kvm_device_attr *attr); 1522 1523 int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, 1524 struct kvm_arm_copy_mte_tags *copy_tags); 1525 int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm, 1526 struct kvm_arm_counter_offset *offset); 1527 int kvm_vm_ioctl_get_reg_writable_masks(struct kvm *kvm, 1528 struct reg_mask_range *range); 1529 1530 /* Guest/host FPSIMD coordination helpers */ 1531 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu); 1532 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu); 1533 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu); 1534 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu); 1535 1536 static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr) 1537 { 1538 return (!has_vhe() && attr->exclude_host); 1539 } 1540 1541 #ifdef CONFIG_KVM 1542 void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr); 1543 void kvm_clr_pmu_events(u64 clr); 1544 bool kvm_set_pmuserenr(u64 val); 1545 void kvm_enable_trbe(void); 1546 void kvm_disable_trbe(void); 1547 void kvm_tracing_set_el1_configuration(u64 trfcr_while_in_guest); 1548 #else 1549 static inline void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr) {} 1550 static inline void kvm_clr_pmu_events(u64 clr) {} 1551 static inline bool kvm_set_pmuserenr(u64 val) 1552 { 1553 return false; 1554 } 1555 static inline void kvm_enable_trbe(void) {} 1556 static inline void kvm_disable_trbe(void) {} 1557 static inline void kvm_tracing_set_el1_configuration(u64 trfcr_while_in_guest) {} 1558 #endif 1559 1560 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu); 1561 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu); 1562 1563 int __init kvm_set_ipa_limit(void); 1564 u32 kvm_get_pa_bits(struct kvm *kvm); 1565 1566 #define __KVM_HAVE_ARCH_VM_ALLOC 1567 struct kvm *kvm_arch_alloc_vm(void); 1568 1569 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS 1570 1571 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE 1572 1573 #ifdef __KVM_NVHE_HYPERVISOR__ 1574 1575 #define kvm_vm_is_protected(kvm) \ 1576 (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)) 1577 /* 1578 * Accessing vcpu->kvm from nVHE hyp stub is tricky, as we need to convert the 1579 * pointer to the hyp VA. With pKVM, the nVHE code runs with the hyp_vcpu, 1580 * which is populated correctly and is gated on is_protected_kvm_enabled(). 1581 */ 1582 #define vcpu_is_protected(vcpu) \ 1583 ({ \ 1584 struct kvm *__kvm = READ_ONCE((vcpu)->kvm); \ 1585 \ 1586 (__kvm && kvm_vm_is_protected(__kvm)); \ 1587 }) 1588 1589 #else 1590 1591 #define kvm_vm_is_protected(kvm) ((kvm)->arch.vm_flavor >= __VM_PROTECTED) 1592 #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm) 1593 1594 #define kvm_vm_hyp_is_distrusting(kvm) ((kvm)->arch.vm_flavor >= __VM_DISTRUSTING_HYP) 1595 1596 #endif /* __KVM_NVHE_HYPERVISOR__ */ 1597 1598 #define kvm_has_mte(kvm) \ 1599 (system_supports_mte() && \ 1600 test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &(kvm)->arch.flags)) 1601 1602 #define kvm_supports_32bit_el0() \ 1603 (system_supports_32bit_el0() && \ 1604 !static_branch_unlikely(&arm64_mismatched_32bit_el0)) 1605 1606 #define kvm_vm_has_ran_once(kvm) \ 1607 (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &(kvm)->arch.flags)) 1608 1609 static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature) 1610 { 1611 return test_bit(feature, ka->vcpu_features); 1612 } 1613 1614 #if ARM64_S390_COMMON == 1 1615 1616 #define kvm_vcpu_has_feature(k, f) __vcpu_has_feature(&(k)->arch, (f)) 1617 #define vcpu_has_feature(v, f) __vcpu_has_feature(&(v)->kvm->arch, (f)) 1618 1619 #define kvm_vcpu_initialized(v) vcpu_get_flag(v, VCPU_INITIALIZED) 1620 1621 #endif /* ARM64_S390_COMMON == 1 */ 1622 1623 int kvm_trng_call(struct kvm_vcpu *vcpu); 1624 #ifdef CONFIG_KVM 1625 extern phys_addr_t hyp_mem_base; 1626 extern phys_addr_t hyp_mem_size; 1627 void __init kvm_hyp_reserve(void); 1628 #else 1629 static inline void kvm_hyp_reserve(void) { } 1630 #endif 1631 1632 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu); 1633 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu); 1634 bool kvm_pkvm_vcpu_is_powered_off(struct kvm_vcpu *vcpu); 1635 1636 static inline u64 *__vm_id_reg(struct kvm_arch *ka, u32 reg) 1637 { 1638 switch (reg) { 1639 case sys_reg(3, 0, 0, 1, 0) ... sys_reg(3, 0, 0, 7, 7): 1640 return &ka->id_regs[IDREG_IDX(reg)]; 1641 case SYS_CTR_EL0: 1642 return &ka->ctr_el0; 1643 case SYS_MIDR_EL1: 1644 return &ka->midr_el1; 1645 case SYS_REVIDR_EL1: 1646 return &ka->revidr_el1; 1647 case SYS_AIDR_EL1: 1648 return &ka->aidr_el1; 1649 default: 1650 WARN_ON_ONCE(1); 1651 return NULL; 1652 } 1653 } 1654 1655 #define kvm_read_vm_id_reg(kvm, reg) \ 1656 ({ u64 __val = *__vm_id_reg(&(kvm)->arch, reg); __val; }) 1657 1658 void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val); 1659 1660 #define __expand_field_sign_unsigned(id, fld, val) \ 1661 ((u64)SYS_FIELD_VALUE(id, fld, val)) 1662 1663 #define __expand_field_sign_signed(id, fld, val) \ 1664 ({ \ 1665 u64 __val = SYS_FIELD_VALUE(id, fld, val); \ 1666 sign_extend64(__val, id##_##fld##_WIDTH - 1); \ 1667 }) 1668 1669 #define get_idreg_field_unsigned(kvm, id, fld) \ 1670 ({ \ 1671 u64 __val = kvm_read_vm_id_reg((kvm), SYS_##id); \ 1672 FIELD_GET(id##_##fld##_MASK, __val); \ 1673 }) 1674 1675 #define get_idreg_field_signed(kvm, id, fld) \ 1676 ({ \ 1677 u64 __val = get_idreg_field_unsigned(kvm, id, fld); \ 1678 sign_extend64(__val, id##_##fld##_WIDTH - 1); \ 1679 }) 1680 1681 #define get_idreg_field_enum(kvm, id, fld) \ 1682 get_idreg_field_unsigned(kvm, id, fld) 1683 1684 #define kvm_cmp_feat_signed(kvm, id, fld, op, limit) \ 1685 (get_idreg_field_signed((kvm), id, fld) op __expand_field_sign_signed(id, fld, limit)) 1686 1687 #define kvm_cmp_feat_unsigned(kvm, id, fld, op, limit) \ 1688 (get_idreg_field_unsigned((kvm), id, fld) op __expand_field_sign_unsigned(id, fld, limit)) 1689 1690 #define kvm_cmp_feat(kvm, id, fld, op, limit) \ 1691 (id##_##fld##_SIGNED ? \ 1692 kvm_cmp_feat_signed(kvm, id, fld, op, limit) : \ 1693 kvm_cmp_feat_unsigned(kvm, id, fld, op, limit)) 1694 1695 #define __kvm_has_feat(kvm, id, fld, limit) \ 1696 kvm_cmp_feat(kvm, id, fld, >=, limit) 1697 1698 #define kvm_has_feat(kvm, ...) __kvm_has_feat(kvm, __VA_ARGS__) 1699 1700 #define __kvm_has_feat_enum(kvm, id, fld, val) \ 1701 kvm_cmp_feat_unsigned(kvm, id, fld, ==, val) 1702 1703 #define kvm_has_feat_enum(kvm, ...) __kvm_has_feat_enum(kvm, __VA_ARGS__) 1704 1705 #define kvm_has_feat_range(kvm, id, fld, min, max) \ 1706 (kvm_cmp_feat(kvm, id, fld, >=, min) && \ 1707 kvm_cmp_feat(kvm, id, fld, <=, max)) 1708 1709 /* Check for a given level of PAuth support */ 1710 #define kvm_has_pauth(k, l) \ 1711 ({ \ 1712 bool pa, pi, pa3; \ 1713 \ 1714 pa = kvm_has_feat((k), ID_AA64ISAR1_EL1, APA, l); \ 1715 pa &= kvm_has_feat((k), ID_AA64ISAR1_EL1, GPA, IMP); \ 1716 pi = kvm_has_feat((k), ID_AA64ISAR1_EL1, API, l); \ 1717 pi &= kvm_has_feat((k), ID_AA64ISAR1_EL1, GPI, IMP); \ 1718 pa3 = kvm_has_feat((k), ID_AA64ISAR2_EL1, APA3, l); \ 1719 pa3 &= kvm_has_feat((k), ID_AA64ISAR2_EL1, GPA3, IMP); \ 1720 \ 1721 (pa + pi + pa3) == 1; \ 1722 }) 1723 1724 #define kvm_has_fpmr(k) \ 1725 (system_supports_fpmr() && \ 1726 kvm_has_feat((k), ID_AA64PFR2_EL1, FPMR, IMP)) 1727 1728 #define kvm_has_tcr2(k) \ 1729 (kvm_has_feat((k), ID_AA64MMFR3_EL1, TCRX, IMP)) 1730 1731 #define kvm_has_s1pie(k) \ 1732 (kvm_has_feat((k), ID_AA64MMFR3_EL1, S1PIE, IMP)) 1733 1734 #define kvm_has_s1poe(k) \ 1735 (system_supports_poe() && \ 1736 kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP)) 1737 1738 #define kvm_has_ras(k) \ 1739 (kvm_has_feat((k), ID_AA64PFR0_EL1, RAS, IMP)) 1740 1741 #define kvm_has_sctlr2(k) \ 1742 (kvm_has_feat((k), ID_AA64MMFR3_EL1, SCTLRX, IMP)) 1743 1744 static inline bool kvm_arch_has_irq_bypass(void) 1745 { 1746 return true; 1747 } 1748 1749 void compute_fgu(struct kvm *kvm, enum fgt_group_id fgt); 1750 struct resx get_reg_fixed_bits(struct kvm *kvm, enum vcpu_sysreg reg); 1751 void check_feature_map(void); 1752 void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu); 1753 1754 static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg reg) 1755 { 1756 switch (reg) { 1757 case HFGRTR_EL2: 1758 case HFGWTR_EL2: 1759 return HFGRTR_GROUP; 1760 case HFGITR_EL2: 1761 return HFGITR_GROUP; 1762 case HDFGRTR_EL2: 1763 case HDFGWTR_EL2: 1764 return HDFGRTR_GROUP; 1765 case HAFGRTR_EL2: 1766 return HAFGRTR_GROUP; 1767 case HFGRTR2_EL2: 1768 case HFGWTR2_EL2: 1769 return HFGRTR2_GROUP; 1770 case HFGITR2_EL2: 1771 return HFGITR2_GROUP; 1772 case HDFGRTR2_EL2: 1773 case HDFGWTR2_EL2: 1774 return HDFGRTR2_GROUP; 1775 case ICH_HFGRTR_EL2: 1776 case ICH_HFGWTR_EL2: 1777 return ICH_HFGRTR_GROUP; 1778 case ICH_HFGITR_EL2: 1779 return ICH_HFGITR_GROUP; 1780 default: 1781 BUILD_BUG_ON(1); 1782 } 1783 } 1784 1785 #define vcpu_fgt(vcpu, reg) \ 1786 ({ \ 1787 enum fgt_group_id id = __fgt_reg_to_group_id(reg); \ 1788 u64 *p; \ 1789 switch (reg) { \ 1790 case HFGWTR_EL2: \ 1791 case HDFGWTR_EL2: \ 1792 case HFGWTR2_EL2: \ 1793 case HDFGWTR2_EL2: \ 1794 case ICH_HFGWTR_EL2: \ 1795 p = &(vcpu)->arch.fgt[id].w; \ 1796 break; \ 1797 default: \ 1798 p = &(vcpu)->arch.fgt[id].r; \ 1799 break; \ 1800 } \ 1801 \ 1802 p; \ 1803 }) 1804 1805 long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext); 1806 1807 #endif /* __ARM64_KVM_HOST_H__ */ 1808