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