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 /* Random stuff */ 586 ACCDATA_EL1, /* Accelerator Data */ 587 588 POR_EL0, /* Permission Overlay Register 0 (EL0) */ 589 590 /* FP/SIMD/SVE */ 591 SVCR, 592 FPMR, 593 594 /* 32bit specific registers. */ 595 DACR32_EL2, /* Domain Access Control Register */ 596 IFSR32_EL2, /* Instruction Fault Status Register */ 597 FPEXC32_EL2, /* Floating-Point Exception Control Register */ 598 DBGVCR32_EL2, /* Debug Vector Catch Register */ 599 600 /* EL2 registers */ 601 ACTLR_EL2, /* Auxiliary Control Register (EL2) */ 602 CPTR_EL2, /* Architectural Feature Trap Register (EL2) */ 603 HACR_EL2, /* Hypervisor Auxiliary Control Register */ 604 TTBR0_EL2, /* Translation Table Base Register 0 (EL2) */ 605 TTBR1_EL2, /* Translation Table Base Register 1 (EL2) */ 606 PIRE0_EL2, /* Permission Indirection Register 0 (EL2) */ 607 PIR_EL2, /* Permission Indirection Register 1 (EL2) */ 608 POR_EL2, /* Permission Overlay Register 2 (EL2) */ 609 SPSR_EL2, /* EL2 saved program status register */ 610 ELR_EL2, /* EL2 exception link register */ 611 AFSR0_EL2, /* Auxiliary Fault Status Register 0 (EL2) */ 612 AFSR1_EL2, /* Auxiliary Fault Status Register 1 (EL2) */ 613 ESR_EL2, /* Exception Syndrome Register (EL2) */ 614 FAR_EL2, /* Fault Address Register (EL2) */ 615 HPFAR_EL2, /* Hypervisor IPA Fault Address Register */ 616 MAIR_EL2, /* Memory Attribute Indirection Register (EL2) */ 617 AMAIR_EL2, /* Auxiliary Memory Attribute Indirection Register (EL2) */ 618 VBAR_EL2, /* Vector Base Address Register (EL2) */ 619 RVBAR_EL2, /* Reset Vector Base Address Register */ 620 CONTEXTIDR_EL2, /* Context ID Register (EL2) */ 621 SP_EL2, /* EL2 Stack Pointer */ 622 CNTHP_CTL_EL2, 623 CNTHP_CVAL_EL2, 624 CNTHV_CTL_EL2, 625 CNTHV_CVAL_EL2, 626 627 /* Anything from this can be RES0/RES1 sanitised */ 628 MARKER(__SANITISED_REG_START__), 629 SCTLR_EL2, /* System Control Register (EL2) */ 630 TCR_EL2, /* Translation Control Register (EL2) */ 631 TCR2_EL2, /* Extended Translation Control Register (EL2) */ 632 SCTLR2_EL2, /* System Control Register 2 (EL2) */ 633 MDCR_EL2, /* Monitor Debug Configuration Register (EL2) */ 634 CNTHCTL_EL2, /* Counter-timer Hypervisor Control register */ 635 ZCR_EL2, /* SVE Control Register (EL2) */ 636 HCR_EL2, /* Hypervisor Control Register */ 637 638 /* Any VNCR-capable reg goes after this point */ 639 MARKER(__VNCR_START__), 640 641 VNCR(SCTLR_EL1),/* System Control Register */ 642 VNCR(ACTLR_EL1),/* Auxiliary Control Register */ 643 VNCR(CPACR_EL1),/* Coprocessor Access Control */ 644 VNCR(ZCR_EL1), /* SVE Control */ 645 VNCR(TTBR0_EL1),/* Translation Table Base Register 0 */ 646 VNCR(TTBR1_EL1),/* Translation Table Base Register 1 */ 647 VNCR(TCR_EL1), /* Translation Control Register */ 648 VNCR(TCR2_EL1), /* Extended Translation Control Register */ 649 VNCR(SCTLR2_EL1), /* System Control Register 2 */ 650 VNCR(ESR_EL1), /* Exception Syndrome Register */ 651 VNCR(AFSR0_EL1),/* Auxiliary Fault Status Register 0 */ 652 VNCR(AFSR1_EL1),/* Auxiliary Fault Status Register 1 */ 653 VNCR(FAR_EL1), /* Fault Address Register */ 654 VNCR(MAIR_EL1), /* Memory Attribute Indirection Register */ 655 VNCR(VBAR_EL1), /* Vector Base Address Register */ 656 VNCR(CONTEXTIDR_EL1), /* Context ID Register */ 657 VNCR(AMAIR_EL1),/* Aux Memory Attribute Indirection Register */ 658 VNCR(MDSCR_EL1),/* Monitor Debug System Control Register */ 659 VNCR(ELR_EL1), 660 VNCR(SP_EL1), 661 VNCR(SPSR_EL1), 662 VNCR(TFSR_EL1), /* Tag Fault Status Register (EL1) */ 663 VNCR(VPIDR_EL2),/* Virtualization Processor ID Register */ 664 VNCR(VMPIDR_EL2),/* Virtualization Multiprocessor ID Register */ 665 VNCR(NVHCR_EL2),/* NV Hypervisor Configuration Register */ 666 VNCR(HSTR_EL2), /* Hypervisor System Trap Register */ 667 VNCR(VTTBR_EL2),/* Virtualization Translation Table Base Register */ 668 VNCR(VTCR_EL2), /* Virtualization Translation Control Register */ 669 VNCR(TPIDR_EL2),/* EL2 Software Thread ID Register */ 670 VNCR(HCRX_EL2), /* Extended Hypervisor Configuration Register */ 671 672 /* Permission Indirection Extension registers */ 673 VNCR(PIR_EL1), /* Permission Indirection Register 1 (EL1) */ 674 VNCR(PIRE0_EL1), /* Permission Indirection Register 0 (EL1) */ 675 676 VNCR(POR_EL1), /* Permission Overlay Register 1 (EL1) */ 677 678 /* FEAT_RAS registers */ 679 VNCR(VDISR_EL2), 680 VNCR(VSESR_EL2), 681 682 VNCR(HFGRTR_EL2), 683 VNCR(HFGWTR_EL2), 684 VNCR(HFGITR_EL2), 685 VNCR(HDFGRTR_EL2), 686 VNCR(HDFGWTR_EL2), 687 VNCR(HAFGRTR_EL2), 688 VNCR(HFGRTR2_EL2), 689 VNCR(HFGWTR2_EL2), 690 VNCR(HFGITR2_EL2), 691 VNCR(HDFGRTR2_EL2), 692 VNCR(HDFGWTR2_EL2), 693 694 VNCR(VNCR_EL2), 695 696 VNCR(CNTVOFF_EL2), 697 VNCR(CNTV_CVAL_EL0), 698 VNCR(CNTV_CTL_EL0), 699 VNCR(CNTP_CVAL_EL0), 700 VNCR(CNTP_CTL_EL0), 701 702 VNCR(ICH_LR0_EL2), 703 VNCR(ICH_LR1_EL2), 704 VNCR(ICH_LR2_EL2), 705 VNCR(ICH_LR3_EL2), 706 VNCR(ICH_LR4_EL2), 707 VNCR(ICH_LR5_EL2), 708 VNCR(ICH_LR6_EL2), 709 VNCR(ICH_LR7_EL2), 710 VNCR(ICH_LR8_EL2), 711 VNCR(ICH_LR9_EL2), 712 VNCR(ICH_LR10_EL2), 713 VNCR(ICH_LR11_EL2), 714 VNCR(ICH_LR12_EL2), 715 VNCR(ICH_LR13_EL2), 716 VNCR(ICH_LR14_EL2), 717 VNCR(ICH_LR15_EL2), 718 719 VNCR(ICH_AP0R0_EL2), 720 VNCR(ICH_AP0R1_EL2), 721 VNCR(ICH_AP0R2_EL2), 722 VNCR(ICH_AP0R3_EL2), 723 VNCR(ICH_AP1R0_EL2), 724 VNCR(ICH_AP1R1_EL2), 725 VNCR(ICH_AP1R2_EL2), 726 VNCR(ICH_AP1R3_EL2), 727 VNCR(ICH_HCR_EL2), 728 VNCR(ICH_VMCR_EL2), 729 730 VNCR(ICH_HFGRTR_EL2), 731 VNCR(ICH_HFGWTR_EL2), 732 VNCR(ICH_HFGITR_EL2), 733 734 NR_SYS_REGS /* Nothing after this line! */ 735 }; 736 737 struct resx { 738 u64 res0; 739 u64 res1; 740 }; 741 742 struct kvm_sysreg_masks { 743 struct resx mask[NR_SYS_REGS - __SANITISED_REG_START__]; 744 }; 745 746 static inline struct resx __kvm_get_sysreg_resx(struct kvm_arch *arch, 747 enum vcpu_sysreg sr) 748 { 749 struct kvm_sysreg_masks *masks; 750 751 masks = arch->sysreg_masks; 752 if (likely(masks && 753 sr >= __SANITISED_REG_START__ && sr < NR_SYS_REGS)) 754 return masks->mask[sr - __SANITISED_REG_START__]; 755 756 return (struct resx){}; 757 } 758 759 #define kvm_get_sysreg_resx(k, sr) __kvm_get_sysreg_resx(&(k)->arch, (sr)) 760 761 static inline void __kvm_set_sysreg_resx(struct kvm_arch *arch, 762 enum vcpu_sysreg sr, struct resx resx) 763 { 764 arch->sysreg_masks->mask[sr - __SANITISED_REG_START__] = resx; 765 } 766 767 #define kvm_set_sysreg_resx(k, sr, resx) \ 768 __kvm_set_sysreg_resx(&(k)->arch, (sr), (resx)) 769 770 struct fgt_masks { 771 const char *str; 772 u64 mask; 773 u64 nmask; 774 u64 res0; 775 u64 res1; 776 }; 777 778 extern struct fgt_masks hfgrtr_masks; 779 extern struct fgt_masks hfgwtr_masks; 780 extern struct fgt_masks hfgitr_masks; 781 extern struct fgt_masks hdfgrtr_masks; 782 extern struct fgt_masks hdfgwtr_masks; 783 extern struct fgt_masks hafgrtr_masks; 784 extern struct fgt_masks hfgrtr2_masks; 785 extern struct fgt_masks hfgwtr2_masks; 786 extern struct fgt_masks hfgitr2_masks; 787 extern struct fgt_masks hdfgrtr2_masks; 788 extern struct fgt_masks hdfgwtr2_masks; 789 extern struct fgt_masks ich_hfgrtr_masks; 790 extern struct fgt_masks ich_hfgwtr_masks; 791 extern struct fgt_masks ich_hfgitr_masks; 792 793 extern struct fgt_masks kvm_nvhe_sym(hfgrtr_masks); 794 extern struct fgt_masks kvm_nvhe_sym(hfgwtr_masks); 795 extern struct fgt_masks kvm_nvhe_sym(hfgitr_masks); 796 extern struct fgt_masks kvm_nvhe_sym(hdfgrtr_masks); 797 extern struct fgt_masks kvm_nvhe_sym(hdfgwtr_masks); 798 extern struct fgt_masks kvm_nvhe_sym(hafgrtr_masks); 799 extern struct fgt_masks kvm_nvhe_sym(hfgrtr2_masks); 800 extern struct fgt_masks kvm_nvhe_sym(hfgwtr2_masks); 801 extern struct fgt_masks kvm_nvhe_sym(hfgitr2_masks); 802 extern struct fgt_masks kvm_nvhe_sym(hdfgrtr2_masks); 803 extern struct fgt_masks kvm_nvhe_sym(hdfgwtr2_masks); 804 extern struct fgt_masks kvm_nvhe_sym(ich_hfgrtr_masks); 805 extern struct fgt_masks kvm_nvhe_sym(ich_hfgwtr_masks); 806 extern struct fgt_masks kvm_nvhe_sym(ich_hfgitr_masks); 807 808 struct kvm_cpu_context { 809 struct user_pt_regs regs; /* sp = sp_el0 */ 810 811 u64 spsr_abt; 812 u64 spsr_und; 813 u64 spsr_irq; 814 u64 spsr_fiq; 815 816 struct user_fpsimd_state fp_regs; 817 818 u64 sys_regs[NR_SYS_REGS]; 819 820 struct kvm_vcpu *__hyp_running_vcpu; 821 822 /* This pointer has to be 4kB aligned. */ 823 u64 *vncr_array; 824 }; 825 826 /* 827 * This structure is instantiated on a per-CPU basis, and contains 828 * data that is: 829 * 830 * - tied to a single physical CPU, and 831 * - either have a lifetime that does not extend past vcpu_put() 832 * - or is an invariant for the lifetime of the system 833 * 834 * Use host_data_ptr(field) as a way to access a pointer to such a 835 * field. 836 */ 837 struct kvm_host_data { 838 #define KVM_HOST_DATA_FLAG_HAS_SPE 0 839 #define KVM_HOST_DATA_FLAG_HAS_TRBE 1 840 #define KVM_HOST_DATA_FLAG_TRBE_ENABLED 2 841 #define KVM_HOST_DATA_FLAG_EL1_TRACING_CONFIGURED 3 842 #define KVM_HOST_DATA_FLAG_VCPU_IN_HYP_CONTEXT 4 843 #define KVM_HOST_DATA_FLAG_L1_VNCR_MAPPED 5 844 #define KVM_HOST_DATA_FLAG_HAS_BRBE 6 845 unsigned long flags; 846 847 struct kvm_cpu_context host_ctxt; 848 849 /* 850 * Hyp VA. 851 * sve_regs is only used in pKVM and if system_supports_sve(). 852 */ 853 struct arm64_sve_state *sve_regs; 854 855 /* Ownership of the FP regs */ 856 enum { 857 FP_STATE_FREE, 858 FP_STATE_HOST_OWNED, 859 FP_STATE_GUEST_OWNED, 860 } fp_owner; 861 862 /* 863 * host_debug_state contains the host registers which are 864 * saved and restored during world switches. 865 */ 866 struct { 867 /* {Break,watch}point registers */ 868 struct kvm_guest_debug_arch regs; 869 /* Statistical profiling extension */ 870 u64 pmscr_el1; 871 u64 pmblimitr_el1; 872 /* Self-hosted trace */ 873 u64 trfcr_el1; 874 u64 trblimitr_el1; 875 /* Values of trap registers for the host before guest entry. */ 876 u64 mdcr_el2; 877 u64 brbcr_el1; 878 } host_debug_state; 879 880 /* Guest trace filter value */ 881 u64 trfcr_while_in_guest; 882 883 /* Number of programmable event counters (PMCR_EL0.N) for this CPU */ 884 unsigned int nr_event_counters; 885 886 /* Number of debug breakpoints/watchpoints for this CPU (minus 1) */ 887 unsigned int debug_brps; 888 unsigned int debug_wrps; 889 890 /* Last vgic_irq part of the AP list recorded in an LR */ 891 struct vgic_irq *last_lr_irq; 892 893 /* PPI state tracking for GICv5-based guests */ 894 struct { 895 DECLARE_BITMAP(pendr, VGIC_V5_NR_PRIVATE_IRQS); 896 897 /* The saved state of the regs when leaving the guest */ 898 DECLARE_BITMAP(activer_exit, VGIC_V5_NR_PRIVATE_IRQS); 899 } vgic_v5_ppi_state; 900 }; 901 902 struct kvm_host_psci_config { 903 /* PSCI version used by host. */ 904 u32 version; 905 u32 smccc_version; 906 907 /* Function IDs used by host if version is v0.1. */ 908 struct psci_0_1_function_ids function_ids_0_1; 909 910 bool psci_0_1_cpu_suspend_implemented; 911 bool psci_0_1_cpu_on_implemented; 912 bool psci_0_1_cpu_off_implemented; 913 bool psci_0_1_migrate_implemented; 914 }; 915 916 extern struct kvm_host_psci_config kvm_nvhe_sym(kvm_host_psci_config); 917 #define kvm_host_psci_config CHOOSE_NVHE_SYM(kvm_host_psci_config) 918 919 extern s64 kvm_nvhe_sym(hyp_physvirt_offset); 920 #define hyp_physvirt_offset CHOOSE_NVHE_SYM(hyp_physvirt_offset) 921 922 extern u64 kvm_nvhe_sym(hyp_cpu_logical_map)[NR_CPUS]; 923 #define hyp_cpu_logical_map CHOOSE_NVHE_SYM(hyp_cpu_logical_map) 924 925 struct vncr_tlb; 926 927 struct kvm_vcpu_arch { 928 struct kvm_cpu_context ctxt; 929 const struct kvm_vcpu_ops *vcpu_ops; 930 931 /* 932 * Guest floating point state 933 * 934 * The architecture has two main floating point extensions, 935 * the original FPSIMD and SVE. These have overlapping 936 * register views, with the FPSIMD V registers occupying the 937 * low 128 bits of the SVE Z registers. When the core 938 * floating point code saves the register state of a task it 939 * records which view it saved in fp_type. 940 */ 941 struct arm64_sve_state *sve_state; 942 enum fp_type fp_type; 943 unsigned int sve_max_vl; 944 945 /* Stage 2 paging state used by the hardware on next switch */ 946 struct kvm_s2_mmu *hw_mmu; 947 948 /* Values of trap registers for the guest. */ 949 u64 hcr_el2; 950 u64 hcrx_el2; 951 u64 mdcr_el2; 952 953 struct { 954 u64 r; 955 u64 w; 956 } fgt[__NR_FGT_GROUP_IDS__]; 957 958 /* Exception Information */ 959 struct kvm_vcpu_fault_info fault; 960 961 /* Configuration flags, set once and for all before the vcpu can run */ 962 u8 cflags; 963 964 /* Input flags to the hypervisor code, potentially cleared after use */ 965 u8 iflags; 966 967 /* State flags for kernel bookkeeping, unused by the hypervisor code */ 968 u16 sflags; 969 970 /* 971 * Don't run the guest (internal implementation need). 972 * 973 * Contrary to the flags above, this is updated outside of 974 * a vcpu context, and thus cannot be mixed with the flags. 975 */ 976 atomic_t pause; 977 978 /* 979 * We maintain more than a single set of debug registers to support 980 * debugging the guest from the host and to maintain separate host and 981 * guest state during world switches. vcpu_debug_state are the debug 982 * registers of the vcpu as the guest sees them. 983 * 984 * external_debug_state contains the debug values we want to debug the 985 * guest. This is set via the KVM_SET_GUEST_DEBUG ioctl. 986 */ 987 struct kvm_guest_debug_arch vcpu_debug_state; 988 struct kvm_guest_debug_arch external_debug_state; 989 u64 external_mdscr_el1; 990 991 enum { 992 VCPU_DEBUG_FREE, 993 VCPU_DEBUG_HOST_OWNED, 994 VCPU_DEBUG_GUEST_OWNED, 995 } debug_owner; 996 997 /* VGIC state */ 998 struct vgic_cpu vgic_cpu; 999 struct arch_timer_cpu timer_cpu; 1000 struct kvm_pmu pmu; 1001 1002 /* vcpu power state */ 1003 struct kvm_mp_state mp_state; 1004 spinlock_t mp_state_lock; 1005 1006 /* Cache some mmu pages needed inside spinlock regions */ 1007 struct kvm_mmu_memory_cache mmu_page_cache; 1008 1009 /* Pages to top-up the pKVM/EL2 guest stage-2 pool */ 1010 struct kvm_hyp_memcache stage2_mc; 1011 1012 /* Virtual SError ESR to restore when HCR_EL2.VSE is set */ 1013 u64 vsesr_el2; 1014 1015 /* Additional reset state */ 1016 struct vcpu_reset_state reset_state; 1017 1018 /* Guest PV state */ 1019 struct { 1020 u64 last_steal; 1021 gpa_t base; 1022 } steal; 1023 1024 /* Per-vcpu CCSIDR override or NULL */ 1025 u32 *ccsidr; 1026 1027 /* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */ 1028 struct vncr_tlb *vncr_tlb; 1029 1030 /* Hyp-readable copy of kvm_vcpu::pid */ 1031 pid_t pid; 1032 }; 1033 1034 #if ARM64_S390_COMMON == 1 1035 /* 1036 * Each 'flag' is composed of a comma-separated triplet: 1037 * 1038 * - the flag-set it belongs to in the vcpu->arch structure 1039 * - the value for that flag 1040 * - the mask for that flag 1041 * 1042 * __vcpu_single_flag() builds such a triplet for a single-bit flag. 1043 * unpack_vcpu_flag() extract the flag value from the triplet for 1044 * direct use outside of the flag accessors. 1045 */ 1046 #define __vcpu_single_flag(_set, _f) _set, (_f), (_f) 1047 1048 #define __unpack_flag(_set, _f, _m) _f 1049 #define unpack_vcpu_flag(...) __unpack_flag(__VA_ARGS__) 1050 1051 #define __build_check_flag(v, flagset, f, m) \ 1052 do { \ 1053 typeof(v->arch.flagset) *_fset; \ 1054 \ 1055 /* Check that the flags fit in the mask */ \ 1056 BUILD_BUG_ON(HWEIGHT(m) != HWEIGHT((f) | (m))); \ 1057 /* Check that the flags fit in the type */ \ 1058 BUILD_BUG_ON((sizeof(*_fset) * 8) <= __fls(m)); \ 1059 } while (0) 1060 1061 #define __vcpu_get_flag(v, flagset, f, m) \ 1062 ({ \ 1063 __build_check_flag(v, flagset, f, m); \ 1064 \ 1065 READ_ONCE(v->arch.flagset) & (m); \ 1066 }) 1067 1068 /* 1069 * Note that the set/clear accessors must be preempt-safe in order to 1070 * avoid nesting them with load/put which also manipulate flags... 1071 */ 1072 #ifdef __KVM_NVHE_HYPERVISOR__ 1073 /* the nVHE hypervisor is always non-preemptible */ 1074 #define __vcpu_flags_preempt_disable() 1075 #define __vcpu_flags_preempt_enable() 1076 #else 1077 #define __vcpu_flags_preempt_disable() preempt_disable() 1078 #define __vcpu_flags_preempt_enable() preempt_enable() 1079 #endif 1080 1081 #define __vcpu_set_flag(v, flagset, f, m) \ 1082 do { \ 1083 typeof(v->arch.flagset) *fset; \ 1084 \ 1085 __build_check_flag(v, flagset, f, m); \ 1086 \ 1087 fset = &v->arch.flagset; \ 1088 __vcpu_flags_preempt_disable(); \ 1089 if (HWEIGHT(m) > 1) \ 1090 *fset &= ~(m); \ 1091 *fset |= (f); \ 1092 __vcpu_flags_preempt_enable(); \ 1093 } while (0) 1094 1095 #define __vcpu_clear_flag(v, flagset, f, m) \ 1096 do { \ 1097 typeof(v->arch.flagset) *fset; \ 1098 \ 1099 __build_check_flag(v, flagset, f, m); \ 1100 \ 1101 fset = &v->arch.flagset; \ 1102 __vcpu_flags_preempt_disable(); \ 1103 *fset &= ~(m); \ 1104 __vcpu_flags_preempt_enable(); \ 1105 } while (0) 1106 1107 #define __vcpu_test_and_clear_flag(v, flagset, f, m) \ 1108 ({ \ 1109 typeof(v->arch.flagset) set; \ 1110 \ 1111 set = __vcpu_get_flag(v, flagset, f, m); \ 1112 __vcpu_clear_flag(v, flagset, f, m); \ 1113 \ 1114 set; \ 1115 }) 1116 1117 #define __vcpu_copy_flag(vt, vs, flagset, f, m) \ 1118 do { \ 1119 typeof(vs->arch.flagset) tmp, val; \ 1120 \ 1121 __build_check_flag(vs, flagset, f, m); \ 1122 \ 1123 val = READ_ONCE(vs->arch.flagset); \ 1124 val &= (m); \ 1125 __vcpu_flags_preempt_disable(); \ 1126 tmp = READ_ONCE(vt->arch.flagset); \ 1127 tmp &= ~(m); \ 1128 tmp |= val; \ 1129 WRITE_ONCE(vt->arch.flagset, tmp); \ 1130 __vcpu_flags_preempt_enable(); \ 1131 } while (0) 1132 1133 #define vcpu_get_flag(v, ...) __vcpu_get_flag((v), __VA_ARGS__) 1134 #define vcpu_set_flag(v, ...) __vcpu_set_flag((v), __VA_ARGS__) 1135 #define vcpu_clear_flag(v, ...) __vcpu_clear_flag((v), __VA_ARGS__) 1136 #define vcpu_test_and_clear_flag(v, ...) \ 1137 __vcpu_test_and_clear_flag((v), __VA_ARGS__) 1138 #define vcpu_copy_flag(vt, vs, ...) __vcpu_copy_flag((vt), (vs), __VA_ARGS__) 1139 1140 /* KVM_ARM_VCPU_INIT completed */ 1141 #define VCPU_INITIALIZED __vcpu_single_flag(cflags, BIT(0)) 1142 /* SVE config completed */ 1143 #define VCPU_SVE_FINALIZED __vcpu_single_flag(cflags, BIT(1)) 1144 /* pKVM VCPU setup completed */ 1145 #define VCPU_PKVM_FINALIZED __vcpu_single_flag(cflags, BIT(2)) 1146 1147 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature); 1148 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu); 1149 1150 #define kvm_arm_vcpu_sve_finalized(vcpu) vcpu_get_flag(vcpu, VCPU_SVE_FINALIZED) 1151 1152 /* Exception pending */ 1153 #define PENDING_EXCEPTION __vcpu_single_flag(iflags, BIT(0)) 1154 /* 1155 * PC increment. Overlaps with EXCEPT_MASK on purpose so that it can't 1156 * be set together with an exception... 1157 */ 1158 #define INCREMENT_PC __vcpu_single_flag(iflags, BIT(1)) 1159 /* Target EL/MODE (not a single flag, but let's abuse the macro) */ 1160 #define EXCEPT_MASK __vcpu_single_flag(iflags, GENMASK(3, 1)) 1161 /* Cover both PENDING_EXCEPTION and EXCEPT_MASK for global operations */ 1162 #define PC_UPDATE_REQ __vcpu_single_flag(iflags, GENMASK(3, 0)) 1163 /* Host-set: the hyp flushes the non-protected vCPU state in on entry */ 1164 #define PKVM_HOST_STATE_DIRTY __vcpu_single_flag(iflags, BIT(4)) 1165 1166 /* Helpers to encode exceptions with minimum fuss */ 1167 #define __EXCEPT_MASK_VAL unpack_vcpu_flag(EXCEPT_MASK) 1168 #define __EXCEPT_SHIFT __builtin_ctzl(__EXCEPT_MASK_VAL) 1169 #define __vcpu_except_flags(_f) iflags, (_f << __EXCEPT_SHIFT), __EXCEPT_MASK_VAL 1170 1171 /* 1172 * When PENDING_EXCEPTION is set, EXCEPT_MASK can take the following 1173 * values: 1174 * 1175 * For AArch32 EL1: 1176 */ 1177 #define EXCEPT_AA32_UND __vcpu_except_flags(0) 1178 #define EXCEPT_AA32_IABT __vcpu_except_flags(1) 1179 #define EXCEPT_AA32_DABT __vcpu_except_flags(2) 1180 /* For AArch64: */ 1181 #define EXCEPT_AA64_EL1_SYNC __vcpu_except_flags(0) 1182 #define EXCEPT_AA64_EL1_IRQ __vcpu_except_flags(1) 1183 #define EXCEPT_AA64_EL1_FIQ __vcpu_except_flags(2) 1184 #define EXCEPT_AA64_EL1_SERR __vcpu_except_flags(3) 1185 /* For AArch64 with NV: */ 1186 #define EXCEPT_AA64_EL2_SYNC __vcpu_except_flags(4) 1187 #define EXCEPT_AA64_EL2_IRQ __vcpu_except_flags(5) 1188 #define EXCEPT_AA64_EL2_FIQ __vcpu_except_flags(6) 1189 #define EXCEPT_AA64_EL2_SERR __vcpu_except_flags(7) 1190 1191 /* Physical CPU not in supported_cpus */ 1192 #define ON_UNSUPPORTED_CPU __vcpu_single_flag(sflags, BIT(0)) 1193 /* WFIT instruction trapped */ 1194 #define IN_WFIT __vcpu_single_flag(sflags, BIT(1)) 1195 /* vcpu system registers loaded on physical CPU */ 1196 #define SYSREGS_ON_CPU __vcpu_single_flag(sflags, BIT(2)) 1197 /* Software step state is Active-pending for external debug */ 1198 #define HOST_SS_ACTIVE_PENDING __vcpu_single_flag(sflags, BIT(3)) 1199 /* Software step state is Active pending for guest debug */ 1200 #define GUEST_SS_ACTIVE_PENDING __vcpu_single_flag(sflags, BIT(4)) 1201 /* PMUSERENR for the guest EL0 is on physical CPU */ 1202 #define PMUSERENR_ON_CPU __vcpu_single_flag(sflags, BIT(5)) 1203 /* WFI instruction trapped */ 1204 #define IN_WFI __vcpu_single_flag(sflags, BIT(6)) 1205 /* KVM is currently emulating a nested ERET */ 1206 #define IN_NESTED_ERET __vcpu_single_flag(sflags, BIT(7)) 1207 /* SError pending for nested guest */ 1208 #define NESTED_SERROR_PENDING __vcpu_single_flag(sflags, BIT(8)) 1209 /* KVM is currently emulating an L2 to L1 exception */ 1210 #define IN_NESTED_EXCEPTION __vcpu_single_flag(sflags, BIT(9)) 1211 1212 #endif /* ARM64_S390_COMMON == 1 */ 1213 1214 #define vcpu_sve_max_vq(vcpu) sve_vq_from_vl((vcpu)->arch.sve_max_vl) 1215 1216 #define vcpu_sve_zcr_elx(vcpu) \ 1217 (unlikely(is_hyp_ctxt(vcpu)) ? ZCR_EL2 : ZCR_EL1) 1218 1219 #define sve_state_size_from_vl(sve_max_vl) ({ \ 1220 size_t __size_ret; \ 1221 unsigned int __vq; \ 1222 \ 1223 if (WARN_ON(!sve_vl_valid(sve_max_vl))) { \ 1224 __size_ret = 0; \ 1225 } else { \ 1226 __vq = sve_vq_from_vl(sve_max_vl); \ 1227 __size_ret = SVE_SIG_REGS_SIZE(__vq); \ 1228 } \ 1229 \ 1230 __size_ret; \ 1231 }) 1232 1233 #define vcpu_sve_state_size(vcpu) sve_state_size_from_vl((vcpu)->arch.sve_max_vl) 1234 1235 #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \ 1236 KVM_GUESTDBG_USE_SW_BP | \ 1237 KVM_GUESTDBG_USE_HW | \ 1238 KVM_GUESTDBG_SINGLESTEP) 1239 1240 #define kvm_has_sve(kvm) (system_supports_sve() && \ 1241 test_bit(KVM_ARCH_FLAG_GUEST_HAS_SVE, &(kvm)->arch.flags)) 1242 1243 #ifdef __KVM_NVHE_HYPERVISOR__ 1244 #define vcpu_has_sve(vcpu) kvm_has_sve(kern_hyp_va((vcpu)->kvm)) 1245 #else 1246 #define vcpu_has_sve(vcpu) kvm_has_sve((vcpu)->kvm) 1247 #endif 1248 1249 #ifdef CONFIG_ARM64_PTR_AUTH 1250 #define vcpu_has_ptrauth(vcpu) \ 1251 ((cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH) || \ 1252 cpus_have_final_cap(ARM64_HAS_GENERIC_AUTH)) && \ 1253 (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_ADDRESS) || \ 1254 vcpu_has_feature(vcpu, KVM_ARM_VCPU_PTRAUTH_GENERIC))) 1255 #else 1256 #define vcpu_has_ptrauth(vcpu) false 1257 #endif 1258 1259 #define kvm_id_regs_final(kvm) \ 1260 test_bit(KVM_ARCH_FLAG_ID_REGS_FINAL, &(kvm)->arch.flags) 1261 1262 #define vcpu_on_unsupported_cpu(vcpu) \ 1263 vcpu_get_flag(vcpu, ON_UNSUPPORTED_CPU) 1264 1265 #define vcpu_set_on_unsupported_cpu(vcpu) \ 1266 vcpu_set_flag(vcpu, ON_UNSUPPORTED_CPU) 1267 1268 #define vcpu_clear_on_unsupported_cpu(vcpu) \ 1269 vcpu_clear_flag(vcpu, ON_UNSUPPORTED_CPU) 1270 1271 #define vcpu_gp_regs(v) ((v)->arch.ctxt.regs.regs) 1272 1273 /* 1274 * Only use __vcpu_sys_reg/ctxt_sys_reg if you know you want the 1275 * memory backed version of a register, and not the one most recently 1276 * accessed by a running VCPU. For example, for userspace access or 1277 * for system registers that are never context switched, but only 1278 * emulated. 1279 * 1280 * Don't bother with VNCR-based accesses in the nVHE code, it has no 1281 * business dealing with NV. 1282 */ 1283 static inline u64 *___ctxt_sys_reg(const struct kvm_cpu_context *ctxt, int r) 1284 { 1285 #if !defined (__KVM_NVHE_HYPERVISOR__) 1286 if (unlikely(cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) && 1287 r >= __VNCR_START__ && ctxt->vncr_array)) 1288 return &ctxt->vncr_array[r - __VNCR_START__]; 1289 #endif 1290 return (u64 *)&ctxt->sys_regs[r]; 1291 } 1292 1293 #define __ctxt_sys_reg(c,r) \ 1294 ({ \ 1295 BUILD_BUG_ON(__builtin_constant_p(r) && \ 1296 (r) >= NR_SYS_REGS); \ 1297 ___ctxt_sys_reg(c, r); \ 1298 }) 1299 1300 #define ctxt_sys_reg(c,r) (*__ctxt_sys_reg(c,r)) 1301 1302 u64 kvm_vcpu_apply_reg_masks(const struct kvm_vcpu *, enum vcpu_sysreg, u64); 1303 1304 #define __vcpu_assign_sys_reg(v, r, val) \ 1305 do { \ 1306 const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \ 1307 u64 __v = (val); \ 1308 if (vcpu_has_nv((v)) && (r) >= __SANITISED_REG_START__) \ 1309 __v = kvm_vcpu_apply_reg_masks((v), (r), __v); \ 1310 \ 1311 ctxt_sys_reg(ctxt, (r)) = __v; \ 1312 } while (0) 1313 1314 #define __vcpu_rmw_sys_reg(v, r, op, val) \ 1315 do { \ 1316 const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \ 1317 u64 __v = ctxt_sys_reg(ctxt, (r)); \ 1318 __v op (val); \ 1319 if (vcpu_has_nv((v)) && (r) >= __SANITISED_REG_START__) \ 1320 __v = kvm_vcpu_apply_reg_masks((v), (r), __v); \ 1321 \ 1322 ctxt_sys_reg(ctxt, (r)) = __v; \ 1323 } while (0) 1324 1325 #define __vcpu_sys_reg(v,r) \ 1326 ({ \ 1327 const struct kvm_cpu_context *ctxt = &(v)->arch.ctxt; \ 1328 u64 __v = ctxt_sys_reg(ctxt, (r)); \ 1329 if (vcpu_has_nv((v)) && (r) >= __SANITISED_REG_START__) \ 1330 __v = kvm_vcpu_apply_reg_masks((v), (r), __v); \ 1331 __v; \ 1332 }) 1333 1334 u64 vcpu_read_sys_reg(const struct kvm_vcpu *, enum vcpu_sysreg); 1335 void vcpu_write_sys_reg(struct kvm_vcpu *, u64, enum vcpu_sysreg); 1336 1337 struct kvm_vm_stat { 1338 struct kvm_vm_stat_generic generic; 1339 }; 1340 1341 struct kvm_vcpu_stat { 1342 struct kvm_vcpu_stat_generic generic; 1343 u64 hvc_exit_stat; 1344 u64 wfe_exit_stat; 1345 u64 wfi_exit_stat; 1346 u64 mmio_exit_user; 1347 u64 mmio_exit_kernel; 1348 u64 signal_exits; 1349 u64 exits; 1350 }; 1351 1352 int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, 1353 struct kvm_vcpu_events *events); 1354 1355 int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, 1356 struct kvm_vcpu_events *events); 1357 1358 void kvm_arm_halt_guest(struct kvm *kvm); 1359 void kvm_arm_resume_guest(struct kvm *kvm); 1360 void kvm_pause_vcpu(struct kvm_vcpu *vcpu); 1361 void kvm_resume_vcpu(struct kvm_vcpu *vcpu); 1362 1363 #define vcpu_has_run_once(vcpu) (!!READ_ONCE((vcpu)->pid)) 1364 1365 int handle_exit(struct kvm_vcpu *vcpu, int exception_index); 1366 void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index); 1367 1368 int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu); 1369 int kvm_handle_cp14_32(struct kvm_vcpu *vcpu); 1370 int kvm_handle_cp14_64(struct kvm_vcpu *vcpu); 1371 int kvm_handle_cp15_32(struct kvm_vcpu *vcpu); 1372 int kvm_handle_cp15_64(struct kvm_vcpu *vcpu); 1373 int kvm_handle_sys_reg(struct kvm_vcpu *vcpu); 1374 int kvm_handle_cp10_id(struct kvm_vcpu *vcpu); 1375 1376 void kvm_sys_regs_create_debugfs(struct kvm *kvm); 1377 void kvm_reset_sys_regs(struct kvm_vcpu *vcpu); 1378 1379 int __init kvm_sys_reg_table_init(void); 1380 struct sys_reg_desc; 1381 int __init populate_sysreg_config(const struct sys_reg_desc *sr, 1382 unsigned int idx, bool needs_nv_trap); 1383 int __init populate_nv_trap_config(void); 1384 1385 void kvm_calculate_traps(struct kvm_vcpu *vcpu); 1386 1387 #if ARM64_S390_COMMON == 1 1388 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu); 1389 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices); 1390 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 1391 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg); 1392 1393 unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu); 1394 int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices); 1395 1396 /* MMIO helpers */ 1397 void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data); 1398 unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len); 1399 1400 int kvm_handle_mmio_return(struct kvm_vcpu *vcpu); 1401 int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa); 1402 1403 #endif /* ARM64_S390_COMMON == 1 */ 1404 1405 /* 1406 * Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event, 1407 * arrived in guest context. For arm64, any event that arrives while a vCPU is 1408 * loaded is considered to be "in guest". 1409 */ 1410 static inline bool kvm_arch_pmi_in_guest(struct kvm_vcpu *vcpu) 1411 { 1412 return IS_ENABLED(CONFIG_GUEST_PERF_EVENTS) && !!vcpu; 1413 } 1414 1415 long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu); 1416 gpa_t kvm_init_stolen_time(struct kvm_vcpu *vcpu); 1417 void kvm_update_stolen_time(struct kvm_vcpu *vcpu); 1418 1419 bool kvm_arm_pvtime_supported(struct kvm *kvm); 1420 int kvm_arm_pvtime_set_attr(struct kvm_vcpu *vcpu, 1421 struct kvm_device_attr *attr); 1422 int kvm_arm_pvtime_get_attr(struct kvm_vcpu *vcpu, 1423 struct kvm_device_attr *attr); 1424 int kvm_arm_pvtime_has_attr(struct kvm_vcpu *vcpu, 1425 struct kvm_device_attr *attr); 1426 1427 extern unsigned int __ro_after_init kvm_arm_vmid_bits; 1428 int __init kvm_arm_vmid_alloc_init(void); 1429 void __init kvm_arm_vmid_alloc_free(void); 1430 void kvm_arm_vmid_update(struct kvm_vmid *kvm_vmid); 1431 void kvm_arm_vmid_clear_active(void); 1432 1433 static inline void kvm_arm_pvtime_vcpu_init(struct kvm_vcpu_arch *vcpu_arch) 1434 { 1435 vcpu_arch->steal.base = INVALID_GPA; 1436 } 1437 1438 static inline bool kvm_arm_is_pvtime_enabled(struct kvm_vcpu_arch *vcpu_arch) 1439 { 1440 return (vcpu_arch->steal.base != INVALID_GPA); 1441 } 1442 1443 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr); 1444 1445 DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data); 1446 1447 /* 1448 * How we access per-CPU host data depends on the where we access it from, 1449 * and the mode we're in: 1450 * 1451 * - VHE and nVHE hypervisor bits use their locally defined instance 1452 * 1453 * - the rest of the kernel use either the VHE or nVHE one, depending on 1454 * the mode we're running in. 1455 * 1456 * Unless we're in protected mode, fully deprivileged, and the nVHE 1457 * per-CPU stuff is exclusively accessible to the protected EL2 code. 1458 * In this case, the EL1 code uses the *VHE* data as its private state 1459 * (which makes sense in a way as there shouldn't be any shared state 1460 * between the host and the hypervisor). 1461 * 1462 * Yes, this is all totally trivial. Shoot me now. 1463 */ 1464 #if defined(__KVM_NVHE_HYPERVISOR__) || defined(__KVM_VHE_HYPERVISOR__) 1465 #define host_data_ptr(f) (&this_cpu_ptr(&kvm_host_data)->f) 1466 #else 1467 #define host_data_ptr(f) \ 1468 (static_branch_unlikely(&kvm_protected_mode_initialized) ? \ 1469 &this_cpu_ptr(&kvm_host_data)->f : \ 1470 &this_cpu_ptr_hyp_sym(kvm_host_data)->f) 1471 #endif 1472 1473 #define host_data_test_flag(flag) \ 1474 (test_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags))) 1475 #define host_data_set_flag(flag) \ 1476 set_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags)) 1477 #define host_data_clear_flag(flag) \ 1478 clear_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags)) 1479 1480 /* Check whether the FP regs are owned by the guest */ 1481 static inline bool guest_owns_fp_regs(void) 1482 { 1483 return *host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED; 1484 } 1485 1486 /* Check whether the FP regs are owned by the host */ 1487 static inline bool host_owns_fp_regs(void) 1488 { 1489 return *host_data_ptr(fp_owner) == FP_STATE_HOST_OWNED; 1490 } 1491 1492 static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt) 1493 { 1494 /* The host's MPIDR is immutable, so let's set it up at boot time */ 1495 ctxt_sys_reg(cpu_ctxt, MPIDR_EL1) = read_cpuid_mpidr(); 1496 } 1497 1498 static inline bool kvm_system_needs_idmapped_vectors(void) 1499 { 1500 return cpus_have_final_cap(ARM64_SPECTRE_V3A); 1501 } 1502 1503 void kvm_init_host_debug_data(void); 1504 void kvm_debug_init_vhe(void); 1505 void kvm_vcpu_load_debug(struct kvm_vcpu *vcpu); 1506 void kvm_vcpu_put_debug(struct kvm_vcpu *vcpu); 1507 void kvm_debug_set_guest_ownership(struct kvm_vcpu *vcpu); 1508 void kvm_debug_handle_oslar(struct kvm_vcpu *vcpu, u64 val); 1509 1510 #define kvm_vcpu_os_lock_enabled(vcpu) \ 1511 (!!(__vcpu_sys_reg(vcpu, OSLSR_EL1) & OSLSR_EL1_OSLK)) 1512 1513 #define kvm_debug_regs_in_use(vcpu) \ 1514 ((vcpu)->arch.debug_owner != VCPU_DEBUG_FREE) 1515 #define kvm_host_owns_debug_regs(vcpu) \ 1516 ((vcpu)->arch.debug_owner == VCPU_DEBUG_HOST_OWNED) 1517 #define kvm_guest_owns_debug_regs(vcpu) \ 1518 ((vcpu)->arch.debug_owner == VCPU_DEBUG_GUEST_OWNED) 1519 1520 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, 1521 struct kvm_device_attr *attr); 1522 int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu, 1523 struct kvm_device_attr *attr); 1524 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu, 1525 struct kvm_device_attr *attr); 1526 1527 int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, 1528 struct kvm_arm_copy_mte_tags *copy_tags); 1529 int kvm_vm_ioctl_set_counter_offset(struct kvm *kvm, 1530 struct kvm_arm_counter_offset *offset); 1531 int kvm_vm_ioctl_get_reg_writable_masks(struct kvm *kvm, 1532 struct reg_mask_range *range); 1533 1534 /* Guest/host FPSIMD coordination helpers */ 1535 void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu); 1536 void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu); 1537 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu); 1538 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu); 1539 1540 static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr) 1541 { 1542 return (!has_vhe() && attr->exclude_host); 1543 } 1544 1545 #ifdef CONFIG_KVM 1546 void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr); 1547 void kvm_clr_pmu_events(u64 clr); 1548 bool kvm_set_pmuserenr(u64 val); 1549 void kvm_enable_trbe(void); 1550 void kvm_disable_trbe(void); 1551 void kvm_tracing_set_el1_configuration(u64 trfcr_while_in_guest); 1552 #else 1553 static inline void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr) {} 1554 static inline void kvm_clr_pmu_events(u64 clr) {} 1555 static inline bool kvm_set_pmuserenr(u64 val) 1556 { 1557 return false; 1558 } 1559 static inline void kvm_enable_trbe(void) {} 1560 static inline void kvm_disable_trbe(void) {} 1561 static inline void kvm_tracing_set_el1_configuration(u64 trfcr_while_in_guest) {} 1562 #endif 1563 1564 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu); 1565 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu); 1566 1567 int __init kvm_set_ipa_limit(void); 1568 u32 kvm_get_pa_bits(struct kvm *kvm); 1569 1570 #define __KVM_HAVE_ARCH_VM_ALLOC 1571 struct kvm *kvm_arch_alloc_vm(void); 1572 1573 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS 1574 1575 #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE 1576 1577 #ifdef __KVM_NVHE_HYPERVISOR__ 1578 1579 #define kvm_vm_is_protected(kvm) \ 1580 (is_protected_kvm_enabled() && ((kvm)->arch.vm_flavor == VM_PROTECTED_PKVM)) 1581 /* 1582 * Accessing vcpu->kvm from nVHE hyp stub is tricky, as we need to convert the 1583 * pointer to the hyp VA. With pKVM, the nVHE code runs with the hyp_vcpu, 1584 * which is populated correctly and is gated on is_protected_kvm_enabled(). 1585 */ 1586 #define vcpu_is_protected(vcpu) \ 1587 ({ \ 1588 struct kvm *__kvm = READ_ONCE((vcpu)->kvm); \ 1589 \ 1590 (__kvm && kvm_vm_is_protected(__kvm)); \ 1591 }) 1592 1593 #else 1594 1595 #define kvm_vm_is_protected(kvm) ((kvm)->arch.vm_flavor >= __VM_PROTECTED) 1596 #define vcpu_is_protected(vcpu) kvm_vm_is_protected((vcpu)->kvm) 1597 1598 #define kvm_vm_hyp_is_distrusting(kvm) ((kvm)->arch.vm_flavor >= __VM_DISTRUSTING_HYP) 1599 1600 #endif /* __KVM_NVHE_HYPERVISOR__ */ 1601 1602 #define kvm_has_mte(kvm) \ 1603 (system_supports_mte() && \ 1604 test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &(kvm)->arch.flags)) 1605 1606 #define kvm_supports_32bit_el0() \ 1607 (system_supports_32bit_el0() && \ 1608 !static_branch_unlikely(&arm64_mismatched_32bit_el0)) 1609 1610 #define kvm_vm_has_ran_once(kvm) \ 1611 (test_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &(kvm)->arch.flags)) 1612 1613 static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature) 1614 { 1615 return test_bit(feature, ka->vcpu_features); 1616 } 1617 1618 #if ARM64_S390_COMMON == 1 1619 1620 #define kvm_vcpu_has_feature(k, f) __vcpu_has_feature(&(k)->arch, (f)) 1621 #define vcpu_has_feature(v, f) __vcpu_has_feature(&(v)->kvm->arch, (f)) 1622 1623 #define kvm_vcpu_initialized(v) vcpu_get_flag(v, VCPU_INITIALIZED) 1624 1625 #endif /* ARM64_S390_COMMON == 1 */ 1626 1627 int kvm_trng_call(struct kvm_vcpu *vcpu); 1628 #ifdef CONFIG_KVM 1629 extern phys_addr_t hyp_mem_base; 1630 extern phys_addr_t hyp_mem_size; 1631 void __init kvm_hyp_reserve(void); 1632 #else 1633 static inline void kvm_hyp_reserve(void) { } 1634 #endif 1635 1636 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu); 1637 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu); 1638 bool kvm_pkvm_vcpu_is_powered_off(struct kvm_vcpu *vcpu); 1639 1640 static inline u64 *__vm_id_reg(struct kvm_arch *ka, u32 reg) 1641 { 1642 switch (reg) { 1643 case sys_reg(3, 0, 0, 1, 0) ... sys_reg(3, 0, 0, 7, 7): 1644 return &ka->id_regs[IDREG_IDX(reg)]; 1645 case SYS_CTR_EL0: 1646 return &ka->ctr_el0; 1647 case SYS_MIDR_EL1: 1648 return &ka->midr_el1; 1649 case SYS_REVIDR_EL1: 1650 return &ka->revidr_el1; 1651 case SYS_AIDR_EL1: 1652 return &ka->aidr_el1; 1653 default: 1654 WARN_ON_ONCE(1); 1655 return NULL; 1656 } 1657 } 1658 1659 #define kvm_read_vm_id_reg(kvm, reg) \ 1660 ({ u64 __val = *__vm_id_reg(&(kvm)->arch, reg); __val; }) 1661 1662 void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val); 1663 1664 #define __expand_field_sign_unsigned(id, fld, val) \ 1665 ((u64)SYS_FIELD_VALUE(id, fld, val)) 1666 1667 #define __expand_field_sign_signed(id, fld, val) \ 1668 ({ \ 1669 u64 __val = SYS_FIELD_VALUE(id, fld, val); \ 1670 sign_extend64(__val, id##_##fld##_WIDTH - 1); \ 1671 }) 1672 1673 #define get_idreg_field_unsigned(kvm, id, fld) \ 1674 ({ \ 1675 u64 __val = kvm_read_vm_id_reg((kvm), SYS_##id); \ 1676 FIELD_GET(id##_##fld##_MASK, __val); \ 1677 }) 1678 1679 #define get_idreg_field_signed(kvm, id, fld) \ 1680 ({ \ 1681 u64 __val = get_idreg_field_unsigned(kvm, id, fld); \ 1682 sign_extend64(__val, id##_##fld##_WIDTH - 1); \ 1683 }) 1684 1685 #define get_idreg_field_enum(kvm, id, fld) \ 1686 get_idreg_field_unsigned(kvm, id, fld) 1687 1688 #define kvm_cmp_feat_signed(kvm, id, fld, op, limit) \ 1689 (get_idreg_field_signed((kvm), id, fld) op __expand_field_sign_signed(id, fld, limit)) 1690 1691 #define kvm_cmp_feat_unsigned(kvm, id, fld, op, limit) \ 1692 (get_idreg_field_unsigned((kvm), id, fld) op __expand_field_sign_unsigned(id, fld, limit)) 1693 1694 #define kvm_cmp_feat(kvm, id, fld, op, limit) \ 1695 (id##_##fld##_SIGNED ? \ 1696 kvm_cmp_feat_signed(kvm, id, fld, op, limit) : \ 1697 kvm_cmp_feat_unsigned(kvm, id, fld, op, limit)) 1698 1699 #define __kvm_has_feat(kvm, id, fld, limit) \ 1700 kvm_cmp_feat(kvm, id, fld, >=, limit) 1701 1702 #define kvm_has_feat(kvm, ...) __kvm_has_feat(kvm, __VA_ARGS__) 1703 1704 #define __kvm_has_feat_enum(kvm, id, fld, val) \ 1705 kvm_cmp_feat_unsigned(kvm, id, fld, ==, val) 1706 1707 #define kvm_has_feat_enum(kvm, ...) __kvm_has_feat_enum(kvm, __VA_ARGS__) 1708 1709 #define kvm_has_feat_range(kvm, id, fld, min, max) \ 1710 (kvm_cmp_feat(kvm, id, fld, >=, min) && \ 1711 kvm_cmp_feat(kvm, id, fld, <=, max)) 1712 1713 /* Check for a given level of PAuth support */ 1714 #define kvm_has_pauth(k, l) \ 1715 ({ \ 1716 bool pa, pi, pa3; \ 1717 \ 1718 pa = kvm_has_feat((k), ID_AA64ISAR1_EL1, APA, l); \ 1719 pa &= kvm_has_feat((k), ID_AA64ISAR1_EL1, GPA, IMP); \ 1720 pi = kvm_has_feat((k), ID_AA64ISAR1_EL1, API, l); \ 1721 pi &= kvm_has_feat((k), ID_AA64ISAR1_EL1, GPI, IMP); \ 1722 pa3 = kvm_has_feat((k), ID_AA64ISAR2_EL1, APA3, l); \ 1723 pa3 &= kvm_has_feat((k), ID_AA64ISAR2_EL1, GPA3, IMP); \ 1724 \ 1725 (pa + pi + pa3) == 1; \ 1726 }) 1727 1728 #define kvm_has_fpmr(k) \ 1729 (system_supports_fpmr() && \ 1730 kvm_has_feat((k), ID_AA64PFR2_EL1, FPMR, IMP)) 1731 1732 #define kvm_has_tcr2(k) \ 1733 (kvm_has_feat((k), ID_AA64MMFR3_EL1, TCRX, IMP)) 1734 1735 #define kvm_has_s1pie(k) \ 1736 (kvm_has_feat((k), ID_AA64MMFR3_EL1, S1PIE, IMP)) 1737 1738 #define kvm_has_s1poe(k) \ 1739 (system_supports_poe() && \ 1740 kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP)) 1741 1742 #define kvm_has_ras(k) \ 1743 (kvm_has_feat((k), ID_AA64PFR0_EL1, RAS, IMP)) 1744 1745 #define kvm_has_sctlr2(k) \ 1746 (kvm_has_feat((k), ID_AA64MMFR3_EL1, SCTLRX, IMP)) 1747 1748 static inline bool kvm_arch_has_irq_bypass(void) 1749 { 1750 return true; 1751 } 1752 1753 void kvm_idregs_enforce_dependencies(struct kvm *kvm); 1754 void compute_fgu(struct kvm *kvm, enum fgt_group_id fgt); 1755 struct resx get_reg_fixed_bits(struct kvm *kvm, enum vcpu_sysreg reg); 1756 void check_feature_map(void); 1757 void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu); 1758 1759 static __always_inline enum fgt_group_id __fgt_reg_to_group_id(enum vcpu_sysreg reg) 1760 { 1761 switch (reg) { 1762 case HFGRTR_EL2: 1763 case HFGWTR_EL2: 1764 return HFGRTR_GROUP; 1765 case HFGITR_EL2: 1766 return HFGITR_GROUP; 1767 case HDFGRTR_EL2: 1768 case HDFGWTR_EL2: 1769 return HDFGRTR_GROUP; 1770 case HAFGRTR_EL2: 1771 return HAFGRTR_GROUP; 1772 case HFGRTR2_EL2: 1773 case HFGWTR2_EL2: 1774 return HFGRTR2_GROUP; 1775 case HFGITR2_EL2: 1776 return HFGITR2_GROUP; 1777 case HDFGRTR2_EL2: 1778 case HDFGWTR2_EL2: 1779 return HDFGRTR2_GROUP; 1780 case ICH_HFGRTR_EL2: 1781 case ICH_HFGWTR_EL2: 1782 return ICH_HFGRTR_GROUP; 1783 case ICH_HFGITR_EL2: 1784 return ICH_HFGITR_GROUP; 1785 default: 1786 BUILD_BUG_ON(1); 1787 } 1788 } 1789 1790 #define vcpu_fgt(vcpu, reg) \ 1791 ({ \ 1792 enum fgt_group_id id = __fgt_reg_to_group_id(reg); \ 1793 u64 *p; \ 1794 switch (reg) { \ 1795 case HFGWTR_EL2: \ 1796 case HDFGWTR_EL2: \ 1797 case HFGWTR2_EL2: \ 1798 case HDFGWTR2_EL2: \ 1799 case ICH_HFGWTR_EL2: \ 1800 p = &(vcpu)->arch.fgt[id].w; \ 1801 break; \ 1802 default: \ 1803 p = &(vcpu)->arch.fgt[id].r; \ 1804 break; \ 1805 } \ 1806 \ 1807 p; \ 1808 }) 1809 1810 long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext); 1811 1812 #endif /* __ARM64_KVM_HOST_H__ */ 1813