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