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_mmu.h> 38 #include <asm/kvm_emulate.h> 39 #include <asm/sections.h> 40 41 #include <kvm/arm_hypercalls.h> 42 #include <kvm/arm_pmu.h> 43 #include <kvm/arm_psci.h> 44 45 #ifdef REQUIRES_VIRT 46 __asm__(".arch_extension virt"); 47 #endif 48 49 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT; 50 DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); 51 52 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); 53 54 static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); 55 unsigned long kvm_arm_hyp_percpu_base[NR_CPUS]; 56 DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); 57 58 /* The VMID used in the VTTBR */ 59 static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1); 60 static u32 kvm_next_vmid; 61 static DEFINE_SPINLOCK(kvm_vmid_lock); 62 63 static bool vgic_present; 64 65 static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled); 66 DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use); 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_arch_hardware_setup(void *opaque) 74 { 75 return 0; 76 } 77 78 int kvm_arch_check_processor_compat(void *opaque) 79 { 80 return 0; 81 } 82 83 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, 84 struct kvm_enable_cap *cap) 85 { 86 int r; 87 88 if (cap->flags) 89 return -EINVAL; 90 91 switch (cap->cap) { 92 case KVM_CAP_ARM_NISV_TO_USER: 93 r = 0; 94 kvm->arch.return_nisv_io_abort_to_user = true; 95 break; 96 case KVM_CAP_ARM_MTE: 97 if (!system_supports_mte() || kvm->created_vcpus) 98 return -EINVAL; 99 r = 0; 100 kvm->arch.mte_enabled = true; 101 break; 102 default: 103 r = -EINVAL; 104 break; 105 } 106 107 return r; 108 } 109 110 static int kvm_arm_default_max_vcpus(void) 111 { 112 return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; 113 } 114 115 static void set_default_spectre(struct kvm *kvm) 116 { 117 /* 118 * The default is to expose CSV2 == 1 if the HW isn't affected. 119 * Although this is a per-CPU feature, we make it global because 120 * asymmetric systems are just a nuisance. 121 * 122 * Userspace can override this as long as it doesn't promise 123 * the impossible. 124 */ 125 if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) 126 kvm->arch.pfr0_csv2 = 1; 127 if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) 128 kvm->arch.pfr0_csv3 = 1; 129 } 130 131 /** 132 * kvm_arch_init_vm - initializes a VM data structure 133 * @kvm: pointer to the KVM struct 134 */ 135 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 136 { 137 int ret; 138 139 ret = kvm_arm_setup_stage2(kvm, type); 140 if (ret) 141 return ret; 142 143 ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu); 144 if (ret) 145 return ret; 146 147 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP); 148 if (ret) 149 goto out_free_stage2_pgd; 150 151 kvm_vgic_early_init(kvm); 152 153 /* The maximum number of VCPUs is limited by the host's GIC model */ 154 kvm->arch.max_vcpus = kvm_arm_default_max_vcpus(); 155 156 set_default_spectre(kvm); 157 158 return ret; 159 out_free_stage2_pgd: 160 kvm_free_stage2_pgd(&kvm->arch.mmu); 161 return ret; 162 } 163 164 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 165 { 166 return VM_FAULT_SIGBUS; 167 } 168 169 170 /** 171 * kvm_arch_destroy_vm - destroy the VM data structure 172 * @kvm: pointer to the KVM struct 173 */ 174 void kvm_arch_destroy_vm(struct kvm *kvm) 175 { 176 int i; 177 178 bitmap_free(kvm->arch.pmu_filter); 179 180 kvm_vgic_destroy(kvm); 181 182 for (i = 0; i < KVM_MAX_VCPUS; ++i) { 183 if (kvm->vcpus[i]) { 184 kvm_vcpu_destroy(kvm->vcpus[i]); 185 kvm->vcpus[i] = NULL; 186 } 187 } 188 atomic_set(&kvm->online_vcpus, 0); 189 } 190 191 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 192 { 193 int r; 194 switch (ext) { 195 case KVM_CAP_IRQCHIP: 196 r = vgic_present; 197 break; 198 case KVM_CAP_IOEVENTFD: 199 case KVM_CAP_DEVICE_CTRL: 200 case KVM_CAP_USER_MEMORY: 201 case KVM_CAP_SYNC_MMU: 202 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: 203 case KVM_CAP_ONE_REG: 204 case KVM_CAP_ARM_PSCI: 205 case KVM_CAP_ARM_PSCI_0_2: 206 case KVM_CAP_READONLY_MEM: 207 case KVM_CAP_MP_STATE: 208 case KVM_CAP_IMMEDIATE_EXIT: 209 case KVM_CAP_VCPU_EVENTS: 210 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2: 211 case KVM_CAP_ARM_NISV_TO_USER: 212 case KVM_CAP_ARM_INJECT_EXT_DABT: 213 case KVM_CAP_SET_GUEST_DEBUG: 214 case KVM_CAP_VCPU_ATTRIBUTES: 215 case KVM_CAP_PTP_KVM: 216 r = 1; 217 break; 218 case KVM_CAP_SET_GUEST_DEBUG2: 219 return KVM_GUESTDBG_VALID_MASK; 220 case KVM_CAP_ARM_SET_DEVICE_ADDR: 221 r = 1; 222 break; 223 case KVM_CAP_NR_VCPUS: 224 r = num_online_cpus(); 225 break; 226 case KVM_CAP_MAX_VCPUS: 227 case KVM_CAP_MAX_VCPU_ID: 228 if (kvm) 229 r = kvm->arch.max_vcpus; 230 else 231 r = kvm_arm_default_max_vcpus(); 232 break; 233 case KVM_CAP_MSI_DEVID: 234 if (!kvm) 235 r = -EINVAL; 236 else 237 r = kvm->arch.vgic.msis_require_devid; 238 break; 239 case KVM_CAP_ARM_USER_IRQ: 240 /* 241 * 1: EL1_VTIMER, EL1_PTIMER, and PMU. 242 * (bump this number if adding more devices) 243 */ 244 r = 1; 245 break; 246 case KVM_CAP_ARM_MTE: 247 r = system_supports_mte(); 248 break; 249 case KVM_CAP_STEAL_TIME: 250 r = kvm_arm_pvtime_supported(); 251 break; 252 case KVM_CAP_ARM_EL1_32BIT: 253 r = cpus_have_const_cap(ARM64_HAS_32BIT_EL1); 254 break; 255 case KVM_CAP_GUEST_DEBUG_HW_BPS: 256 r = get_num_brps(); 257 break; 258 case KVM_CAP_GUEST_DEBUG_HW_WPS: 259 r = get_num_wrps(); 260 break; 261 case KVM_CAP_ARM_PMU_V3: 262 r = kvm_arm_support_pmu_v3(); 263 break; 264 case KVM_CAP_ARM_INJECT_SERROR_ESR: 265 r = cpus_have_const_cap(ARM64_HAS_RAS_EXTN); 266 break; 267 case KVM_CAP_ARM_VM_IPA_SIZE: 268 r = get_kvm_ipa_limit(); 269 break; 270 case KVM_CAP_ARM_SVE: 271 r = system_supports_sve(); 272 break; 273 case KVM_CAP_ARM_PTRAUTH_ADDRESS: 274 case KVM_CAP_ARM_PTRAUTH_GENERIC: 275 r = system_has_full_ptr_auth(); 276 break; 277 default: 278 r = 0; 279 } 280 281 return r; 282 } 283 284 long kvm_arch_dev_ioctl(struct file *filp, 285 unsigned int ioctl, unsigned long arg) 286 { 287 return -EINVAL; 288 } 289 290 struct kvm *kvm_arch_alloc_vm(void) 291 { 292 if (!has_vhe()) 293 return kzalloc(sizeof(struct kvm), GFP_KERNEL); 294 295 return vzalloc(sizeof(struct kvm)); 296 } 297 298 void kvm_arch_free_vm(struct kvm *kvm) 299 { 300 if (!has_vhe()) 301 kfree(kvm); 302 else 303 vfree(kvm); 304 } 305 306 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 307 { 308 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) 309 return -EBUSY; 310 311 if (id >= kvm->arch.max_vcpus) 312 return -EINVAL; 313 314 return 0; 315 } 316 317 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 318 { 319 int err; 320 321 /* Force users to call KVM_ARM_VCPU_INIT */ 322 vcpu->arch.target = -1; 323 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); 324 325 vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO; 326 327 /* Set up the timer */ 328 kvm_timer_vcpu_init(vcpu); 329 330 kvm_pmu_vcpu_init(vcpu); 331 332 kvm_arm_reset_debug_ptr(vcpu); 333 334 kvm_arm_pvtime_vcpu_init(&vcpu->arch); 335 336 vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu; 337 338 err = kvm_vgic_vcpu_init(vcpu); 339 if (err) 340 return err; 341 342 return create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP); 343 } 344 345 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 346 { 347 } 348 349 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 350 { 351 if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm))) 352 static_branch_dec(&userspace_irqchip_in_use); 353 354 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); 355 kvm_timer_vcpu_terminate(vcpu); 356 kvm_pmu_vcpu_destroy(vcpu); 357 358 kvm_arm_vcpu_destroy(vcpu); 359 } 360 361 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) 362 { 363 return kvm_timer_is_pending(vcpu); 364 } 365 366 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) 367 { 368 /* 369 * If we're about to block (most likely because we've just hit a 370 * WFI), we need to sync back the state of the GIC CPU interface 371 * so that we have the latest PMR and group enables. This ensures 372 * that kvm_arch_vcpu_runnable has up-to-date data to decide 373 * whether we have pending interrupts. 374 * 375 * For the same reason, we want to tell GICv4 that we need 376 * doorbells to be signalled, should an interrupt become pending. 377 */ 378 preempt_disable(); 379 kvm_vgic_vmcr_sync(vcpu); 380 vgic_v4_put(vcpu, true); 381 preempt_enable(); 382 } 383 384 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) 385 { 386 preempt_disable(); 387 vgic_v4_load(vcpu); 388 preempt_enable(); 389 } 390 391 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 392 { 393 struct kvm_s2_mmu *mmu; 394 int *last_ran; 395 396 mmu = vcpu->arch.hw_mmu; 397 last_ran = this_cpu_ptr(mmu->last_vcpu_ran); 398 399 /* 400 * We guarantee that both TLBs and I-cache are private to each 401 * vcpu. If detecting that a vcpu from the same VM has 402 * previously run on the same physical CPU, call into the 403 * hypervisor code to nuke the relevant contexts. 404 * 405 * We might get preempted before the vCPU actually runs, but 406 * over-invalidation doesn't affect correctness. 407 */ 408 if (*last_ran != vcpu->vcpu_id) { 409 kvm_call_hyp(__kvm_flush_cpu_context, mmu); 410 *last_ran = vcpu->vcpu_id; 411 } 412 413 vcpu->cpu = cpu; 414 415 kvm_vgic_load(vcpu); 416 kvm_timer_vcpu_load(vcpu); 417 if (has_vhe()) 418 kvm_vcpu_load_sysregs_vhe(vcpu); 419 kvm_arch_vcpu_load_fp(vcpu); 420 kvm_vcpu_pmu_restore_guest(vcpu); 421 if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) 422 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); 423 424 if (single_task_running()) 425 vcpu_clear_wfx_traps(vcpu); 426 else 427 vcpu_set_wfx_traps(vcpu); 428 429 if (vcpu_has_ptrauth(vcpu)) 430 vcpu_ptrauth_disable(vcpu); 431 kvm_arch_vcpu_load_debug_state_flags(vcpu); 432 } 433 434 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 435 { 436 kvm_arch_vcpu_put_debug_state_flags(vcpu); 437 kvm_arch_vcpu_put_fp(vcpu); 438 if (has_vhe()) 439 kvm_vcpu_put_sysregs_vhe(vcpu); 440 kvm_timer_vcpu_put(vcpu); 441 kvm_vgic_put(vcpu); 442 kvm_vcpu_pmu_restore_host(vcpu); 443 444 vcpu->cpu = -1; 445 } 446 447 static void vcpu_power_off(struct kvm_vcpu *vcpu) 448 { 449 vcpu->arch.power_off = true; 450 kvm_make_request(KVM_REQ_SLEEP, vcpu); 451 kvm_vcpu_kick(vcpu); 452 } 453 454 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 455 struct kvm_mp_state *mp_state) 456 { 457 if (vcpu->arch.power_off) 458 mp_state->mp_state = KVM_MP_STATE_STOPPED; 459 else 460 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; 461 462 return 0; 463 } 464 465 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 466 struct kvm_mp_state *mp_state) 467 { 468 int ret = 0; 469 470 switch (mp_state->mp_state) { 471 case KVM_MP_STATE_RUNNABLE: 472 vcpu->arch.power_off = false; 473 break; 474 case KVM_MP_STATE_STOPPED: 475 vcpu_power_off(vcpu); 476 break; 477 default: 478 ret = -EINVAL; 479 } 480 481 return ret; 482 } 483 484 /** 485 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled 486 * @v: The VCPU pointer 487 * 488 * If the guest CPU is not waiting for interrupts or an interrupt line is 489 * asserted, the CPU is by definition runnable. 490 */ 491 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) 492 { 493 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF); 494 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v)) 495 && !v->arch.power_off && !v->arch.pause); 496 } 497 498 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 499 { 500 return vcpu_mode_priv(vcpu); 501 } 502 503 /* Just ensure a guest exit from a particular CPU */ 504 static void exit_vm_noop(void *info) 505 { 506 } 507 508 void force_vm_exit(const cpumask_t *mask) 509 { 510 preempt_disable(); 511 smp_call_function_many(mask, exit_vm_noop, NULL, true); 512 preempt_enable(); 513 } 514 515 /** 516 * need_new_vmid_gen - check that the VMID is still valid 517 * @vmid: The VMID to check 518 * 519 * return true if there is a new generation of VMIDs being used 520 * 521 * The hardware supports a limited set of values with the value zero reserved 522 * for the host, so we check if an assigned value belongs to a previous 523 * generation, which requires us to assign a new value. If we're the first to 524 * use a VMID for the new generation, we must flush necessary caches and TLBs 525 * on all CPUs. 526 */ 527 static bool need_new_vmid_gen(struct kvm_vmid *vmid) 528 { 529 u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen); 530 smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */ 531 return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen); 532 } 533 534 /** 535 * update_vmid - Update the vmid with a valid VMID for the current generation 536 * @vmid: The stage-2 VMID information struct 537 */ 538 static void update_vmid(struct kvm_vmid *vmid) 539 { 540 if (!need_new_vmid_gen(vmid)) 541 return; 542 543 spin_lock(&kvm_vmid_lock); 544 545 /* 546 * We need to re-check the vmid_gen here to ensure that if another vcpu 547 * already allocated a valid vmid for this vm, then this vcpu should 548 * use the same vmid. 549 */ 550 if (!need_new_vmid_gen(vmid)) { 551 spin_unlock(&kvm_vmid_lock); 552 return; 553 } 554 555 /* First user of a new VMID generation? */ 556 if (unlikely(kvm_next_vmid == 0)) { 557 atomic64_inc(&kvm_vmid_gen); 558 kvm_next_vmid = 1; 559 560 /* 561 * On SMP we know no other CPUs can use this CPU's or each 562 * other's VMID after force_vm_exit returns since the 563 * kvm_vmid_lock blocks them from reentry to the guest. 564 */ 565 force_vm_exit(cpu_all_mask); 566 /* 567 * Now broadcast TLB + ICACHE invalidation over the inner 568 * shareable domain to make sure all data structures are 569 * clean. 570 */ 571 kvm_call_hyp(__kvm_flush_vm_context); 572 } 573 574 vmid->vmid = kvm_next_vmid; 575 kvm_next_vmid++; 576 kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1; 577 578 smp_wmb(); 579 WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen)); 580 581 spin_unlock(&kvm_vmid_lock); 582 } 583 584 static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) 585 { 586 struct kvm *kvm = vcpu->kvm; 587 int ret = 0; 588 589 if (likely(vcpu->arch.has_run_once)) 590 return 0; 591 592 if (!kvm_arm_vcpu_is_finalized(vcpu)) 593 return -EPERM; 594 595 vcpu->arch.has_run_once = true; 596 597 kvm_arm_vcpu_init_debug(vcpu); 598 599 if (likely(irqchip_in_kernel(kvm))) { 600 /* 601 * Map the VGIC hardware resources before running a vcpu the 602 * first time on this VM. 603 */ 604 ret = kvm_vgic_map_resources(kvm); 605 if (ret) 606 return ret; 607 } else { 608 /* 609 * Tell the rest of the code that there are userspace irqchip 610 * VMs in the wild. 611 */ 612 static_branch_inc(&userspace_irqchip_in_use); 613 } 614 615 ret = kvm_timer_enable(vcpu); 616 if (ret) 617 return ret; 618 619 ret = kvm_arm_pmu_v3_enable(vcpu); 620 621 return ret; 622 } 623 624 bool kvm_arch_intc_initialized(struct kvm *kvm) 625 { 626 return vgic_initialized(kvm); 627 } 628 629 void kvm_arm_halt_guest(struct kvm *kvm) 630 { 631 int i; 632 struct kvm_vcpu *vcpu; 633 634 kvm_for_each_vcpu(i, vcpu, kvm) 635 vcpu->arch.pause = true; 636 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP); 637 } 638 639 void kvm_arm_resume_guest(struct kvm *kvm) 640 { 641 int i; 642 struct kvm_vcpu *vcpu; 643 644 kvm_for_each_vcpu(i, vcpu, kvm) { 645 vcpu->arch.pause = false; 646 rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu)); 647 } 648 } 649 650 static void vcpu_req_sleep(struct kvm_vcpu *vcpu) 651 { 652 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); 653 654 rcuwait_wait_event(wait, 655 (!vcpu->arch.power_off) &&(!vcpu->arch.pause), 656 TASK_INTERRUPTIBLE); 657 658 if (vcpu->arch.power_off || vcpu->arch.pause) { 659 /* Awaken to handle a signal, request we sleep again later. */ 660 kvm_make_request(KVM_REQ_SLEEP, vcpu); 661 } 662 663 /* 664 * Make sure we will observe a potential reset request if we've 665 * observed a change to the power state. Pairs with the smp_wmb() in 666 * kvm_psci_vcpu_on(). 667 */ 668 smp_rmb(); 669 } 670 671 static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) 672 { 673 return vcpu->arch.target >= 0; 674 } 675 676 static void check_vcpu_requests(struct kvm_vcpu *vcpu) 677 { 678 if (kvm_request_pending(vcpu)) { 679 if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) 680 vcpu_req_sleep(vcpu); 681 682 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) 683 kvm_reset_vcpu(vcpu); 684 685 /* 686 * Clear IRQ_PENDING requests that were made to guarantee 687 * that a VCPU sees new virtual interrupts. 688 */ 689 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu); 690 691 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu)) 692 kvm_update_stolen_time(vcpu); 693 694 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) { 695 /* The distributor enable bits were changed */ 696 preempt_disable(); 697 vgic_v4_put(vcpu, false); 698 vgic_v4_load(vcpu); 699 preempt_enable(); 700 } 701 702 if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu)) 703 kvm_pmu_handle_pmcr(vcpu, 704 __vcpu_sys_reg(vcpu, PMCR_EL0)); 705 } 706 } 707 708 static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu) 709 { 710 if (likely(!vcpu_mode_is_32bit(vcpu))) 711 return false; 712 713 return !system_supports_32bit_el0() || 714 static_branch_unlikely(&arm64_mismatched_32bit_el0); 715 } 716 717 /** 718 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code 719 * @vcpu: The VCPU pointer 720 * 721 * This function is called through the VCPU_RUN ioctl called from user space. It 722 * will execute VM code in a loop until the time slice for the process is used 723 * or some emulation is needed from user space in which case the function will 724 * return with return value 0 and with the kvm_run structure filled in with the 725 * required data for the requested emulation. 726 */ 727 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 728 { 729 struct kvm_run *run = vcpu->run; 730 int ret; 731 732 if (unlikely(!kvm_vcpu_initialized(vcpu))) 733 return -ENOEXEC; 734 735 ret = kvm_vcpu_first_run_init(vcpu); 736 if (ret) 737 return ret; 738 739 if (run->exit_reason == KVM_EXIT_MMIO) { 740 ret = kvm_handle_mmio_return(vcpu); 741 if (ret) 742 return ret; 743 } 744 745 vcpu_load(vcpu); 746 747 if (run->immediate_exit) { 748 ret = -EINTR; 749 goto out; 750 } 751 752 kvm_sigset_activate(vcpu); 753 754 ret = 1; 755 run->exit_reason = KVM_EXIT_UNKNOWN; 756 while (ret > 0) { 757 /* 758 * Check conditions before entering the guest 759 */ 760 cond_resched(); 761 762 update_vmid(&vcpu->arch.hw_mmu->vmid); 763 764 check_vcpu_requests(vcpu); 765 766 /* 767 * Preparing the interrupts to be injected also 768 * involves poking the GIC, which must be done in a 769 * non-preemptible context. 770 */ 771 preempt_disable(); 772 773 kvm_pmu_flush_hwstate(vcpu); 774 775 local_irq_disable(); 776 777 kvm_vgic_flush_hwstate(vcpu); 778 779 /* 780 * Exit if we have a signal pending so that we can deliver the 781 * signal to user space. 782 */ 783 if (signal_pending(current)) { 784 ret = -EINTR; 785 run->exit_reason = KVM_EXIT_INTR; 786 } 787 788 /* 789 * If we're using a userspace irqchip, then check if we need 790 * to tell a userspace irqchip about timer or PMU level 791 * changes and if so, exit to userspace (the actual level 792 * state gets updated in kvm_timer_update_run and 793 * kvm_pmu_update_run below). 794 */ 795 if (static_branch_unlikely(&userspace_irqchip_in_use)) { 796 if (kvm_timer_should_notify_user(vcpu) || 797 kvm_pmu_should_notify_user(vcpu)) { 798 ret = -EINTR; 799 run->exit_reason = KVM_EXIT_INTR; 800 } 801 } 802 803 /* 804 * Ensure we set mode to IN_GUEST_MODE after we disable 805 * interrupts and before the final VCPU requests check. 806 * See the comment in kvm_vcpu_exiting_guest_mode() and 807 * Documentation/virt/kvm/vcpu-requests.rst 808 */ 809 smp_store_mb(vcpu->mode, IN_GUEST_MODE); 810 811 if (ret <= 0 || need_new_vmid_gen(&vcpu->arch.hw_mmu->vmid) || 812 kvm_request_pending(vcpu)) { 813 vcpu->mode = OUTSIDE_GUEST_MODE; 814 isb(); /* Ensure work in x_flush_hwstate is committed */ 815 kvm_pmu_sync_hwstate(vcpu); 816 if (static_branch_unlikely(&userspace_irqchip_in_use)) 817 kvm_timer_sync_user(vcpu); 818 kvm_vgic_sync_hwstate(vcpu); 819 local_irq_enable(); 820 preempt_enable(); 821 continue; 822 } 823 824 kvm_arm_setup_debug(vcpu); 825 826 /************************************************************** 827 * Enter the guest 828 */ 829 trace_kvm_entry(*vcpu_pc(vcpu)); 830 guest_enter_irqoff(); 831 832 ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu); 833 834 vcpu->mode = OUTSIDE_GUEST_MODE; 835 vcpu->stat.exits++; 836 /* 837 * Back from guest 838 *************************************************************/ 839 840 kvm_arm_clear_debug(vcpu); 841 842 /* 843 * We must sync the PMU state before the vgic state so 844 * that the vgic can properly sample the updated state of the 845 * interrupt line. 846 */ 847 kvm_pmu_sync_hwstate(vcpu); 848 849 /* 850 * Sync the vgic state before syncing the timer state because 851 * the timer code needs to know if the virtual timer 852 * interrupts are active. 853 */ 854 kvm_vgic_sync_hwstate(vcpu); 855 856 /* 857 * Sync the timer hardware state before enabling interrupts as 858 * we don't want vtimer interrupts to race with syncing the 859 * timer virtual interrupt state. 860 */ 861 if (static_branch_unlikely(&userspace_irqchip_in_use)) 862 kvm_timer_sync_user(vcpu); 863 864 kvm_arch_vcpu_ctxsync_fp(vcpu); 865 866 /* 867 * We may have taken a host interrupt in HYP mode (ie 868 * while executing the guest). This interrupt is still 869 * pending, as we haven't serviced it yet! 870 * 871 * We're now back in SVC mode, with interrupts 872 * disabled. Enabling the interrupts now will have 873 * the effect of taking the interrupt again, in SVC 874 * mode this time. 875 */ 876 local_irq_enable(); 877 878 /* 879 * We do local_irq_enable() before calling guest_exit() so 880 * that if a timer interrupt hits while running the guest we 881 * account that tick as being spent in the guest. We enable 882 * preemption after calling guest_exit() so that if we get 883 * preempted we make sure ticks after that is not counted as 884 * guest time. 885 */ 886 guest_exit(); 887 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu)); 888 889 /* Exit types that need handling before we can be preempted */ 890 handle_exit_early(vcpu, ret); 891 892 preempt_enable(); 893 894 /* 895 * The ARMv8 architecture doesn't give the hypervisor 896 * a mechanism to prevent a guest from dropping to AArch32 EL0 897 * if implemented by the CPU. If we spot the guest in such 898 * state and that we decided it wasn't supposed to do so (like 899 * with the asymmetric AArch32 case), return to userspace with 900 * a fatal error. 901 */ 902 if (vcpu_mode_is_bad_32bit(vcpu)) { 903 /* 904 * As we have caught the guest red-handed, decide that 905 * it isn't fit for purpose anymore by making the vcpu 906 * invalid. The VMM can try and fix it by issuing a 907 * KVM_ARM_VCPU_INIT if it really wants to. 908 */ 909 vcpu->arch.target = -1; 910 ret = ARM_EXCEPTION_IL; 911 } 912 913 ret = handle_exit(vcpu, ret); 914 } 915 916 /* Tell userspace about in-kernel device output levels */ 917 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { 918 kvm_timer_update_run(vcpu); 919 kvm_pmu_update_run(vcpu); 920 } 921 922 kvm_sigset_deactivate(vcpu); 923 924 out: 925 /* 926 * In the unlikely event that we are returning to userspace 927 * with pending exceptions or PC adjustment, commit these 928 * adjustments in order to give userspace a consistent view of 929 * the vcpu state. Note that this relies on __kvm_adjust_pc() 930 * being preempt-safe on VHE. 931 */ 932 if (unlikely(vcpu->arch.flags & (KVM_ARM64_PENDING_EXCEPTION | 933 KVM_ARM64_INCREMENT_PC))) 934 kvm_call_hyp(__kvm_adjust_pc, vcpu); 935 936 vcpu_put(vcpu); 937 return ret; 938 } 939 940 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) 941 { 942 int bit_index; 943 bool set; 944 unsigned long *hcr; 945 946 if (number == KVM_ARM_IRQ_CPU_IRQ) 947 bit_index = __ffs(HCR_VI); 948 else /* KVM_ARM_IRQ_CPU_FIQ */ 949 bit_index = __ffs(HCR_VF); 950 951 hcr = vcpu_hcr(vcpu); 952 if (level) 953 set = test_and_set_bit(bit_index, hcr); 954 else 955 set = test_and_clear_bit(bit_index, hcr); 956 957 /* 958 * If we didn't change anything, no need to wake up or kick other CPUs 959 */ 960 if (set == level) 961 return 0; 962 963 /* 964 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and 965 * trigger a world-switch round on the running physical CPU to set the 966 * virtual IRQ/FIQ fields in the HCR appropriately. 967 */ 968 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); 969 kvm_vcpu_kick(vcpu); 970 971 return 0; 972 } 973 974 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, 975 bool line_status) 976 { 977 u32 irq = irq_level->irq; 978 unsigned int irq_type, vcpu_idx, irq_num; 979 int nrcpus = atomic_read(&kvm->online_vcpus); 980 struct kvm_vcpu *vcpu = NULL; 981 bool level = irq_level->level; 982 983 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; 984 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; 985 vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1); 986 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; 987 988 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level); 989 990 switch (irq_type) { 991 case KVM_ARM_IRQ_TYPE_CPU: 992 if (irqchip_in_kernel(kvm)) 993 return -ENXIO; 994 995 if (vcpu_idx >= nrcpus) 996 return -EINVAL; 997 998 vcpu = kvm_get_vcpu(kvm, vcpu_idx); 999 if (!vcpu) 1000 return -EINVAL; 1001 1002 if (irq_num > KVM_ARM_IRQ_CPU_FIQ) 1003 return -EINVAL; 1004 1005 return vcpu_interrupt_line(vcpu, irq_num, level); 1006 case KVM_ARM_IRQ_TYPE_PPI: 1007 if (!irqchip_in_kernel(kvm)) 1008 return -ENXIO; 1009 1010 if (vcpu_idx >= nrcpus) 1011 return -EINVAL; 1012 1013 vcpu = kvm_get_vcpu(kvm, vcpu_idx); 1014 if (!vcpu) 1015 return -EINVAL; 1016 1017 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS) 1018 return -EINVAL; 1019 1020 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL); 1021 case KVM_ARM_IRQ_TYPE_SPI: 1022 if (!irqchip_in_kernel(kvm)) 1023 return -ENXIO; 1024 1025 if (irq_num < VGIC_NR_PRIVATE_IRQS) 1026 return -EINVAL; 1027 1028 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL); 1029 } 1030 1031 return -EINVAL; 1032 } 1033 1034 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1035 const struct kvm_vcpu_init *init) 1036 { 1037 unsigned int i, ret; 1038 int phys_target = kvm_target_cpu(); 1039 1040 if (init->target != phys_target) 1041 return -EINVAL; 1042 1043 /* 1044 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must 1045 * use the same target. 1046 */ 1047 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target) 1048 return -EINVAL; 1049 1050 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */ 1051 for (i = 0; i < sizeof(init->features) * 8; i++) { 1052 bool set = (init->features[i / 32] & (1 << (i % 32))); 1053 1054 if (set && i >= KVM_VCPU_MAX_FEATURES) 1055 return -ENOENT; 1056 1057 /* 1058 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must 1059 * use the same feature set. 1060 */ 1061 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES && 1062 test_bit(i, vcpu->arch.features) != set) 1063 return -EINVAL; 1064 1065 if (set) 1066 set_bit(i, vcpu->arch.features); 1067 } 1068 1069 vcpu->arch.target = phys_target; 1070 1071 /* Now we know what it is, we can reset it. */ 1072 ret = kvm_reset_vcpu(vcpu); 1073 if (ret) { 1074 vcpu->arch.target = -1; 1075 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); 1076 } 1077 1078 return ret; 1079 } 1080 1081 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, 1082 struct kvm_vcpu_init *init) 1083 { 1084 int ret; 1085 1086 ret = kvm_vcpu_set_target(vcpu, init); 1087 if (ret) 1088 return ret; 1089 1090 /* 1091 * Ensure a rebooted VM will fault in RAM pages and detect if the 1092 * guest MMU is turned off and flush the caches as needed. 1093 * 1094 * S2FWB enforces all memory accesses to RAM being cacheable, 1095 * ensuring that the data side is always coherent. We still 1096 * need to invalidate the I-cache though, as FWB does *not* 1097 * imply CTR_EL0.DIC. 1098 */ 1099 if (vcpu->arch.has_run_once) { 1100 if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) 1101 stage2_unmap_vm(vcpu->kvm); 1102 else 1103 icache_inval_all_pou(); 1104 } 1105 1106 vcpu_reset_hcr(vcpu); 1107 1108 /* 1109 * Handle the "start in power-off" case. 1110 */ 1111 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) 1112 vcpu_power_off(vcpu); 1113 else 1114 vcpu->arch.power_off = false; 1115 1116 return 0; 1117 } 1118 1119 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, 1120 struct kvm_device_attr *attr) 1121 { 1122 int ret = -ENXIO; 1123 1124 switch (attr->group) { 1125 default: 1126 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr); 1127 break; 1128 } 1129 1130 return ret; 1131 } 1132 1133 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, 1134 struct kvm_device_attr *attr) 1135 { 1136 int ret = -ENXIO; 1137 1138 switch (attr->group) { 1139 default: 1140 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr); 1141 break; 1142 } 1143 1144 return ret; 1145 } 1146 1147 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, 1148 struct kvm_device_attr *attr) 1149 { 1150 int ret = -ENXIO; 1151 1152 switch (attr->group) { 1153 default: 1154 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr); 1155 break; 1156 } 1157 1158 return ret; 1159 } 1160 1161 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, 1162 struct kvm_vcpu_events *events) 1163 { 1164 memset(events, 0, sizeof(*events)); 1165 1166 return __kvm_arm_vcpu_get_events(vcpu, events); 1167 } 1168 1169 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, 1170 struct kvm_vcpu_events *events) 1171 { 1172 int i; 1173 1174 /* check whether the reserved field is zero */ 1175 for (i = 0; i < ARRAY_SIZE(events->reserved); i++) 1176 if (events->reserved[i]) 1177 return -EINVAL; 1178 1179 /* check whether the pad field is zero */ 1180 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++) 1181 if (events->exception.pad[i]) 1182 return -EINVAL; 1183 1184 return __kvm_arm_vcpu_set_events(vcpu, events); 1185 } 1186 1187 long kvm_arch_vcpu_ioctl(struct file *filp, 1188 unsigned int ioctl, unsigned long arg) 1189 { 1190 struct kvm_vcpu *vcpu = filp->private_data; 1191 void __user *argp = (void __user *)arg; 1192 struct kvm_device_attr attr; 1193 long r; 1194 1195 switch (ioctl) { 1196 case KVM_ARM_VCPU_INIT: { 1197 struct kvm_vcpu_init init; 1198 1199 r = -EFAULT; 1200 if (copy_from_user(&init, argp, sizeof(init))) 1201 break; 1202 1203 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); 1204 break; 1205 } 1206 case KVM_SET_ONE_REG: 1207 case KVM_GET_ONE_REG: { 1208 struct kvm_one_reg reg; 1209 1210 r = -ENOEXEC; 1211 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1212 break; 1213 1214 r = -EFAULT; 1215 if (copy_from_user(®, argp, sizeof(reg))) 1216 break; 1217 1218 if (ioctl == KVM_SET_ONE_REG) 1219 r = kvm_arm_set_reg(vcpu, ®); 1220 else 1221 r = kvm_arm_get_reg(vcpu, ®); 1222 break; 1223 } 1224 case KVM_GET_REG_LIST: { 1225 struct kvm_reg_list __user *user_list = argp; 1226 struct kvm_reg_list reg_list; 1227 unsigned n; 1228 1229 r = -ENOEXEC; 1230 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1231 break; 1232 1233 r = -EPERM; 1234 if (!kvm_arm_vcpu_is_finalized(vcpu)) 1235 break; 1236 1237 r = -EFAULT; 1238 if (copy_from_user(®_list, user_list, sizeof(reg_list))) 1239 break; 1240 n = reg_list.n; 1241 reg_list.n = kvm_arm_num_regs(vcpu); 1242 if (copy_to_user(user_list, ®_list, sizeof(reg_list))) 1243 break; 1244 r = -E2BIG; 1245 if (n < reg_list.n) 1246 break; 1247 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg); 1248 break; 1249 } 1250 case KVM_SET_DEVICE_ATTR: { 1251 r = -EFAULT; 1252 if (copy_from_user(&attr, argp, sizeof(attr))) 1253 break; 1254 r = kvm_arm_vcpu_set_attr(vcpu, &attr); 1255 break; 1256 } 1257 case KVM_GET_DEVICE_ATTR: { 1258 r = -EFAULT; 1259 if (copy_from_user(&attr, argp, sizeof(attr))) 1260 break; 1261 r = kvm_arm_vcpu_get_attr(vcpu, &attr); 1262 break; 1263 } 1264 case KVM_HAS_DEVICE_ATTR: { 1265 r = -EFAULT; 1266 if (copy_from_user(&attr, argp, sizeof(attr))) 1267 break; 1268 r = kvm_arm_vcpu_has_attr(vcpu, &attr); 1269 break; 1270 } 1271 case KVM_GET_VCPU_EVENTS: { 1272 struct kvm_vcpu_events events; 1273 1274 if (kvm_arm_vcpu_get_events(vcpu, &events)) 1275 return -EINVAL; 1276 1277 if (copy_to_user(argp, &events, sizeof(events))) 1278 return -EFAULT; 1279 1280 return 0; 1281 } 1282 case KVM_SET_VCPU_EVENTS: { 1283 struct kvm_vcpu_events events; 1284 1285 if (copy_from_user(&events, argp, sizeof(events))) 1286 return -EFAULT; 1287 1288 return kvm_arm_vcpu_set_events(vcpu, &events); 1289 } 1290 case KVM_ARM_VCPU_FINALIZE: { 1291 int what; 1292 1293 if (!kvm_vcpu_initialized(vcpu)) 1294 return -ENOEXEC; 1295 1296 if (get_user(what, (const int __user *)argp)) 1297 return -EFAULT; 1298 1299 return kvm_arm_vcpu_finalize(vcpu, what); 1300 } 1301 default: 1302 r = -EINVAL; 1303 } 1304 1305 return r; 1306 } 1307 1308 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 1309 { 1310 1311 } 1312 1313 void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm, 1314 const struct kvm_memory_slot *memslot) 1315 { 1316 kvm_flush_remote_tlbs(kvm); 1317 } 1318 1319 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, 1320 struct kvm_arm_device_addr *dev_addr) 1321 { 1322 unsigned long dev_id, type; 1323 1324 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >> 1325 KVM_ARM_DEVICE_ID_SHIFT; 1326 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >> 1327 KVM_ARM_DEVICE_TYPE_SHIFT; 1328 1329 switch (dev_id) { 1330 case KVM_ARM_DEVICE_VGIC_V2: 1331 if (!vgic_present) 1332 return -ENXIO; 1333 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true); 1334 default: 1335 return -ENODEV; 1336 } 1337 } 1338 1339 long kvm_arch_vm_ioctl(struct file *filp, 1340 unsigned int ioctl, unsigned long arg) 1341 { 1342 struct kvm *kvm = filp->private_data; 1343 void __user *argp = (void __user *)arg; 1344 1345 switch (ioctl) { 1346 case KVM_CREATE_IRQCHIP: { 1347 int ret; 1348 if (!vgic_present) 1349 return -ENXIO; 1350 mutex_lock(&kvm->lock); 1351 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); 1352 mutex_unlock(&kvm->lock); 1353 return ret; 1354 } 1355 case KVM_ARM_SET_DEVICE_ADDR: { 1356 struct kvm_arm_device_addr dev_addr; 1357 1358 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) 1359 return -EFAULT; 1360 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); 1361 } 1362 case KVM_ARM_PREFERRED_TARGET: { 1363 int err; 1364 struct kvm_vcpu_init init; 1365 1366 err = kvm_vcpu_preferred_target(&init); 1367 if (err) 1368 return err; 1369 1370 if (copy_to_user(argp, &init, sizeof(init))) 1371 return -EFAULT; 1372 1373 return 0; 1374 } 1375 case KVM_ARM_MTE_COPY_TAGS: { 1376 struct kvm_arm_copy_mte_tags copy_tags; 1377 1378 if (copy_from_user(©_tags, argp, sizeof(copy_tags))) 1379 return -EFAULT; 1380 return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags); 1381 } 1382 default: 1383 return -EINVAL; 1384 } 1385 } 1386 1387 static unsigned long nvhe_percpu_size(void) 1388 { 1389 return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) - 1390 (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start); 1391 } 1392 1393 static unsigned long nvhe_percpu_order(void) 1394 { 1395 unsigned long size = nvhe_percpu_size(); 1396 1397 return size ? get_order(size) : 0; 1398 } 1399 1400 /* A lookup table holding the hypervisor VA for each vector slot */ 1401 static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS]; 1402 1403 static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot) 1404 { 1405 hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot); 1406 } 1407 1408 static int kvm_init_vector_slots(void) 1409 { 1410 int err; 1411 void *base; 1412 1413 base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 1414 kvm_init_vector_slot(base, HYP_VECTOR_DIRECT); 1415 1416 base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs)); 1417 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT); 1418 1419 if (!cpus_have_const_cap(ARM64_SPECTRE_V3A)) 1420 return 0; 1421 1422 if (!has_vhe()) { 1423 err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs), 1424 __BP_HARDEN_HYP_VECS_SZ, &base); 1425 if (err) 1426 return err; 1427 } 1428 1429 kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT); 1430 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT); 1431 return 0; 1432 } 1433 1434 static void cpu_prepare_hyp_mode(int cpu) 1435 { 1436 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 1437 unsigned long tcr; 1438 1439 /* 1440 * Calculate the raw per-cpu offset without a translation from the 1441 * kernel's mapping to the linear mapping, and store it in tpidr_el2 1442 * so that we can use adr_l to access per-cpu variables in EL2. 1443 * Also drop the KASAN tag which gets in the way... 1444 */ 1445 params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) - 1446 (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start)); 1447 1448 params->mair_el2 = read_sysreg(mair_el1); 1449 1450 /* 1451 * The ID map may be configured to use an extended virtual address 1452 * range. This is only the case if system RAM is out of range for the 1453 * currently configured page size and VA_BITS, in which case we will 1454 * also need the extended virtual range for the HYP ID map, or we won't 1455 * be able to enable the EL2 MMU. 1456 * 1457 * However, at EL2, there is only one TTBR register, and we can't switch 1458 * between translation tables *and* update TCR_EL2.T0SZ at the same 1459 * time. Bottom line: we need to use the extended range with *both* our 1460 * translation tables. 1461 * 1462 * So use the same T0SZ value we use for the ID map. 1463 */ 1464 tcr = (read_sysreg(tcr_el1) & TCR_EL2_MASK) | TCR_EL2_RES1; 1465 tcr &= ~TCR_T0SZ_MASK; 1466 tcr |= (idmap_t0sz & GENMASK(TCR_TxSZ_WIDTH - 1, 0)) << TCR_T0SZ_OFFSET; 1467 params->tcr_el2 = tcr; 1468 1469 params->stack_hyp_va = kern_hyp_va(per_cpu(kvm_arm_hyp_stack_page, cpu) + PAGE_SIZE); 1470 params->pgd_pa = kvm_mmu_get_httbr(); 1471 if (is_protected_kvm_enabled()) 1472 params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; 1473 else 1474 params->hcr_el2 = HCR_HOST_NVHE_FLAGS; 1475 params->vttbr = params->vtcr = 0; 1476 1477 /* 1478 * Flush the init params from the data cache because the struct will 1479 * be read while the MMU is off. 1480 */ 1481 kvm_flush_dcache_to_poc(params, sizeof(*params)); 1482 } 1483 1484 static void hyp_install_host_vector(void) 1485 { 1486 struct kvm_nvhe_init_params *params; 1487 struct arm_smccc_res res; 1488 1489 /* Switch from the HYP stub to our own HYP init vector */ 1490 __hyp_set_vectors(kvm_get_idmap_vector()); 1491 1492 /* 1493 * Call initialization code, and switch to the full blown HYP code. 1494 * If the cpucaps haven't been finalized yet, something has gone very 1495 * wrong, and hyp will crash and burn when it uses any 1496 * cpus_have_const_cap() wrapper. 1497 */ 1498 BUG_ON(!system_capabilities_finalized()); 1499 params = this_cpu_ptr_nvhe_sym(kvm_init_params); 1500 arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res); 1501 WARN_ON(res.a0 != SMCCC_RET_SUCCESS); 1502 } 1503 1504 static void cpu_init_hyp_mode(void) 1505 { 1506 hyp_install_host_vector(); 1507 1508 /* 1509 * Disabling SSBD on a non-VHE system requires us to enable SSBS 1510 * at EL2. 1511 */ 1512 if (this_cpu_has_cap(ARM64_SSBS) && 1513 arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) { 1514 kvm_call_hyp_nvhe(__kvm_enable_ssbs); 1515 } 1516 } 1517 1518 static void cpu_hyp_reset(void) 1519 { 1520 if (!is_kernel_in_hyp_mode()) 1521 __hyp_reset_vectors(); 1522 } 1523 1524 /* 1525 * EL2 vectors can be mapped and rerouted in a number of ways, 1526 * depending on the kernel configuration and CPU present: 1527 * 1528 * - If the CPU is affected by Spectre-v2, the hardening sequence is 1529 * placed in one of the vector slots, which is executed before jumping 1530 * to the real vectors. 1531 * 1532 * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot 1533 * containing the hardening sequence is mapped next to the idmap page, 1534 * and executed before jumping to the real vectors. 1535 * 1536 * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an 1537 * empty slot is selected, mapped next to the idmap page, and 1538 * executed before jumping to the real vectors. 1539 * 1540 * Note that ARM64_SPECTRE_V3A is somewhat incompatible with 1541 * VHE, as we don't have hypervisor-specific mappings. If the system 1542 * is VHE and yet selects this capability, it will be ignored. 1543 */ 1544 static void cpu_set_hyp_vector(void) 1545 { 1546 struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data); 1547 void *vector = hyp_spectre_vector_selector[data->slot]; 1548 1549 if (!is_protected_kvm_enabled()) 1550 *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector; 1551 else 1552 kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot); 1553 } 1554 1555 static void cpu_hyp_reinit(void) 1556 { 1557 kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt); 1558 1559 cpu_hyp_reset(); 1560 1561 if (is_kernel_in_hyp_mode()) 1562 kvm_timer_init_vhe(); 1563 else 1564 cpu_init_hyp_mode(); 1565 1566 cpu_set_hyp_vector(); 1567 1568 kvm_arm_init_debug(); 1569 1570 if (vgic_present) 1571 kvm_vgic_init_cpu_hardware(); 1572 } 1573 1574 static void _kvm_arch_hardware_enable(void *discard) 1575 { 1576 if (!__this_cpu_read(kvm_arm_hardware_enabled)) { 1577 cpu_hyp_reinit(); 1578 __this_cpu_write(kvm_arm_hardware_enabled, 1); 1579 } 1580 } 1581 1582 int kvm_arch_hardware_enable(void) 1583 { 1584 _kvm_arch_hardware_enable(NULL); 1585 return 0; 1586 } 1587 1588 static void _kvm_arch_hardware_disable(void *discard) 1589 { 1590 if (__this_cpu_read(kvm_arm_hardware_enabled)) { 1591 cpu_hyp_reset(); 1592 __this_cpu_write(kvm_arm_hardware_enabled, 0); 1593 } 1594 } 1595 1596 void kvm_arch_hardware_disable(void) 1597 { 1598 if (!is_protected_kvm_enabled()) 1599 _kvm_arch_hardware_disable(NULL); 1600 } 1601 1602 #ifdef CONFIG_CPU_PM 1603 static int hyp_init_cpu_pm_notifier(struct notifier_block *self, 1604 unsigned long cmd, 1605 void *v) 1606 { 1607 /* 1608 * kvm_arm_hardware_enabled is left with its old value over 1609 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should 1610 * re-enable hyp. 1611 */ 1612 switch (cmd) { 1613 case CPU_PM_ENTER: 1614 if (__this_cpu_read(kvm_arm_hardware_enabled)) 1615 /* 1616 * don't update kvm_arm_hardware_enabled here 1617 * so that the hardware will be re-enabled 1618 * when we resume. See below. 1619 */ 1620 cpu_hyp_reset(); 1621 1622 return NOTIFY_OK; 1623 case CPU_PM_ENTER_FAILED: 1624 case CPU_PM_EXIT: 1625 if (__this_cpu_read(kvm_arm_hardware_enabled)) 1626 /* The hardware was enabled before suspend. */ 1627 cpu_hyp_reinit(); 1628 1629 return NOTIFY_OK; 1630 1631 default: 1632 return NOTIFY_DONE; 1633 } 1634 } 1635 1636 static struct notifier_block hyp_init_cpu_pm_nb = { 1637 .notifier_call = hyp_init_cpu_pm_notifier, 1638 }; 1639 1640 static void hyp_cpu_pm_init(void) 1641 { 1642 if (!is_protected_kvm_enabled()) 1643 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); 1644 } 1645 static void hyp_cpu_pm_exit(void) 1646 { 1647 if (!is_protected_kvm_enabled()) 1648 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); 1649 } 1650 #else 1651 static inline void hyp_cpu_pm_init(void) 1652 { 1653 } 1654 static inline void hyp_cpu_pm_exit(void) 1655 { 1656 } 1657 #endif 1658 1659 static void init_cpu_logical_map(void) 1660 { 1661 unsigned int cpu; 1662 1663 /* 1664 * Copy the MPIDR <-> logical CPU ID mapping to hyp. 1665 * Only copy the set of online CPUs whose features have been chacked 1666 * against the finalized system capabilities. The hypervisor will not 1667 * allow any other CPUs from the `possible` set to boot. 1668 */ 1669 for_each_online_cpu(cpu) 1670 hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu); 1671 } 1672 1673 #define init_psci_0_1_impl_state(config, what) \ 1674 config.psci_0_1_ ## what ## _implemented = psci_ops.what 1675 1676 static bool init_psci_relay(void) 1677 { 1678 /* 1679 * If PSCI has not been initialized, protected KVM cannot install 1680 * itself on newly booted CPUs. 1681 */ 1682 if (!psci_ops.get_version) { 1683 kvm_err("Cannot initialize protected mode without PSCI\n"); 1684 return false; 1685 } 1686 1687 kvm_host_psci_config.version = psci_ops.get_version(); 1688 1689 if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) { 1690 kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids(); 1691 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend); 1692 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on); 1693 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off); 1694 init_psci_0_1_impl_state(kvm_host_psci_config, migrate); 1695 } 1696 return true; 1697 } 1698 1699 static int init_common_resources(void) 1700 { 1701 return kvm_set_ipa_limit(); 1702 } 1703 1704 static int init_subsystems(void) 1705 { 1706 int err = 0; 1707 1708 /* 1709 * Enable hardware so that subsystem initialisation can access EL2. 1710 */ 1711 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1); 1712 1713 /* 1714 * Register CPU lower-power notifier 1715 */ 1716 hyp_cpu_pm_init(); 1717 1718 /* 1719 * Init HYP view of VGIC 1720 */ 1721 err = kvm_vgic_hyp_init(); 1722 switch (err) { 1723 case 0: 1724 vgic_present = true; 1725 break; 1726 case -ENODEV: 1727 case -ENXIO: 1728 vgic_present = false; 1729 err = 0; 1730 break; 1731 default: 1732 goto out; 1733 } 1734 1735 /* 1736 * Init HYP architected timer support 1737 */ 1738 err = kvm_timer_hyp_init(vgic_present); 1739 if (err) 1740 goto out; 1741 1742 kvm_perf_init(); 1743 kvm_sys_reg_table_init(); 1744 1745 out: 1746 if (err || !is_protected_kvm_enabled()) 1747 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1); 1748 1749 return err; 1750 } 1751 1752 static void teardown_hyp_mode(void) 1753 { 1754 int cpu; 1755 1756 free_hyp_pgds(); 1757 for_each_possible_cpu(cpu) { 1758 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); 1759 free_pages(kvm_arm_hyp_percpu_base[cpu], nvhe_percpu_order()); 1760 } 1761 } 1762 1763 static int do_pkvm_init(u32 hyp_va_bits) 1764 { 1765 void *per_cpu_base = kvm_ksym_ref(kvm_arm_hyp_percpu_base); 1766 int ret; 1767 1768 preempt_disable(); 1769 hyp_install_host_vector(); 1770 ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size, 1771 num_possible_cpus(), kern_hyp_va(per_cpu_base), 1772 hyp_va_bits); 1773 preempt_enable(); 1774 1775 return ret; 1776 } 1777 1778 static int kvm_hyp_init_protection(u32 hyp_va_bits) 1779 { 1780 void *addr = phys_to_virt(hyp_mem_base); 1781 int ret; 1782 1783 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); 1784 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 1785 1786 ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP); 1787 if (ret) 1788 return ret; 1789 1790 ret = do_pkvm_init(hyp_va_bits); 1791 if (ret) 1792 return ret; 1793 1794 free_hyp_pgds(); 1795 1796 return 0; 1797 } 1798 1799 /** 1800 * Inits Hyp-mode on all online CPUs 1801 */ 1802 static int init_hyp_mode(void) 1803 { 1804 u32 hyp_va_bits; 1805 int cpu; 1806 int err = -ENOMEM; 1807 1808 /* 1809 * The protected Hyp-mode cannot be initialized if the memory pool 1810 * allocation has failed. 1811 */ 1812 if (is_protected_kvm_enabled() && !hyp_mem_base) 1813 goto out_err; 1814 1815 /* 1816 * Allocate Hyp PGD and setup Hyp identity mapping 1817 */ 1818 err = kvm_mmu_init(&hyp_va_bits); 1819 if (err) 1820 goto out_err; 1821 1822 /* 1823 * Allocate stack pages for Hypervisor-mode 1824 */ 1825 for_each_possible_cpu(cpu) { 1826 unsigned long stack_page; 1827 1828 stack_page = __get_free_page(GFP_KERNEL); 1829 if (!stack_page) { 1830 err = -ENOMEM; 1831 goto out_err; 1832 } 1833 1834 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; 1835 } 1836 1837 /* 1838 * Allocate and initialize pages for Hypervisor-mode percpu regions. 1839 */ 1840 for_each_possible_cpu(cpu) { 1841 struct page *page; 1842 void *page_addr; 1843 1844 page = alloc_pages(GFP_KERNEL, nvhe_percpu_order()); 1845 if (!page) { 1846 err = -ENOMEM; 1847 goto out_err; 1848 } 1849 1850 page_addr = page_address(page); 1851 memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size()); 1852 kvm_arm_hyp_percpu_base[cpu] = (unsigned long)page_addr; 1853 } 1854 1855 /* 1856 * Map the Hyp-code called directly from the host 1857 */ 1858 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), 1859 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); 1860 if (err) { 1861 kvm_err("Cannot map world-switch code\n"); 1862 goto out_err; 1863 } 1864 1865 err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start), 1866 kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO); 1867 if (err) { 1868 kvm_err("Cannot map .hyp.rodata section\n"); 1869 goto out_err; 1870 } 1871 1872 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), 1873 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); 1874 if (err) { 1875 kvm_err("Cannot map rodata section\n"); 1876 goto out_err; 1877 } 1878 1879 /* 1880 * .hyp.bss is guaranteed to be placed at the beginning of the .bss 1881 * section thanks to an assertion in the linker script. Map it RW and 1882 * the rest of .bss RO. 1883 */ 1884 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start), 1885 kvm_ksym_ref(__hyp_bss_end), PAGE_HYP); 1886 if (err) { 1887 kvm_err("Cannot map hyp bss section: %d\n", err); 1888 goto out_err; 1889 } 1890 1891 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end), 1892 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); 1893 if (err) { 1894 kvm_err("Cannot map bss section\n"); 1895 goto out_err; 1896 } 1897 1898 /* 1899 * Map the Hyp stack pages 1900 */ 1901 for_each_possible_cpu(cpu) { 1902 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); 1903 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE, 1904 PAGE_HYP); 1905 1906 if (err) { 1907 kvm_err("Cannot map hyp stack\n"); 1908 goto out_err; 1909 } 1910 } 1911 1912 for_each_possible_cpu(cpu) { 1913 char *percpu_begin = (char *)kvm_arm_hyp_percpu_base[cpu]; 1914 char *percpu_end = percpu_begin + nvhe_percpu_size(); 1915 1916 /* Map Hyp percpu pages */ 1917 err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP); 1918 if (err) { 1919 kvm_err("Cannot map hyp percpu region\n"); 1920 goto out_err; 1921 } 1922 1923 /* Prepare the CPU initialization parameters */ 1924 cpu_prepare_hyp_mode(cpu); 1925 } 1926 1927 if (is_protected_kvm_enabled()) { 1928 init_cpu_logical_map(); 1929 1930 if (!init_psci_relay()) { 1931 err = -ENODEV; 1932 goto out_err; 1933 } 1934 } 1935 1936 if (is_protected_kvm_enabled()) { 1937 err = kvm_hyp_init_protection(hyp_va_bits); 1938 if (err) { 1939 kvm_err("Failed to init hyp memory protection\n"); 1940 goto out_err; 1941 } 1942 } 1943 1944 return 0; 1945 1946 out_err: 1947 teardown_hyp_mode(); 1948 kvm_err("error initializing Hyp mode: %d\n", err); 1949 return err; 1950 } 1951 1952 static void _kvm_host_prot_finalize(void *discard) 1953 { 1954 WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize)); 1955 } 1956 1957 static inline int pkvm_mark_hyp(phys_addr_t start, phys_addr_t end) 1958 { 1959 return kvm_call_hyp_nvhe(__pkvm_mark_hyp, start, end); 1960 } 1961 1962 #define pkvm_mark_hyp_section(__section) \ 1963 pkvm_mark_hyp(__pa_symbol(__section##_start), \ 1964 __pa_symbol(__section##_end)) 1965 1966 static int finalize_hyp_mode(void) 1967 { 1968 int cpu, ret; 1969 1970 if (!is_protected_kvm_enabled()) 1971 return 0; 1972 1973 ret = pkvm_mark_hyp_section(__hyp_idmap_text); 1974 if (ret) 1975 return ret; 1976 1977 ret = pkvm_mark_hyp_section(__hyp_text); 1978 if (ret) 1979 return ret; 1980 1981 ret = pkvm_mark_hyp_section(__hyp_rodata); 1982 if (ret) 1983 return ret; 1984 1985 ret = pkvm_mark_hyp_section(__hyp_bss); 1986 if (ret) 1987 return ret; 1988 1989 ret = pkvm_mark_hyp(hyp_mem_base, hyp_mem_base + hyp_mem_size); 1990 if (ret) 1991 return ret; 1992 1993 for_each_possible_cpu(cpu) { 1994 phys_addr_t start = virt_to_phys((void *)kvm_arm_hyp_percpu_base[cpu]); 1995 phys_addr_t end = start + (PAGE_SIZE << nvhe_percpu_order()); 1996 1997 ret = pkvm_mark_hyp(start, end); 1998 if (ret) 1999 return ret; 2000 2001 start = virt_to_phys((void *)per_cpu(kvm_arm_hyp_stack_page, cpu)); 2002 end = start + PAGE_SIZE; 2003 ret = pkvm_mark_hyp(start, end); 2004 if (ret) 2005 return ret; 2006 } 2007 2008 /* 2009 * Flip the static key upfront as that may no longer be possible 2010 * once the host stage 2 is installed. 2011 */ 2012 static_branch_enable(&kvm_protected_mode_initialized); 2013 on_each_cpu(_kvm_host_prot_finalize, NULL, 1); 2014 2015 return 0; 2016 } 2017 2018 static void check_kvm_target_cpu(void *ret) 2019 { 2020 *(int *)ret = kvm_target_cpu(); 2021 } 2022 2023 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) 2024 { 2025 struct kvm_vcpu *vcpu; 2026 int i; 2027 2028 mpidr &= MPIDR_HWID_BITMASK; 2029 kvm_for_each_vcpu(i, vcpu, kvm) { 2030 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) 2031 return vcpu; 2032 } 2033 return NULL; 2034 } 2035 2036 bool kvm_arch_has_irq_bypass(void) 2037 { 2038 return true; 2039 } 2040 2041 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, 2042 struct irq_bypass_producer *prod) 2043 { 2044 struct kvm_kernel_irqfd *irqfd = 2045 container_of(cons, struct kvm_kernel_irqfd, consumer); 2046 2047 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, 2048 &irqfd->irq_entry); 2049 } 2050 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, 2051 struct irq_bypass_producer *prod) 2052 { 2053 struct kvm_kernel_irqfd *irqfd = 2054 container_of(cons, struct kvm_kernel_irqfd, consumer); 2055 2056 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq, 2057 &irqfd->irq_entry); 2058 } 2059 2060 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) 2061 { 2062 struct kvm_kernel_irqfd *irqfd = 2063 container_of(cons, struct kvm_kernel_irqfd, consumer); 2064 2065 kvm_arm_halt_guest(irqfd->kvm); 2066 } 2067 2068 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) 2069 { 2070 struct kvm_kernel_irqfd *irqfd = 2071 container_of(cons, struct kvm_kernel_irqfd, consumer); 2072 2073 kvm_arm_resume_guest(irqfd->kvm); 2074 } 2075 2076 /** 2077 * Initialize Hyp-mode and memory mappings on all CPUs. 2078 */ 2079 int kvm_arch_init(void *opaque) 2080 { 2081 int err; 2082 int ret, cpu; 2083 bool in_hyp_mode; 2084 2085 if (!is_hyp_mode_available()) { 2086 kvm_info("HYP mode not available\n"); 2087 return -ENODEV; 2088 } 2089 2090 in_hyp_mode = is_kernel_in_hyp_mode(); 2091 2092 if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) || 2093 cpus_have_final_cap(ARM64_WORKAROUND_1508412)) 2094 kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \ 2095 "Only trusted guests should be used on this system.\n"); 2096 2097 for_each_online_cpu(cpu) { 2098 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1); 2099 if (ret < 0) { 2100 kvm_err("Error, CPU %d not supported!\n", cpu); 2101 return -ENODEV; 2102 } 2103 } 2104 2105 err = init_common_resources(); 2106 if (err) 2107 return err; 2108 2109 err = kvm_arm_init_sve(); 2110 if (err) 2111 return err; 2112 2113 if (!in_hyp_mode) { 2114 err = init_hyp_mode(); 2115 if (err) 2116 goto out_err; 2117 } 2118 2119 err = kvm_init_vector_slots(); 2120 if (err) { 2121 kvm_err("Cannot initialise vector slots\n"); 2122 goto out_err; 2123 } 2124 2125 err = init_subsystems(); 2126 if (err) 2127 goto out_hyp; 2128 2129 if (!in_hyp_mode) { 2130 err = finalize_hyp_mode(); 2131 if (err) { 2132 kvm_err("Failed to finalize Hyp protection\n"); 2133 goto out_hyp; 2134 } 2135 } 2136 2137 if (is_protected_kvm_enabled()) { 2138 kvm_info("Protected nVHE mode initialized successfully\n"); 2139 } else if (in_hyp_mode) { 2140 kvm_info("VHE mode initialized successfully\n"); 2141 } else { 2142 kvm_info("Hyp mode initialized successfully\n"); 2143 } 2144 2145 return 0; 2146 2147 out_hyp: 2148 hyp_cpu_pm_exit(); 2149 if (!in_hyp_mode) 2150 teardown_hyp_mode(); 2151 out_err: 2152 return err; 2153 } 2154 2155 /* NOP: Compiling as a module not supported */ 2156 void kvm_arch_exit(void) 2157 { 2158 kvm_perf_teardown(); 2159 } 2160 2161 static int __init early_kvm_mode_cfg(char *arg) 2162 { 2163 if (!arg) 2164 return -EINVAL; 2165 2166 if (strcmp(arg, "protected") == 0) { 2167 kvm_mode = KVM_MODE_PROTECTED; 2168 return 0; 2169 } 2170 2171 if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) 2172 return 0; 2173 2174 return -EINVAL; 2175 } 2176 early_param("kvm-arm.mode", early_kvm_mode_cfg); 2177 2178 enum kvm_mode kvm_get_mode(void) 2179 { 2180 return kvm_mode; 2181 } 2182 2183 static int arm_init(void) 2184 { 2185 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 2186 return rc; 2187 } 2188 2189 module_init(arm_init); 2190