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