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