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