1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University 4 * Author: Christoffer Dall <c.dall@virtualopensystems.com> 5 */ 6 7 #include <linux/bug.h> 8 #include <linux/cpu_pm.h> 9 #include <linux/entry-kvm.h> 10 #include <linux/errno.h> 11 #include <linux/err.h> 12 #include <linux/kvm_host.h> 13 #include <linux/list.h> 14 #include <linux/module.h> 15 #include <linux/vmalloc.h> 16 #include <linux/fs.h> 17 #include <linux/mman.h> 18 #include <linux/sched.h> 19 #include <linux/kvm.h> 20 #include <linux/kvm_irqfd.h> 21 #include <linux/irqbypass.h> 22 #include <linux/sched/stat.h> 23 #include <linux/psci.h> 24 #include <trace/events/kvm.h> 25 26 #define CREATE_TRACE_POINTS 27 #include "trace_arm.h" 28 29 #include <linux/uaccess.h> 30 #include <asm/ptrace.h> 31 #include <asm/mman.h> 32 #include <asm/tlbflush.h> 33 #include <asm/cacheflush.h> 34 #include <asm/cpufeature.h> 35 #include <asm/virt.h> 36 #include <asm/kvm_arm.h> 37 #include <asm/kvm_asm.h> 38 #include <asm/kvm_emulate.h> 39 #include <asm/kvm_mmu.h> 40 #include <asm/kvm_nested.h> 41 #include <asm/kvm_pkvm.h> 42 #include <asm/kvm_ptrauth.h> 43 #include <asm/sections.h> 44 45 #include <kvm/arm_hypercalls.h> 46 #include <kvm/arm_pmu.h> 47 #include <kvm/arm_psci.h> 48 49 #include "sys_regs.h" 50 51 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT; 52 53 enum kvm_wfx_trap_policy { 54 KVM_WFX_NOTRAP_SINGLE_TASK, /* Default option */ 55 KVM_WFX_NOTRAP, 56 KVM_WFX_TRAP, 57 }; 58 59 static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK; 60 static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK; 61 62 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); 63 64 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base); 65 DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); 66 67 DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); 68 69 static bool vgic_present, kvm_arm_initialised; 70 71 static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized); 72 73 bool is_kvm_arm_initialised(void) 74 { 75 return kvm_arm_initialised; 76 } 77 78 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 79 { 80 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; 81 } 82 83 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, 84 struct kvm_enable_cap *cap) 85 { 86 int r = -EINVAL; 87 88 if (cap->flags) 89 return -EINVAL; 90 91 if (kvm_vm_is_protected(kvm) && !kvm_pvm_ext_allowed(cap->cap)) 92 return -EINVAL; 93 94 switch (cap->cap) { 95 case KVM_CAP_ARM_NISV_TO_USER: 96 r = 0; 97 set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, 98 &kvm->arch.flags); 99 break; 100 case KVM_CAP_ARM_MTE: 101 mutex_lock(&kvm->lock); 102 if (system_supports_mte() && !kvm->created_vcpus) { 103 r = 0; 104 set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags); 105 } 106 mutex_unlock(&kvm->lock); 107 break; 108 case KVM_CAP_ARM_SYSTEM_SUSPEND: 109 r = 0; 110 set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags); 111 break; 112 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE: 113 mutex_lock(&kvm->slots_lock); 114 /* 115 * To keep things simple, allow changing the chunk 116 * size only when no memory slots have been created. 117 */ 118 if (kvm_are_all_memslots_empty(kvm)) { 119 u64 new_cap = cap->args[0]; 120 121 if (!new_cap || kvm_is_block_size_supported(new_cap)) { 122 r = 0; 123 kvm->arch.mmu.split_page_chunk_size = new_cap; 124 } 125 } 126 mutex_unlock(&kvm->slots_lock); 127 break; 128 case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS: 129 mutex_lock(&kvm->lock); 130 if (!kvm->created_vcpus) { 131 r = 0; 132 set_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &kvm->arch.flags); 133 } 134 mutex_unlock(&kvm->lock); 135 break; 136 default: 137 break; 138 } 139 140 return r; 141 } 142 143 static int kvm_arm_default_max_vcpus(void) 144 { 145 return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; 146 } 147 148 /** 149 * kvm_arch_init_vm - initializes a VM data structure 150 * @kvm: pointer to the KVM struct 151 * @type: kvm device type 152 */ 153 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 154 { 155 int ret; 156 157 mutex_init(&kvm->arch.config_lock); 158 159 #ifdef CONFIG_LOCKDEP 160 /* Clue in lockdep that the config_lock must be taken inside kvm->lock */ 161 mutex_lock(&kvm->lock); 162 mutex_lock(&kvm->arch.config_lock); 163 mutex_unlock(&kvm->arch.config_lock); 164 mutex_unlock(&kvm->lock); 165 #endif 166 167 kvm_init_nested(kvm); 168 169 ret = kvm_share_hyp(kvm, kvm + 1); 170 if (ret) 171 return ret; 172 173 ret = pkvm_init_host_vm(kvm); 174 if (ret) 175 goto err_unshare_kvm; 176 177 if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) { 178 ret = -ENOMEM; 179 goto err_unshare_kvm; 180 } 181 cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask); 182 183 ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type); 184 if (ret) 185 goto err_free_cpumask; 186 187 kvm_vgic_early_init(kvm); 188 189 kvm_timer_init_vm(kvm); 190 191 /* The maximum number of VCPUs is limited by the host's GIC model */ 192 kvm->max_vcpus = kvm_arm_default_max_vcpus(); 193 194 kvm_arm_init_hypercalls(kvm); 195 196 bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES); 197 198 return 0; 199 200 err_free_cpumask: 201 free_cpumask_var(kvm->arch.supported_cpus); 202 err_unshare_kvm: 203 kvm_unshare_hyp(kvm, kvm + 1); 204 return ret; 205 } 206 207 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 208 { 209 return VM_FAULT_SIGBUS; 210 } 211 212 void kvm_arch_create_vm_debugfs(struct kvm *kvm) 213 { 214 kvm_sys_regs_create_debugfs(kvm); 215 kvm_s2_ptdump_create_debugfs(kvm); 216 } 217 218 static void kvm_destroy_mpidr_data(struct kvm *kvm) 219 { 220 struct kvm_mpidr_data *data; 221 222 mutex_lock(&kvm->arch.config_lock); 223 224 data = rcu_dereference_protected(kvm->arch.mpidr_data, 225 lockdep_is_held(&kvm->arch.config_lock)); 226 if (data) { 227 rcu_assign_pointer(kvm->arch.mpidr_data, NULL); 228 synchronize_rcu(); 229 kfree(data); 230 } 231 232 mutex_unlock(&kvm->arch.config_lock); 233 } 234 235 /** 236 * kvm_arch_destroy_vm - destroy the VM data structure 237 * @kvm: pointer to the KVM struct 238 */ 239 void kvm_arch_destroy_vm(struct kvm *kvm) 240 { 241 bitmap_free(kvm->arch.pmu_filter); 242 free_cpumask_var(kvm->arch.supported_cpus); 243 244 kvm_vgic_destroy(kvm); 245 246 if (is_protected_kvm_enabled()) 247 pkvm_destroy_hyp_vm(kvm); 248 249 kvm_destroy_mpidr_data(kvm); 250 251 kfree(kvm->arch.sysreg_masks); 252 kvm_destroy_vcpus(kvm); 253 254 kvm_unshare_hyp(kvm, kvm + 1); 255 256 kvm_arm_teardown_hypercalls(kvm); 257 } 258 259 static bool kvm_has_full_ptr_auth(void) 260 { 261 bool apa, gpa, api, gpi, apa3, gpa3; 262 u64 isar1, isar2, val; 263 264 /* 265 * Check that: 266 * 267 * - both Address and Generic auth are implemented for a given 268 * algorithm (Q5, IMPDEF or Q3) 269 * - only a single algorithm is implemented. 270 */ 271 if (!system_has_full_ptr_auth()) 272 return false; 273 274 isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); 275 isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); 276 277 apa = !!FIELD_GET(ID_AA64ISAR1_EL1_APA_MASK, isar1); 278 val = FIELD_GET(ID_AA64ISAR1_EL1_GPA_MASK, isar1); 279 gpa = (val == ID_AA64ISAR1_EL1_GPA_IMP); 280 281 api = !!FIELD_GET(ID_AA64ISAR1_EL1_API_MASK, isar1); 282 val = FIELD_GET(ID_AA64ISAR1_EL1_GPI_MASK, isar1); 283 gpi = (val == ID_AA64ISAR1_EL1_GPI_IMP); 284 285 apa3 = !!FIELD_GET(ID_AA64ISAR2_EL1_APA3_MASK, isar2); 286 val = FIELD_GET(ID_AA64ISAR2_EL1_GPA3_MASK, isar2); 287 gpa3 = (val == ID_AA64ISAR2_EL1_GPA3_IMP); 288 289 return (apa == gpa && api == gpi && apa3 == gpa3 && 290 (apa + api + apa3) == 1); 291 } 292 293 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 294 { 295 int r; 296 297 if (kvm && kvm_vm_is_protected(kvm) && !kvm_pvm_ext_allowed(ext)) 298 return 0; 299 300 switch (ext) { 301 case KVM_CAP_IRQCHIP: 302 r = vgic_present; 303 break; 304 case KVM_CAP_IOEVENTFD: 305 case KVM_CAP_USER_MEMORY: 306 case KVM_CAP_SYNC_MMU: 307 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: 308 case KVM_CAP_ONE_REG: 309 case KVM_CAP_ARM_PSCI: 310 case KVM_CAP_ARM_PSCI_0_2: 311 case KVM_CAP_READONLY_MEM: 312 case KVM_CAP_MP_STATE: 313 case KVM_CAP_IMMEDIATE_EXIT: 314 case KVM_CAP_VCPU_EVENTS: 315 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2: 316 case KVM_CAP_ARM_NISV_TO_USER: 317 case KVM_CAP_ARM_INJECT_EXT_DABT: 318 case KVM_CAP_SET_GUEST_DEBUG: 319 case KVM_CAP_VCPU_ATTRIBUTES: 320 case KVM_CAP_PTP_KVM: 321 case KVM_CAP_ARM_SYSTEM_SUSPEND: 322 case KVM_CAP_IRQFD_RESAMPLE: 323 case KVM_CAP_COUNTER_OFFSET: 324 case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS: 325 r = 1; 326 break; 327 case KVM_CAP_SET_GUEST_DEBUG2: 328 return KVM_GUESTDBG_VALID_MASK; 329 case KVM_CAP_ARM_SET_DEVICE_ADDR: 330 r = 1; 331 break; 332 case KVM_CAP_NR_VCPUS: 333 /* 334 * ARM64 treats KVM_CAP_NR_CPUS differently from all other 335 * architectures, as it does not always bound it to 336 * KVM_CAP_MAX_VCPUS. It should not matter much because 337 * this is just an advisory value. 338 */ 339 r = min_t(unsigned int, num_online_cpus(), 340 kvm_arm_default_max_vcpus()); 341 break; 342 case KVM_CAP_MAX_VCPUS: 343 case KVM_CAP_MAX_VCPU_ID: 344 if (kvm) 345 r = kvm->max_vcpus; 346 else 347 r = kvm_arm_default_max_vcpus(); 348 break; 349 case KVM_CAP_MSI_DEVID: 350 if (!kvm) 351 r = -EINVAL; 352 else 353 r = kvm->arch.vgic.msis_require_devid; 354 break; 355 case KVM_CAP_ARM_USER_IRQ: 356 /* 357 * 1: EL1_VTIMER, EL1_PTIMER, and PMU. 358 * (bump this number if adding more devices) 359 */ 360 r = 1; 361 break; 362 case KVM_CAP_ARM_MTE: 363 r = system_supports_mte(); 364 break; 365 case KVM_CAP_STEAL_TIME: 366 r = kvm_arm_pvtime_supported(); 367 break; 368 case KVM_CAP_ARM_EL1_32BIT: 369 r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1); 370 break; 371 case KVM_CAP_ARM_EL2: 372 r = cpus_have_final_cap(ARM64_HAS_NESTED_VIRT); 373 break; 374 case KVM_CAP_ARM_EL2_E2H0: 375 r = cpus_have_final_cap(ARM64_HAS_HCR_NV1); 376 break; 377 case KVM_CAP_GUEST_DEBUG_HW_BPS: 378 r = get_num_brps(); 379 break; 380 case KVM_CAP_GUEST_DEBUG_HW_WPS: 381 r = get_num_wrps(); 382 break; 383 case KVM_CAP_ARM_PMU_V3: 384 r = kvm_supports_guest_pmuv3(); 385 break; 386 case KVM_CAP_ARM_INJECT_SERROR_ESR: 387 r = cpus_have_final_cap(ARM64_HAS_RAS_EXTN); 388 break; 389 case KVM_CAP_ARM_VM_IPA_SIZE: 390 r = get_kvm_ipa_limit(); 391 break; 392 case KVM_CAP_ARM_SVE: 393 r = system_supports_sve(); 394 break; 395 case KVM_CAP_ARM_PTRAUTH_ADDRESS: 396 case KVM_CAP_ARM_PTRAUTH_GENERIC: 397 r = kvm_has_full_ptr_auth(); 398 break; 399 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE: 400 if (kvm) 401 r = kvm->arch.mmu.split_page_chunk_size; 402 else 403 r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT; 404 break; 405 case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES: 406 r = kvm_supported_block_sizes(); 407 break; 408 case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES: 409 r = BIT(0); 410 break; 411 case KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED: 412 if (!kvm) 413 r = -EINVAL; 414 else 415 r = kvm_supports_cacheable_pfnmap(); 416 break; 417 418 default: 419 r = 0; 420 } 421 422 return r; 423 } 424 425 long kvm_arch_dev_ioctl(struct file *filp, 426 unsigned int ioctl, unsigned long arg) 427 { 428 return -EINVAL; 429 } 430 431 struct kvm *kvm_arch_alloc_vm(void) 432 { 433 size_t sz = sizeof(struct kvm); 434 435 if (!has_vhe()) 436 return kzalloc(sz, GFP_KERNEL_ACCOUNT); 437 438 return __vmalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM | __GFP_ZERO); 439 } 440 441 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 442 { 443 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) 444 return -EBUSY; 445 446 if (id >= kvm->max_vcpus) 447 return -EINVAL; 448 449 return 0; 450 } 451 452 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 453 { 454 int err; 455 456 spin_lock_init(&vcpu->arch.mp_state_lock); 457 458 #ifdef CONFIG_LOCKDEP 459 /* Inform lockdep that the config_lock is acquired after vcpu->mutex */ 460 mutex_lock(&vcpu->mutex); 461 mutex_lock(&vcpu->kvm->arch.config_lock); 462 mutex_unlock(&vcpu->kvm->arch.config_lock); 463 mutex_unlock(&vcpu->mutex); 464 #endif 465 466 /* Force users to call KVM_ARM_VCPU_INIT */ 467 vcpu_clear_flag(vcpu, VCPU_INITIALIZED); 468 469 vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO; 470 471 /* Set up the timer */ 472 kvm_timer_vcpu_init(vcpu); 473 474 kvm_pmu_vcpu_init(vcpu); 475 476 kvm_arm_pvtime_vcpu_init(&vcpu->arch); 477 478 vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu; 479 480 /* 481 * This vCPU may have been created after mpidr_data was initialized. 482 * Throw out the pre-computed mappings if that is the case which forces 483 * KVM to fall back to iteratively searching the vCPUs. 484 */ 485 kvm_destroy_mpidr_data(vcpu->kvm); 486 487 err = kvm_vgic_vcpu_init(vcpu); 488 if (err) 489 return err; 490 491 err = kvm_share_hyp(vcpu, vcpu + 1); 492 if (err) 493 kvm_vgic_vcpu_destroy(vcpu); 494 495 return err; 496 } 497 498 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 499 { 500 } 501 502 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 503 { 504 if (!is_protected_kvm_enabled()) 505 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); 506 else 507 free_hyp_memcache(&vcpu->arch.pkvm_memcache); 508 kvm_timer_vcpu_terminate(vcpu); 509 kvm_pmu_vcpu_destroy(vcpu); 510 kvm_vgic_vcpu_destroy(vcpu); 511 kvm_arm_vcpu_destroy(vcpu); 512 } 513 514 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) 515 { 516 517 } 518 519 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) 520 { 521 522 } 523 524 static void vcpu_set_pauth_traps(struct kvm_vcpu *vcpu) 525 { 526 if (vcpu_has_ptrauth(vcpu) && !is_protected_kvm_enabled()) { 527 /* 528 * Either we're running an L2 guest, and the API/APK bits come 529 * from L1's HCR_EL2, or API/APK are both set. 530 */ 531 if (unlikely(is_nested_ctxt(vcpu))) { 532 u64 val; 533 534 val = __vcpu_sys_reg(vcpu, HCR_EL2); 535 val &= (HCR_API | HCR_APK); 536 vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK); 537 vcpu->arch.hcr_el2 |= val; 538 } else { 539 vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK); 540 } 541 542 /* 543 * Save the host keys if there is any chance for the guest 544 * to use pauth, as the entry code will reload the guest 545 * keys in that case. 546 */ 547 if (vcpu->arch.hcr_el2 & (HCR_API | HCR_APK)) { 548 struct kvm_cpu_context *ctxt; 549 550 ctxt = this_cpu_ptr_hyp_sym(kvm_hyp_ctxt); 551 ptrauth_save_keys(ctxt); 552 } 553 } 554 } 555 556 static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu) 557 { 558 if (unlikely(kvm_wfi_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK)) 559 return kvm_wfi_trap_policy == KVM_WFX_NOTRAP; 560 561 return single_task_running() && 562 (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) || 563 vcpu->kvm->arch.vgic.nassgireq); 564 } 565 566 static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu) 567 { 568 if (unlikely(kvm_wfe_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK)) 569 return kvm_wfe_trap_policy == KVM_WFX_NOTRAP; 570 571 return single_task_running(); 572 } 573 574 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 575 { 576 struct kvm_s2_mmu *mmu; 577 int *last_ran; 578 579 if (is_protected_kvm_enabled()) 580 goto nommu; 581 582 if (vcpu_has_nv(vcpu)) 583 kvm_vcpu_load_hw_mmu(vcpu); 584 585 mmu = vcpu->arch.hw_mmu; 586 last_ran = this_cpu_ptr(mmu->last_vcpu_ran); 587 588 /* 589 * Ensure a VMID is allocated for the MMU before programming VTTBR_EL2, 590 * which happens eagerly in VHE. 591 * 592 * Also, the VMID allocator only preserves VMIDs that are active at the 593 * time of rollover, so KVM might need to grab a new VMID for the MMU if 594 * this is called from kvm_sched_in(). 595 */ 596 kvm_arm_vmid_update(&mmu->vmid); 597 598 /* 599 * We guarantee that both TLBs and I-cache are private to each 600 * vcpu. If detecting that a vcpu from the same VM has 601 * previously run on the same physical CPU, call into the 602 * hypervisor code to nuke the relevant contexts. 603 * 604 * We might get preempted before the vCPU actually runs, but 605 * over-invalidation doesn't affect correctness. 606 */ 607 if (*last_ran != vcpu->vcpu_idx) { 608 kvm_call_hyp(__kvm_flush_cpu_context, mmu); 609 *last_ran = vcpu->vcpu_idx; 610 } 611 612 nommu: 613 vcpu->cpu = cpu; 614 615 /* 616 * The timer must be loaded before the vgic to correctly set up physical 617 * interrupt deactivation in nested state (e.g. timer interrupt). 618 */ 619 kvm_timer_vcpu_load(vcpu); 620 kvm_vgic_load(vcpu); 621 kvm_vcpu_load_debug(vcpu); 622 if (has_vhe()) 623 kvm_vcpu_load_vhe(vcpu); 624 kvm_arch_vcpu_load_fp(vcpu); 625 kvm_vcpu_pmu_restore_guest(vcpu); 626 if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) 627 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); 628 629 if (kvm_vcpu_should_clear_twe(vcpu)) 630 vcpu->arch.hcr_el2 &= ~HCR_TWE; 631 else 632 vcpu->arch.hcr_el2 |= HCR_TWE; 633 634 if (kvm_vcpu_should_clear_twi(vcpu)) 635 vcpu->arch.hcr_el2 &= ~HCR_TWI; 636 else 637 vcpu->arch.hcr_el2 |= HCR_TWI; 638 639 vcpu_set_pauth_traps(vcpu); 640 641 if (is_protected_kvm_enabled()) { 642 kvm_call_hyp_nvhe(__pkvm_vcpu_load, 643 vcpu->kvm->arch.pkvm.handle, 644 vcpu->vcpu_idx, vcpu->arch.hcr_el2); 645 kvm_call_hyp(__vgic_v3_restore_vmcr_aprs, 646 &vcpu->arch.vgic_cpu.vgic_v3); 647 } 648 649 if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus)) 650 vcpu_set_on_unsupported_cpu(vcpu); 651 } 652 653 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 654 { 655 if (is_protected_kvm_enabled()) { 656 kvm_call_hyp(__vgic_v3_save_vmcr_aprs, 657 &vcpu->arch.vgic_cpu.vgic_v3); 658 kvm_call_hyp_nvhe(__pkvm_vcpu_put); 659 } 660 661 kvm_vcpu_put_debug(vcpu); 662 kvm_arch_vcpu_put_fp(vcpu); 663 if (has_vhe()) 664 kvm_vcpu_put_vhe(vcpu); 665 kvm_timer_vcpu_put(vcpu); 666 kvm_vgic_put(vcpu); 667 kvm_vcpu_pmu_restore_host(vcpu); 668 if (vcpu_has_nv(vcpu)) 669 kvm_vcpu_put_hw_mmu(vcpu); 670 kvm_arm_vmid_clear_active(); 671 672 vcpu_clear_on_unsupported_cpu(vcpu); 673 vcpu->cpu = -1; 674 } 675 676 static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu) 677 { 678 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED); 679 kvm_make_request(KVM_REQ_SLEEP, vcpu); 680 kvm_vcpu_kick(vcpu); 681 } 682 683 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu) 684 { 685 spin_lock(&vcpu->arch.mp_state_lock); 686 __kvm_arm_vcpu_power_off(vcpu); 687 spin_unlock(&vcpu->arch.mp_state_lock); 688 } 689 690 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu) 691 { 692 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED; 693 } 694 695 static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu) 696 { 697 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED); 698 kvm_make_request(KVM_REQ_SUSPEND, vcpu); 699 kvm_vcpu_kick(vcpu); 700 } 701 702 static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu) 703 { 704 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED; 705 } 706 707 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 708 struct kvm_mp_state *mp_state) 709 { 710 *mp_state = READ_ONCE(vcpu->arch.mp_state); 711 712 return 0; 713 } 714 715 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 716 struct kvm_mp_state *mp_state) 717 { 718 int ret = 0; 719 720 spin_lock(&vcpu->arch.mp_state_lock); 721 722 switch (mp_state->mp_state) { 723 case KVM_MP_STATE_RUNNABLE: 724 WRITE_ONCE(vcpu->arch.mp_state, *mp_state); 725 break; 726 case KVM_MP_STATE_STOPPED: 727 __kvm_arm_vcpu_power_off(vcpu); 728 break; 729 case KVM_MP_STATE_SUSPENDED: 730 kvm_arm_vcpu_suspend(vcpu); 731 break; 732 default: 733 ret = -EINVAL; 734 } 735 736 spin_unlock(&vcpu->arch.mp_state_lock); 737 738 return ret; 739 } 740 741 /** 742 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled 743 * @v: The VCPU pointer 744 * 745 * If the guest CPU is not waiting for interrupts or an interrupt line is 746 * asserted, the CPU is by definition runnable. 747 */ 748 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) 749 { 750 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF | HCR_VSE); 751 752 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v)) 753 && !kvm_arm_vcpu_stopped(v) && !v->arch.pause); 754 } 755 756 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 757 { 758 return vcpu_mode_priv(vcpu); 759 } 760 761 #ifdef CONFIG_GUEST_PERF_EVENTS 762 unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu) 763 { 764 return *vcpu_pc(vcpu); 765 } 766 #endif 767 768 static void kvm_init_mpidr_data(struct kvm *kvm) 769 { 770 struct kvm_mpidr_data *data = NULL; 771 unsigned long c, mask, nr_entries; 772 u64 aff_set = 0, aff_clr = ~0UL; 773 struct kvm_vcpu *vcpu; 774 775 mutex_lock(&kvm->arch.config_lock); 776 777 if (rcu_access_pointer(kvm->arch.mpidr_data) || 778 atomic_read(&kvm->online_vcpus) == 1) 779 goto out; 780 781 kvm_for_each_vcpu(c, vcpu, kvm) { 782 u64 aff = kvm_vcpu_get_mpidr_aff(vcpu); 783 aff_set |= aff; 784 aff_clr &= aff; 785 } 786 787 /* 788 * A significant bit can be either 0 or 1, and will only appear in 789 * aff_set. Use aff_clr to weed out the useless stuff. 790 */ 791 mask = aff_set ^ aff_clr; 792 nr_entries = BIT_ULL(hweight_long(mask)); 793 794 /* 795 * Don't let userspace fool us. If we need more than a single page 796 * to describe the compressed MPIDR array, just fall back to the 797 * iterative method. Single vcpu VMs do not need this either. 798 */ 799 if (struct_size(data, cmpidr_to_idx, nr_entries) <= PAGE_SIZE) 800 data = kzalloc(struct_size(data, cmpidr_to_idx, nr_entries), 801 GFP_KERNEL_ACCOUNT); 802 803 if (!data) 804 goto out; 805 806 data->mpidr_mask = mask; 807 808 kvm_for_each_vcpu(c, vcpu, kvm) { 809 u64 aff = kvm_vcpu_get_mpidr_aff(vcpu); 810 u16 index = kvm_mpidr_index(data, aff); 811 812 data->cmpidr_to_idx[index] = c; 813 } 814 815 rcu_assign_pointer(kvm->arch.mpidr_data, data); 816 out: 817 mutex_unlock(&kvm->arch.config_lock); 818 } 819 820 /* 821 * Handle both the initialisation that is being done when the vcpu is 822 * run for the first time, as well as the updates that must be 823 * performed each time we get a new thread dealing with this vcpu. 824 */ 825 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) 826 { 827 struct kvm *kvm = vcpu->kvm; 828 int ret; 829 830 if (!kvm_vcpu_initialized(vcpu)) 831 return -ENOEXEC; 832 833 if (!kvm_arm_vcpu_is_finalized(vcpu)) 834 return -EPERM; 835 836 if (likely(vcpu_has_run_once(vcpu))) 837 return 0; 838 839 kvm_init_mpidr_data(kvm); 840 841 if (likely(irqchip_in_kernel(kvm))) { 842 /* 843 * Map the VGIC hardware resources before running a vcpu the 844 * first time on this VM. 845 */ 846 ret = kvm_vgic_map_resources(kvm); 847 if (ret) 848 return ret; 849 } 850 851 ret = kvm_finalize_sys_regs(vcpu); 852 if (ret) 853 return ret; 854 855 if (vcpu_has_nv(vcpu)) { 856 ret = kvm_vcpu_allocate_vncr_tlb(vcpu); 857 if (ret) 858 return ret; 859 860 ret = kvm_vgic_vcpu_nv_init(vcpu); 861 if (ret) 862 return ret; 863 } 864 865 /* 866 * This needs to happen after any restriction has been applied 867 * to the feature set. 868 */ 869 kvm_calculate_traps(vcpu); 870 871 ret = kvm_timer_enable(vcpu); 872 if (ret) 873 return ret; 874 875 if (kvm_vcpu_has_pmu(vcpu)) { 876 ret = kvm_arm_pmu_v3_enable(vcpu); 877 if (ret) 878 return ret; 879 } 880 881 if (is_protected_kvm_enabled()) { 882 ret = pkvm_create_hyp_vm(kvm); 883 if (ret) 884 return ret; 885 886 ret = pkvm_create_hyp_vcpu(vcpu); 887 if (ret) 888 return ret; 889 } 890 891 mutex_lock(&kvm->arch.config_lock); 892 set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags); 893 mutex_unlock(&kvm->arch.config_lock); 894 895 return ret; 896 } 897 898 bool kvm_arch_intc_initialized(struct kvm *kvm) 899 { 900 return vgic_initialized(kvm); 901 } 902 903 void kvm_arm_halt_guest(struct kvm *kvm) 904 { 905 unsigned long i; 906 struct kvm_vcpu *vcpu; 907 908 kvm_for_each_vcpu(i, vcpu, kvm) 909 vcpu->arch.pause = true; 910 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP); 911 } 912 913 void kvm_arm_resume_guest(struct kvm *kvm) 914 { 915 unsigned long i; 916 struct kvm_vcpu *vcpu; 917 918 kvm_for_each_vcpu(i, vcpu, kvm) { 919 vcpu->arch.pause = false; 920 __kvm_vcpu_wake_up(vcpu); 921 } 922 } 923 924 static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu) 925 { 926 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); 927 928 rcuwait_wait_event(wait, 929 (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause), 930 TASK_INTERRUPTIBLE); 931 932 if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) { 933 /* Awaken to handle a signal, request we sleep again later. */ 934 kvm_make_request(KVM_REQ_SLEEP, vcpu); 935 } 936 937 /* 938 * Make sure we will observe a potential reset request if we've 939 * observed a change to the power state. Pairs with the smp_wmb() in 940 * kvm_psci_vcpu_on(). 941 */ 942 smp_rmb(); 943 } 944 945 /** 946 * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior 947 * @vcpu: The VCPU pointer 948 * 949 * Suspend execution of a vCPU until a valid wake event is detected, i.e. until 950 * the vCPU is runnable. The vCPU may or may not be scheduled out, depending 951 * on when a wake event arrives, e.g. there may already be a pending wake event. 952 */ 953 void kvm_vcpu_wfi(struct kvm_vcpu *vcpu) 954 { 955 /* 956 * Sync back the state of the GIC CPU interface so that we have 957 * the latest PMR and group enables. This ensures that 958 * kvm_arch_vcpu_runnable has up-to-date data to decide whether 959 * we have pending interrupts, e.g. when determining if the 960 * vCPU should block. 961 * 962 * For the same reason, we want to tell GICv4 that we need 963 * doorbells to be signalled, should an interrupt become pending. 964 */ 965 preempt_disable(); 966 vcpu_set_flag(vcpu, IN_WFI); 967 kvm_vgic_put(vcpu); 968 preempt_enable(); 969 970 kvm_vcpu_halt(vcpu); 971 vcpu_clear_flag(vcpu, IN_WFIT); 972 973 preempt_disable(); 974 vcpu_clear_flag(vcpu, IN_WFI); 975 kvm_vgic_load(vcpu); 976 preempt_enable(); 977 } 978 979 static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu) 980 { 981 if (!kvm_arm_vcpu_suspended(vcpu)) 982 return 1; 983 984 kvm_vcpu_wfi(vcpu); 985 986 /* 987 * The suspend state is sticky; we do not leave it until userspace 988 * explicitly marks the vCPU as runnable. Request that we suspend again 989 * later. 990 */ 991 kvm_make_request(KVM_REQ_SUSPEND, vcpu); 992 993 /* 994 * Check to make sure the vCPU is actually runnable. If so, exit to 995 * userspace informing it of the wakeup condition. 996 */ 997 if (kvm_arch_vcpu_runnable(vcpu)) { 998 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); 999 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP; 1000 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; 1001 return 0; 1002 } 1003 1004 /* 1005 * Otherwise, we were unblocked to process a different event, such as a 1006 * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to 1007 * process the event. 1008 */ 1009 return 1; 1010 } 1011 1012 /** 1013 * check_vcpu_requests - check and handle pending vCPU requests 1014 * @vcpu: the VCPU pointer 1015 * 1016 * Return: 1 if we should enter the guest 1017 * 0 if we should exit to userspace 1018 * < 0 if we should exit to userspace, where the return value indicates 1019 * an error 1020 */ 1021 static int check_vcpu_requests(struct kvm_vcpu *vcpu) 1022 { 1023 if (kvm_request_pending(vcpu)) { 1024 if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) 1025 return -EIO; 1026 1027 if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) 1028 kvm_vcpu_sleep(vcpu); 1029 1030 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) 1031 kvm_reset_vcpu(vcpu); 1032 1033 /* 1034 * Clear IRQ_PENDING requests that were made to guarantee 1035 * that a VCPU sees new virtual interrupts. 1036 */ 1037 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu); 1038 1039 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu)) 1040 kvm_update_stolen_time(vcpu); 1041 1042 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) { 1043 /* The distributor enable bits were changed */ 1044 preempt_disable(); 1045 vgic_v4_put(vcpu); 1046 vgic_v4_load(vcpu); 1047 preempt_enable(); 1048 } 1049 1050 if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu)) 1051 kvm_vcpu_reload_pmu(vcpu); 1052 1053 if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu)) 1054 kvm_vcpu_pmu_restore_guest(vcpu); 1055 1056 if (kvm_check_request(KVM_REQ_SUSPEND, vcpu)) 1057 return kvm_vcpu_suspend(vcpu); 1058 1059 if (kvm_dirty_ring_check_request(vcpu)) 1060 return 0; 1061 1062 check_nested_vcpu_requests(vcpu); 1063 } 1064 1065 return 1; 1066 } 1067 1068 static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu) 1069 { 1070 if (likely(!vcpu_mode_is_32bit(vcpu))) 1071 return false; 1072 1073 if (vcpu_has_nv(vcpu)) 1074 return true; 1075 1076 return !kvm_supports_32bit_el0(); 1077 } 1078 1079 /** 1080 * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest 1081 * @vcpu: The VCPU pointer 1082 * @ret: Pointer to write optional return code 1083 * 1084 * Returns: true if the VCPU needs to return to a preemptible + interruptible 1085 * and skip guest entry. 1086 * 1087 * This function disambiguates between two different types of exits: exits to a 1088 * preemptible + interruptible kernel context and exits to userspace. For an 1089 * exit to userspace, this function will write the return code to ret and return 1090 * true. For an exit to preemptible + interruptible kernel context (i.e. check 1091 * for pending work and re-enter), return true without writing to ret. 1092 */ 1093 static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret) 1094 { 1095 struct kvm_run *run = vcpu->run; 1096 1097 /* 1098 * If we're using a userspace irqchip, then check if we need 1099 * to tell a userspace irqchip about timer or PMU level 1100 * changes and if so, exit to userspace (the actual level 1101 * state gets updated in kvm_timer_update_run and 1102 * kvm_pmu_update_run below). 1103 */ 1104 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { 1105 if (kvm_timer_should_notify_user(vcpu) || 1106 kvm_pmu_should_notify_user(vcpu)) { 1107 *ret = -EINTR; 1108 run->exit_reason = KVM_EXIT_INTR; 1109 return true; 1110 } 1111 } 1112 1113 if (unlikely(vcpu_on_unsupported_cpu(vcpu))) { 1114 run->exit_reason = KVM_EXIT_FAIL_ENTRY; 1115 run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED; 1116 run->fail_entry.cpu = smp_processor_id(); 1117 *ret = 0; 1118 return true; 1119 } 1120 1121 return kvm_request_pending(vcpu) || 1122 xfer_to_guest_mode_work_pending(); 1123 } 1124 1125 /* 1126 * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while 1127 * the vCPU is running. 1128 * 1129 * This must be noinstr as instrumentation may make use of RCU, and this is not 1130 * safe during the EQS. 1131 */ 1132 static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu) 1133 { 1134 int ret; 1135 1136 guest_state_enter_irqoff(); 1137 ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu); 1138 guest_state_exit_irqoff(); 1139 1140 return ret; 1141 } 1142 1143 /** 1144 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code 1145 * @vcpu: The VCPU pointer 1146 * 1147 * This function is called through the VCPU_RUN ioctl called from user space. It 1148 * will execute VM code in a loop until the time slice for the process is used 1149 * or some emulation is needed from user space in which case the function will 1150 * return with return value 0 and with the kvm_run structure filled in with the 1151 * required data for the requested emulation. 1152 */ 1153 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 1154 { 1155 struct kvm_run *run = vcpu->run; 1156 int ret; 1157 1158 if (run->exit_reason == KVM_EXIT_MMIO) { 1159 ret = kvm_handle_mmio_return(vcpu); 1160 if (ret <= 0) 1161 return ret; 1162 } 1163 1164 vcpu_load(vcpu); 1165 1166 if (!vcpu->wants_to_run) { 1167 ret = -EINTR; 1168 goto out; 1169 } 1170 1171 kvm_sigset_activate(vcpu); 1172 1173 ret = 1; 1174 run->exit_reason = KVM_EXIT_UNKNOWN; 1175 run->flags = 0; 1176 while (ret > 0) { 1177 /* 1178 * Check conditions before entering the guest 1179 */ 1180 ret = xfer_to_guest_mode_handle_work(vcpu); 1181 if (!ret) 1182 ret = 1; 1183 1184 if (ret > 0) 1185 ret = check_vcpu_requests(vcpu); 1186 1187 /* 1188 * Preparing the interrupts to be injected also 1189 * involves poking the GIC, which must be done in a 1190 * non-preemptible context. 1191 */ 1192 preempt_disable(); 1193 1194 kvm_nested_flush_hwstate(vcpu); 1195 1196 if (kvm_vcpu_has_pmu(vcpu)) 1197 kvm_pmu_flush_hwstate(vcpu); 1198 1199 local_irq_disable(); 1200 1201 kvm_vgic_flush_hwstate(vcpu); 1202 1203 kvm_pmu_update_vcpu_events(vcpu); 1204 1205 /* 1206 * Ensure we set mode to IN_GUEST_MODE after we disable 1207 * interrupts and before the final VCPU requests check. 1208 * See the comment in kvm_vcpu_exiting_guest_mode() and 1209 * Documentation/virt/kvm/vcpu-requests.rst 1210 */ 1211 smp_store_mb(vcpu->mode, IN_GUEST_MODE); 1212 1213 if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) { 1214 vcpu->mode = OUTSIDE_GUEST_MODE; 1215 isb(); /* Ensure work in x_flush_hwstate is committed */ 1216 if (kvm_vcpu_has_pmu(vcpu)) 1217 kvm_pmu_sync_hwstate(vcpu); 1218 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) 1219 kvm_timer_sync_user(vcpu); 1220 kvm_vgic_sync_hwstate(vcpu); 1221 local_irq_enable(); 1222 preempt_enable(); 1223 continue; 1224 } 1225 1226 kvm_arch_vcpu_ctxflush_fp(vcpu); 1227 1228 /************************************************************** 1229 * Enter the guest 1230 */ 1231 trace_kvm_entry(*vcpu_pc(vcpu)); 1232 guest_timing_enter_irqoff(); 1233 1234 ret = kvm_arm_vcpu_enter_exit(vcpu); 1235 1236 vcpu->mode = OUTSIDE_GUEST_MODE; 1237 vcpu->stat.exits++; 1238 /* 1239 * Back from guest 1240 *************************************************************/ 1241 1242 /* 1243 * We must sync the PMU state before the vgic state so 1244 * that the vgic can properly sample the updated state of the 1245 * interrupt line. 1246 */ 1247 if (kvm_vcpu_has_pmu(vcpu)) 1248 kvm_pmu_sync_hwstate(vcpu); 1249 1250 /* 1251 * Sync the vgic state before syncing the timer state because 1252 * the timer code needs to know if the virtual timer 1253 * interrupts are active. 1254 */ 1255 kvm_vgic_sync_hwstate(vcpu); 1256 1257 /* 1258 * Sync the timer hardware state before enabling interrupts as 1259 * we don't want vtimer interrupts to race with syncing the 1260 * timer virtual interrupt state. 1261 */ 1262 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) 1263 kvm_timer_sync_user(vcpu); 1264 1265 if (is_hyp_ctxt(vcpu)) 1266 kvm_timer_sync_nested(vcpu); 1267 1268 kvm_arch_vcpu_ctxsync_fp(vcpu); 1269 1270 /* 1271 * We must ensure that any pending interrupts are taken before 1272 * we exit guest timing so that timer ticks are accounted as 1273 * guest time. Transiently unmask interrupts so that any 1274 * pending interrupts are taken. 1275 * 1276 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other 1277 * context synchronization event) is necessary to ensure that 1278 * pending interrupts are taken. 1279 */ 1280 if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) { 1281 local_irq_enable(); 1282 isb(); 1283 local_irq_disable(); 1284 } 1285 1286 guest_timing_exit_irqoff(); 1287 1288 local_irq_enable(); 1289 1290 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu)); 1291 1292 /* Exit types that need handling before we can be preempted */ 1293 handle_exit_early(vcpu, ret); 1294 1295 kvm_nested_sync_hwstate(vcpu); 1296 1297 preempt_enable(); 1298 1299 /* 1300 * The ARMv8 architecture doesn't give the hypervisor 1301 * a mechanism to prevent a guest from dropping to AArch32 EL0 1302 * if implemented by the CPU. If we spot the guest in such 1303 * state and that we decided it wasn't supposed to do so (like 1304 * with the asymmetric AArch32 case), return to userspace with 1305 * a fatal error. 1306 */ 1307 if (vcpu_mode_is_bad_32bit(vcpu)) { 1308 /* 1309 * As we have caught the guest red-handed, decide that 1310 * it isn't fit for purpose anymore by making the vcpu 1311 * invalid. The VMM can try and fix it by issuing a 1312 * KVM_ARM_VCPU_INIT if it really wants to. 1313 */ 1314 vcpu_clear_flag(vcpu, VCPU_INITIALIZED); 1315 ret = ARM_EXCEPTION_IL; 1316 } 1317 1318 ret = handle_exit(vcpu, ret); 1319 } 1320 1321 /* Tell userspace about in-kernel device output levels */ 1322 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { 1323 kvm_timer_update_run(vcpu); 1324 kvm_pmu_update_run(vcpu); 1325 } 1326 1327 kvm_sigset_deactivate(vcpu); 1328 1329 out: 1330 /* 1331 * In the unlikely event that we are returning to userspace 1332 * with pending exceptions or PC adjustment, commit these 1333 * adjustments in order to give userspace a consistent view of 1334 * the vcpu state. Note that this relies on __kvm_adjust_pc() 1335 * being preempt-safe on VHE. 1336 */ 1337 if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) || 1338 vcpu_get_flag(vcpu, INCREMENT_PC))) 1339 kvm_call_hyp(__kvm_adjust_pc, vcpu); 1340 1341 vcpu_put(vcpu); 1342 return ret; 1343 } 1344 1345 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) 1346 { 1347 int bit_index; 1348 bool set; 1349 unsigned long *hcr; 1350 1351 if (number == KVM_ARM_IRQ_CPU_IRQ) 1352 bit_index = __ffs(HCR_VI); 1353 else /* KVM_ARM_IRQ_CPU_FIQ */ 1354 bit_index = __ffs(HCR_VF); 1355 1356 hcr = vcpu_hcr(vcpu); 1357 if (level) 1358 set = test_and_set_bit(bit_index, hcr); 1359 else 1360 set = test_and_clear_bit(bit_index, hcr); 1361 1362 /* 1363 * If we didn't change anything, no need to wake up or kick other CPUs 1364 */ 1365 if (set == level) 1366 return 0; 1367 1368 /* 1369 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and 1370 * trigger a world-switch round on the running physical CPU to set the 1371 * virtual IRQ/FIQ fields in the HCR appropriately. 1372 */ 1373 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); 1374 kvm_vcpu_kick(vcpu); 1375 1376 return 0; 1377 } 1378 1379 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, 1380 bool line_status) 1381 { 1382 u32 irq = irq_level->irq; 1383 unsigned int irq_type, vcpu_id, irq_num; 1384 struct kvm_vcpu *vcpu = NULL; 1385 bool level = irq_level->level; 1386 1387 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; 1388 vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; 1389 vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1); 1390 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; 1391 1392 trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level); 1393 1394 switch (irq_type) { 1395 case KVM_ARM_IRQ_TYPE_CPU: 1396 if (irqchip_in_kernel(kvm)) 1397 return -ENXIO; 1398 1399 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); 1400 if (!vcpu) 1401 return -EINVAL; 1402 1403 if (irq_num > KVM_ARM_IRQ_CPU_FIQ) 1404 return -EINVAL; 1405 1406 return vcpu_interrupt_line(vcpu, irq_num, level); 1407 case KVM_ARM_IRQ_TYPE_PPI: 1408 if (!irqchip_in_kernel(kvm)) 1409 return -ENXIO; 1410 1411 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); 1412 if (!vcpu) 1413 return -EINVAL; 1414 1415 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS) 1416 return -EINVAL; 1417 1418 return kvm_vgic_inject_irq(kvm, vcpu, irq_num, level, NULL); 1419 case KVM_ARM_IRQ_TYPE_SPI: 1420 if (!irqchip_in_kernel(kvm)) 1421 return -ENXIO; 1422 1423 if (irq_num < VGIC_NR_PRIVATE_IRQS) 1424 return -EINVAL; 1425 1426 return kvm_vgic_inject_irq(kvm, NULL, irq_num, level, NULL); 1427 } 1428 1429 return -EINVAL; 1430 } 1431 1432 static unsigned long system_supported_vcpu_features(void) 1433 { 1434 unsigned long features = KVM_VCPU_VALID_FEATURES; 1435 1436 if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1)) 1437 clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features); 1438 1439 if (!kvm_supports_guest_pmuv3()) 1440 clear_bit(KVM_ARM_VCPU_PMU_V3, &features); 1441 1442 if (!system_supports_sve()) 1443 clear_bit(KVM_ARM_VCPU_SVE, &features); 1444 1445 if (!kvm_has_full_ptr_auth()) { 1446 clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features); 1447 clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features); 1448 } 1449 1450 if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) 1451 clear_bit(KVM_ARM_VCPU_HAS_EL2, &features); 1452 1453 return features; 1454 } 1455 1456 static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu, 1457 const struct kvm_vcpu_init *init) 1458 { 1459 unsigned long features = init->features[0]; 1460 int i; 1461 1462 if (features & ~KVM_VCPU_VALID_FEATURES) 1463 return -ENOENT; 1464 1465 for (i = 1; i < ARRAY_SIZE(init->features); i++) { 1466 if (init->features[i]) 1467 return -ENOENT; 1468 } 1469 1470 if (features & ~system_supported_vcpu_features()) 1471 return -EINVAL; 1472 1473 /* 1474 * For now make sure that both address/generic pointer authentication 1475 * features are requested by the userspace together. 1476 */ 1477 if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features) != 1478 test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features)) 1479 return -EINVAL; 1480 1481 if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features)) 1482 return 0; 1483 1484 /* MTE is incompatible with AArch32 */ 1485 if (kvm_has_mte(vcpu->kvm)) 1486 return -EINVAL; 1487 1488 /* NV is incompatible with AArch32 */ 1489 if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features)) 1490 return -EINVAL; 1491 1492 return 0; 1493 } 1494 1495 static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu, 1496 const struct kvm_vcpu_init *init) 1497 { 1498 unsigned long features = init->features[0]; 1499 1500 return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features, 1501 KVM_VCPU_MAX_FEATURES); 1502 } 1503 1504 static int kvm_setup_vcpu(struct kvm_vcpu *vcpu) 1505 { 1506 struct kvm *kvm = vcpu->kvm; 1507 int ret = 0; 1508 1509 /* 1510 * When the vCPU has a PMU, but no PMU is set for the guest 1511 * yet, set the default one. 1512 */ 1513 if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu) 1514 ret = kvm_arm_set_default_pmu(kvm); 1515 1516 /* Prepare for nested if required */ 1517 if (!ret && vcpu_has_nv(vcpu)) 1518 ret = kvm_vcpu_init_nested(vcpu); 1519 1520 return ret; 1521 } 1522 1523 static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1524 const struct kvm_vcpu_init *init) 1525 { 1526 unsigned long features = init->features[0]; 1527 struct kvm *kvm = vcpu->kvm; 1528 int ret = -EINVAL; 1529 1530 mutex_lock(&kvm->arch.config_lock); 1531 1532 if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) && 1533 kvm_vcpu_init_changed(vcpu, init)) 1534 goto out_unlock; 1535 1536 bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES); 1537 1538 ret = kvm_setup_vcpu(vcpu); 1539 if (ret) 1540 goto out_unlock; 1541 1542 /* Now we know what it is, we can reset it. */ 1543 kvm_reset_vcpu(vcpu); 1544 1545 set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags); 1546 vcpu_set_flag(vcpu, VCPU_INITIALIZED); 1547 ret = 0; 1548 out_unlock: 1549 mutex_unlock(&kvm->arch.config_lock); 1550 return ret; 1551 } 1552 1553 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1554 const struct kvm_vcpu_init *init) 1555 { 1556 int ret; 1557 1558 if (init->target != KVM_ARM_TARGET_GENERIC_V8 && 1559 init->target != kvm_target_cpu()) 1560 return -EINVAL; 1561 1562 ret = kvm_vcpu_init_check_features(vcpu, init); 1563 if (ret) 1564 return ret; 1565 1566 if (!kvm_vcpu_initialized(vcpu)) 1567 return __kvm_vcpu_set_target(vcpu, init); 1568 1569 if (kvm_vcpu_init_changed(vcpu, init)) 1570 return -EINVAL; 1571 1572 kvm_reset_vcpu(vcpu); 1573 return 0; 1574 } 1575 1576 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, 1577 struct kvm_vcpu_init *init) 1578 { 1579 bool power_off = false; 1580 int ret; 1581 1582 /* 1583 * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid 1584 * reflecting it in the finalized feature set, thus limiting its scope 1585 * to a single KVM_ARM_VCPU_INIT call. 1586 */ 1587 if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) { 1588 init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF); 1589 power_off = true; 1590 } 1591 1592 ret = kvm_vcpu_set_target(vcpu, init); 1593 if (ret) 1594 return ret; 1595 1596 /* 1597 * Ensure a rebooted VM will fault in RAM pages and detect if the 1598 * guest MMU is turned off and flush the caches as needed. 1599 * 1600 * S2FWB enforces all memory accesses to RAM being cacheable, 1601 * ensuring that the data side is always coherent. We still 1602 * need to invalidate the I-cache though, as FWB does *not* 1603 * imply CTR_EL0.DIC. 1604 */ 1605 if (vcpu_has_run_once(vcpu)) { 1606 if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) 1607 stage2_unmap_vm(vcpu->kvm); 1608 else 1609 icache_inval_all_pou(); 1610 } 1611 1612 vcpu_reset_hcr(vcpu); 1613 1614 /* 1615 * Handle the "start in power-off" case. 1616 */ 1617 spin_lock(&vcpu->arch.mp_state_lock); 1618 1619 if (power_off) 1620 __kvm_arm_vcpu_power_off(vcpu); 1621 else 1622 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE); 1623 1624 spin_unlock(&vcpu->arch.mp_state_lock); 1625 1626 return 0; 1627 } 1628 1629 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, 1630 struct kvm_device_attr *attr) 1631 { 1632 int ret = -ENXIO; 1633 1634 switch (attr->group) { 1635 default: 1636 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr); 1637 break; 1638 } 1639 1640 return ret; 1641 } 1642 1643 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, 1644 struct kvm_device_attr *attr) 1645 { 1646 int ret = -ENXIO; 1647 1648 switch (attr->group) { 1649 default: 1650 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr); 1651 break; 1652 } 1653 1654 return ret; 1655 } 1656 1657 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, 1658 struct kvm_device_attr *attr) 1659 { 1660 int ret = -ENXIO; 1661 1662 switch (attr->group) { 1663 default: 1664 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr); 1665 break; 1666 } 1667 1668 return ret; 1669 } 1670 1671 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, 1672 struct kvm_vcpu_events *events) 1673 { 1674 memset(events, 0, sizeof(*events)); 1675 1676 return __kvm_arm_vcpu_get_events(vcpu, events); 1677 } 1678 1679 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, 1680 struct kvm_vcpu_events *events) 1681 { 1682 int i; 1683 1684 /* check whether the reserved field is zero */ 1685 for (i = 0; i < ARRAY_SIZE(events->reserved); i++) 1686 if (events->reserved[i]) 1687 return -EINVAL; 1688 1689 /* check whether the pad field is zero */ 1690 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++) 1691 if (events->exception.pad[i]) 1692 return -EINVAL; 1693 1694 return __kvm_arm_vcpu_set_events(vcpu, events); 1695 } 1696 1697 long kvm_arch_vcpu_ioctl(struct file *filp, 1698 unsigned int ioctl, unsigned long arg) 1699 { 1700 struct kvm_vcpu *vcpu = filp->private_data; 1701 void __user *argp = (void __user *)arg; 1702 struct kvm_device_attr attr; 1703 long r; 1704 1705 switch (ioctl) { 1706 case KVM_ARM_VCPU_INIT: { 1707 struct kvm_vcpu_init init; 1708 1709 r = -EFAULT; 1710 if (copy_from_user(&init, argp, sizeof(init))) 1711 break; 1712 1713 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); 1714 break; 1715 } 1716 case KVM_SET_ONE_REG: 1717 case KVM_GET_ONE_REG: { 1718 struct kvm_one_reg reg; 1719 1720 r = -ENOEXEC; 1721 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1722 break; 1723 1724 r = -EFAULT; 1725 if (copy_from_user(®, argp, sizeof(reg))) 1726 break; 1727 1728 /* 1729 * We could owe a reset due to PSCI. Handle the pending reset 1730 * here to ensure userspace register accesses are ordered after 1731 * the reset. 1732 */ 1733 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) 1734 kvm_reset_vcpu(vcpu); 1735 1736 if (ioctl == KVM_SET_ONE_REG) 1737 r = kvm_arm_set_reg(vcpu, ®); 1738 else 1739 r = kvm_arm_get_reg(vcpu, ®); 1740 break; 1741 } 1742 case KVM_GET_REG_LIST: { 1743 struct kvm_reg_list __user *user_list = argp; 1744 struct kvm_reg_list reg_list; 1745 unsigned n; 1746 1747 r = -ENOEXEC; 1748 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1749 break; 1750 1751 r = -EPERM; 1752 if (!kvm_arm_vcpu_is_finalized(vcpu)) 1753 break; 1754 1755 r = -EFAULT; 1756 if (copy_from_user(®_list, user_list, sizeof(reg_list))) 1757 break; 1758 n = reg_list.n; 1759 reg_list.n = kvm_arm_num_regs(vcpu); 1760 if (copy_to_user(user_list, ®_list, sizeof(reg_list))) 1761 break; 1762 r = -E2BIG; 1763 if (n < reg_list.n) 1764 break; 1765 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg); 1766 break; 1767 } 1768 case KVM_SET_DEVICE_ATTR: { 1769 r = -EFAULT; 1770 if (copy_from_user(&attr, argp, sizeof(attr))) 1771 break; 1772 r = kvm_arm_vcpu_set_attr(vcpu, &attr); 1773 break; 1774 } 1775 case KVM_GET_DEVICE_ATTR: { 1776 r = -EFAULT; 1777 if (copy_from_user(&attr, argp, sizeof(attr))) 1778 break; 1779 r = kvm_arm_vcpu_get_attr(vcpu, &attr); 1780 break; 1781 } 1782 case KVM_HAS_DEVICE_ATTR: { 1783 r = -EFAULT; 1784 if (copy_from_user(&attr, argp, sizeof(attr))) 1785 break; 1786 r = kvm_arm_vcpu_has_attr(vcpu, &attr); 1787 break; 1788 } 1789 case KVM_GET_VCPU_EVENTS: { 1790 struct kvm_vcpu_events events; 1791 1792 if (kvm_arm_vcpu_get_events(vcpu, &events)) 1793 return -EINVAL; 1794 1795 if (copy_to_user(argp, &events, sizeof(events))) 1796 return -EFAULT; 1797 1798 return 0; 1799 } 1800 case KVM_SET_VCPU_EVENTS: { 1801 struct kvm_vcpu_events events; 1802 1803 if (copy_from_user(&events, argp, sizeof(events))) 1804 return -EFAULT; 1805 1806 return kvm_arm_vcpu_set_events(vcpu, &events); 1807 } 1808 case KVM_ARM_VCPU_FINALIZE: { 1809 int what; 1810 1811 if (!kvm_vcpu_initialized(vcpu)) 1812 return -ENOEXEC; 1813 1814 if (get_user(what, (const int __user *)argp)) 1815 return -EFAULT; 1816 1817 return kvm_arm_vcpu_finalize(vcpu, what); 1818 } 1819 default: 1820 r = -EINVAL; 1821 } 1822 1823 return r; 1824 } 1825 1826 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 1827 { 1828 1829 } 1830 1831 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, 1832 struct kvm_arm_device_addr *dev_addr) 1833 { 1834 switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) { 1835 case KVM_ARM_DEVICE_VGIC_V2: 1836 if (!vgic_present) 1837 return -ENXIO; 1838 return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr); 1839 default: 1840 return -ENODEV; 1841 } 1842 } 1843 1844 static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1845 { 1846 switch (attr->group) { 1847 case KVM_ARM_VM_SMCCC_CTRL: 1848 return kvm_vm_smccc_has_attr(kvm, attr); 1849 default: 1850 return -ENXIO; 1851 } 1852 } 1853 1854 static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1855 { 1856 switch (attr->group) { 1857 case KVM_ARM_VM_SMCCC_CTRL: 1858 return kvm_vm_smccc_set_attr(kvm, attr); 1859 default: 1860 return -ENXIO; 1861 } 1862 } 1863 1864 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) 1865 { 1866 struct kvm *kvm = filp->private_data; 1867 void __user *argp = (void __user *)arg; 1868 struct kvm_device_attr attr; 1869 1870 switch (ioctl) { 1871 case KVM_CREATE_IRQCHIP: { 1872 int ret; 1873 if (!vgic_present) 1874 return -ENXIO; 1875 mutex_lock(&kvm->lock); 1876 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); 1877 mutex_unlock(&kvm->lock); 1878 return ret; 1879 } 1880 case KVM_ARM_SET_DEVICE_ADDR: { 1881 struct kvm_arm_device_addr dev_addr; 1882 1883 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) 1884 return -EFAULT; 1885 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); 1886 } 1887 case KVM_ARM_PREFERRED_TARGET: { 1888 struct kvm_vcpu_init init = { 1889 .target = KVM_ARM_TARGET_GENERIC_V8, 1890 }; 1891 1892 if (copy_to_user(argp, &init, sizeof(init))) 1893 return -EFAULT; 1894 1895 return 0; 1896 } 1897 case KVM_ARM_MTE_COPY_TAGS: { 1898 struct kvm_arm_copy_mte_tags copy_tags; 1899 1900 if (copy_from_user(©_tags, argp, sizeof(copy_tags))) 1901 return -EFAULT; 1902 return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags); 1903 } 1904 case KVM_ARM_SET_COUNTER_OFFSET: { 1905 struct kvm_arm_counter_offset offset; 1906 1907 if (copy_from_user(&offset, argp, sizeof(offset))) 1908 return -EFAULT; 1909 return kvm_vm_ioctl_set_counter_offset(kvm, &offset); 1910 } 1911 case KVM_HAS_DEVICE_ATTR: { 1912 if (copy_from_user(&attr, argp, sizeof(attr))) 1913 return -EFAULT; 1914 1915 return kvm_vm_has_attr(kvm, &attr); 1916 } 1917 case KVM_SET_DEVICE_ATTR: { 1918 if (copy_from_user(&attr, argp, sizeof(attr))) 1919 return -EFAULT; 1920 1921 return kvm_vm_set_attr(kvm, &attr); 1922 } 1923 case KVM_ARM_GET_REG_WRITABLE_MASKS: { 1924 struct reg_mask_range range; 1925 1926 if (copy_from_user(&range, argp, sizeof(range))) 1927 return -EFAULT; 1928 return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range); 1929 } 1930 default: 1931 return -EINVAL; 1932 } 1933 } 1934 1935 static unsigned long nvhe_percpu_size(void) 1936 { 1937 return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) - 1938 (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start); 1939 } 1940 1941 static unsigned long nvhe_percpu_order(void) 1942 { 1943 unsigned long size = nvhe_percpu_size(); 1944 1945 return size ? get_order(size) : 0; 1946 } 1947 1948 static size_t pkvm_host_sve_state_order(void) 1949 { 1950 return get_order(pkvm_host_sve_state_size()); 1951 } 1952 1953 /* A lookup table holding the hypervisor VA for each vector slot */ 1954 static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS]; 1955 1956 static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot) 1957 { 1958 hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot); 1959 } 1960 1961 static int kvm_init_vector_slots(void) 1962 { 1963 int err; 1964 void *base; 1965 1966 base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 1967 kvm_init_vector_slot(base, HYP_VECTOR_DIRECT); 1968 1969 base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs)); 1970 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT); 1971 1972 if (kvm_system_needs_idmapped_vectors() && 1973 !is_protected_kvm_enabled()) { 1974 err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs), 1975 __BP_HARDEN_HYP_VECS_SZ, &base); 1976 if (err) 1977 return err; 1978 } 1979 1980 kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT); 1981 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT); 1982 return 0; 1983 } 1984 1985 static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits) 1986 { 1987 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 1988 unsigned long tcr; 1989 1990 /* 1991 * Calculate the raw per-cpu offset without a translation from the 1992 * kernel's mapping to the linear mapping, and store it in tpidr_el2 1993 * so that we can use adr_l to access per-cpu variables in EL2. 1994 * Also drop the KASAN tag which gets in the way... 1995 */ 1996 params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) - 1997 (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start)); 1998 1999 params->mair_el2 = read_sysreg(mair_el1); 2000 2001 tcr = read_sysreg(tcr_el1); 2002 if (cpus_have_final_cap(ARM64_KVM_HVHE)) { 2003 tcr &= ~(TCR_HD | TCR_HA | TCR_A1 | TCR_T0SZ_MASK); 2004 tcr |= TCR_EPD1_MASK; 2005 } else { 2006 unsigned long ips = FIELD_GET(TCR_IPS_MASK, tcr); 2007 2008 tcr &= TCR_EL2_MASK; 2009 tcr |= TCR_EL2_RES1 | FIELD_PREP(TCR_EL2_PS_MASK, ips); 2010 if (lpa2_is_enabled()) 2011 tcr |= TCR_EL2_DS; 2012 } 2013 tcr |= TCR_T0SZ(hyp_va_bits); 2014 params->tcr_el2 = tcr; 2015 2016 params->pgd_pa = kvm_mmu_get_httbr(); 2017 if (is_protected_kvm_enabled()) 2018 params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; 2019 else 2020 params->hcr_el2 = HCR_HOST_NVHE_FLAGS; 2021 if (cpus_have_final_cap(ARM64_KVM_HVHE)) 2022 params->hcr_el2 |= HCR_E2H; 2023 params->vttbr = params->vtcr = 0; 2024 2025 /* 2026 * Flush the init params from the data cache because the struct will 2027 * be read while the MMU is off. 2028 */ 2029 kvm_flush_dcache_to_poc(params, sizeof(*params)); 2030 } 2031 2032 static void hyp_install_host_vector(void) 2033 { 2034 struct kvm_nvhe_init_params *params; 2035 struct arm_smccc_res res; 2036 2037 /* Switch from the HYP stub to our own HYP init vector */ 2038 __hyp_set_vectors(kvm_get_idmap_vector()); 2039 2040 /* 2041 * Call initialization code, and switch to the full blown HYP code. 2042 * If the cpucaps haven't been finalized yet, something has gone very 2043 * wrong, and hyp will crash and burn when it uses any 2044 * cpus_have_*_cap() wrapper. 2045 */ 2046 BUG_ON(!system_capabilities_finalized()); 2047 params = this_cpu_ptr_nvhe_sym(kvm_init_params); 2048 arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res); 2049 WARN_ON(res.a0 != SMCCC_RET_SUCCESS); 2050 } 2051 2052 static void cpu_init_hyp_mode(void) 2053 { 2054 hyp_install_host_vector(); 2055 2056 /* 2057 * Disabling SSBD on a non-VHE system requires us to enable SSBS 2058 * at EL2. 2059 */ 2060 if (this_cpu_has_cap(ARM64_SSBS) && 2061 arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) { 2062 kvm_call_hyp_nvhe(__kvm_enable_ssbs); 2063 } 2064 } 2065 2066 static void cpu_hyp_reset(void) 2067 { 2068 if (!is_kernel_in_hyp_mode()) 2069 __hyp_reset_vectors(); 2070 } 2071 2072 /* 2073 * EL2 vectors can be mapped and rerouted in a number of ways, 2074 * depending on the kernel configuration and CPU present: 2075 * 2076 * - If the CPU is affected by Spectre-v2, the hardening sequence is 2077 * placed in one of the vector slots, which is executed before jumping 2078 * to the real vectors. 2079 * 2080 * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot 2081 * containing the hardening sequence is mapped next to the idmap page, 2082 * and executed before jumping to the real vectors. 2083 * 2084 * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an 2085 * empty slot is selected, mapped next to the idmap page, and 2086 * executed before jumping to the real vectors. 2087 * 2088 * Note that ARM64_SPECTRE_V3A is somewhat incompatible with 2089 * VHE, as we don't have hypervisor-specific mappings. If the system 2090 * is VHE and yet selects this capability, it will be ignored. 2091 */ 2092 static void cpu_set_hyp_vector(void) 2093 { 2094 struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data); 2095 void *vector = hyp_spectre_vector_selector[data->slot]; 2096 2097 if (!is_protected_kvm_enabled()) 2098 *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector; 2099 else 2100 kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot); 2101 } 2102 2103 static void cpu_hyp_init_context(void) 2104 { 2105 kvm_init_host_cpu_context(host_data_ptr(host_ctxt)); 2106 kvm_init_host_debug_data(); 2107 2108 if (!is_kernel_in_hyp_mode()) 2109 cpu_init_hyp_mode(); 2110 } 2111 2112 static void cpu_hyp_init_features(void) 2113 { 2114 cpu_set_hyp_vector(); 2115 2116 if (is_kernel_in_hyp_mode()) 2117 kvm_timer_init_vhe(); 2118 2119 if (vgic_present) 2120 kvm_vgic_init_cpu_hardware(); 2121 } 2122 2123 static void cpu_hyp_reinit(void) 2124 { 2125 cpu_hyp_reset(); 2126 cpu_hyp_init_context(); 2127 cpu_hyp_init_features(); 2128 } 2129 2130 static void cpu_hyp_init(void *discard) 2131 { 2132 if (!__this_cpu_read(kvm_hyp_initialized)) { 2133 cpu_hyp_reinit(); 2134 __this_cpu_write(kvm_hyp_initialized, 1); 2135 } 2136 } 2137 2138 static void cpu_hyp_uninit(void *discard) 2139 { 2140 if (!is_protected_kvm_enabled() && __this_cpu_read(kvm_hyp_initialized)) { 2141 cpu_hyp_reset(); 2142 __this_cpu_write(kvm_hyp_initialized, 0); 2143 } 2144 } 2145 2146 int kvm_arch_enable_virtualization_cpu(void) 2147 { 2148 /* 2149 * Most calls to this function are made with migration 2150 * disabled, but not with preemption disabled. The former is 2151 * enough to ensure correctness, but most of the helpers 2152 * expect the later and will throw a tantrum otherwise. 2153 */ 2154 preempt_disable(); 2155 2156 cpu_hyp_init(NULL); 2157 2158 kvm_vgic_cpu_up(); 2159 kvm_timer_cpu_up(); 2160 2161 preempt_enable(); 2162 2163 return 0; 2164 } 2165 2166 void kvm_arch_disable_virtualization_cpu(void) 2167 { 2168 kvm_timer_cpu_down(); 2169 kvm_vgic_cpu_down(); 2170 2171 if (!is_protected_kvm_enabled()) 2172 cpu_hyp_uninit(NULL); 2173 } 2174 2175 #ifdef CONFIG_CPU_PM 2176 static int hyp_init_cpu_pm_notifier(struct notifier_block *self, 2177 unsigned long cmd, 2178 void *v) 2179 { 2180 /* 2181 * kvm_hyp_initialized is left with its old value over 2182 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should 2183 * re-enable hyp. 2184 */ 2185 switch (cmd) { 2186 case CPU_PM_ENTER: 2187 if (__this_cpu_read(kvm_hyp_initialized)) 2188 /* 2189 * don't update kvm_hyp_initialized here 2190 * so that the hyp will be re-enabled 2191 * when we resume. See below. 2192 */ 2193 cpu_hyp_reset(); 2194 2195 return NOTIFY_OK; 2196 case CPU_PM_ENTER_FAILED: 2197 case CPU_PM_EXIT: 2198 if (__this_cpu_read(kvm_hyp_initialized)) 2199 /* The hyp was enabled before suspend. */ 2200 cpu_hyp_reinit(); 2201 2202 return NOTIFY_OK; 2203 2204 default: 2205 return NOTIFY_DONE; 2206 } 2207 } 2208 2209 static struct notifier_block hyp_init_cpu_pm_nb = { 2210 .notifier_call = hyp_init_cpu_pm_notifier, 2211 }; 2212 2213 static void __init hyp_cpu_pm_init(void) 2214 { 2215 if (!is_protected_kvm_enabled()) 2216 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); 2217 } 2218 static void __init hyp_cpu_pm_exit(void) 2219 { 2220 if (!is_protected_kvm_enabled()) 2221 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); 2222 } 2223 #else 2224 static inline void __init hyp_cpu_pm_init(void) 2225 { 2226 } 2227 static inline void __init hyp_cpu_pm_exit(void) 2228 { 2229 } 2230 #endif 2231 2232 static void __init init_cpu_logical_map(void) 2233 { 2234 unsigned int cpu; 2235 2236 /* 2237 * Copy the MPIDR <-> logical CPU ID mapping to hyp. 2238 * Only copy the set of online CPUs whose features have been checked 2239 * against the finalized system capabilities. The hypervisor will not 2240 * allow any other CPUs from the `possible` set to boot. 2241 */ 2242 for_each_online_cpu(cpu) 2243 hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu); 2244 } 2245 2246 #define init_psci_0_1_impl_state(config, what) \ 2247 config.psci_0_1_ ## what ## _implemented = psci_ops.what 2248 2249 static bool __init init_psci_relay(void) 2250 { 2251 /* 2252 * If PSCI has not been initialized, protected KVM cannot install 2253 * itself on newly booted CPUs. 2254 */ 2255 if (!psci_ops.get_version) { 2256 kvm_err("Cannot initialize protected mode without PSCI\n"); 2257 return false; 2258 } 2259 2260 kvm_host_psci_config.version = psci_ops.get_version(); 2261 kvm_host_psci_config.smccc_version = arm_smccc_get_version(); 2262 2263 if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) { 2264 kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids(); 2265 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend); 2266 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on); 2267 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off); 2268 init_psci_0_1_impl_state(kvm_host_psci_config, migrate); 2269 } 2270 return true; 2271 } 2272 2273 static int __init init_subsystems(void) 2274 { 2275 int err = 0; 2276 2277 /* 2278 * Enable hardware so that subsystem initialisation can access EL2. 2279 */ 2280 on_each_cpu(cpu_hyp_init, NULL, 1); 2281 2282 /* 2283 * Register CPU lower-power notifier 2284 */ 2285 hyp_cpu_pm_init(); 2286 2287 /* 2288 * Init HYP view of VGIC 2289 */ 2290 err = kvm_vgic_hyp_init(); 2291 switch (err) { 2292 case 0: 2293 vgic_present = true; 2294 break; 2295 case -ENODEV: 2296 case -ENXIO: 2297 /* 2298 * No VGIC? No pKVM for you. 2299 * 2300 * Protected mode assumes that VGICv3 is present, so no point 2301 * in trying to hobble along if vgic initialization fails. 2302 */ 2303 if (is_protected_kvm_enabled()) 2304 goto out; 2305 2306 /* 2307 * Otherwise, userspace could choose to implement a GIC for its 2308 * guest on non-cooperative hardware. 2309 */ 2310 vgic_present = false; 2311 err = 0; 2312 break; 2313 default: 2314 goto out; 2315 } 2316 2317 if (kvm_mode == KVM_MODE_NV && 2318 !(vgic_present && kvm_vgic_global_state.type == VGIC_V3)) { 2319 kvm_err("NV support requires GICv3, giving up\n"); 2320 err = -EINVAL; 2321 goto out; 2322 } 2323 2324 /* 2325 * Init HYP architected timer support 2326 */ 2327 err = kvm_timer_hyp_init(vgic_present); 2328 if (err) 2329 goto out; 2330 2331 kvm_register_perf_callbacks(NULL); 2332 2333 out: 2334 if (err) 2335 hyp_cpu_pm_exit(); 2336 2337 if (err || !is_protected_kvm_enabled()) 2338 on_each_cpu(cpu_hyp_uninit, NULL, 1); 2339 2340 return err; 2341 } 2342 2343 static void __init teardown_subsystems(void) 2344 { 2345 kvm_unregister_perf_callbacks(); 2346 hyp_cpu_pm_exit(); 2347 } 2348 2349 static void __init teardown_hyp_mode(void) 2350 { 2351 bool free_sve = system_supports_sve() && is_protected_kvm_enabled(); 2352 int cpu; 2353 2354 free_hyp_pgds(); 2355 for_each_possible_cpu(cpu) { 2356 if (per_cpu(kvm_hyp_initialized, cpu)) 2357 continue; 2358 2359 free_pages(per_cpu(kvm_arm_hyp_stack_base, cpu), NVHE_STACK_SHIFT - PAGE_SHIFT); 2360 2361 if (!kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]) 2362 continue; 2363 2364 if (free_sve) { 2365 struct cpu_sve_state *sve_state; 2366 2367 sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state; 2368 free_pages((unsigned long) sve_state, pkvm_host_sve_state_order()); 2369 } 2370 2371 free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order()); 2372 2373 } 2374 } 2375 2376 static int __init do_pkvm_init(u32 hyp_va_bits) 2377 { 2378 void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)); 2379 int ret; 2380 2381 preempt_disable(); 2382 cpu_hyp_init_context(); 2383 ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size, 2384 num_possible_cpus(), kern_hyp_va(per_cpu_base), 2385 hyp_va_bits); 2386 cpu_hyp_init_features(); 2387 2388 /* 2389 * The stub hypercalls are now disabled, so set our local flag to 2390 * prevent a later re-init attempt in kvm_arch_enable_virtualization_cpu(). 2391 */ 2392 __this_cpu_write(kvm_hyp_initialized, 1); 2393 preempt_enable(); 2394 2395 return ret; 2396 } 2397 2398 static u64 get_hyp_id_aa64pfr0_el1(void) 2399 { 2400 /* 2401 * Track whether the system isn't affected by spectre/meltdown in the 2402 * hypervisor's view of id_aa64pfr0_el1, used for protected VMs. 2403 * Although this is per-CPU, we make it global for simplicity, e.g., not 2404 * to have to worry about vcpu migration. 2405 * 2406 * Unlike for non-protected VMs, userspace cannot override this for 2407 * protected VMs. 2408 */ 2409 u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); 2410 2411 val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) | 2412 ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3)); 2413 2414 val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 2415 arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED); 2416 val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 2417 arm64_get_meltdown_state() == SPECTRE_UNAFFECTED); 2418 2419 return val; 2420 } 2421 2422 static void kvm_hyp_init_symbols(void) 2423 { 2424 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1(); 2425 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1); 2426 kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1); 2427 kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); 2428 kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); 2429 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); 2430 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 2431 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1); 2432 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1); 2433 kvm_nvhe_sym(__icache_flags) = __icache_flags; 2434 kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits; 2435 2436 /* Propagate the FGT state to the the nVHE side */ 2437 kvm_nvhe_sym(hfgrtr_masks) = hfgrtr_masks; 2438 kvm_nvhe_sym(hfgwtr_masks) = hfgwtr_masks; 2439 kvm_nvhe_sym(hfgitr_masks) = hfgitr_masks; 2440 kvm_nvhe_sym(hdfgrtr_masks) = hdfgrtr_masks; 2441 kvm_nvhe_sym(hdfgwtr_masks) = hdfgwtr_masks; 2442 kvm_nvhe_sym(hafgrtr_masks) = hafgrtr_masks; 2443 kvm_nvhe_sym(hfgrtr2_masks) = hfgrtr2_masks; 2444 kvm_nvhe_sym(hfgwtr2_masks) = hfgwtr2_masks; 2445 kvm_nvhe_sym(hfgitr2_masks) = hfgitr2_masks; 2446 kvm_nvhe_sym(hdfgrtr2_masks)= hdfgrtr2_masks; 2447 kvm_nvhe_sym(hdfgwtr2_masks)= hdfgwtr2_masks; 2448 2449 /* 2450 * Flush entire BSS since part of its data containing init symbols is read 2451 * while the MMU is off. 2452 */ 2453 kvm_flush_dcache_to_poc(kvm_ksym_ref(__hyp_bss_start), 2454 kvm_ksym_ref(__hyp_bss_end) - kvm_ksym_ref(__hyp_bss_start)); 2455 } 2456 2457 static int __init kvm_hyp_init_protection(u32 hyp_va_bits) 2458 { 2459 void *addr = phys_to_virt(hyp_mem_base); 2460 int ret; 2461 2462 ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP); 2463 if (ret) 2464 return ret; 2465 2466 ret = do_pkvm_init(hyp_va_bits); 2467 if (ret) 2468 return ret; 2469 2470 free_hyp_pgds(); 2471 2472 return 0; 2473 } 2474 2475 static int init_pkvm_host_sve_state(void) 2476 { 2477 int cpu; 2478 2479 if (!system_supports_sve()) 2480 return 0; 2481 2482 /* Allocate pages for host sve state in protected mode. */ 2483 for_each_possible_cpu(cpu) { 2484 struct page *page = alloc_pages(GFP_KERNEL, pkvm_host_sve_state_order()); 2485 2486 if (!page) 2487 return -ENOMEM; 2488 2489 per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = page_address(page); 2490 } 2491 2492 /* 2493 * Don't map the pages in hyp since these are only used in protected 2494 * mode, which will (re)create its own mapping when initialized. 2495 */ 2496 2497 return 0; 2498 } 2499 2500 /* 2501 * Finalizes the initialization of hyp mode, once everything else is initialized 2502 * and the initialziation process cannot fail. 2503 */ 2504 static void finalize_init_hyp_mode(void) 2505 { 2506 int cpu; 2507 2508 if (system_supports_sve() && is_protected_kvm_enabled()) { 2509 for_each_possible_cpu(cpu) { 2510 struct cpu_sve_state *sve_state; 2511 2512 sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state; 2513 per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = 2514 kern_hyp_va(sve_state); 2515 } 2516 } 2517 } 2518 2519 static void pkvm_hyp_init_ptrauth(void) 2520 { 2521 struct kvm_cpu_context *hyp_ctxt; 2522 int cpu; 2523 2524 for_each_possible_cpu(cpu) { 2525 hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu); 2526 hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long(); 2527 hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long(); 2528 hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long(); 2529 hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long(); 2530 hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long(); 2531 hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long(); 2532 hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long(); 2533 hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long(); 2534 hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long(); 2535 hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long(); 2536 } 2537 } 2538 2539 /* Inits Hyp-mode on all online CPUs */ 2540 static int __init init_hyp_mode(void) 2541 { 2542 u32 hyp_va_bits; 2543 int cpu; 2544 int err = -ENOMEM; 2545 2546 /* 2547 * The protected Hyp-mode cannot be initialized if the memory pool 2548 * allocation has failed. 2549 */ 2550 if (is_protected_kvm_enabled() && !hyp_mem_base) 2551 goto out_err; 2552 2553 /* 2554 * Allocate Hyp PGD and setup Hyp identity mapping 2555 */ 2556 err = kvm_mmu_init(&hyp_va_bits); 2557 if (err) 2558 goto out_err; 2559 2560 /* 2561 * Allocate stack pages for Hypervisor-mode 2562 */ 2563 for_each_possible_cpu(cpu) { 2564 unsigned long stack_base; 2565 2566 stack_base = __get_free_pages(GFP_KERNEL, NVHE_STACK_SHIFT - PAGE_SHIFT); 2567 if (!stack_base) { 2568 err = -ENOMEM; 2569 goto out_err; 2570 } 2571 2572 per_cpu(kvm_arm_hyp_stack_base, cpu) = stack_base; 2573 } 2574 2575 /* 2576 * Allocate and initialize pages for Hypervisor-mode percpu regions. 2577 */ 2578 for_each_possible_cpu(cpu) { 2579 struct page *page; 2580 void *page_addr; 2581 2582 page = alloc_pages(GFP_KERNEL, nvhe_percpu_order()); 2583 if (!page) { 2584 err = -ENOMEM; 2585 goto out_err; 2586 } 2587 2588 page_addr = page_address(page); 2589 memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size()); 2590 kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr; 2591 } 2592 2593 /* 2594 * Map the Hyp-code called directly from the host 2595 */ 2596 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), 2597 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); 2598 if (err) { 2599 kvm_err("Cannot map world-switch code\n"); 2600 goto out_err; 2601 } 2602 2603 err = create_hyp_mappings(kvm_ksym_ref(__hyp_data_start), 2604 kvm_ksym_ref(__hyp_data_end), PAGE_HYP); 2605 if (err) { 2606 kvm_err("Cannot map .hyp.data section\n"); 2607 goto out_err; 2608 } 2609 2610 err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start), 2611 kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO); 2612 if (err) { 2613 kvm_err("Cannot map .hyp.rodata section\n"); 2614 goto out_err; 2615 } 2616 2617 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), 2618 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); 2619 if (err) { 2620 kvm_err("Cannot map rodata section\n"); 2621 goto out_err; 2622 } 2623 2624 /* 2625 * .hyp.bss is guaranteed to be placed at the beginning of the .bss 2626 * section thanks to an assertion in the linker script. Map it RW and 2627 * the rest of .bss RO. 2628 */ 2629 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start), 2630 kvm_ksym_ref(__hyp_bss_end), PAGE_HYP); 2631 if (err) { 2632 kvm_err("Cannot map hyp bss section: %d\n", err); 2633 goto out_err; 2634 } 2635 2636 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end), 2637 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); 2638 if (err) { 2639 kvm_err("Cannot map bss section\n"); 2640 goto out_err; 2641 } 2642 2643 /* 2644 * Map the Hyp stack pages 2645 */ 2646 for_each_possible_cpu(cpu) { 2647 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 2648 char *stack_base = (char *)per_cpu(kvm_arm_hyp_stack_base, cpu); 2649 2650 err = create_hyp_stack(__pa(stack_base), ¶ms->stack_hyp_va); 2651 if (err) { 2652 kvm_err("Cannot map hyp stack\n"); 2653 goto out_err; 2654 } 2655 2656 /* 2657 * Save the stack PA in nvhe_init_params. This will be needed 2658 * to recreate the stack mapping in protected nVHE mode. 2659 * __hyp_pa() won't do the right thing there, since the stack 2660 * has been mapped in the flexible private VA space. 2661 */ 2662 params->stack_pa = __pa(stack_base); 2663 } 2664 2665 for_each_possible_cpu(cpu) { 2666 char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]; 2667 char *percpu_end = percpu_begin + nvhe_percpu_size(); 2668 2669 /* Map Hyp percpu pages */ 2670 err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP); 2671 if (err) { 2672 kvm_err("Cannot map hyp percpu region\n"); 2673 goto out_err; 2674 } 2675 2676 /* Prepare the CPU initialization parameters */ 2677 cpu_prepare_hyp_mode(cpu, hyp_va_bits); 2678 } 2679 2680 kvm_hyp_init_symbols(); 2681 2682 if (is_protected_kvm_enabled()) { 2683 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) && 2684 cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH)) 2685 pkvm_hyp_init_ptrauth(); 2686 2687 init_cpu_logical_map(); 2688 2689 if (!init_psci_relay()) { 2690 err = -ENODEV; 2691 goto out_err; 2692 } 2693 2694 err = init_pkvm_host_sve_state(); 2695 if (err) 2696 goto out_err; 2697 2698 err = kvm_hyp_init_protection(hyp_va_bits); 2699 if (err) { 2700 kvm_err("Failed to init hyp memory protection\n"); 2701 goto out_err; 2702 } 2703 } 2704 2705 return 0; 2706 2707 out_err: 2708 teardown_hyp_mode(); 2709 kvm_err("error initializing Hyp mode: %d\n", err); 2710 return err; 2711 } 2712 2713 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) 2714 { 2715 struct kvm_vcpu *vcpu = NULL; 2716 struct kvm_mpidr_data *data; 2717 unsigned long i; 2718 2719 mpidr &= MPIDR_HWID_BITMASK; 2720 2721 rcu_read_lock(); 2722 data = rcu_dereference(kvm->arch.mpidr_data); 2723 2724 if (data) { 2725 u16 idx = kvm_mpidr_index(data, mpidr); 2726 2727 vcpu = kvm_get_vcpu(kvm, data->cmpidr_to_idx[idx]); 2728 if (mpidr != kvm_vcpu_get_mpidr_aff(vcpu)) 2729 vcpu = NULL; 2730 } 2731 2732 rcu_read_unlock(); 2733 2734 if (vcpu) 2735 return vcpu; 2736 2737 kvm_for_each_vcpu(i, vcpu, kvm) { 2738 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) 2739 return vcpu; 2740 } 2741 return NULL; 2742 } 2743 2744 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) 2745 { 2746 return irqchip_in_kernel(kvm); 2747 } 2748 2749 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, 2750 struct irq_bypass_producer *prod) 2751 { 2752 struct kvm_kernel_irqfd *irqfd = 2753 container_of(cons, struct kvm_kernel_irqfd, consumer); 2754 struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry; 2755 2756 /* 2757 * The only thing we have a chance of directly-injecting is LPIs. Maybe 2758 * one day... 2759 */ 2760 if (irq_entry->type != KVM_IRQ_ROUTING_MSI) 2761 return 0; 2762 2763 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, 2764 &irqfd->irq_entry); 2765 } 2766 2767 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, 2768 struct irq_bypass_producer *prod) 2769 { 2770 struct kvm_kernel_irqfd *irqfd = 2771 container_of(cons, struct kvm_kernel_irqfd, consumer); 2772 struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry; 2773 2774 if (irq_entry->type != KVM_IRQ_ROUTING_MSI) 2775 return; 2776 2777 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq); 2778 } 2779 2780 void kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, 2781 struct kvm_kernel_irq_routing_entry *old, 2782 struct kvm_kernel_irq_routing_entry *new) 2783 { 2784 if (old->type == KVM_IRQ_ROUTING_MSI && 2785 new->type == KVM_IRQ_ROUTING_MSI && 2786 !memcmp(&old->msi, &new->msi, sizeof(new->msi))) 2787 return; 2788 2789 /* 2790 * Remapping the vLPI requires taking the its_lock mutex to resolve 2791 * the new translation. We're in spinlock land at this point, so no 2792 * chance of resolving the translation. 2793 * 2794 * Unmap the vLPI and fall back to software LPI injection. 2795 */ 2796 return kvm_vgic_v4_unset_forwarding(irqfd->kvm, irqfd->producer->irq); 2797 } 2798 2799 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) 2800 { 2801 struct kvm_kernel_irqfd *irqfd = 2802 container_of(cons, struct kvm_kernel_irqfd, consumer); 2803 2804 kvm_arm_halt_guest(irqfd->kvm); 2805 } 2806 2807 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) 2808 { 2809 struct kvm_kernel_irqfd *irqfd = 2810 container_of(cons, struct kvm_kernel_irqfd, consumer); 2811 2812 kvm_arm_resume_guest(irqfd->kvm); 2813 } 2814 2815 /* Initialize Hyp-mode and memory mappings on all CPUs */ 2816 static __init int kvm_arm_init(void) 2817 { 2818 int err; 2819 bool in_hyp_mode; 2820 2821 if (!is_hyp_mode_available()) { 2822 kvm_info("HYP mode not available\n"); 2823 return -ENODEV; 2824 } 2825 2826 if (kvm_get_mode() == KVM_MODE_NONE) { 2827 kvm_info("KVM disabled from command line\n"); 2828 return -ENODEV; 2829 } 2830 2831 err = kvm_sys_reg_table_init(); 2832 if (err) { 2833 kvm_info("Error initializing system register tables"); 2834 return err; 2835 } 2836 2837 in_hyp_mode = is_kernel_in_hyp_mode(); 2838 2839 if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) || 2840 cpus_have_final_cap(ARM64_WORKAROUND_1508412)) 2841 kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \ 2842 "Only trusted guests should be used on this system.\n"); 2843 2844 err = kvm_set_ipa_limit(); 2845 if (err) 2846 return err; 2847 2848 err = kvm_arm_init_sve(); 2849 if (err) 2850 return err; 2851 2852 err = kvm_arm_vmid_alloc_init(); 2853 if (err) { 2854 kvm_err("Failed to initialize VMID allocator.\n"); 2855 return err; 2856 } 2857 2858 if (!in_hyp_mode) { 2859 err = init_hyp_mode(); 2860 if (err) 2861 goto out_err; 2862 } 2863 2864 err = kvm_init_vector_slots(); 2865 if (err) { 2866 kvm_err("Cannot initialise vector slots\n"); 2867 goto out_hyp; 2868 } 2869 2870 err = init_subsystems(); 2871 if (err) 2872 goto out_hyp; 2873 2874 kvm_info("%s%sVHE%s mode initialized successfully\n", 2875 in_hyp_mode ? "" : (is_protected_kvm_enabled() ? 2876 "Protected " : "Hyp "), 2877 in_hyp_mode ? "" : (cpus_have_final_cap(ARM64_KVM_HVHE) ? 2878 "h" : "n"), 2879 cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) ? "+NV2": ""); 2880 2881 /* 2882 * FIXME: Do something reasonable if kvm_init() fails after pKVM 2883 * hypervisor protection is finalized. 2884 */ 2885 err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE); 2886 if (err) 2887 goto out_subs; 2888 2889 /* 2890 * This should be called after initialization is done and failure isn't 2891 * possible anymore. 2892 */ 2893 if (!in_hyp_mode) 2894 finalize_init_hyp_mode(); 2895 2896 kvm_arm_initialised = true; 2897 2898 return 0; 2899 2900 out_subs: 2901 teardown_subsystems(); 2902 out_hyp: 2903 if (!in_hyp_mode) 2904 teardown_hyp_mode(); 2905 out_err: 2906 kvm_arm_vmid_alloc_free(); 2907 return err; 2908 } 2909 2910 static int __init early_kvm_mode_cfg(char *arg) 2911 { 2912 if (!arg) 2913 return -EINVAL; 2914 2915 if (strcmp(arg, "none") == 0) { 2916 kvm_mode = KVM_MODE_NONE; 2917 return 0; 2918 } 2919 2920 if (!is_hyp_mode_available()) { 2921 pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n"); 2922 return 0; 2923 } 2924 2925 if (strcmp(arg, "protected") == 0) { 2926 if (!is_kernel_in_hyp_mode()) 2927 kvm_mode = KVM_MODE_PROTECTED; 2928 else 2929 pr_warn_once("Protected KVM not available with VHE\n"); 2930 2931 return 0; 2932 } 2933 2934 if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) { 2935 kvm_mode = KVM_MODE_DEFAULT; 2936 return 0; 2937 } 2938 2939 if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) { 2940 kvm_mode = KVM_MODE_NV; 2941 return 0; 2942 } 2943 2944 return -EINVAL; 2945 } 2946 early_param("kvm-arm.mode", early_kvm_mode_cfg); 2947 2948 static int __init early_kvm_wfx_trap_policy_cfg(char *arg, enum kvm_wfx_trap_policy *p) 2949 { 2950 if (!arg) 2951 return -EINVAL; 2952 2953 if (strcmp(arg, "trap") == 0) { 2954 *p = KVM_WFX_TRAP; 2955 return 0; 2956 } 2957 2958 if (strcmp(arg, "notrap") == 0) { 2959 *p = KVM_WFX_NOTRAP; 2960 return 0; 2961 } 2962 2963 return -EINVAL; 2964 } 2965 2966 static int __init early_kvm_wfi_trap_policy_cfg(char *arg) 2967 { 2968 return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfi_trap_policy); 2969 } 2970 early_param("kvm-arm.wfi_trap_policy", early_kvm_wfi_trap_policy_cfg); 2971 2972 static int __init early_kvm_wfe_trap_policy_cfg(char *arg) 2973 { 2974 return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfe_trap_policy); 2975 } 2976 early_param("kvm-arm.wfe_trap_policy", early_kvm_wfe_trap_policy_cfg); 2977 2978 enum kvm_mode kvm_get_mode(void) 2979 { 2980 return kvm_mode; 2981 } 2982 2983 module_init(kvm_arm_init); 2984