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 (!vcpu->wants_to_run) { 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 if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features)) 1462 return 0; 1463 1464 /* MTE is incompatible with AArch32 */ 1465 if (kvm_has_mte(vcpu->kvm)) 1466 return -EINVAL; 1467 1468 /* NV is incompatible with AArch32 */ 1469 if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features)) 1470 return -EINVAL; 1471 1472 return 0; 1473 } 1474 1475 static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu, 1476 const struct kvm_vcpu_init *init) 1477 { 1478 unsigned long features = init->features[0]; 1479 1480 return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features, 1481 KVM_VCPU_MAX_FEATURES); 1482 } 1483 1484 static int kvm_setup_vcpu(struct kvm_vcpu *vcpu) 1485 { 1486 struct kvm *kvm = vcpu->kvm; 1487 int ret = 0; 1488 1489 /* 1490 * When the vCPU has a PMU, but no PMU is set for the guest 1491 * yet, set the default one. 1492 */ 1493 if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu) 1494 ret = kvm_arm_set_default_pmu(kvm); 1495 1496 /* Prepare for nested if required */ 1497 if (!ret && vcpu_has_nv(vcpu)) 1498 ret = kvm_vcpu_init_nested(vcpu); 1499 1500 return ret; 1501 } 1502 1503 static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1504 const struct kvm_vcpu_init *init) 1505 { 1506 unsigned long features = init->features[0]; 1507 struct kvm *kvm = vcpu->kvm; 1508 int ret = -EINVAL; 1509 1510 mutex_lock(&kvm->arch.config_lock); 1511 1512 if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) && 1513 kvm_vcpu_init_changed(vcpu, init)) 1514 goto out_unlock; 1515 1516 bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES); 1517 1518 ret = kvm_setup_vcpu(vcpu); 1519 if (ret) 1520 goto out_unlock; 1521 1522 /* Now we know what it is, we can reset it. */ 1523 kvm_reset_vcpu(vcpu); 1524 1525 set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags); 1526 vcpu_set_flag(vcpu, VCPU_INITIALIZED); 1527 ret = 0; 1528 out_unlock: 1529 mutex_unlock(&kvm->arch.config_lock); 1530 return ret; 1531 } 1532 1533 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1534 const struct kvm_vcpu_init *init) 1535 { 1536 int ret; 1537 1538 if (init->target != KVM_ARM_TARGET_GENERIC_V8 && 1539 init->target != kvm_target_cpu()) 1540 return -EINVAL; 1541 1542 ret = kvm_vcpu_init_check_features(vcpu, init); 1543 if (ret) 1544 return ret; 1545 1546 if (!kvm_vcpu_initialized(vcpu)) 1547 return __kvm_vcpu_set_target(vcpu, init); 1548 1549 if (kvm_vcpu_init_changed(vcpu, init)) 1550 return -EINVAL; 1551 1552 kvm_reset_vcpu(vcpu); 1553 return 0; 1554 } 1555 1556 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, 1557 struct kvm_vcpu_init *init) 1558 { 1559 bool power_off = false; 1560 int ret; 1561 1562 /* 1563 * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid 1564 * reflecting it in the finalized feature set, thus limiting its scope 1565 * to a single KVM_ARM_VCPU_INIT call. 1566 */ 1567 if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) { 1568 init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF); 1569 power_off = true; 1570 } 1571 1572 ret = kvm_vcpu_set_target(vcpu, init); 1573 if (ret) 1574 return ret; 1575 1576 /* 1577 * Ensure a rebooted VM will fault in RAM pages and detect if the 1578 * guest MMU is turned off and flush the caches as needed. 1579 * 1580 * S2FWB enforces all memory accesses to RAM being cacheable, 1581 * ensuring that the data side is always coherent. We still 1582 * need to invalidate the I-cache though, as FWB does *not* 1583 * imply CTR_EL0.DIC. 1584 */ 1585 if (vcpu_has_run_once(vcpu)) { 1586 if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) 1587 stage2_unmap_vm(vcpu->kvm); 1588 else 1589 icache_inval_all_pou(); 1590 } 1591 1592 vcpu_reset_hcr(vcpu); 1593 vcpu->arch.cptr_el2 = kvm_get_reset_cptr_el2(vcpu); 1594 1595 /* 1596 * Handle the "start in power-off" case. 1597 */ 1598 spin_lock(&vcpu->arch.mp_state_lock); 1599 1600 if (power_off) 1601 __kvm_arm_vcpu_power_off(vcpu); 1602 else 1603 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE); 1604 1605 spin_unlock(&vcpu->arch.mp_state_lock); 1606 1607 return 0; 1608 } 1609 1610 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, 1611 struct kvm_device_attr *attr) 1612 { 1613 int ret = -ENXIO; 1614 1615 switch (attr->group) { 1616 default: 1617 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr); 1618 break; 1619 } 1620 1621 return ret; 1622 } 1623 1624 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, 1625 struct kvm_device_attr *attr) 1626 { 1627 int ret = -ENXIO; 1628 1629 switch (attr->group) { 1630 default: 1631 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr); 1632 break; 1633 } 1634 1635 return ret; 1636 } 1637 1638 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, 1639 struct kvm_device_attr *attr) 1640 { 1641 int ret = -ENXIO; 1642 1643 switch (attr->group) { 1644 default: 1645 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr); 1646 break; 1647 } 1648 1649 return ret; 1650 } 1651 1652 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, 1653 struct kvm_vcpu_events *events) 1654 { 1655 memset(events, 0, sizeof(*events)); 1656 1657 return __kvm_arm_vcpu_get_events(vcpu, events); 1658 } 1659 1660 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, 1661 struct kvm_vcpu_events *events) 1662 { 1663 int i; 1664 1665 /* check whether the reserved field is zero */ 1666 for (i = 0; i < ARRAY_SIZE(events->reserved); i++) 1667 if (events->reserved[i]) 1668 return -EINVAL; 1669 1670 /* check whether the pad field is zero */ 1671 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++) 1672 if (events->exception.pad[i]) 1673 return -EINVAL; 1674 1675 return __kvm_arm_vcpu_set_events(vcpu, events); 1676 } 1677 1678 long kvm_arch_vcpu_ioctl(struct file *filp, 1679 unsigned int ioctl, unsigned long arg) 1680 { 1681 struct kvm_vcpu *vcpu = filp->private_data; 1682 void __user *argp = (void __user *)arg; 1683 struct kvm_device_attr attr; 1684 long r; 1685 1686 switch (ioctl) { 1687 case KVM_ARM_VCPU_INIT: { 1688 struct kvm_vcpu_init init; 1689 1690 r = -EFAULT; 1691 if (copy_from_user(&init, argp, sizeof(init))) 1692 break; 1693 1694 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); 1695 break; 1696 } 1697 case KVM_SET_ONE_REG: 1698 case KVM_GET_ONE_REG: { 1699 struct kvm_one_reg reg; 1700 1701 r = -ENOEXEC; 1702 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1703 break; 1704 1705 r = -EFAULT; 1706 if (copy_from_user(®, argp, sizeof(reg))) 1707 break; 1708 1709 /* 1710 * We could owe a reset due to PSCI. Handle the pending reset 1711 * here to ensure userspace register accesses are ordered after 1712 * the reset. 1713 */ 1714 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) 1715 kvm_reset_vcpu(vcpu); 1716 1717 if (ioctl == KVM_SET_ONE_REG) 1718 r = kvm_arm_set_reg(vcpu, ®); 1719 else 1720 r = kvm_arm_get_reg(vcpu, ®); 1721 break; 1722 } 1723 case KVM_GET_REG_LIST: { 1724 struct kvm_reg_list __user *user_list = argp; 1725 struct kvm_reg_list reg_list; 1726 unsigned n; 1727 1728 r = -ENOEXEC; 1729 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1730 break; 1731 1732 r = -EPERM; 1733 if (!kvm_arm_vcpu_is_finalized(vcpu)) 1734 break; 1735 1736 r = -EFAULT; 1737 if (copy_from_user(®_list, user_list, sizeof(reg_list))) 1738 break; 1739 n = reg_list.n; 1740 reg_list.n = kvm_arm_num_regs(vcpu); 1741 if (copy_to_user(user_list, ®_list, sizeof(reg_list))) 1742 break; 1743 r = -E2BIG; 1744 if (n < reg_list.n) 1745 break; 1746 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg); 1747 break; 1748 } 1749 case KVM_SET_DEVICE_ATTR: { 1750 r = -EFAULT; 1751 if (copy_from_user(&attr, argp, sizeof(attr))) 1752 break; 1753 r = kvm_arm_vcpu_set_attr(vcpu, &attr); 1754 break; 1755 } 1756 case KVM_GET_DEVICE_ATTR: { 1757 r = -EFAULT; 1758 if (copy_from_user(&attr, argp, sizeof(attr))) 1759 break; 1760 r = kvm_arm_vcpu_get_attr(vcpu, &attr); 1761 break; 1762 } 1763 case KVM_HAS_DEVICE_ATTR: { 1764 r = -EFAULT; 1765 if (copy_from_user(&attr, argp, sizeof(attr))) 1766 break; 1767 r = kvm_arm_vcpu_has_attr(vcpu, &attr); 1768 break; 1769 } 1770 case KVM_GET_VCPU_EVENTS: { 1771 struct kvm_vcpu_events events; 1772 1773 if (kvm_arm_vcpu_get_events(vcpu, &events)) 1774 return -EINVAL; 1775 1776 if (copy_to_user(argp, &events, sizeof(events))) 1777 return -EFAULT; 1778 1779 return 0; 1780 } 1781 case KVM_SET_VCPU_EVENTS: { 1782 struct kvm_vcpu_events events; 1783 1784 if (copy_from_user(&events, argp, sizeof(events))) 1785 return -EFAULT; 1786 1787 return kvm_arm_vcpu_set_events(vcpu, &events); 1788 } 1789 case KVM_ARM_VCPU_FINALIZE: { 1790 int what; 1791 1792 if (!kvm_vcpu_initialized(vcpu)) 1793 return -ENOEXEC; 1794 1795 if (get_user(what, (const int __user *)argp)) 1796 return -EFAULT; 1797 1798 return kvm_arm_vcpu_finalize(vcpu, what); 1799 } 1800 default: 1801 r = -EINVAL; 1802 } 1803 1804 return r; 1805 } 1806 1807 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 1808 { 1809 1810 } 1811 1812 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, 1813 struct kvm_arm_device_addr *dev_addr) 1814 { 1815 switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) { 1816 case KVM_ARM_DEVICE_VGIC_V2: 1817 if (!vgic_present) 1818 return -ENXIO; 1819 return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr); 1820 default: 1821 return -ENODEV; 1822 } 1823 } 1824 1825 static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1826 { 1827 switch (attr->group) { 1828 case KVM_ARM_VM_SMCCC_CTRL: 1829 return kvm_vm_smccc_has_attr(kvm, attr); 1830 default: 1831 return -ENXIO; 1832 } 1833 } 1834 1835 static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 1836 { 1837 switch (attr->group) { 1838 case KVM_ARM_VM_SMCCC_CTRL: 1839 return kvm_vm_smccc_set_attr(kvm, attr); 1840 default: 1841 return -ENXIO; 1842 } 1843 } 1844 1845 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) 1846 { 1847 struct kvm *kvm = filp->private_data; 1848 void __user *argp = (void __user *)arg; 1849 struct kvm_device_attr attr; 1850 1851 switch (ioctl) { 1852 case KVM_CREATE_IRQCHIP: { 1853 int ret; 1854 if (!vgic_present) 1855 return -ENXIO; 1856 mutex_lock(&kvm->lock); 1857 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); 1858 mutex_unlock(&kvm->lock); 1859 return ret; 1860 } 1861 case KVM_ARM_SET_DEVICE_ADDR: { 1862 struct kvm_arm_device_addr dev_addr; 1863 1864 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) 1865 return -EFAULT; 1866 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); 1867 } 1868 case KVM_ARM_PREFERRED_TARGET: { 1869 struct kvm_vcpu_init init = { 1870 .target = KVM_ARM_TARGET_GENERIC_V8, 1871 }; 1872 1873 if (copy_to_user(argp, &init, sizeof(init))) 1874 return -EFAULT; 1875 1876 return 0; 1877 } 1878 case KVM_ARM_MTE_COPY_TAGS: { 1879 struct kvm_arm_copy_mte_tags copy_tags; 1880 1881 if (copy_from_user(©_tags, argp, sizeof(copy_tags))) 1882 return -EFAULT; 1883 return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags); 1884 } 1885 case KVM_ARM_SET_COUNTER_OFFSET: { 1886 struct kvm_arm_counter_offset offset; 1887 1888 if (copy_from_user(&offset, argp, sizeof(offset))) 1889 return -EFAULT; 1890 return kvm_vm_ioctl_set_counter_offset(kvm, &offset); 1891 } 1892 case KVM_HAS_DEVICE_ATTR: { 1893 if (copy_from_user(&attr, argp, sizeof(attr))) 1894 return -EFAULT; 1895 1896 return kvm_vm_has_attr(kvm, &attr); 1897 } 1898 case KVM_SET_DEVICE_ATTR: { 1899 if (copy_from_user(&attr, argp, sizeof(attr))) 1900 return -EFAULT; 1901 1902 return kvm_vm_set_attr(kvm, &attr); 1903 } 1904 case KVM_ARM_GET_REG_WRITABLE_MASKS: { 1905 struct reg_mask_range range; 1906 1907 if (copy_from_user(&range, argp, sizeof(range))) 1908 return -EFAULT; 1909 return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range); 1910 } 1911 default: 1912 return -EINVAL; 1913 } 1914 } 1915 1916 /* unlocks vcpus from @vcpu_lock_idx and smaller */ 1917 static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx) 1918 { 1919 struct kvm_vcpu *tmp_vcpu; 1920 1921 for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { 1922 tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx); 1923 mutex_unlock(&tmp_vcpu->mutex); 1924 } 1925 } 1926 1927 void unlock_all_vcpus(struct kvm *kvm) 1928 { 1929 lockdep_assert_held(&kvm->lock); 1930 1931 unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1); 1932 } 1933 1934 /* Returns true if all vcpus were locked, false otherwise */ 1935 bool lock_all_vcpus(struct kvm *kvm) 1936 { 1937 struct kvm_vcpu *tmp_vcpu; 1938 unsigned long c; 1939 1940 lockdep_assert_held(&kvm->lock); 1941 1942 /* 1943 * Any time a vcpu is in an ioctl (including running), the 1944 * core KVM code tries to grab the vcpu->mutex. 1945 * 1946 * By grabbing the vcpu->mutex of all VCPUs we ensure that no 1947 * other VCPUs can fiddle with the state while we access it. 1948 */ 1949 kvm_for_each_vcpu(c, tmp_vcpu, kvm) { 1950 if (!mutex_trylock(&tmp_vcpu->mutex)) { 1951 unlock_vcpus(kvm, c - 1); 1952 return false; 1953 } 1954 } 1955 1956 return true; 1957 } 1958 1959 static unsigned long nvhe_percpu_size(void) 1960 { 1961 return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) - 1962 (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start); 1963 } 1964 1965 static unsigned long nvhe_percpu_order(void) 1966 { 1967 unsigned long size = nvhe_percpu_size(); 1968 1969 return size ? get_order(size) : 0; 1970 } 1971 1972 static size_t pkvm_host_sve_state_order(void) 1973 { 1974 return get_order(pkvm_host_sve_state_size()); 1975 } 1976 1977 /* A lookup table holding the hypervisor VA for each vector slot */ 1978 static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS]; 1979 1980 static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot) 1981 { 1982 hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot); 1983 } 1984 1985 static int kvm_init_vector_slots(void) 1986 { 1987 int err; 1988 void *base; 1989 1990 base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 1991 kvm_init_vector_slot(base, HYP_VECTOR_DIRECT); 1992 1993 base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs)); 1994 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT); 1995 1996 if (kvm_system_needs_idmapped_vectors() && 1997 !is_protected_kvm_enabled()) { 1998 err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs), 1999 __BP_HARDEN_HYP_VECS_SZ, &base); 2000 if (err) 2001 return err; 2002 } 2003 2004 kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT); 2005 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT); 2006 return 0; 2007 } 2008 2009 static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits) 2010 { 2011 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 2012 u64 mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); 2013 unsigned long tcr; 2014 2015 /* 2016 * Calculate the raw per-cpu offset without a translation from the 2017 * kernel's mapping to the linear mapping, and store it in tpidr_el2 2018 * so that we can use adr_l to access per-cpu variables in EL2. 2019 * Also drop the KASAN tag which gets in the way... 2020 */ 2021 params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) - 2022 (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start)); 2023 2024 params->mair_el2 = read_sysreg(mair_el1); 2025 2026 tcr = read_sysreg(tcr_el1); 2027 if (cpus_have_final_cap(ARM64_KVM_HVHE)) { 2028 tcr |= TCR_EPD1_MASK; 2029 } else { 2030 tcr &= TCR_EL2_MASK; 2031 tcr |= TCR_EL2_RES1; 2032 } 2033 tcr &= ~TCR_T0SZ_MASK; 2034 tcr |= TCR_T0SZ(hyp_va_bits); 2035 tcr &= ~TCR_EL2_PS_MASK; 2036 tcr |= FIELD_PREP(TCR_EL2_PS_MASK, kvm_get_parange(mmfr0)); 2037 if (kvm_lpa2_is_enabled()) 2038 tcr |= TCR_EL2_DS; 2039 params->tcr_el2 = tcr; 2040 2041 params->pgd_pa = kvm_mmu_get_httbr(); 2042 if (is_protected_kvm_enabled()) 2043 params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; 2044 else 2045 params->hcr_el2 = HCR_HOST_NVHE_FLAGS; 2046 if (cpus_have_final_cap(ARM64_KVM_HVHE)) 2047 params->hcr_el2 |= HCR_E2H; 2048 params->vttbr = params->vtcr = 0; 2049 2050 /* 2051 * Flush the init params from the data cache because the struct will 2052 * be read while the MMU is off. 2053 */ 2054 kvm_flush_dcache_to_poc(params, sizeof(*params)); 2055 } 2056 2057 static void hyp_install_host_vector(void) 2058 { 2059 struct kvm_nvhe_init_params *params; 2060 struct arm_smccc_res res; 2061 2062 /* Switch from the HYP stub to our own HYP init vector */ 2063 __hyp_set_vectors(kvm_get_idmap_vector()); 2064 2065 /* 2066 * Call initialization code, and switch to the full blown HYP code. 2067 * If the cpucaps haven't been finalized yet, something has gone very 2068 * wrong, and hyp will crash and burn when it uses any 2069 * cpus_have_*_cap() wrapper. 2070 */ 2071 BUG_ON(!system_capabilities_finalized()); 2072 params = this_cpu_ptr_nvhe_sym(kvm_init_params); 2073 arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res); 2074 WARN_ON(res.a0 != SMCCC_RET_SUCCESS); 2075 } 2076 2077 static void cpu_init_hyp_mode(void) 2078 { 2079 hyp_install_host_vector(); 2080 2081 /* 2082 * Disabling SSBD on a non-VHE system requires us to enable SSBS 2083 * at EL2. 2084 */ 2085 if (this_cpu_has_cap(ARM64_SSBS) && 2086 arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) { 2087 kvm_call_hyp_nvhe(__kvm_enable_ssbs); 2088 } 2089 } 2090 2091 static void cpu_hyp_reset(void) 2092 { 2093 if (!is_kernel_in_hyp_mode()) 2094 __hyp_reset_vectors(); 2095 } 2096 2097 /* 2098 * EL2 vectors can be mapped and rerouted in a number of ways, 2099 * depending on the kernel configuration and CPU present: 2100 * 2101 * - If the CPU is affected by Spectre-v2, the hardening sequence is 2102 * placed in one of the vector slots, which is executed before jumping 2103 * to the real vectors. 2104 * 2105 * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot 2106 * containing the hardening sequence is mapped next to the idmap page, 2107 * and executed before jumping to the real vectors. 2108 * 2109 * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an 2110 * empty slot is selected, mapped next to the idmap page, and 2111 * executed before jumping to the real vectors. 2112 * 2113 * Note that ARM64_SPECTRE_V3A is somewhat incompatible with 2114 * VHE, as we don't have hypervisor-specific mappings. If the system 2115 * is VHE and yet selects this capability, it will be ignored. 2116 */ 2117 static void cpu_set_hyp_vector(void) 2118 { 2119 struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data); 2120 void *vector = hyp_spectre_vector_selector[data->slot]; 2121 2122 if (!is_protected_kvm_enabled()) 2123 *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector; 2124 else 2125 kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot); 2126 } 2127 2128 static void cpu_hyp_init_context(void) 2129 { 2130 kvm_init_host_cpu_context(host_data_ptr(host_ctxt)); 2131 2132 if (!is_kernel_in_hyp_mode()) 2133 cpu_init_hyp_mode(); 2134 } 2135 2136 static void cpu_hyp_init_features(void) 2137 { 2138 cpu_set_hyp_vector(); 2139 kvm_arm_init_debug(); 2140 2141 if (is_kernel_in_hyp_mode()) 2142 kvm_timer_init_vhe(); 2143 2144 if (vgic_present) 2145 kvm_vgic_init_cpu_hardware(); 2146 } 2147 2148 static void cpu_hyp_reinit(void) 2149 { 2150 cpu_hyp_reset(); 2151 cpu_hyp_init_context(); 2152 cpu_hyp_init_features(); 2153 } 2154 2155 static void cpu_hyp_init(void *discard) 2156 { 2157 if (!__this_cpu_read(kvm_hyp_initialized)) { 2158 cpu_hyp_reinit(); 2159 __this_cpu_write(kvm_hyp_initialized, 1); 2160 } 2161 } 2162 2163 static void cpu_hyp_uninit(void *discard) 2164 { 2165 if (__this_cpu_read(kvm_hyp_initialized)) { 2166 cpu_hyp_reset(); 2167 __this_cpu_write(kvm_hyp_initialized, 0); 2168 } 2169 } 2170 2171 int kvm_arch_hardware_enable(void) 2172 { 2173 /* 2174 * Most calls to this function are made with migration 2175 * disabled, but not with preemption disabled. The former is 2176 * enough to ensure correctness, but most of the helpers 2177 * expect the later and will throw a tantrum otherwise. 2178 */ 2179 preempt_disable(); 2180 2181 cpu_hyp_init(NULL); 2182 2183 kvm_vgic_cpu_up(); 2184 kvm_timer_cpu_up(); 2185 2186 preempt_enable(); 2187 2188 return 0; 2189 } 2190 2191 void kvm_arch_hardware_disable(void) 2192 { 2193 kvm_timer_cpu_down(); 2194 kvm_vgic_cpu_down(); 2195 2196 if (!is_protected_kvm_enabled()) 2197 cpu_hyp_uninit(NULL); 2198 } 2199 2200 #ifdef CONFIG_CPU_PM 2201 static int hyp_init_cpu_pm_notifier(struct notifier_block *self, 2202 unsigned long cmd, 2203 void *v) 2204 { 2205 /* 2206 * kvm_hyp_initialized is left with its old value over 2207 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should 2208 * re-enable hyp. 2209 */ 2210 switch (cmd) { 2211 case CPU_PM_ENTER: 2212 if (__this_cpu_read(kvm_hyp_initialized)) 2213 /* 2214 * don't update kvm_hyp_initialized here 2215 * so that the hyp will be re-enabled 2216 * when we resume. See below. 2217 */ 2218 cpu_hyp_reset(); 2219 2220 return NOTIFY_OK; 2221 case CPU_PM_ENTER_FAILED: 2222 case CPU_PM_EXIT: 2223 if (__this_cpu_read(kvm_hyp_initialized)) 2224 /* The hyp was enabled before suspend. */ 2225 cpu_hyp_reinit(); 2226 2227 return NOTIFY_OK; 2228 2229 default: 2230 return NOTIFY_DONE; 2231 } 2232 } 2233 2234 static struct notifier_block hyp_init_cpu_pm_nb = { 2235 .notifier_call = hyp_init_cpu_pm_notifier, 2236 }; 2237 2238 static void __init hyp_cpu_pm_init(void) 2239 { 2240 if (!is_protected_kvm_enabled()) 2241 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); 2242 } 2243 static void __init hyp_cpu_pm_exit(void) 2244 { 2245 if (!is_protected_kvm_enabled()) 2246 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); 2247 } 2248 #else 2249 static inline void __init hyp_cpu_pm_init(void) 2250 { 2251 } 2252 static inline void __init hyp_cpu_pm_exit(void) 2253 { 2254 } 2255 #endif 2256 2257 static void __init init_cpu_logical_map(void) 2258 { 2259 unsigned int cpu; 2260 2261 /* 2262 * Copy the MPIDR <-> logical CPU ID mapping to hyp. 2263 * Only copy the set of online CPUs whose features have been checked 2264 * against the finalized system capabilities. The hypervisor will not 2265 * allow any other CPUs from the `possible` set to boot. 2266 */ 2267 for_each_online_cpu(cpu) 2268 hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu); 2269 } 2270 2271 #define init_psci_0_1_impl_state(config, what) \ 2272 config.psci_0_1_ ## what ## _implemented = psci_ops.what 2273 2274 static bool __init init_psci_relay(void) 2275 { 2276 /* 2277 * If PSCI has not been initialized, protected KVM cannot install 2278 * itself on newly booted CPUs. 2279 */ 2280 if (!psci_ops.get_version) { 2281 kvm_err("Cannot initialize protected mode without PSCI\n"); 2282 return false; 2283 } 2284 2285 kvm_host_psci_config.version = psci_ops.get_version(); 2286 kvm_host_psci_config.smccc_version = arm_smccc_get_version(); 2287 2288 if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) { 2289 kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids(); 2290 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend); 2291 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on); 2292 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off); 2293 init_psci_0_1_impl_state(kvm_host_psci_config, migrate); 2294 } 2295 return true; 2296 } 2297 2298 static int __init init_subsystems(void) 2299 { 2300 int err = 0; 2301 2302 /* 2303 * Enable hardware so that subsystem initialisation can access EL2. 2304 */ 2305 on_each_cpu(cpu_hyp_init, NULL, 1); 2306 2307 /* 2308 * Register CPU lower-power notifier 2309 */ 2310 hyp_cpu_pm_init(); 2311 2312 /* 2313 * Init HYP view of VGIC 2314 */ 2315 err = kvm_vgic_hyp_init(); 2316 switch (err) { 2317 case 0: 2318 vgic_present = true; 2319 break; 2320 case -ENODEV: 2321 case -ENXIO: 2322 vgic_present = false; 2323 err = 0; 2324 break; 2325 default: 2326 goto out; 2327 } 2328 2329 /* 2330 * Init HYP architected timer support 2331 */ 2332 err = kvm_timer_hyp_init(vgic_present); 2333 if (err) 2334 goto out; 2335 2336 kvm_register_perf_callbacks(NULL); 2337 2338 out: 2339 if (err) 2340 hyp_cpu_pm_exit(); 2341 2342 if (err || !is_protected_kvm_enabled()) 2343 on_each_cpu(cpu_hyp_uninit, NULL, 1); 2344 2345 return err; 2346 } 2347 2348 static void __init teardown_subsystems(void) 2349 { 2350 kvm_unregister_perf_callbacks(); 2351 hyp_cpu_pm_exit(); 2352 } 2353 2354 static void __init teardown_hyp_mode(void) 2355 { 2356 bool free_sve = system_supports_sve() && is_protected_kvm_enabled(); 2357 int cpu; 2358 2359 free_hyp_pgds(); 2360 for_each_possible_cpu(cpu) { 2361 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); 2362 free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order()); 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 } 2372 2373 static int __init do_pkvm_init(u32 hyp_va_bits) 2374 { 2375 void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)); 2376 int ret; 2377 2378 preempt_disable(); 2379 cpu_hyp_init_context(); 2380 ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size, 2381 num_possible_cpus(), kern_hyp_va(per_cpu_base), 2382 hyp_va_bits); 2383 cpu_hyp_init_features(); 2384 2385 /* 2386 * The stub hypercalls are now disabled, so set our local flag to 2387 * prevent a later re-init attempt in kvm_arch_hardware_enable(). 2388 */ 2389 __this_cpu_write(kvm_hyp_initialized, 1); 2390 preempt_enable(); 2391 2392 return ret; 2393 } 2394 2395 static u64 get_hyp_id_aa64pfr0_el1(void) 2396 { 2397 /* 2398 * Track whether the system isn't affected by spectre/meltdown in the 2399 * hypervisor's view of id_aa64pfr0_el1, used for protected VMs. 2400 * Although this is per-CPU, we make it global for simplicity, e.g., not 2401 * to have to worry about vcpu migration. 2402 * 2403 * Unlike for non-protected VMs, userspace cannot override this for 2404 * protected VMs. 2405 */ 2406 u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); 2407 2408 val &= ~(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2) | 2409 ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3)); 2410 2411 val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV2), 2412 arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED); 2413 val |= FIELD_PREP(ARM64_FEATURE_MASK(ID_AA64PFR0_EL1_CSV3), 2414 arm64_get_meltdown_state() == SPECTRE_UNAFFECTED); 2415 2416 return val; 2417 } 2418 2419 static void kvm_hyp_init_symbols(void) 2420 { 2421 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1(); 2422 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1); 2423 kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1); 2424 kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); 2425 kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); 2426 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); 2427 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 2428 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1); 2429 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1); 2430 kvm_nvhe_sym(__icache_flags) = __icache_flags; 2431 kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits; 2432 } 2433 2434 static int __init kvm_hyp_init_protection(u32 hyp_va_bits) 2435 { 2436 void *addr = phys_to_virt(hyp_mem_base); 2437 int ret; 2438 2439 ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP); 2440 if (ret) 2441 return ret; 2442 2443 ret = do_pkvm_init(hyp_va_bits); 2444 if (ret) 2445 return ret; 2446 2447 free_hyp_pgds(); 2448 2449 return 0; 2450 } 2451 2452 static int init_pkvm_host_sve_state(void) 2453 { 2454 int cpu; 2455 2456 if (!system_supports_sve()) 2457 return 0; 2458 2459 /* Allocate pages for host sve state in protected mode. */ 2460 for_each_possible_cpu(cpu) { 2461 struct page *page = alloc_pages(GFP_KERNEL, pkvm_host_sve_state_order()); 2462 2463 if (!page) 2464 return -ENOMEM; 2465 2466 per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = page_address(page); 2467 } 2468 2469 /* 2470 * Don't map the pages in hyp since these are only used in protected 2471 * mode, which will (re)create its own mapping when initialized. 2472 */ 2473 2474 return 0; 2475 } 2476 2477 /* 2478 * Finalizes the initialization of hyp mode, once everything else is initialized 2479 * and the initialziation process cannot fail. 2480 */ 2481 static void finalize_init_hyp_mode(void) 2482 { 2483 int cpu; 2484 2485 if (system_supports_sve() && is_protected_kvm_enabled()) { 2486 for_each_possible_cpu(cpu) { 2487 struct cpu_sve_state *sve_state; 2488 2489 sve_state = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state; 2490 per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_state = 2491 kern_hyp_va(sve_state); 2492 } 2493 } else { 2494 for_each_possible_cpu(cpu) { 2495 struct user_fpsimd_state *fpsimd_state; 2496 2497 fpsimd_state = &per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->host_ctxt.fp_regs; 2498 per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->fpsimd_state = 2499 kern_hyp_va(fpsimd_state); 2500 } 2501 } 2502 } 2503 2504 static void pkvm_hyp_init_ptrauth(void) 2505 { 2506 struct kvm_cpu_context *hyp_ctxt; 2507 int cpu; 2508 2509 for_each_possible_cpu(cpu) { 2510 hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu); 2511 hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long(); 2512 hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long(); 2513 hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long(); 2514 hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long(); 2515 hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long(); 2516 hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long(); 2517 hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long(); 2518 hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long(); 2519 hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long(); 2520 hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long(); 2521 } 2522 } 2523 2524 /* Inits Hyp-mode on all online CPUs */ 2525 static int __init init_hyp_mode(void) 2526 { 2527 u32 hyp_va_bits; 2528 int cpu; 2529 int err = -ENOMEM; 2530 2531 /* 2532 * The protected Hyp-mode cannot be initialized if the memory pool 2533 * allocation has failed. 2534 */ 2535 if (is_protected_kvm_enabled() && !hyp_mem_base) 2536 goto out_err; 2537 2538 /* 2539 * Allocate Hyp PGD and setup Hyp identity mapping 2540 */ 2541 err = kvm_mmu_init(&hyp_va_bits); 2542 if (err) 2543 goto out_err; 2544 2545 /* 2546 * Allocate stack pages for Hypervisor-mode 2547 */ 2548 for_each_possible_cpu(cpu) { 2549 unsigned long stack_page; 2550 2551 stack_page = __get_free_page(GFP_KERNEL); 2552 if (!stack_page) { 2553 err = -ENOMEM; 2554 goto out_err; 2555 } 2556 2557 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; 2558 } 2559 2560 /* 2561 * Allocate and initialize pages for Hypervisor-mode percpu regions. 2562 */ 2563 for_each_possible_cpu(cpu) { 2564 struct page *page; 2565 void *page_addr; 2566 2567 page = alloc_pages(GFP_KERNEL, nvhe_percpu_order()); 2568 if (!page) { 2569 err = -ENOMEM; 2570 goto out_err; 2571 } 2572 2573 page_addr = page_address(page); 2574 memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size()); 2575 kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr; 2576 } 2577 2578 /* 2579 * Map the Hyp-code called directly from the host 2580 */ 2581 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), 2582 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); 2583 if (err) { 2584 kvm_err("Cannot map world-switch code\n"); 2585 goto out_err; 2586 } 2587 2588 err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start), 2589 kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO); 2590 if (err) { 2591 kvm_err("Cannot map .hyp.rodata section\n"); 2592 goto out_err; 2593 } 2594 2595 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), 2596 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); 2597 if (err) { 2598 kvm_err("Cannot map rodata section\n"); 2599 goto out_err; 2600 } 2601 2602 /* 2603 * .hyp.bss is guaranteed to be placed at the beginning of the .bss 2604 * section thanks to an assertion in the linker script. Map it RW and 2605 * the rest of .bss RO. 2606 */ 2607 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start), 2608 kvm_ksym_ref(__hyp_bss_end), PAGE_HYP); 2609 if (err) { 2610 kvm_err("Cannot map hyp bss section: %d\n", err); 2611 goto out_err; 2612 } 2613 2614 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end), 2615 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); 2616 if (err) { 2617 kvm_err("Cannot map bss section\n"); 2618 goto out_err; 2619 } 2620 2621 /* 2622 * Map the Hyp stack pages 2623 */ 2624 for_each_possible_cpu(cpu) { 2625 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 2626 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); 2627 2628 err = create_hyp_stack(__pa(stack_page), ¶ms->stack_hyp_va); 2629 if (err) { 2630 kvm_err("Cannot map hyp stack\n"); 2631 goto out_err; 2632 } 2633 2634 /* 2635 * Save the stack PA in nvhe_init_params. This will be needed 2636 * to recreate the stack mapping in protected nVHE mode. 2637 * __hyp_pa() won't do the right thing there, since the stack 2638 * has been mapped in the flexible private VA space. 2639 */ 2640 params->stack_pa = __pa(stack_page); 2641 } 2642 2643 for_each_possible_cpu(cpu) { 2644 char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]; 2645 char *percpu_end = percpu_begin + nvhe_percpu_size(); 2646 2647 /* Map Hyp percpu pages */ 2648 err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP); 2649 if (err) { 2650 kvm_err("Cannot map hyp percpu region\n"); 2651 goto out_err; 2652 } 2653 2654 /* Prepare the CPU initialization parameters */ 2655 cpu_prepare_hyp_mode(cpu, hyp_va_bits); 2656 } 2657 2658 kvm_hyp_init_symbols(); 2659 2660 if (is_protected_kvm_enabled()) { 2661 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) && 2662 cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH)) 2663 pkvm_hyp_init_ptrauth(); 2664 2665 init_cpu_logical_map(); 2666 2667 if (!init_psci_relay()) { 2668 err = -ENODEV; 2669 goto out_err; 2670 } 2671 2672 err = init_pkvm_host_sve_state(); 2673 if (err) 2674 goto out_err; 2675 2676 err = kvm_hyp_init_protection(hyp_va_bits); 2677 if (err) { 2678 kvm_err("Failed to init hyp memory protection\n"); 2679 goto out_err; 2680 } 2681 } 2682 2683 return 0; 2684 2685 out_err: 2686 teardown_hyp_mode(); 2687 kvm_err("error initializing Hyp mode: %d\n", err); 2688 return err; 2689 } 2690 2691 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) 2692 { 2693 struct kvm_vcpu *vcpu = NULL; 2694 struct kvm_mpidr_data *data; 2695 unsigned long i; 2696 2697 mpidr &= MPIDR_HWID_BITMASK; 2698 2699 rcu_read_lock(); 2700 data = rcu_dereference(kvm->arch.mpidr_data); 2701 2702 if (data) { 2703 u16 idx = kvm_mpidr_index(data, mpidr); 2704 2705 vcpu = kvm_get_vcpu(kvm, data->cmpidr_to_idx[idx]); 2706 if (mpidr != kvm_vcpu_get_mpidr_aff(vcpu)) 2707 vcpu = NULL; 2708 } 2709 2710 rcu_read_unlock(); 2711 2712 if (vcpu) 2713 return vcpu; 2714 2715 kvm_for_each_vcpu(i, vcpu, kvm) { 2716 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) 2717 return vcpu; 2718 } 2719 return NULL; 2720 } 2721 2722 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) 2723 { 2724 return irqchip_in_kernel(kvm); 2725 } 2726 2727 bool kvm_arch_has_irq_bypass(void) 2728 { 2729 return true; 2730 } 2731 2732 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, 2733 struct irq_bypass_producer *prod) 2734 { 2735 struct kvm_kernel_irqfd *irqfd = 2736 container_of(cons, struct kvm_kernel_irqfd, consumer); 2737 2738 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, 2739 &irqfd->irq_entry); 2740 } 2741 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, 2742 struct irq_bypass_producer *prod) 2743 { 2744 struct kvm_kernel_irqfd *irqfd = 2745 container_of(cons, struct kvm_kernel_irqfd, consumer); 2746 2747 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq, 2748 &irqfd->irq_entry); 2749 } 2750 2751 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) 2752 { 2753 struct kvm_kernel_irqfd *irqfd = 2754 container_of(cons, struct kvm_kernel_irqfd, consumer); 2755 2756 kvm_arm_halt_guest(irqfd->kvm); 2757 } 2758 2759 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) 2760 { 2761 struct kvm_kernel_irqfd *irqfd = 2762 container_of(cons, struct kvm_kernel_irqfd, consumer); 2763 2764 kvm_arm_resume_guest(irqfd->kvm); 2765 } 2766 2767 /* Initialize Hyp-mode and memory mappings on all CPUs */ 2768 static __init int kvm_arm_init(void) 2769 { 2770 int err; 2771 bool in_hyp_mode; 2772 2773 if (!is_hyp_mode_available()) { 2774 kvm_info("HYP mode not available\n"); 2775 return -ENODEV; 2776 } 2777 2778 if (kvm_get_mode() == KVM_MODE_NONE) { 2779 kvm_info("KVM disabled from command line\n"); 2780 return -ENODEV; 2781 } 2782 2783 err = kvm_sys_reg_table_init(); 2784 if (err) { 2785 kvm_info("Error initializing system register tables"); 2786 return err; 2787 } 2788 2789 in_hyp_mode = is_kernel_in_hyp_mode(); 2790 2791 if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) || 2792 cpus_have_final_cap(ARM64_WORKAROUND_1508412)) 2793 kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \ 2794 "Only trusted guests should be used on this system.\n"); 2795 2796 err = kvm_set_ipa_limit(); 2797 if (err) 2798 return err; 2799 2800 err = kvm_arm_init_sve(); 2801 if (err) 2802 return err; 2803 2804 err = kvm_arm_vmid_alloc_init(); 2805 if (err) { 2806 kvm_err("Failed to initialize VMID allocator.\n"); 2807 return err; 2808 } 2809 2810 if (!in_hyp_mode) { 2811 err = init_hyp_mode(); 2812 if (err) 2813 goto out_err; 2814 } 2815 2816 err = kvm_init_vector_slots(); 2817 if (err) { 2818 kvm_err("Cannot initialise vector slots\n"); 2819 goto out_hyp; 2820 } 2821 2822 err = init_subsystems(); 2823 if (err) 2824 goto out_hyp; 2825 2826 kvm_info("%s%sVHE mode initialized successfully\n", 2827 in_hyp_mode ? "" : (is_protected_kvm_enabled() ? 2828 "Protected " : "Hyp "), 2829 in_hyp_mode ? "" : (cpus_have_final_cap(ARM64_KVM_HVHE) ? 2830 "h" : "n")); 2831 2832 /* 2833 * FIXME: Do something reasonable if kvm_init() fails after pKVM 2834 * hypervisor protection is finalized. 2835 */ 2836 err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE); 2837 if (err) 2838 goto out_subs; 2839 2840 /* 2841 * This should be called after initialization is done and failure isn't 2842 * possible anymore. 2843 */ 2844 if (!in_hyp_mode) 2845 finalize_init_hyp_mode(); 2846 2847 kvm_arm_initialised = true; 2848 2849 return 0; 2850 2851 out_subs: 2852 teardown_subsystems(); 2853 out_hyp: 2854 if (!in_hyp_mode) 2855 teardown_hyp_mode(); 2856 out_err: 2857 kvm_arm_vmid_alloc_free(); 2858 return err; 2859 } 2860 2861 static int __init early_kvm_mode_cfg(char *arg) 2862 { 2863 if (!arg) 2864 return -EINVAL; 2865 2866 if (strcmp(arg, "none") == 0) { 2867 kvm_mode = KVM_MODE_NONE; 2868 return 0; 2869 } 2870 2871 if (!is_hyp_mode_available()) { 2872 pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n"); 2873 return 0; 2874 } 2875 2876 if (strcmp(arg, "protected") == 0) { 2877 if (!is_kernel_in_hyp_mode()) 2878 kvm_mode = KVM_MODE_PROTECTED; 2879 else 2880 pr_warn_once("Protected KVM not available with VHE\n"); 2881 2882 return 0; 2883 } 2884 2885 if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) { 2886 kvm_mode = KVM_MODE_DEFAULT; 2887 return 0; 2888 } 2889 2890 if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) { 2891 kvm_mode = KVM_MODE_NV; 2892 return 0; 2893 } 2894 2895 return -EINVAL; 2896 } 2897 early_param("kvm-arm.mode", early_kvm_mode_cfg); 2898 2899 static int __init early_kvm_wfx_trap_policy_cfg(char *arg, enum kvm_wfx_trap_policy *p) 2900 { 2901 if (!arg) 2902 return -EINVAL; 2903 2904 if (strcmp(arg, "trap") == 0) { 2905 *p = KVM_WFX_TRAP; 2906 return 0; 2907 } 2908 2909 if (strcmp(arg, "notrap") == 0) { 2910 *p = KVM_WFX_NOTRAP; 2911 return 0; 2912 } 2913 2914 return -EINVAL; 2915 } 2916 2917 static int __init early_kvm_wfi_trap_policy_cfg(char *arg) 2918 { 2919 return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfi_trap_policy); 2920 } 2921 early_param("kvm-arm.wfi_trap_policy", early_kvm_wfi_trap_policy_cfg); 2922 2923 static int __init early_kvm_wfe_trap_policy_cfg(char *arg) 2924 { 2925 return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfe_trap_policy); 2926 } 2927 early_param("kvm-arm.wfe_trap_policy", early_kvm_wfe_trap_policy_cfg); 2928 2929 enum kvm_mode kvm_get_mode(void) 2930 { 2931 return kvm_mode; 2932 } 2933 2934 module_init(kvm_arm_init); 2935