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