1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University 4 * Author: Christoffer Dall <c.dall@virtualopensystems.com> 5 */ 6 7 #include <linux/bug.h> 8 #include <linux/cpu_pm.h> 9 #include <linux/errno.h> 10 #include <linux/err.h> 11 #include <linux/kvm_host.h> 12 #include <linux/list.h> 13 #include <linux/module.h> 14 #include <linux/vmalloc.h> 15 #include <linux/fs.h> 16 #include <linux/mman.h> 17 #include <linux/sched.h> 18 #include <linux/kvm.h> 19 #include <linux/kvm_irqfd.h> 20 #include <linux/irqbypass.h> 21 #include <linux/sched/stat.h> 22 #include <trace/events/kvm.h> 23 24 #define CREATE_TRACE_POINTS 25 #include "trace_arm.h" 26 27 #include <linux/uaccess.h> 28 #include <asm/ptrace.h> 29 #include <asm/mman.h> 30 #include <asm/tlbflush.h> 31 #include <asm/cacheflush.h> 32 #include <asm/cpufeature.h> 33 #include <asm/virt.h> 34 #include <asm/kvm_arm.h> 35 #include <asm/kvm_asm.h> 36 #include <asm/kvm_mmu.h> 37 #include <asm/kvm_emulate.h> 38 #include <asm/kvm_coproc.h> 39 #include <asm/sections.h> 40 41 #include <kvm/arm_hypercalls.h> 42 #include <kvm/arm_pmu.h> 43 #include <kvm/arm_psci.h> 44 45 #ifdef REQUIRES_VIRT 46 __asm__(".arch_extension virt"); 47 #endif 48 49 DEFINE_PER_CPU(kvm_host_data_t, kvm_host_data); 50 static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); 51 52 /* The VMID used in the VTTBR */ 53 static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1); 54 static u32 kvm_next_vmid; 55 static DEFINE_SPINLOCK(kvm_vmid_lock); 56 57 static bool vgic_present; 58 59 static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled); 60 DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use); 61 62 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 63 { 64 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; 65 } 66 67 int kvm_arch_hardware_setup(void *opaque) 68 { 69 return 0; 70 } 71 72 int kvm_arch_check_processor_compat(void *opaque) 73 { 74 return 0; 75 } 76 77 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, 78 struct kvm_enable_cap *cap) 79 { 80 int r; 81 82 if (cap->flags) 83 return -EINVAL; 84 85 switch (cap->cap) { 86 case KVM_CAP_ARM_NISV_TO_USER: 87 r = 0; 88 kvm->arch.return_nisv_io_abort_to_user = true; 89 break; 90 default: 91 r = -EINVAL; 92 break; 93 } 94 95 return r; 96 } 97 98 static int kvm_arm_default_max_vcpus(void) 99 { 100 return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; 101 } 102 103 /** 104 * kvm_arch_init_vm - initializes a VM data structure 105 * @kvm: pointer to the KVM struct 106 */ 107 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 108 { 109 int ret, cpu; 110 111 ret = kvm_arm_setup_stage2(kvm, type); 112 if (ret) 113 return ret; 114 115 kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran)); 116 if (!kvm->arch.last_vcpu_ran) 117 return -ENOMEM; 118 119 for_each_possible_cpu(cpu) 120 *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1; 121 122 ret = kvm_alloc_stage2_pgd(kvm); 123 if (ret) 124 goto out_fail_alloc; 125 126 ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP); 127 if (ret) 128 goto out_free_stage2_pgd; 129 130 kvm_vgic_early_init(kvm); 131 132 /* Mark the initial VMID generation invalid */ 133 kvm->arch.vmid.vmid_gen = 0; 134 135 /* The maximum number of VCPUs is limited by the host's GIC model */ 136 kvm->arch.max_vcpus = kvm_arm_default_max_vcpus(); 137 138 return ret; 139 out_free_stage2_pgd: 140 kvm_free_stage2_pgd(kvm); 141 out_fail_alloc: 142 free_percpu(kvm->arch.last_vcpu_ran); 143 kvm->arch.last_vcpu_ran = NULL; 144 return ret; 145 } 146 147 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) 148 { 149 return 0; 150 } 151 152 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 153 { 154 return VM_FAULT_SIGBUS; 155 } 156 157 158 /** 159 * kvm_arch_destroy_vm - destroy the VM data structure 160 * @kvm: pointer to the KVM struct 161 */ 162 void kvm_arch_destroy_vm(struct kvm *kvm) 163 { 164 int i; 165 166 kvm_vgic_destroy(kvm); 167 168 free_percpu(kvm->arch.last_vcpu_ran); 169 kvm->arch.last_vcpu_ran = NULL; 170 171 for (i = 0; i < KVM_MAX_VCPUS; ++i) { 172 if (kvm->vcpus[i]) { 173 kvm_vcpu_destroy(kvm->vcpus[i]); 174 kvm->vcpus[i] = NULL; 175 } 176 } 177 atomic_set(&kvm->online_vcpus, 0); 178 } 179 180 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 181 { 182 int r; 183 switch (ext) { 184 case KVM_CAP_IRQCHIP: 185 r = vgic_present; 186 break; 187 case KVM_CAP_IOEVENTFD: 188 case KVM_CAP_DEVICE_CTRL: 189 case KVM_CAP_USER_MEMORY: 190 case KVM_CAP_SYNC_MMU: 191 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: 192 case KVM_CAP_ONE_REG: 193 case KVM_CAP_ARM_PSCI: 194 case KVM_CAP_ARM_PSCI_0_2: 195 case KVM_CAP_READONLY_MEM: 196 case KVM_CAP_MP_STATE: 197 case KVM_CAP_IMMEDIATE_EXIT: 198 case KVM_CAP_VCPU_EVENTS: 199 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2: 200 case KVM_CAP_ARM_NISV_TO_USER: 201 case KVM_CAP_ARM_INJECT_EXT_DABT: 202 r = 1; 203 break; 204 case KVM_CAP_ARM_SET_DEVICE_ADDR: 205 r = 1; 206 break; 207 case KVM_CAP_NR_VCPUS: 208 r = num_online_cpus(); 209 break; 210 case KVM_CAP_MAX_VCPUS: 211 case KVM_CAP_MAX_VCPU_ID: 212 if (kvm) 213 r = kvm->arch.max_vcpus; 214 else 215 r = kvm_arm_default_max_vcpus(); 216 break; 217 case KVM_CAP_MSI_DEVID: 218 if (!kvm) 219 r = -EINVAL; 220 else 221 r = kvm->arch.vgic.msis_require_devid; 222 break; 223 case KVM_CAP_ARM_USER_IRQ: 224 /* 225 * 1: EL1_VTIMER, EL1_PTIMER, and PMU. 226 * (bump this number if adding more devices) 227 */ 228 r = 1; 229 break; 230 default: 231 r = kvm_arch_vm_ioctl_check_extension(kvm, ext); 232 break; 233 } 234 return r; 235 } 236 237 long kvm_arch_dev_ioctl(struct file *filp, 238 unsigned int ioctl, unsigned long arg) 239 { 240 return -EINVAL; 241 } 242 243 struct kvm *kvm_arch_alloc_vm(void) 244 { 245 if (!has_vhe()) 246 return kzalloc(sizeof(struct kvm), GFP_KERNEL); 247 248 return vzalloc(sizeof(struct kvm)); 249 } 250 251 void kvm_arch_free_vm(struct kvm *kvm) 252 { 253 if (!has_vhe()) 254 kfree(kvm); 255 else 256 vfree(kvm); 257 } 258 259 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 260 { 261 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) 262 return -EBUSY; 263 264 if (id >= kvm->arch.max_vcpus) 265 return -EINVAL; 266 267 return 0; 268 } 269 270 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 271 { 272 int err; 273 274 /* Force users to call KVM_ARM_VCPU_INIT */ 275 vcpu->arch.target = -1; 276 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); 277 278 /* Set up the timer */ 279 kvm_timer_vcpu_init(vcpu); 280 281 kvm_pmu_vcpu_init(vcpu); 282 283 kvm_arm_reset_debug_ptr(vcpu); 284 285 kvm_arm_pvtime_vcpu_init(&vcpu->arch); 286 287 err = kvm_vgic_vcpu_init(vcpu); 288 if (err) 289 return err; 290 291 return create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP); 292 } 293 294 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 295 { 296 } 297 298 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 299 { 300 if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm))) 301 static_branch_dec(&userspace_irqchip_in_use); 302 303 kvm_mmu_free_memory_caches(vcpu); 304 kvm_timer_vcpu_terminate(vcpu); 305 kvm_pmu_vcpu_destroy(vcpu); 306 307 kvm_arm_vcpu_destroy(vcpu); 308 } 309 310 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) 311 { 312 return kvm_timer_is_pending(vcpu); 313 } 314 315 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) 316 { 317 /* 318 * If we're about to block (most likely because we've just hit a 319 * WFI), we need to sync back the state of the GIC CPU interface 320 * so that we have the latest PMR and group enables. This ensures 321 * that kvm_arch_vcpu_runnable has up-to-date data to decide 322 * whether we have pending interrupts. 323 * 324 * For the same reason, we want to tell GICv4 that we need 325 * doorbells to be signalled, should an interrupt become pending. 326 */ 327 preempt_disable(); 328 kvm_vgic_vmcr_sync(vcpu); 329 vgic_v4_put(vcpu, true); 330 preempt_enable(); 331 } 332 333 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) 334 { 335 preempt_disable(); 336 vgic_v4_load(vcpu); 337 preempt_enable(); 338 } 339 340 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 341 { 342 int *last_ran; 343 kvm_host_data_t *cpu_data; 344 345 last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran); 346 cpu_data = this_cpu_ptr(&kvm_host_data); 347 348 /* 349 * We might get preempted before the vCPU actually runs, but 350 * over-invalidation doesn't affect correctness. 351 */ 352 if (*last_ran != vcpu->vcpu_id) { 353 kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu); 354 *last_ran = vcpu->vcpu_id; 355 } 356 357 vcpu->cpu = cpu; 358 vcpu->arch.host_cpu_context = &cpu_data->host_ctxt; 359 360 kvm_vgic_load(vcpu); 361 kvm_timer_vcpu_load(vcpu); 362 kvm_vcpu_load_sysregs(vcpu); 363 kvm_arch_vcpu_load_fp(vcpu); 364 kvm_vcpu_pmu_restore_guest(vcpu); 365 if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) 366 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); 367 368 if (single_task_running()) 369 vcpu_clear_wfx_traps(vcpu); 370 else 371 vcpu_set_wfx_traps(vcpu); 372 373 vcpu_ptrauth_setup_lazy(vcpu); 374 } 375 376 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 377 { 378 kvm_arch_vcpu_put_fp(vcpu); 379 kvm_vcpu_put_sysregs(vcpu); 380 kvm_timer_vcpu_put(vcpu); 381 kvm_vgic_put(vcpu); 382 kvm_vcpu_pmu_restore_host(vcpu); 383 384 vcpu->cpu = -1; 385 } 386 387 static void vcpu_power_off(struct kvm_vcpu *vcpu) 388 { 389 vcpu->arch.power_off = true; 390 kvm_make_request(KVM_REQ_SLEEP, vcpu); 391 kvm_vcpu_kick(vcpu); 392 } 393 394 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 395 struct kvm_mp_state *mp_state) 396 { 397 if (vcpu->arch.power_off) 398 mp_state->mp_state = KVM_MP_STATE_STOPPED; 399 else 400 mp_state->mp_state = KVM_MP_STATE_RUNNABLE; 401 402 return 0; 403 } 404 405 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 406 struct kvm_mp_state *mp_state) 407 { 408 int ret = 0; 409 410 switch (mp_state->mp_state) { 411 case KVM_MP_STATE_RUNNABLE: 412 vcpu->arch.power_off = false; 413 break; 414 case KVM_MP_STATE_STOPPED: 415 vcpu_power_off(vcpu); 416 break; 417 default: 418 ret = -EINVAL; 419 } 420 421 return ret; 422 } 423 424 /** 425 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled 426 * @v: The VCPU pointer 427 * 428 * If the guest CPU is not waiting for interrupts or an interrupt line is 429 * asserted, the CPU is by definition runnable. 430 */ 431 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) 432 { 433 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF); 434 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v)) 435 && !v->arch.power_off && !v->arch.pause); 436 } 437 438 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 439 { 440 return vcpu_mode_priv(vcpu); 441 } 442 443 /* Just ensure a guest exit from a particular CPU */ 444 static void exit_vm_noop(void *info) 445 { 446 } 447 448 void force_vm_exit(const cpumask_t *mask) 449 { 450 preempt_disable(); 451 smp_call_function_many(mask, exit_vm_noop, NULL, true); 452 preempt_enable(); 453 } 454 455 /** 456 * need_new_vmid_gen - check that the VMID is still valid 457 * @vmid: The VMID to check 458 * 459 * return true if there is a new generation of VMIDs being used 460 * 461 * The hardware supports a limited set of values with the value zero reserved 462 * for the host, so we check if an assigned value belongs to a previous 463 * generation, which requires us to assign a new value. If we're the first to 464 * use a VMID for the new generation, we must flush necessary caches and TLBs 465 * on all CPUs. 466 */ 467 static bool need_new_vmid_gen(struct kvm_vmid *vmid) 468 { 469 u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen); 470 smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */ 471 return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen); 472 } 473 474 /** 475 * update_vmid - Update the vmid with a valid VMID for the current generation 476 * @kvm: The guest that struct vmid belongs to 477 * @vmid: The stage-2 VMID information struct 478 */ 479 static void update_vmid(struct kvm_vmid *vmid) 480 { 481 if (!need_new_vmid_gen(vmid)) 482 return; 483 484 spin_lock(&kvm_vmid_lock); 485 486 /* 487 * We need to re-check the vmid_gen here to ensure that if another vcpu 488 * already allocated a valid vmid for this vm, then this vcpu should 489 * use the same vmid. 490 */ 491 if (!need_new_vmid_gen(vmid)) { 492 spin_unlock(&kvm_vmid_lock); 493 return; 494 } 495 496 /* First user of a new VMID generation? */ 497 if (unlikely(kvm_next_vmid == 0)) { 498 atomic64_inc(&kvm_vmid_gen); 499 kvm_next_vmid = 1; 500 501 /* 502 * On SMP we know no other CPUs can use this CPU's or each 503 * other's VMID after force_vm_exit returns since the 504 * kvm_vmid_lock blocks them from reentry to the guest. 505 */ 506 force_vm_exit(cpu_all_mask); 507 /* 508 * Now broadcast TLB + ICACHE invalidation over the inner 509 * shareable domain to make sure all data structures are 510 * clean. 511 */ 512 kvm_call_hyp(__kvm_flush_vm_context); 513 } 514 515 vmid->vmid = kvm_next_vmid; 516 kvm_next_vmid++; 517 kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1; 518 519 smp_wmb(); 520 WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen)); 521 522 spin_unlock(&kvm_vmid_lock); 523 } 524 525 static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) 526 { 527 struct kvm *kvm = vcpu->kvm; 528 int ret = 0; 529 530 if (likely(vcpu->arch.has_run_once)) 531 return 0; 532 533 if (!kvm_arm_vcpu_is_finalized(vcpu)) 534 return -EPERM; 535 536 vcpu->arch.has_run_once = true; 537 538 if (likely(irqchip_in_kernel(kvm))) { 539 /* 540 * Map the VGIC hardware resources before running a vcpu the 541 * first time on this VM. 542 */ 543 if (unlikely(!vgic_ready(kvm))) { 544 ret = kvm_vgic_map_resources(kvm); 545 if (ret) 546 return ret; 547 } 548 } else { 549 /* 550 * Tell the rest of the code that there are userspace irqchip 551 * VMs in the wild. 552 */ 553 static_branch_inc(&userspace_irqchip_in_use); 554 } 555 556 ret = kvm_timer_enable(vcpu); 557 if (ret) 558 return ret; 559 560 ret = kvm_arm_pmu_v3_enable(vcpu); 561 562 return ret; 563 } 564 565 bool kvm_arch_intc_initialized(struct kvm *kvm) 566 { 567 return vgic_initialized(kvm); 568 } 569 570 void kvm_arm_halt_guest(struct kvm *kvm) 571 { 572 int i; 573 struct kvm_vcpu *vcpu; 574 575 kvm_for_each_vcpu(i, vcpu, kvm) 576 vcpu->arch.pause = true; 577 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP); 578 } 579 580 void kvm_arm_resume_guest(struct kvm *kvm) 581 { 582 int i; 583 struct kvm_vcpu *vcpu; 584 585 kvm_for_each_vcpu(i, vcpu, kvm) { 586 vcpu->arch.pause = false; 587 rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu)); 588 } 589 } 590 591 static void vcpu_req_sleep(struct kvm_vcpu *vcpu) 592 { 593 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); 594 595 rcuwait_wait_event(wait, 596 (!vcpu->arch.power_off) &&(!vcpu->arch.pause), 597 TASK_INTERRUPTIBLE); 598 599 if (vcpu->arch.power_off || vcpu->arch.pause) { 600 /* Awaken to handle a signal, request we sleep again later. */ 601 kvm_make_request(KVM_REQ_SLEEP, vcpu); 602 } 603 604 /* 605 * Make sure we will observe a potential reset request if we've 606 * observed a change to the power state. Pairs with the smp_wmb() in 607 * kvm_psci_vcpu_on(). 608 */ 609 smp_rmb(); 610 } 611 612 static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) 613 { 614 return vcpu->arch.target >= 0; 615 } 616 617 static void check_vcpu_requests(struct kvm_vcpu *vcpu) 618 { 619 if (kvm_request_pending(vcpu)) { 620 if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) 621 vcpu_req_sleep(vcpu); 622 623 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) 624 kvm_reset_vcpu(vcpu); 625 626 /* 627 * Clear IRQ_PENDING requests that were made to guarantee 628 * that a VCPU sees new virtual interrupts. 629 */ 630 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu); 631 632 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu)) 633 kvm_update_stolen_time(vcpu); 634 635 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) { 636 /* The distributor enable bits were changed */ 637 preempt_disable(); 638 vgic_v4_put(vcpu, false); 639 vgic_v4_load(vcpu); 640 preempt_enable(); 641 } 642 } 643 } 644 645 /** 646 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code 647 * @vcpu: The VCPU pointer 648 * 649 * This function is called through the VCPU_RUN ioctl called from user space. It 650 * will execute VM code in a loop until the time slice for the process is used 651 * or some emulation is needed from user space in which case the function will 652 * return with return value 0 and with the kvm_run structure filled in with the 653 * required data for the requested emulation. 654 */ 655 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 656 { 657 struct kvm_run *run = vcpu->run; 658 int ret; 659 660 if (unlikely(!kvm_vcpu_initialized(vcpu))) 661 return -ENOEXEC; 662 663 ret = kvm_vcpu_first_run_init(vcpu); 664 if (ret) 665 return ret; 666 667 if (run->exit_reason == KVM_EXIT_MMIO) { 668 ret = kvm_handle_mmio_return(vcpu, run); 669 if (ret) 670 return ret; 671 } 672 673 if (run->immediate_exit) 674 return -EINTR; 675 676 vcpu_load(vcpu); 677 678 kvm_sigset_activate(vcpu); 679 680 ret = 1; 681 run->exit_reason = KVM_EXIT_UNKNOWN; 682 while (ret > 0) { 683 /* 684 * Check conditions before entering the guest 685 */ 686 cond_resched(); 687 688 update_vmid(&vcpu->kvm->arch.vmid); 689 690 check_vcpu_requests(vcpu); 691 692 /* 693 * Preparing the interrupts to be injected also 694 * involves poking the GIC, which must be done in a 695 * non-preemptible context. 696 */ 697 preempt_disable(); 698 699 kvm_pmu_flush_hwstate(vcpu); 700 701 local_irq_disable(); 702 703 kvm_vgic_flush_hwstate(vcpu); 704 705 /* 706 * Exit if we have a signal pending so that we can deliver the 707 * signal to user space. 708 */ 709 if (signal_pending(current)) { 710 ret = -EINTR; 711 run->exit_reason = KVM_EXIT_INTR; 712 } 713 714 /* 715 * If we're using a userspace irqchip, then check if we need 716 * to tell a userspace irqchip about timer or PMU level 717 * changes and if so, exit to userspace (the actual level 718 * state gets updated in kvm_timer_update_run and 719 * kvm_pmu_update_run below). 720 */ 721 if (static_branch_unlikely(&userspace_irqchip_in_use)) { 722 if (kvm_timer_should_notify_user(vcpu) || 723 kvm_pmu_should_notify_user(vcpu)) { 724 ret = -EINTR; 725 run->exit_reason = KVM_EXIT_INTR; 726 } 727 } 728 729 /* 730 * Ensure we set mode to IN_GUEST_MODE after we disable 731 * interrupts and before the final VCPU requests check. 732 * See the comment in kvm_vcpu_exiting_guest_mode() and 733 * Documentation/virt/kvm/vcpu-requests.rst 734 */ 735 smp_store_mb(vcpu->mode, IN_GUEST_MODE); 736 737 if (ret <= 0 || need_new_vmid_gen(&vcpu->kvm->arch.vmid) || 738 kvm_request_pending(vcpu)) { 739 vcpu->mode = OUTSIDE_GUEST_MODE; 740 isb(); /* Ensure work in x_flush_hwstate is committed */ 741 kvm_pmu_sync_hwstate(vcpu); 742 if (static_branch_unlikely(&userspace_irqchip_in_use)) 743 kvm_timer_sync_hwstate(vcpu); 744 kvm_vgic_sync_hwstate(vcpu); 745 local_irq_enable(); 746 preempt_enable(); 747 continue; 748 } 749 750 kvm_arm_setup_debug(vcpu); 751 752 /************************************************************** 753 * Enter the guest 754 */ 755 trace_kvm_entry(*vcpu_pc(vcpu)); 756 guest_enter_irqoff(); 757 758 if (has_vhe()) { 759 ret = kvm_vcpu_run_vhe(vcpu); 760 } else { 761 ret = kvm_call_hyp_ret(__kvm_vcpu_run_nvhe, vcpu); 762 } 763 764 vcpu->mode = OUTSIDE_GUEST_MODE; 765 vcpu->stat.exits++; 766 /* 767 * Back from guest 768 *************************************************************/ 769 770 kvm_arm_clear_debug(vcpu); 771 772 /* 773 * We must sync the PMU state before the vgic state so 774 * that the vgic can properly sample the updated state of the 775 * interrupt line. 776 */ 777 kvm_pmu_sync_hwstate(vcpu); 778 779 /* 780 * Sync the vgic state before syncing the timer state because 781 * the timer code needs to know if the virtual timer 782 * interrupts are active. 783 */ 784 kvm_vgic_sync_hwstate(vcpu); 785 786 /* 787 * Sync the timer hardware state before enabling interrupts as 788 * we don't want vtimer interrupts to race with syncing the 789 * timer virtual interrupt state. 790 */ 791 if (static_branch_unlikely(&userspace_irqchip_in_use)) 792 kvm_timer_sync_hwstate(vcpu); 793 794 kvm_arch_vcpu_ctxsync_fp(vcpu); 795 796 /* 797 * We may have taken a host interrupt in HYP mode (ie 798 * while executing the guest). This interrupt is still 799 * pending, as we haven't serviced it yet! 800 * 801 * We're now back in SVC mode, with interrupts 802 * disabled. Enabling the interrupts now will have 803 * the effect of taking the interrupt again, in SVC 804 * mode this time. 805 */ 806 local_irq_enable(); 807 808 /* 809 * We do local_irq_enable() before calling guest_exit() so 810 * that if a timer interrupt hits while running the guest we 811 * account that tick as being spent in the guest. We enable 812 * preemption after calling guest_exit() so that if we get 813 * preempted we make sure ticks after that is not counted as 814 * guest time. 815 */ 816 guest_exit(); 817 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu)); 818 819 /* Exit types that need handling before we can be preempted */ 820 handle_exit_early(vcpu, run, ret); 821 822 preempt_enable(); 823 824 ret = handle_exit(vcpu, run, ret); 825 } 826 827 /* Tell userspace about in-kernel device output levels */ 828 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { 829 kvm_timer_update_run(vcpu); 830 kvm_pmu_update_run(vcpu); 831 } 832 833 kvm_sigset_deactivate(vcpu); 834 835 vcpu_put(vcpu); 836 return ret; 837 } 838 839 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) 840 { 841 int bit_index; 842 bool set; 843 unsigned long *hcr; 844 845 if (number == KVM_ARM_IRQ_CPU_IRQ) 846 bit_index = __ffs(HCR_VI); 847 else /* KVM_ARM_IRQ_CPU_FIQ */ 848 bit_index = __ffs(HCR_VF); 849 850 hcr = vcpu_hcr(vcpu); 851 if (level) 852 set = test_and_set_bit(bit_index, hcr); 853 else 854 set = test_and_clear_bit(bit_index, hcr); 855 856 /* 857 * If we didn't change anything, no need to wake up or kick other CPUs 858 */ 859 if (set == level) 860 return 0; 861 862 /* 863 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and 864 * trigger a world-switch round on the running physical CPU to set the 865 * virtual IRQ/FIQ fields in the HCR appropriately. 866 */ 867 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); 868 kvm_vcpu_kick(vcpu); 869 870 return 0; 871 } 872 873 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, 874 bool line_status) 875 { 876 u32 irq = irq_level->irq; 877 unsigned int irq_type, vcpu_idx, irq_num; 878 int nrcpus = atomic_read(&kvm->online_vcpus); 879 struct kvm_vcpu *vcpu = NULL; 880 bool level = irq_level->level; 881 882 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; 883 vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; 884 vcpu_idx += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1); 885 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; 886 887 trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level); 888 889 switch (irq_type) { 890 case KVM_ARM_IRQ_TYPE_CPU: 891 if (irqchip_in_kernel(kvm)) 892 return -ENXIO; 893 894 if (vcpu_idx >= nrcpus) 895 return -EINVAL; 896 897 vcpu = kvm_get_vcpu(kvm, vcpu_idx); 898 if (!vcpu) 899 return -EINVAL; 900 901 if (irq_num > KVM_ARM_IRQ_CPU_FIQ) 902 return -EINVAL; 903 904 return vcpu_interrupt_line(vcpu, irq_num, level); 905 case KVM_ARM_IRQ_TYPE_PPI: 906 if (!irqchip_in_kernel(kvm)) 907 return -ENXIO; 908 909 if (vcpu_idx >= nrcpus) 910 return -EINVAL; 911 912 vcpu = kvm_get_vcpu(kvm, vcpu_idx); 913 if (!vcpu) 914 return -EINVAL; 915 916 if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS) 917 return -EINVAL; 918 919 return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL); 920 case KVM_ARM_IRQ_TYPE_SPI: 921 if (!irqchip_in_kernel(kvm)) 922 return -ENXIO; 923 924 if (irq_num < VGIC_NR_PRIVATE_IRQS) 925 return -EINVAL; 926 927 return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL); 928 } 929 930 return -EINVAL; 931 } 932 933 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 934 const struct kvm_vcpu_init *init) 935 { 936 unsigned int i, ret; 937 int phys_target = kvm_target_cpu(); 938 939 if (init->target != phys_target) 940 return -EINVAL; 941 942 /* 943 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must 944 * use the same target. 945 */ 946 if (vcpu->arch.target != -1 && vcpu->arch.target != init->target) 947 return -EINVAL; 948 949 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */ 950 for (i = 0; i < sizeof(init->features) * 8; i++) { 951 bool set = (init->features[i / 32] & (1 << (i % 32))); 952 953 if (set && i >= KVM_VCPU_MAX_FEATURES) 954 return -ENOENT; 955 956 /* 957 * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must 958 * use the same feature set. 959 */ 960 if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES && 961 test_bit(i, vcpu->arch.features) != set) 962 return -EINVAL; 963 964 if (set) 965 set_bit(i, vcpu->arch.features); 966 } 967 968 vcpu->arch.target = phys_target; 969 970 /* Now we know what it is, we can reset it. */ 971 ret = kvm_reset_vcpu(vcpu); 972 if (ret) { 973 vcpu->arch.target = -1; 974 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); 975 } 976 977 return ret; 978 } 979 980 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, 981 struct kvm_vcpu_init *init) 982 { 983 int ret; 984 985 ret = kvm_vcpu_set_target(vcpu, init); 986 if (ret) 987 return ret; 988 989 /* 990 * Ensure a rebooted VM will fault in RAM pages and detect if the 991 * guest MMU is turned off and flush the caches as needed. 992 * 993 * S2FWB enforces all memory accesses to RAM being cacheable, we 994 * ensure that the cache is always coherent. 995 */ 996 if (vcpu->arch.has_run_once && !cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) 997 stage2_unmap_vm(vcpu->kvm); 998 999 vcpu_reset_hcr(vcpu); 1000 1001 /* 1002 * Handle the "start in power-off" case. 1003 */ 1004 if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) 1005 vcpu_power_off(vcpu); 1006 else 1007 vcpu->arch.power_off = false; 1008 1009 return 0; 1010 } 1011 1012 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, 1013 struct kvm_device_attr *attr) 1014 { 1015 int ret = -ENXIO; 1016 1017 switch (attr->group) { 1018 default: 1019 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr); 1020 break; 1021 } 1022 1023 return ret; 1024 } 1025 1026 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, 1027 struct kvm_device_attr *attr) 1028 { 1029 int ret = -ENXIO; 1030 1031 switch (attr->group) { 1032 default: 1033 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr); 1034 break; 1035 } 1036 1037 return ret; 1038 } 1039 1040 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, 1041 struct kvm_device_attr *attr) 1042 { 1043 int ret = -ENXIO; 1044 1045 switch (attr->group) { 1046 default: 1047 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr); 1048 break; 1049 } 1050 1051 return ret; 1052 } 1053 1054 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, 1055 struct kvm_vcpu_events *events) 1056 { 1057 memset(events, 0, sizeof(*events)); 1058 1059 return __kvm_arm_vcpu_get_events(vcpu, events); 1060 } 1061 1062 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, 1063 struct kvm_vcpu_events *events) 1064 { 1065 int i; 1066 1067 /* check whether the reserved field is zero */ 1068 for (i = 0; i < ARRAY_SIZE(events->reserved); i++) 1069 if (events->reserved[i]) 1070 return -EINVAL; 1071 1072 /* check whether the pad field is zero */ 1073 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++) 1074 if (events->exception.pad[i]) 1075 return -EINVAL; 1076 1077 return __kvm_arm_vcpu_set_events(vcpu, events); 1078 } 1079 1080 long kvm_arch_vcpu_ioctl(struct file *filp, 1081 unsigned int ioctl, unsigned long arg) 1082 { 1083 struct kvm_vcpu *vcpu = filp->private_data; 1084 void __user *argp = (void __user *)arg; 1085 struct kvm_device_attr attr; 1086 long r; 1087 1088 switch (ioctl) { 1089 case KVM_ARM_VCPU_INIT: { 1090 struct kvm_vcpu_init init; 1091 1092 r = -EFAULT; 1093 if (copy_from_user(&init, argp, sizeof(init))) 1094 break; 1095 1096 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); 1097 break; 1098 } 1099 case KVM_SET_ONE_REG: 1100 case KVM_GET_ONE_REG: { 1101 struct kvm_one_reg reg; 1102 1103 r = -ENOEXEC; 1104 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1105 break; 1106 1107 r = -EFAULT; 1108 if (copy_from_user(®, argp, sizeof(reg))) 1109 break; 1110 1111 if (ioctl == KVM_SET_ONE_REG) 1112 r = kvm_arm_set_reg(vcpu, ®); 1113 else 1114 r = kvm_arm_get_reg(vcpu, ®); 1115 break; 1116 } 1117 case KVM_GET_REG_LIST: { 1118 struct kvm_reg_list __user *user_list = argp; 1119 struct kvm_reg_list reg_list; 1120 unsigned n; 1121 1122 r = -ENOEXEC; 1123 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1124 break; 1125 1126 r = -EPERM; 1127 if (!kvm_arm_vcpu_is_finalized(vcpu)) 1128 break; 1129 1130 r = -EFAULT; 1131 if (copy_from_user(®_list, user_list, sizeof(reg_list))) 1132 break; 1133 n = reg_list.n; 1134 reg_list.n = kvm_arm_num_regs(vcpu); 1135 if (copy_to_user(user_list, ®_list, sizeof(reg_list))) 1136 break; 1137 r = -E2BIG; 1138 if (n < reg_list.n) 1139 break; 1140 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg); 1141 break; 1142 } 1143 case KVM_SET_DEVICE_ATTR: { 1144 r = -EFAULT; 1145 if (copy_from_user(&attr, argp, sizeof(attr))) 1146 break; 1147 r = kvm_arm_vcpu_set_attr(vcpu, &attr); 1148 break; 1149 } 1150 case KVM_GET_DEVICE_ATTR: { 1151 r = -EFAULT; 1152 if (copy_from_user(&attr, argp, sizeof(attr))) 1153 break; 1154 r = kvm_arm_vcpu_get_attr(vcpu, &attr); 1155 break; 1156 } 1157 case KVM_HAS_DEVICE_ATTR: { 1158 r = -EFAULT; 1159 if (copy_from_user(&attr, argp, sizeof(attr))) 1160 break; 1161 r = kvm_arm_vcpu_has_attr(vcpu, &attr); 1162 break; 1163 } 1164 case KVM_GET_VCPU_EVENTS: { 1165 struct kvm_vcpu_events events; 1166 1167 if (kvm_arm_vcpu_get_events(vcpu, &events)) 1168 return -EINVAL; 1169 1170 if (copy_to_user(argp, &events, sizeof(events))) 1171 return -EFAULT; 1172 1173 return 0; 1174 } 1175 case KVM_SET_VCPU_EVENTS: { 1176 struct kvm_vcpu_events events; 1177 1178 if (copy_from_user(&events, argp, sizeof(events))) 1179 return -EFAULT; 1180 1181 return kvm_arm_vcpu_set_events(vcpu, &events); 1182 } 1183 case KVM_ARM_VCPU_FINALIZE: { 1184 int what; 1185 1186 if (!kvm_vcpu_initialized(vcpu)) 1187 return -ENOEXEC; 1188 1189 if (get_user(what, (const int __user *)argp)) 1190 return -EFAULT; 1191 1192 return kvm_arm_vcpu_finalize(vcpu, what); 1193 } 1194 default: 1195 r = -EINVAL; 1196 } 1197 1198 return r; 1199 } 1200 1201 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 1202 { 1203 1204 } 1205 1206 void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm, 1207 struct kvm_memory_slot *memslot) 1208 { 1209 kvm_flush_remote_tlbs(kvm); 1210 } 1211 1212 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, 1213 struct kvm_arm_device_addr *dev_addr) 1214 { 1215 unsigned long dev_id, type; 1216 1217 dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >> 1218 KVM_ARM_DEVICE_ID_SHIFT; 1219 type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >> 1220 KVM_ARM_DEVICE_TYPE_SHIFT; 1221 1222 switch (dev_id) { 1223 case KVM_ARM_DEVICE_VGIC_V2: 1224 if (!vgic_present) 1225 return -ENXIO; 1226 return kvm_vgic_addr(kvm, type, &dev_addr->addr, true); 1227 default: 1228 return -ENODEV; 1229 } 1230 } 1231 1232 long kvm_arch_vm_ioctl(struct file *filp, 1233 unsigned int ioctl, unsigned long arg) 1234 { 1235 struct kvm *kvm = filp->private_data; 1236 void __user *argp = (void __user *)arg; 1237 1238 switch (ioctl) { 1239 case KVM_CREATE_IRQCHIP: { 1240 int ret; 1241 if (!vgic_present) 1242 return -ENXIO; 1243 mutex_lock(&kvm->lock); 1244 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); 1245 mutex_unlock(&kvm->lock); 1246 return ret; 1247 } 1248 case KVM_ARM_SET_DEVICE_ADDR: { 1249 struct kvm_arm_device_addr dev_addr; 1250 1251 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) 1252 return -EFAULT; 1253 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); 1254 } 1255 case KVM_ARM_PREFERRED_TARGET: { 1256 int err; 1257 struct kvm_vcpu_init init; 1258 1259 err = kvm_vcpu_preferred_target(&init); 1260 if (err) 1261 return err; 1262 1263 if (copy_to_user(argp, &init, sizeof(init))) 1264 return -EFAULT; 1265 1266 return 0; 1267 } 1268 default: 1269 return -EINVAL; 1270 } 1271 } 1272 1273 static void cpu_init_hyp_mode(void) 1274 { 1275 phys_addr_t pgd_ptr; 1276 unsigned long hyp_stack_ptr; 1277 unsigned long vector_ptr; 1278 unsigned long tpidr_el2; 1279 1280 /* Switch from the HYP stub to our own HYP init vector */ 1281 __hyp_set_vectors(kvm_get_idmap_vector()); 1282 1283 /* 1284 * Calculate the raw per-cpu offset without a translation from the 1285 * kernel's mapping to the linear mapping, and store it in tpidr_el2 1286 * so that we can use adr_l to access per-cpu variables in EL2. 1287 */ 1288 tpidr_el2 = ((unsigned long)this_cpu_ptr(&kvm_host_data) - 1289 (unsigned long)kvm_ksym_ref(kvm_host_data)); 1290 1291 pgd_ptr = kvm_mmu_get_httbr(); 1292 hyp_stack_ptr = __this_cpu_read(kvm_arm_hyp_stack_page) + PAGE_SIZE; 1293 vector_ptr = (unsigned long)kvm_get_hyp_vector(); 1294 1295 /* 1296 * Call initialization code, and switch to the full blown HYP code. 1297 * If the cpucaps haven't been finalized yet, something has gone very 1298 * wrong, and hyp will crash and burn when it uses any 1299 * cpus_have_const_cap() wrapper. 1300 */ 1301 BUG_ON(!system_capabilities_finalized()); 1302 __kvm_call_hyp((void *)pgd_ptr, hyp_stack_ptr, vector_ptr, tpidr_el2); 1303 1304 /* 1305 * Disabling SSBD on a non-VHE system requires us to enable SSBS 1306 * at EL2. 1307 */ 1308 if (this_cpu_has_cap(ARM64_SSBS) && 1309 arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) { 1310 kvm_call_hyp(__kvm_enable_ssbs); 1311 } 1312 } 1313 1314 static void cpu_hyp_reset(void) 1315 { 1316 if (!is_kernel_in_hyp_mode()) 1317 __hyp_reset_vectors(); 1318 } 1319 1320 static void cpu_hyp_reinit(void) 1321 { 1322 kvm_init_host_cpu_context(&this_cpu_ptr(&kvm_host_data)->host_ctxt); 1323 1324 cpu_hyp_reset(); 1325 1326 if (is_kernel_in_hyp_mode()) 1327 kvm_timer_init_vhe(); 1328 else 1329 cpu_init_hyp_mode(); 1330 1331 kvm_arm_init_debug(); 1332 1333 if (vgic_present) 1334 kvm_vgic_init_cpu_hardware(); 1335 } 1336 1337 static void _kvm_arch_hardware_enable(void *discard) 1338 { 1339 if (!__this_cpu_read(kvm_arm_hardware_enabled)) { 1340 cpu_hyp_reinit(); 1341 __this_cpu_write(kvm_arm_hardware_enabled, 1); 1342 } 1343 } 1344 1345 int kvm_arch_hardware_enable(void) 1346 { 1347 _kvm_arch_hardware_enable(NULL); 1348 return 0; 1349 } 1350 1351 static void _kvm_arch_hardware_disable(void *discard) 1352 { 1353 if (__this_cpu_read(kvm_arm_hardware_enabled)) { 1354 cpu_hyp_reset(); 1355 __this_cpu_write(kvm_arm_hardware_enabled, 0); 1356 } 1357 } 1358 1359 void kvm_arch_hardware_disable(void) 1360 { 1361 _kvm_arch_hardware_disable(NULL); 1362 } 1363 1364 #ifdef CONFIG_CPU_PM 1365 static int hyp_init_cpu_pm_notifier(struct notifier_block *self, 1366 unsigned long cmd, 1367 void *v) 1368 { 1369 /* 1370 * kvm_arm_hardware_enabled is left with its old value over 1371 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should 1372 * re-enable hyp. 1373 */ 1374 switch (cmd) { 1375 case CPU_PM_ENTER: 1376 if (__this_cpu_read(kvm_arm_hardware_enabled)) 1377 /* 1378 * don't update kvm_arm_hardware_enabled here 1379 * so that the hardware will be re-enabled 1380 * when we resume. See below. 1381 */ 1382 cpu_hyp_reset(); 1383 1384 return NOTIFY_OK; 1385 case CPU_PM_ENTER_FAILED: 1386 case CPU_PM_EXIT: 1387 if (__this_cpu_read(kvm_arm_hardware_enabled)) 1388 /* The hardware was enabled before suspend. */ 1389 cpu_hyp_reinit(); 1390 1391 return NOTIFY_OK; 1392 1393 default: 1394 return NOTIFY_DONE; 1395 } 1396 } 1397 1398 static struct notifier_block hyp_init_cpu_pm_nb = { 1399 .notifier_call = hyp_init_cpu_pm_notifier, 1400 }; 1401 1402 static void __init hyp_cpu_pm_init(void) 1403 { 1404 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); 1405 } 1406 static void __init hyp_cpu_pm_exit(void) 1407 { 1408 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); 1409 } 1410 #else 1411 static inline void hyp_cpu_pm_init(void) 1412 { 1413 } 1414 static inline void hyp_cpu_pm_exit(void) 1415 { 1416 } 1417 #endif 1418 1419 static int init_common_resources(void) 1420 { 1421 return kvm_set_ipa_limit(); 1422 } 1423 1424 static int init_subsystems(void) 1425 { 1426 int err = 0; 1427 1428 /* 1429 * Enable hardware so that subsystem initialisation can access EL2. 1430 */ 1431 on_each_cpu(_kvm_arch_hardware_enable, NULL, 1); 1432 1433 /* 1434 * Register CPU lower-power notifier 1435 */ 1436 hyp_cpu_pm_init(); 1437 1438 /* 1439 * Init HYP view of VGIC 1440 */ 1441 err = kvm_vgic_hyp_init(); 1442 switch (err) { 1443 case 0: 1444 vgic_present = true; 1445 break; 1446 case -ENODEV: 1447 case -ENXIO: 1448 vgic_present = false; 1449 err = 0; 1450 break; 1451 default: 1452 goto out; 1453 } 1454 1455 /* 1456 * Init HYP architected timer support 1457 */ 1458 err = kvm_timer_hyp_init(vgic_present); 1459 if (err) 1460 goto out; 1461 1462 kvm_perf_init(); 1463 kvm_coproc_table_init(); 1464 1465 out: 1466 on_each_cpu(_kvm_arch_hardware_disable, NULL, 1); 1467 1468 return err; 1469 } 1470 1471 static void teardown_hyp_mode(void) 1472 { 1473 int cpu; 1474 1475 free_hyp_pgds(); 1476 for_each_possible_cpu(cpu) 1477 free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); 1478 } 1479 1480 /** 1481 * Inits Hyp-mode on all online CPUs 1482 */ 1483 static int init_hyp_mode(void) 1484 { 1485 int cpu; 1486 int err = 0; 1487 1488 /* 1489 * Allocate Hyp PGD and setup Hyp identity mapping 1490 */ 1491 err = kvm_mmu_init(); 1492 if (err) 1493 goto out_err; 1494 1495 /* 1496 * Allocate stack pages for Hypervisor-mode 1497 */ 1498 for_each_possible_cpu(cpu) { 1499 unsigned long stack_page; 1500 1501 stack_page = __get_free_page(GFP_KERNEL); 1502 if (!stack_page) { 1503 err = -ENOMEM; 1504 goto out_err; 1505 } 1506 1507 per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; 1508 } 1509 1510 /* 1511 * Map the Hyp-code called directly from the host 1512 */ 1513 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), 1514 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); 1515 if (err) { 1516 kvm_err("Cannot map world-switch code\n"); 1517 goto out_err; 1518 } 1519 1520 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), 1521 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); 1522 if (err) { 1523 kvm_err("Cannot map rodata section\n"); 1524 goto out_err; 1525 } 1526 1527 err = create_hyp_mappings(kvm_ksym_ref(__bss_start), 1528 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); 1529 if (err) { 1530 kvm_err("Cannot map bss section\n"); 1531 goto out_err; 1532 } 1533 1534 err = kvm_map_vectors(); 1535 if (err) { 1536 kvm_err("Cannot map vectors\n"); 1537 goto out_err; 1538 } 1539 1540 /* 1541 * Map the Hyp stack pages 1542 */ 1543 for_each_possible_cpu(cpu) { 1544 char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); 1545 err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE, 1546 PAGE_HYP); 1547 1548 if (err) { 1549 kvm_err("Cannot map hyp stack\n"); 1550 goto out_err; 1551 } 1552 } 1553 1554 for_each_possible_cpu(cpu) { 1555 kvm_host_data_t *cpu_data; 1556 1557 cpu_data = per_cpu_ptr(&kvm_host_data, cpu); 1558 err = create_hyp_mappings(cpu_data, cpu_data + 1, PAGE_HYP); 1559 1560 if (err) { 1561 kvm_err("Cannot map host CPU state: %d\n", err); 1562 goto out_err; 1563 } 1564 } 1565 1566 err = hyp_map_aux_data(); 1567 if (err) 1568 kvm_err("Cannot map host auxiliary data: %d\n", err); 1569 1570 return 0; 1571 1572 out_err: 1573 teardown_hyp_mode(); 1574 kvm_err("error initializing Hyp mode: %d\n", err); 1575 return err; 1576 } 1577 1578 static void check_kvm_target_cpu(void *ret) 1579 { 1580 *(int *)ret = kvm_target_cpu(); 1581 } 1582 1583 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) 1584 { 1585 struct kvm_vcpu *vcpu; 1586 int i; 1587 1588 mpidr &= MPIDR_HWID_BITMASK; 1589 kvm_for_each_vcpu(i, vcpu, kvm) { 1590 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) 1591 return vcpu; 1592 } 1593 return NULL; 1594 } 1595 1596 bool kvm_arch_has_irq_bypass(void) 1597 { 1598 return true; 1599 } 1600 1601 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, 1602 struct irq_bypass_producer *prod) 1603 { 1604 struct kvm_kernel_irqfd *irqfd = 1605 container_of(cons, struct kvm_kernel_irqfd, consumer); 1606 1607 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, 1608 &irqfd->irq_entry); 1609 } 1610 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, 1611 struct irq_bypass_producer *prod) 1612 { 1613 struct kvm_kernel_irqfd *irqfd = 1614 container_of(cons, struct kvm_kernel_irqfd, consumer); 1615 1616 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq, 1617 &irqfd->irq_entry); 1618 } 1619 1620 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) 1621 { 1622 struct kvm_kernel_irqfd *irqfd = 1623 container_of(cons, struct kvm_kernel_irqfd, consumer); 1624 1625 kvm_arm_halt_guest(irqfd->kvm); 1626 } 1627 1628 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) 1629 { 1630 struct kvm_kernel_irqfd *irqfd = 1631 container_of(cons, struct kvm_kernel_irqfd, consumer); 1632 1633 kvm_arm_resume_guest(irqfd->kvm); 1634 } 1635 1636 /** 1637 * Initialize Hyp-mode and memory mappings on all CPUs. 1638 */ 1639 int kvm_arch_init(void *opaque) 1640 { 1641 int err; 1642 int ret, cpu; 1643 bool in_hyp_mode; 1644 1645 if (!is_hyp_mode_available()) { 1646 kvm_info("HYP mode not available\n"); 1647 return -ENODEV; 1648 } 1649 1650 in_hyp_mode = is_kernel_in_hyp_mode(); 1651 1652 if (!in_hyp_mode && kvm_arch_requires_vhe()) { 1653 kvm_pr_unimpl("CPU unsupported in non-VHE mode, not initializing\n"); 1654 return -ENODEV; 1655 } 1656 1657 for_each_online_cpu(cpu) { 1658 smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1); 1659 if (ret < 0) { 1660 kvm_err("Error, CPU %d not supported!\n", cpu); 1661 return -ENODEV; 1662 } 1663 } 1664 1665 err = init_common_resources(); 1666 if (err) 1667 return err; 1668 1669 err = kvm_arm_init_sve(); 1670 if (err) 1671 return err; 1672 1673 if (!in_hyp_mode) { 1674 err = init_hyp_mode(); 1675 if (err) 1676 goto out_err; 1677 } 1678 1679 err = init_subsystems(); 1680 if (err) 1681 goto out_hyp; 1682 1683 if (in_hyp_mode) 1684 kvm_info("VHE mode initialized successfully\n"); 1685 else 1686 kvm_info("Hyp mode initialized successfully\n"); 1687 1688 return 0; 1689 1690 out_hyp: 1691 hyp_cpu_pm_exit(); 1692 if (!in_hyp_mode) 1693 teardown_hyp_mode(); 1694 out_err: 1695 return err; 1696 } 1697 1698 /* NOP: Compiling as a module not supported */ 1699 void kvm_arch_exit(void) 1700 { 1701 kvm_perf_teardown(); 1702 } 1703 1704 static int arm_init(void) 1705 { 1706 int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); 1707 return rc; 1708 } 1709 1710 module_init(arm_init); 1711