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() && 1440 !is_protected_kvm_enabled()) { 1441 err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs), 1442 __BP_HARDEN_HYP_VECS_SZ, &base); 1443 if (err) 1444 return err; 1445 } 1446 1447 kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT); 1448 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT); 1449 return 0; 1450 } 1451 1452 static void cpu_prepare_hyp_mode(int cpu) 1453 { 1454 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 1455 unsigned long tcr; 1456 1457 /* 1458 * Calculate the raw per-cpu offset without a translation from the 1459 * kernel's mapping to the linear mapping, and store it in tpidr_el2 1460 * so that we can use adr_l to access per-cpu variables in EL2. 1461 * Also drop the KASAN tag which gets in the way... 1462 */ 1463 params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) - 1464 (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start)); 1465 1466 params->mair_el2 = read_sysreg(mair_el1); 1467 1468 /* 1469 * The ID map may be configured to use an extended virtual address 1470 * range. This is only the case if system RAM is out of range for the 1471 * currently configured page size and VA_BITS, in which case we will 1472 * also need the extended virtual range for the HYP ID map, or we won't 1473 * be able to enable the EL2 MMU. 1474 * 1475 * However, at EL2, there is only one TTBR register, and we can't switch 1476 * between translation tables *and* update TCR_EL2.T0SZ at the same 1477 * time. Bottom line: we need to use the extended range with *both* our 1478 * translation tables. 1479 * 1480 * So use the same T0SZ value we use for the ID map. 1481 */ 1482 tcr = (read_sysreg(tcr_el1) & TCR_EL2_MASK) | TCR_EL2_RES1; 1483 tcr &= ~TCR_T0SZ_MASK; 1484 tcr |= (idmap_t0sz & GENMASK(TCR_TxSZ_WIDTH - 1, 0)) << TCR_T0SZ_OFFSET; 1485 params->tcr_el2 = tcr; 1486 1487 params->stack_hyp_va = kern_hyp_va(per_cpu(kvm_arm_hyp_stack_page, cpu) + PAGE_SIZE); 1488 params->pgd_pa = kvm_mmu_get_httbr(); 1489 if (is_protected_kvm_enabled()) 1490 params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; 1491 else 1492 params->hcr_el2 = HCR_HOST_NVHE_FLAGS; 1493 params->vttbr = params->vtcr = 0; 1494 1495 /* 1496 * Flush the init params from the data cache because the struct will 1497 * be read while the MMU is off. 1498 */ 1499 kvm_flush_dcache_to_poc(params, sizeof(*params)); 1500 } 1501 1502 static void hyp_install_host_vector(void) 1503 { 1504 struct kvm_nvhe_init_params *params; 1505 struct arm_smccc_res res; 1506 1507 /* Switch from the HYP stub to our own HYP init vector */ 1508 __hyp_set_vectors(kvm_get_idmap_vector()); 1509 1510 /* 1511 * Call initialization code, and switch to the full blown HYP code. 1512 * If the cpucaps haven't been finalized yet, something has gone very 1513 * wrong, and hyp will crash and burn when it uses any 1514 * cpus_have_const_cap() wrapper. 1515 */ 1516 BUG_ON(!system_capabilities_finalized()); 1517 params = this_cpu_ptr_nvhe_sym(kvm_init_params); 1518 arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res); 1519 WARN_ON(res.a0 != SMCCC_RET_SUCCESS); 1520 } 1521 1522 static void cpu_init_hyp_mode(void) 1523 { 1524 hyp_install_host_vector(); 1525 1526 /* 1527 * Disabling SSBD on a non-VHE system requires us to enable SSBS 1528 * at EL2. 1529 */ 1530 if (this_cpu_has_cap(ARM64_SSBS) && 1531 arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) { 1532 kvm_call_hyp_nvhe(__kvm_enable_ssbs); 1533 } 1534 } 1535 1536 static void cpu_hyp_reset(void) 1537 { 1538 if (!is_kernel_in_hyp_mode()) 1539 __hyp_reset_vectors(); 1540 } 1541 1542 /* 1543 * EL2 vectors can be mapped and rerouted in a number of ways, 1544 * depending on the kernel configuration and CPU present: 1545 * 1546 * - If the CPU is affected by Spectre-v2, the hardening sequence is 1547 * placed in one of the vector slots, which is executed before jumping 1548 * to the real vectors. 1549 * 1550 * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot 1551 * containing the hardening sequence is mapped next to the idmap page, 1552 * and executed before jumping to the real vectors. 1553 * 1554 * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an 1555 * empty slot is selected, mapped next to the idmap page, and 1556 * executed before jumping to the real vectors. 1557 * 1558 * Note that ARM64_SPECTRE_V3A is somewhat incompatible with 1559 * VHE, as we don't have hypervisor-specific mappings. If the system 1560 * is VHE and yet selects this capability, it will be ignored. 1561 */ 1562 static void cpu_set_hyp_vector(void) 1563 { 1564 struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data); 1565 void *vector = hyp_spectre_vector_selector[data->slot]; 1566 1567 if (!is_protected_kvm_enabled()) 1568 *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector; 1569 else 1570 kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot); 1571 } 1572 1573 static void cpu_hyp_init_context(void) 1574 { 1575 kvm_init_host_cpu_context(&this_cpu_ptr_hyp_sym(kvm_host_data)->host_ctxt); 1576 1577 if (!is_kernel_in_hyp_mode()) 1578 cpu_init_hyp_mode(); 1579 } 1580 1581 static void cpu_hyp_init_features(void) 1582 { 1583 cpu_set_hyp_vector(); 1584 kvm_arm_init_debug(); 1585 1586 if (is_kernel_in_hyp_mode()) 1587 kvm_timer_init_vhe(); 1588 1589 if (vgic_present) 1590 kvm_vgic_init_cpu_hardware(); 1591 } 1592 1593 static void cpu_hyp_reinit(void) 1594 { 1595 cpu_hyp_reset(); 1596 cpu_hyp_init_context(); 1597 cpu_hyp_init_features(); 1598 } 1599 1600 static void _kvm_arch_hardware_enable(void *discard) 1601 { 1602 if (!__this_cpu_read(kvm_arm_hardware_enabled)) { 1603 cpu_hyp_reinit(); 1604 __this_cpu_write(kvm_arm_hardware_enabled, 1); 1605 } 1606 } 1607 1608 int kvm_arch_hardware_enable(void) 1609 { 1610 _kvm_arch_hardware_enable(NULL); 1611 return 0; 1612 } 1613 1614 static void _kvm_arch_hardware_disable(void *discard) 1615 { 1616 if (__this_cpu_read(kvm_arm_hardware_enabled)) { 1617 cpu_hyp_reset(); 1618 __this_cpu_write(kvm_arm_hardware_enabled, 0); 1619 } 1620 } 1621 1622 void kvm_arch_hardware_disable(void) 1623 { 1624 if (!is_protected_kvm_enabled()) 1625 _kvm_arch_hardware_disable(NULL); 1626 } 1627 1628 #ifdef CONFIG_CPU_PM 1629 static int hyp_init_cpu_pm_notifier(struct notifier_block *self, 1630 unsigned long cmd, 1631 void *v) 1632 { 1633 /* 1634 * kvm_arm_hardware_enabled is left with its old value over 1635 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should 1636 * re-enable hyp. 1637 */ 1638 switch (cmd) { 1639 case CPU_PM_ENTER: 1640 if (__this_cpu_read(kvm_arm_hardware_enabled)) 1641 /* 1642 * don't update kvm_arm_hardware_enabled here 1643 * so that the hardware will be re-enabled 1644 * when we resume. See below. 1645 */ 1646 cpu_hyp_reset(); 1647 1648 return NOTIFY_OK; 1649 case CPU_PM_ENTER_FAILED: 1650 case CPU_PM_EXIT: 1651 if (__this_cpu_read(kvm_arm_hardware_enabled)) 1652 /* The hardware was enabled before suspend. */ 1653 cpu_hyp_reinit(); 1654 1655 return NOTIFY_OK; 1656 1657 default: 1658 return NOTIFY_DONE; 1659 } 1660 } 1661 1662 static struct notifier_block hyp_init_cpu_pm_nb = { 1663 .notifier_call = hyp_init_cpu_pm_notifier, 1664 }; 1665 1666 static void hyp_cpu_pm_init(void) 1667 { 1668 if (!is_protected_kvm_enabled()) 1669 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); 1670 } 1671 static void hyp_cpu_pm_exit(void) 1672 { 1673 if (!is_protected_kvm_enabled()) 1674 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); 1675 } 1676 #else 1677 static inline void hyp_cpu_pm_init(void) 1678 { 1679 } 1680 static inline void hyp_cpu_pm_exit(void) 1681 { 1682 } 1683 #endif 1684 1685 static void init_cpu_logical_map(void) 1686 { 1687 unsigned int cpu; 1688 1689 /* 1690 * Copy the MPIDR <-> logical CPU ID mapping to hyp. 1691 * Only copy the set of online CPUs whose features have been checked 1692 * against the finalized system capabilities. The hypervisor will not 1693 * allow any other CPUs from the `possible` set to boot. 1694 */ 1695 for_each_online_cpu(cpu) 1696 hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu); 1697 } 1698 1699 #define init_psci_0_1_impl_state(config, what) \ 1700 config.psci_0_1_ ## what ## _implemented = psci_ops.what 1701 1702 static bool init_psci_relay(void) 1703 { 1704 /* 1705 * If PSCI has not been initialized, protected KVM cannot install 1706 * itself on newly booted CPUs. 1707 */ 1708 if (!psci_ops.get_version) { 1709 kvm_err("Cannot initialize protected mode without PSCI\n"); 1710 return false; 1711 } 1712 1713 kvm_host_psci_config.version = psci_ops.get_version(); 1714 1715 if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) { 1716 kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids(); 1717 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend); 1718 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on); 1719 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off); 1720 init_psci_0_1_impl_state(kvm_host_psci_config, migrate); 1721 } 1722 return true; 1723 } 1724 1725 static int init_subsystems(void) 1726 { 1727 int err = 0; 1728 1729 /* 1730 * Enable hardware so that subsystem initialisation can access EL2. 1731 */ 1732 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1); 1733 1734 /* 1735 * Register CPU lower-power notifier 1736 */ 1737 hyp_cpu_pm_init(); 1738 1739 /* 1740 * Init HYP view of VGIC 1741 */ 1742 err = kvm_vgic_hyp_init(); 1743 switch (err) { 1744 case 0: 1745 vgic_present = true; 1746 break; 1747 case -ENODEV: 1748 case -ENXIO: 1749 vgic_present = false; 1750 err = 0; 1751 break; 1752 default: 1753 goto out; 1754 } 1755 1756 /* 1757 * Init HYP architected timer support 1758 */ 1759 err = kvm_timer_hyp_init(vgic_present); 1760 if (err) 1761 goto out; 1762 1763 kvm_register_perf_callbacks(NULL); 1764 1765 kvm_sys_reg_table_init(); 1766 1767 out: 1768 if (err || !is_protected_kvm_enabled()) 1769 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1); 1770 1771 return err; 1772 } 1773 1774 static void teardown_hyp_mode(void) 1775 { 1776 int cpu; 1777 1778 free_hyp_pgds(); 1779 for_each_possible_cpu(cpu) { 1780 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); 1781 free_pages(kvm_arm_hyp_percpu_base[cpu], nvhe_percpu_order()); 1782 } 1783 } 1784 1785 static int do_pkvm_init(u32 hyp_va_bits) 1786 { 1787 void *per_cpu_base = kvm_ksym_ref(kvm_arm_hyp_percpu_base); 1788 int ret; 1789 1790 preempt_disable(); 1791 cpu_hyp_init_context(); 1792 ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size, 1793 num_possible_cpus(), kern_hyp_va(per_cpu_base), 1794 hyp_va_bits); 1795 cpu_hyp_init_features(); 1796 1797 /* 1798 * The stub hypercalls are now disabled, so set our local flag to 1799 * prevent a later re-init attempt in kvm_arch_hardware_enable(). 1800 */ 1801 __this_cpu_write(kvm_arm_hardware_enabled, 1); 1802 preempt_enable(); 1803 1804 return ret; 1805 } 1806 1807 static int kvm_hyp_init_protection(u32 hyp_va_bits) 1808 { 1809 void *addr = phys_to_virt(hyp_mem_base); 1810 int ret; 1811 1812 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); 1813 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1); 1814 kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1); 1815 kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); 1816 kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); 1817 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); 1818 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 1819 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1); 1820 1821 ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP); 1822 if (ret) 1823 return ret; 1824 1825 ret = do_pkvm_init(hyp_va_bits); 1826 if (ret) 1827 return ret; 1828 1829 free_hyp_pgds(); 1830 1831 return 0; 1832 } 1833 1834 /** 1835 * Inits Hyp-mode on all online CPUs 1836 */ 1837 static int init_hyp_mode(void) 1838 { 1839 u32 hyp_va_bits; 1840 int cpu; 1841 int err = -ENOMEM; 1842 1843 /* 1844 * The protected Hyp-mode cannot be initialized if the memory pool 1845 * allocation has failed. 1846 */ 1847 if (is_protected_kvm_enabled() && !hyp_mem_base) 1848 goto out_err; 1849 1850 /* 1851 * Allocate Hyp PGD and setup Hyp identity mapping 1852 */ 1853 err = kvm_mmu_init(&hyp_va_bits); 1854 if (err) 1855 goto out_err; 1856 1857 /* 1858 * Allocate stack pages for Hypervisor-mode 1859 */ 1860 for_each_possible_cpu(cpu) { 1861 unsigned long stack_page; 1862 1863 stack_page = __get_free_page(GFP_KERNEL); 1864 if (!stack_page) { 1865 err = -ENOMEM; 1866 goto out_err; 1867 } 1868 1869 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; 1870 } 1871 1872 /* 1873 * Allocate and initialize pages for Hypervisor-mode percpu regions. 1874 */ 1875 for_each_possible_cpu(cpu) { 1876 struct page *page; 1877 void *page_addr; 1878 1879 page = alloc_pages(GFP_KERNEL, nvhe_percpu_order()); 1880 if (!page) { 1881 err = -ENOMEM; 1882 goto out_err; 1883 } 1884 1885 page_addr = page_address(page); 1886 memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size()); 1887 kvm_arm_hyp_percpu_base[cpu] = (unsigned long)page_addr; 1888 } 1889 1890 /* 1891 * Map the Hyp-code called directly from the host 1892 */ 1893 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), 1894 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); 1895 if (err) { 1896 kvm_err("Cannot map world-switch code\n"); 1897 goto out_err; 1898 } 1899 1900 err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start), 1901 kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO); 1902 if (err) { 1903 kvm_err("Cannot map .hyp.rodata section\n"); 1904 goto out_err; 1905 } 1906 1907 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), 1908 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); 1909 if (err) { 1910 kvm_err("Cannot map rodata section\n"); 1911 goto out_err; 1912 } 1913 1914 /* 1915 * .hyp.bss is guaranteed to be placed at the beginning of the .bss 1916 * section thanks to an assertion in the linker script. Map it RW and 1917 * the rest of .bss RO. 1918 */ 1919 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start), 1920 kvm_ksym_ref(__hyp_bss_end), PAGE_HYP); 1921 if (err) { 1922 kvm_err("Cannot map hyp bss section: %d\n", err); 1923 goto out_err; 1924 } 1925 1926 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end), 1927 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); 1928 if (err) { 1929 kvm_err("Cannot map bss section\n"); 1930 goto out_err; 1931 } 1932 1933 /* 1934 * Map the Hyp stack pages 1935 */ 1936 for_each_possible_cpu(cpu) { 1937 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); 1938 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE, 1939 PAGE_HYP); 1940 1941 if (err) { 1942 kvm_err("Cannot map hyp stack\n"); 1943 goto out_err; 1944 } 1945 } 1946 1947 for_each_possible_cpu(cpu) { 1948 char *percpu_begin = (char *)kvm_arm_hyp_percpu_base[cpu]; 1949 char *percpu_end = percpu_begin + nvhe_percpu_size(); 1950 1951 /* Map Hyp percpu pages */ 1952 err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP); 1953 if (err) { 1954 kvm_err("Cannot map hyp percpu region\n"); 1955 goto out_err; 1956 } 1957 1958 /* Prepare the CPU initialization parameters */ 1959 cpu_prepare_hyp_mode(cpu); 1960 } 1961 1962 if (is_protected_kvm_enabled()) { 1963 init_cpu_logical_map(); 1964 1965 if (!init_psci_relay()) { 1966 err = -ENODEV; 1967 goto out_err; 1968 } 1969 } 1970 1971 if (is_protected_kvm_enabled()) { 1972 err = kvm_hyp_init_protection(hyp_va_bits); 1973 if (err) { 1974 kvm_err("Failed to init hyp memory protection\n"); 1975 goto out_err; 1976 } 1977 } 1978 1979 return 0; 1980 1981 out_err: 1982 teardown_hyp_mode(); 1983 kvm_err("error initializing Hyp mode: %d\n", err); 1984 return err; 1985 } 1986 1987 static void _kvm_host_prot_finalize(void *arg) 1988 { 1989 int *err = arg; 1990 1991 if (WARN_ON(kvm_call_hyp_nvhe(__pkvm_prot_finalize))) 1992 WRITE_ONCE(*err, -EINVAL); 1993 } 1994 1995 static int pkvm_drop_host_privileges(void) 1996 { 1997 int ret = 0; 1998 1999 /* 2000 * Flip the static key upfront as that may no longer be possible 2001 * once the host stage 2 is installed. 2002 */ 2003 static_branch_enable(&kvm_protected_mode_initialized); 2004 on_each_cpu(_kvm_host_prot_finalize, &ret, 1); 2005 return ret; 2006 } 2007 2008 static int finalize_hyp_mode(void) 2009 { 2010 if (!is_protected_kvm_enabled()) 2011 return 0; 2012 2013 /* 2014 * Exclude HYP BSS from kmemleak so that it doesn't get peeked 2015 * at, which would end badly once the section is inaccessible. 2016 * None of other sections should ever be introspected. 2017 */ 2018 kmemleak_free_part(__hyp_bss_start, __hyp_bss_end - __hyp_bss_start); 2019 return pkvm_drop_host_privileges(); 2020 } 2021 2022 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) 2023 { 2024 struct kvm_vcpu *vcpu; 2025 unsigned long i; 2026 2027 mpidr &= MPIDR_HWID_BITMASK; 2028 kvm_for_each_vcpu(i, vcpu, kvm) { 2029 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) 2030 return vcpu; 2031 } 2032 return NULL; 2033 } 2034 2035 bool kvm_arch_has_irq_bypass(void) 2036 { 2037 return true; 2038 } 2039 2040 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, 2041 struct irq_bypass_producer *prod) 2042 { 2043 struct kvm_kernel_irqfd *irqfd = 2044 container_of(cons, struct kvm_kernel_irqfd, consumer); 2045 2046 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, 2047 &irqfd->irq_entry); 2048 } 2049 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, 2050 struct irq_bypass_producer *prod) 2051 { 2052 struct kvm_kernel_irqfd *irqfd = 2053 container_of(cons, struct kvm_kernel_irqfd, consumer); 2054 2055 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq, 2056 &irqfd->irq_entry); 2057 } 2058 2059 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) 2060 { 2061 struct kvm_kernel_irqfd *irqfd = 2062 container_of(cons, struct kvm_kernel_irqfd, consumer); 2063 2064 kvm_arm_halt_guest(irqfd->kvm); 2065 } 2066 2067 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) 2068 { 2069 struct kvm_kernel_irqfd *irqfd = 2070 container_of(cons, struct kvm_kernel_irqfd, consumer); 2071 2072 kvm_arm_resume_guest(irqfd->kvm); 2073 } 2074 2075 /** 2076 * Initialize Hyp-mode and memory mappings on all CPUs. 2077 */ 2078 int kvm_arch_init(void *opaque) 2079 { 2080 int err; 2081 bool in_hyp_mode; 2082 2083 if (!is_hyp_mode_available()) { 2084 kvm_info("HYP mode not available\n"); 2085 return -ENODEV; 2086 } 2087 2088 if (kvm_get_mode() == KVM_MODE_NONE) { 2089 kvm_info("KVM disabled from command line\n"); 2090 return -ENODEV; 2091 } 2092 2093 in_hyp_mode = is_kernel_in_hyp_mode(); 2094 2095 if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) || 2096 cpus_have_final_cap(ARM64_WORKAROUND_1508412)) 2097 kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \ 2098 "Only trusted guests should be used on this system.\n"); 2099 2100 err = kvm_set_ipa_limit(); 2101 if (err) 2102 return err; 2103 2104 err = kvm_arm_init_sve(); 2105 if (err) 2106 return err; 2107 2108 err = kvm_arm_vmid_alloc_init(); 2109 if (err) { 2110 kvm_err("Failed to initialize VMID allocator.\n"); 2111 return err; 2112 } 2113 2114 if (!in_hyp_mode) { 2115 err = init_hyp_mode(); 2116 if (err) 2117 goto out_err; 2118 } 2119 2120 err = kvm_init_vector_slots(); 2121 if (err) { 2122 kvm_err("Cannot initialise vector slots\n"); 2123 goto out_err; 2124 } 2125 2126 err = init_subsystems(); 2127 if (err) 2128 goto out_hyp; 2129 2130 if (!in_hyp_mode) { 2131 err = finalize_hyp_mode(); 2132 if (err) { 2133 kvm_err("Failed to finalize Hyp protection\n"); 2134 goto out_hyp; 2135 } 2136 } 2137 2138 if (is_protected_kvm_enabled()) { 2139 kvm_info("Protected nVHE mode initialized successfully\n"); 2140 } else if (in_hyp_mode) { 2141 kvm_info("VHE mode initialized successfully\n"); 2142 } else { 2143 kvm_info("Hyp mode initialized successfully\n"); 2144 } 2145 2146 return 0; 2147 2148 out_hyp: 2149 hyp_cpu_pm_exit(); 2150 if (!in_hyp_mode) 2151 teardown_hyp_mode(); 2152 out_err: 2153 kvm_arm_vmid_alloc_free(); 2154 return err; 2155 } 2156 2157 /* NOP: Compiling as a module not supported */ 2158 void kvm_arch_exit(void) 2159 { 2160 kvm_unregister_perf_callbacks(); 2161 } 2162 2163 static int __init early_kvm_mode_cfg(char *arg) 2164 { 2165 if (!arg) 2166 return -EINVAL; 2167 2168 if (strcmp(arg, "protected") == 0) { 2169 kvm_mode = KVM_MODE_PROTECTED; 2170 return 0; 2171 } 2172 2173 if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) { 2174 kvm_mode = KVM_MODE_DEFAULT; 2175 return 0; 2176 } 2177 2178 if (strcmp(arg, "none") == 0) { 2179 kvm_mode = KVM_MODE_NONE; 2180 return 0; 2181 } 2182 2183 return -EINVAL; 2184 } 2185 early_param("kvm-arm.mode", early_kvm_mode_cfg); 2186 2187 enum kvm_mode kvm_get_mode(void) 2188 { 2189 return kvm_mode; 2190 } 2191 2192 static int arm_init(void) 2193 { 2194 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 2195 return rc; 2196 } 2197 2198 module_init(arm_init); 2199