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