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