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