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