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/arm-smccc.h> 8 #include <linux/bug.h> 9 #include <linux/cpu_pm.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/kvm.h> 20 #include <linux/kvm_irqfd.h> 21 #include <linux/irqbypass.h> 22 #include <linux/sched/stat.h> 23 #include <linux/psci.h> 24 #include <trace/events/kvm.h> 25 26 #define CREATE_TRACE_POINTS 27 #include "trace_arm.h" 28 #include "hyp_trace.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_emulate.h> 40 #include <asm/kvm_hyp.h> 41 #include <asm/kvm_mmu.h> 42 #include <asm/kvm_nested.h> 43 #include <asm/kvm_pkvm.h> 44 #include <asm/kvm_ptrauth.h> 45 #include <asm/sections.h> 46 #include <asm/stacktrace/nvhe.h> 47 48 #include <kvm/arm_hypercalls.h> 49 #include <kvm/arm_pmu.h> 50 #include <kvm/arm_psci.h> 51 #include <kvm/arm_vgic.h> 52 53 #include <linux/irqchip/arm-gic-v5.h> 54 55 #include "vgic/vgic.h" 56 #include "sys_regs.h" 57 58 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT; 59 60 enum kvm_wfx_trap_policy { 61 KVM_WFX_NOTRAP_SINGLE_TASK, /* Default option */ 62 KVM_WFX_NOTRAP, 63 KVM_WFX_TRAP, 64 }; 65 66 static enum kvm_wfx_trap_policy kvm_wfi_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK; 67 static enum kvm_wfx_trap_policy kvm_wfe_trap_policy __read_mostly = KVM_WFX_NOTRAP_SINGLE_TASK; 68 69 /* 70 * Tracks KVM IOCTLs and their associated KVM capabilities. 71 */ 72 struct kvm_ioctl_cap_map { 73 unsigned int ioctl; 74 long ext; 75 }; 76 77 /* Make KVM_CAP_NR_VCPUS the reference for features we always supported */ 78 #define KVM_CAP_ARM_BASIC KVM_CAP_NR_VCPUS 79 80 /* 81 * Sorted by ioctl to allow for potential binary search, 82 * though linear scan is sufficient for this size. 83 */ 84 static const struct kvm_ioctl_cap_map vm_ioctl_caps[] = { 85 { KVM_CREATE_IRQCHIP, KVM_CAP_IRQCHIP }, 86 { KVM_ARM_SET_DEVICE_ADDR, KVM_CAP_ARM_SET_DEVICE_ADDR }, 87 { KVM_ARM_MTE_COPY_TAGS, KVM_CAP_ARM_MTE }, 88 { KVM_SET_DEVICE_ATTR, KVM_CAP_DEVICE_CTRL }, 89 { KVM_GET_DEVICE_ATTR, KVM_CAP_DEVICE_CTRL }, 90 { KVM_HAS_DEVICE_ATTR, KVM_CAP_DEVICE_CTRL }, 91 { KVM_ARM_SET_COUNTER_OFFSET, KVM_CAP_COUNTER_OFFSET }, 92 { KVM_ARM_GET_REG_WRITABLE_MASKS, KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES }, 93 { KVM_ARM_PREFERRED_TARGET, KVM_CAP_ARM_BASIC }, 94 }; 95 96 /* 97 * Set *ext to the capability. 98 * Return 0 if found, or -EINVAL if no IOCTL matches. 99 */ 100 long kvm_get_cap_for_kvm_ioctl(unsigned int ioctl, long *ext) 101 { 102 int i; 103 104 for (i = 0; i < ARRAY_SIZE(vm_ioctl_caps); i++) { 105 if (vm_ioctl_caps[i].ioctl == ioctl) { 106 *ext = vm_ioctl_caps[i].ext; 107 return 0; 108 } 109 } 110 111 return -EINVAL; 112 } 113 114 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); 115 116 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_base); 117 DECLARE_KVM_NVHE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); 118 119 DECLARE_KVM_NVHE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt); 120 121 static bool vgic_present, kvm_arm_initialised; 122 123 static DEFINE_PER_CPU(unsigned char, kvm_hyp_initialized); 124 125 bool is_kvm_arm_initialised(void) 126 { 127 return kvm_arm_initialised; 128 } 129 130 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) 131 { 132 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; 133 } 134 135 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, 136 struct kvm_enable_cap *cap) 137 { 138 int r = -EINVAL; 139 140 if (cap->flags) 141 return -EINVAL; 142 143 if (is_protected_kvm_enabled() && !kvm_pkvm_ext_allowed(kvm, cap->cap)) 144 return -EINVAL; 145 146 switch (cap->cap) { 147 case KVM_CAP_ARM_NISV_TO_USER: 148 r = 0; 149 set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER, 150 &kvm->arch.flags); 151 break; 152 case KVM_CAP_ARM_MTE: { 153 struct kvm_memory_slot *memslot; 154 int bkt; 155 156 guard(mutex)(&kvm->lock); 157 if (!system_supports_mte() || kvm->created_vcpus) 158 break; 159 160 r = 0; 161 guard(mutex)(&kvm->slots_lock); 162 kvm_for_each_memslot(memslot, bkt, kvm_memslots(kvm)) { 163 if (kvm_slot_has_gmem(memslot)) { 164 r = -EINVAL; 165 break; 166 } 167 } 168 if (r == 0) 169 set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags); 170 break; 171 172 } 173 case KVM_CAP_ARM_SYSTEM_SUSPEND: 174 r = 0; 175 set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags); 176 break; 177 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE: 178 mutex_lock(&kvm->slots_lock); 179 /* 180 * To keep things simple, allow changing the chunk 181 * size only when no memory slots have been created. 182 */ 183 if (kvm_are_all_memslots_empty(kvm)) { 184 u64 new_cap = cap->args[0]; 185 186 if (!new_cap || kvm_is_block_size_supported(new_cap)) { 187 r = 0; 188 kvm->arch.mmu.split_page_chunk_size = new_cap; 189 } 190 } 191 mutex_unlock(&kvm->slots_lock); 192 break; 193 case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS: 194 mutex_lock(&kvm->lock); 195 if (!kvm->created_vcpus) { 196 r = 0; 197 set_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &kvm->arch.flags); 198 } 199 mutex_unlock(&kvm->lock); 200 break; 201 case KVM_CAP_ARM_SEA_TO_USER: 202 r = 0; 203 set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags); 204 break; 205 default: 206 break; 207 } 208 209 return r; 210 } 211 212 static int kvm_arm_default_max_vcpus(void) 213 { 214 return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; 215 } 216 217 /** 218 * kvm_arch_init_vm - initializes a VM data structure 219 * @kvm: pointer to the KVM struct 220 * @type: kvm device type 221 */ 222 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) 223 { 224 int ret; 225 226 if (type & ~KVM_VM_TYPE_ARM_MASK) 227 return -EINVAL; 228 229 mutex_init(&kvm->arch.config_lock); 230 231 #ifdef CONFIG_LOCKDEP 232 /* Clue in lockdep that the config_lock must be taken inside kvm->lock */ 233 mutex_lock(&kvm->lock); 234 mutex_lock(&kvm->arch.config_lock); 235 mutex_unlock(&kvm->arch.config_lock); 236 mutex_unlock(&kvm->lock); 237 #endif 238 239 ret = kvm_share_hyp(kvm, kvm + 1); 240 if (ret) 241 return ret; 242 243 if (!zalloc_cpumask_var(&kvm->arch.supported_cpus, GFP_KERNEL_ACCOUNT)) { 244 ret = -ENOMEM; 245 goto err_unshare_kvm; 246 } 247 cpumask_copy(kvm->arch.supported_cpus, cpu_possible_mask); 248 249 ret = kvm_init_stage2_mmu(kvm, &kvm->arch.mmu, type); 250 if (ret) 251 goto err_free_cpumask; 252 253 ret = kvm_init_nested(kvm); 254 if (ret) 255 goto err_uninit_mmu; 256 257 if (is_protected_kvm_enabled()) { 258 /* 259 * If any failures occur after this is successful, make sure to 260 * call __pkvm_unreserve_vm to unreserve the VM in hyp. 261 */ 262 ret = pkvm_init_host_vm(kvm, type); 263 if (ret) 264 goto err_uninit_mmu; 265 } else if (type & KVM_VM_TYPE_ARM_PROTECTED) { 266 ret = -EINVAL; 267 goto err_uninit_mmu; 268 } 269 270 kvm_vgic_early_init(kvm); 271 272 kvm_timer_init_vm(kvm); 273 274 /* The maximum number of VCPUs is limited by the host's GIC model */ 275 kvm->max_vcpus = kvm_arm_default_max_vcpus(); 276 277 kvm_arm_init_hypercalls(kvm); 278 279 bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES); 280 281 return 0; 282 283 err_uninit_mmu: 284 kvm_uninit_stage2_mmu(kvm); 285 kvm_destroy_nested(kvm); 286 err_free_cpumask: 287 free_cpumask_var(kvm->arch.supported_cpus); 288 err_unshare_kvm: 289 kvm_unshare_hyp(kvm, kvm + 1); 290 return ret; 291 } 292 293 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) 294 { 295 return VM_FAULT_SIGBUS; 296 } 297 298 void kvm_arch_create_vm_debugfs(struct kvm *kvm) 299 { 300 kvm_sys_regs_create_debugfs(kvm); 301 kvm_s2_ptdump_create_debugfs(kvm); 302 } 303 304 static void kvm_destroy_mpidr_data(struct kvm *kvm) 305 { 306 struct kvm_mpidr_data *data; 307 308 mutex_lock(&kvm->arch.config_lock); 309 310 data = rcu_dereference_protected(kvm->arch.mpidr_data, 311 lockdep_is_held(&kvm->arch.config_lock)); 312 if (data) { 313 rcu_assign_pointer(kvm->arch.mpidr_data, NULL); 314 synchronize_rcu(); 315 kfree(data); 316 } 317 318 mutex_unlock(&kvm->arch.config_lock); 319 } 320 321 /** 322 * kvm_arch_destroy_vm - destroy the VM data structure 323 * @kvm: pointer to the KVM struct 324 */ 325 void kvm_arch_destroy_vm(struct kvm *kvm) 326 { 327 bitmap_free(kvm->arch.pmu_filter); 328 free_cpumask_var(kvm->arch.supported_cpus); 329 330 kvm_vgic_destroy(kvm); 331 332 if (is_protected_kvm_enabled()) 333 pkvm_destroy_hyp_vm(kvm); 334 335 kvm_uninit_stage2_mmu(kvm); 336 kvm_destroy_mpidr_data(kvm); 337 338 kfree(kvm->arch.sysreg_masks); 339 kvm_destroy_vcpus(kvm); 340 341 kvm_unshare_hyp(kvm, kvm + 1); 342 343 kvm_destroy_nested(kvm); 344 kvm_arm_teardown_hypercalls(kvm); 345 } 346 347 static bool kvm_has_full_ptr_auth(void) 348 { 349 bool apa, gpa, api, gpi, apa3, gpa3; 350 u64 isar1, isar2, val; 351 352 /* 353 * Check that: 354 * 355 * - both Address and Generic auth are implemented for a given 356 * algorithm (Q5, IMPDEF or Q3) 357 * - only a single algorithm is implemented. 358 */ 359 if (!system_has_full_ptr_auth()) 360 return false; 361 362 isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); 363 isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); 364 365 apa = !!FIELD_GET(ID_AA64ISAR1_EL1_APA_MASK, isar1); 366 val = FIELD_GET(ID_AA64ISAR1_EL1_GPA_MASK, isar1); 367 gpa = (val == ID_AA64ISAR1_EL1_GPA_IMP); 368 369 api = !!FIELD_GET(ID_AA64ISAR1_EL1_API_MASK, isar1); 370 val = FIELD_GET(ID_AA64ISAR1_EL1_GPI_MASK, isar1); 371 gpi = (val == ID_AA64ISAR1_EL1_GPI_IMP); 372 373 apa3 = !!FIELD_GET(ID_AA64ISAR2_EL1_APA3_MASK, isar2); 374 val = FIELD_GET(ID_AA64ISAR2_EL1_GPA3_MASK, isar2); 375 gpa3 = (val == ID_AA64ISAR2_EL1_GPA3_IMP); 376 377 return (apa == gpa && api == gpi && apa3 == gpa3 && 378 (apa + api + apa3) == 1); 379 } 380 381 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) 382 { 383 int r; 384 385 if (is_protected_kvm_enabled() && !kvm_pkvm_ext_allowed(kvm, ext)) 386 return 0; 387 388 switch (ext) { 389 case KVM_CAP_IRQCHIP: 390 r = vgic_present; 391 break; 392 case KVM_CAP_IOEVENTFD: 393 case KVM_CAP_USER_MEMORY: 394 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: 395 case KVM_CAP_ONE_REG: 396 case KVM_CAP_ARM_PSCI: 397 case KVM_CAP_ARM_PSCI_0_2: 398 case KVM_CAP_READONLY_MEM: 399 case KVM_CAP_MP_STATE: 400 case KVM_CAP_IMMEDIATE_EXIT: 401 case KVM_CAP_VCPU_EVENTS: 402 case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2: 403 case KVM_CAP_ARM_NISV_TO_USER: 404 case KVM_CAP_ARM_INJECT_EXT_DABT: 405 case KVM_CAP_SET_GUEST_DEBUG: 406 case KVM_CAP_VCPU_ATTRIBUTES: 407 case KVM_CAP_PTP_KVM: 408 case KVM_CAP_ARM_SYSTEM_SUSPEND: 409 case KVM_CAP_IRQFD_RESAMPLE: 410 case KVM_CAP_COUNTER_OFFSET: 411 case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS: 412 case KVM_CAP_ARM_SEA_TO_USER: 413 r = 1; 414 break; 415 case KVM_CAP_SET_GUEST_DEBUG2: 416 return KVM_GUESTDBG_VALID_MASK; 417 case KVM_CAP_ARM_SET_DEVICE_ADDR: 418 r = 1; 419 break; 420 case KVM_CAP_NR_VCPUS: 421 /* 422 * ARM64 treats KVM_CAP_NR_CPUS differently from all other 423 * architectures, as it does not always bound it to 424 * KVM_CAP_MAX_VCPUS. It should not matter much because 425 * this is just an advisory value. 426 */ 427 r = min_t(unsigned int, num_online_cpus(), 428 kvm_arm_default_max_vcpus()); 429 break; 430 case KVM_CAP_MAX_VCPUS: 431 case KVM_CAP_MAX_VCPU_ID: 432 if (kvm) 433 r = kvm->max_vcpus; 434 else 435 r = kvm_arm_default_max_vcpus(); 436 break; 437 case KVM_CAP_MSI_DEVID: 438 if (!kvm) 439 r = -EINVAL; 440 else 441 r = kvm->arch.vgic.msis_require_devid; 442 break; 443 case KVM_CAP_ARM_USER_IRQ: 444 /* 445 * 1: EL1_VTIMER, EL1_PTIMER, and PMU. 446 * (bump this number if adding more devices) 447 */ 448 r = 1; 449 break; 450 case KVM_CAP_ARM_MTE: 451 r = system_supports_mte(); 452 break; 453 case KVM_CAP_STEAL_TIME: 454 r = kvm_arm_pvtime_supported(); 455 break; 456 case KVM_CAP_ARM_EL1_32BIT: 457 r = cpus_have_final_cap(ARM64_HAS_32BIT_EL1); 458 break; 459 case KVM_CAP_ARM_EL2: 460 r = cpus_have_final_cap(ARM64_HAS_NESTED_VIRT); 461 break; 462 case KVM_CAP_ARM_EL2_E2H0: 463 r = cpus_have_final_cap(ARM64_HAS_HCR_NV1); 464 break; 465 case KVM_CAP_GUEST_DEBUG_HW_BPS: 466 r = get_num_brps(); 467 break; 468 case KVM_CAP_GUEST_DEBUG_HW_WPS: 469 r = get_num_wrps(); 470 break; 471 case KVM_CAP_ARM_PMU_V3: 472 case KVM_CAP_ARM_PMU_V3_STRICT: 473 r = kvm_supports_guest_pmuv3(); 474 break; 475 case KVM_CAP_ARM_INJECT_SERROR_ESR: 476 r = cpus_have_final_cap(ARM64_HAS_RAS_EXTN); 477 break; 478 case KVM_CAP_ARM_VM_IPA_SIZE: 479 r = get_kvm_ipa_limit(); 480 break; 481 case KVM_CAP_ARM_SVE: 482 r = system_supports_sve(); 483 break; 484 case KVM_CAP_ARM_PTRAUTH_ADDRESS: 485 case KVM_CAP_ARM_PTRAUTH_GENERIC: 486 r = kvm_has_full_ptr_auth(); 487 break; 488 case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE: 489 if (kvm) 490 r = kvm->arch.mmu.split_page_chunk_size; 491 else 492 r = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT; 493 break; 494 case KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES: 495 r = kvm_supported_block_sizes(); 496 break; 497 case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES: 498 r = BIT(0); 499 break; 500 case KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED: 501 if (!kvm) 502 r = -EINVAL; 503 else 504 r = kvm_supports_cacheable_pfnmap(); 505 break; 506 507 default: 508 r = 0; 509 } 510 511 return r; 512 } 513 514 long kvm_arch_dev_ioctl(struct file *filp, 515 unsigned int ioctl, unsigned long arg) 516 { 517 return -EINVAL; 518 } 519 520 struct kvm *kvm_arch_alloc_vm(void) 521 { 522 size_t sz = sizeof(struct kvm); 523 524 if (!has_vhe()) 525 return kzalloc(sz, GFP_KERNEL_ACCOUNT); 526 527 return kvzalloc(sz, GFP_KERNEL_ACCOUNT); 528 } 529 530 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) 531 { 532 if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) 533 return -EBUSY; 534 535 if (id >= kvm->max_vcpus) 536 return -EINVAL; 537 538 return 0; 539 } 540 541 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) 542 { 543 int err; 544 545 spin_lock_init(&vcpu->arch.mp_state_lock); 546 547 #ifdef CONFIG_LOCKDEP 548 /* Inform lockdep that the config_lock is acquired after vcpu->mutex */ 549 mutex_lock(&vcpu->mutex); 550 mutex_lock(&vcpu->kvm->arch.config_lock); 551 mutex_unlock(&vcpu->kvm->arch.config_lock); 552 mutex_unlock(&vcpu->mutex); 553 #endif 554 555 /* Force users to call KVM_ARM_VCPU_INIT */ 556 vcpu_clear_flag(vcpu, VCPU_INITIALIZED); 557 558 vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO; 559 560 /* Set up the timer */ 561 kvm_timer_vcpu_init(vcpu); 562 563 kvm_pmu_vcpu_init(vcpu); 564 565 kvm_arm_pvtime_vcpu_init(&vcpu->arch); 566 567 vcpu->arch.hw_mmu = &vcpu->kvm->arch.mmu; 568 569 /* 570 * This vCPU may have been created after mpidr_data was initialized. 571 * Throw out the pre-computed mappings if that is the case which forces 572 * KVM to fall back to iteratively searching the vCPUs. 573 */ 574 kvm_destroy_mpidr_data(vcpu->kvm); 575 576 err = kvm_vgic_vcpu_init(vcpu); 577 if (err) { 578 kvm_vgic_vcpu_destroy(vcpu); 579 return err; 580 } 581 582 err = kvm_share_hyp(vcpu, vcpu + 1); 583 if (err) 584 kvm_vgic_vcpu_destroy(vcpu); 585 586 return err; 587 } 588 589 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) 590 { 591 } 592 593 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) 594 { 595 if (!is_protected_kvm_enabled()) 596 kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); 597 else 598 free_hyp_memcache(&vcpu->arch.pkvm_memcache); 599 kvm_timer_vcpu_terminate(vcpu); 600 kvm_pmu_vcpu_destroy(vcpu); 601 kvm_vgic_vcpu_destroy(vcpu); 602 kvm_arm_vcpu_destroy(vcpu); 603 } 604 605 void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) 606 { 607 608 } 609 610 void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) 611 { 612 613 } 614 615 static void vcpu_set_pauth_traps(struct kvm_vcpu *vcpu) 616 { 617 if (vcpu_has_ptrauth(vcpu) && !is_protected_kvm_enabled()) { 618 /* 619 * Either we're running an L2 guest, and the API/APK bits come 620 * from L1's HCR_EL2, or API/APK are both set. 621 */ 622 if (unlikely(is_nested_ctxt(vcpu))) { 623 u64 val; 624 625 val = __vcpu_sys_reg(vcpu, HCR_EL2); 626 val &= (HCR_API | HCR_APK); 627 vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK); 628 vcpu->arch.hcr_el2 |= val; 629 } else { 630 vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK); 631 } 632 633 /* 634 * Save the host keys if there is any chance for the guest 635 * to use pauth, as the entry code will reload the guest 636 * keys in that case. 637 */ 638 if (vcpu->arch.hcr_el2 & (HCR_API | HCR_APK)) { 639 struct kvm_cpu_context *ctxt; 640 641 ctxt = this_cpu_ptr_hyp_sym(kvm_hyp_ctxt); 642 ptrauth_save_keys(ctxt); 643 } 644 } 645 } 646 647 static bool kvm_vcpu_should_clear_twi(struct kvm_vcpu *vcpu) 648 { 649 if (unlikely(kvm_wfi_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK)) 650 return kvm_wfi_trap_policy == KVM_WFX_NOTRAP; 651 652 if (vgic_is_v5(vcpu->kvm)) 653 return single_task_running(); 654 655 return single_task_running() && 656 vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 && 657 (atomic_read(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe.vlpi_count) || 658 vcpu->kvm->arch.vgic.nassgireq); 659 } 660 661 static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu) 662 { 663 if (unlikely(kvm_wfe_trap_policy != KVM_WFX_NOTRAP_SINGLE_TASK)) 664 return kvm_wfe_trap_policy == KVM_WFX_NOTRAP; 665 666 return single_task_running(); 667 } 668 669 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 670 { 671 struct kvm_s2_mmu *mmu; 672 int *last_ran; 673 674 if (is_protected_kvm_enabled()) 675 goto nommu; 676 677 if (vcpu_has_nv(vcpu)) 678 kvm_vcpu_load_hw_mmu(vcpu); 679 680 mmu = vcpu->arch.hw_mmu; 681 last_ran = this_cpu_ptr(mmu->last_vcpu_ran); 682 683 /* 684 * Ensure a VMID is allocated for the MMU before programming VTTBR_EL2, 685 * which happens eagerly in VHE. 686 * 687 * Also, the VMID allocator only preserves VMIDs that are active at the 688 * time of rollover, so KVM might need to grab a new VMID for the MMU if 689 * this is called from kvm_sched_in(). 690 */ 691 kvm_arm_vmid_update(&mmu->vmid); 692 693 /* 694 * We guarantee that both TLBs and I-cache are private to each 695 * vcpu. If detecting that a vcpu from the same VM has 696 * previously run on the same physical CPU, call into the 697 * hypervisor code to nuke the relevant contexts. 698 * 699 * We might get preempted before the vCPU actually runs, but 700 * over-invalidation doesn't affect correctness. 701 */ 702 if (*last_ran != vcpu->vcpu_idx) { 703 kvm_call_hyp(__kvm_flush_cpu_context, mmu); 704 *last_ran = vcpu->vcpu_idx; 705 } 706 707 nommu: 708 vcpu->cpu = cpu; 709 710 /* 711 * The timer must be loaded before the vgic to correctly set up physical 712 * interrupt deactivation in nested state (e.g. timer interrupt). 713 */ 714 kvm_timer_vcpu_load(vcpu); 715 kvm_vgic_load(vcpu); 716 kvm_vcpu_load_debug(vcpu); 717 kvm_vcpu_load_fgt(vcpu); 718 if (has_vhe()) 719 kvm_vcpu_load_vhe(vcpu); 720 kvm_arch_vcpu_load_fp(vcpu); 721 kvm_vcpu_pmu_restore_guest(vcpu); 722 if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) 723 kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); 724 725 if (kvm_vcpu_should_clear_twe(vcpu)) 726 vcpu->arch.hcr_el2 &= ~HCR_TWE; 727 else 728 vcpu->arch.hcr_el2 |= HCR_TWE; 729 730 if (kvm_vcpu_should_clear_twi(vcpu)) 731 vcpu->arch.hcr_el2 &= ~HCR_TWI; 732 else 733 vcpu->arch.hcr_el2 |= HCR_TWI; 734 735 vcpu_set_pauth_traps(vcpu); 736 737 if (is_protected_kvm_enabled()) { 738 kvm_call_hyp_nvhe(__pkvm_vcpu_load, 739 vcpu->kvm->arch.pkvm.handle, 740 vcpu->vcpu_idx, vcpu->arch.hcr_el2); 741 kvm_call_hyp(__vgic_v3_restore_vmcr_aprs, 742 &vcpu->arch.vgic_cpu.vgic_v3); 743 } 744 745 if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus)) 746 vcpu_set_on_unsupported_cpu(vcpu); 747 748 vcpu->arch.pid = pid_nr(vcpu->pid); 749 } 750 751 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) 752 { 753 if (is_protected_kvm_enabled()) { 754 kvm_call_hyp(__vgic_v3_save_aprs, &vcpu->arch.vgic_cpu.vgic_v3); 755 kvm_call_hyp_nvhe(__pkvm_vcpu_put); 756 757 /* __pkvm_vcpu_put implies a sync of the state */ 758 if (!kvm_vm_is_protected(vcpu->kvm)) 759 vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY); 760 } 761 762 kvm_vcpu_put_debug(vcpu); 763 kvm_arch_vcpu_put_fp(vcpu); 764 if (has_vhe()) 765 kvm_vcpu_put_vhe(vcpu); 766 kvm_timer_vcpu_put(vcpu); 767 kvm_vgic_put(vcpu); 768 kvm_vcpu_pmu_restore_host(vcpu); 769 if (vcpu_has_nv(vcpu)) 770 kvm_vcpu_put_hw_mmu(vcpu); 771 kvm_arm_vmid_clear_active(); 772 773 vcpu_clear_on_unsupported_cpu(vcpu); 774 vcpu->cpu = -1; 775 } 776 777 static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu) 778 { 779 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED); 780 kvm_make_request(KVM_REQ_SLEEP, vcpu); 781 kvm_vcpu_kick(vcpu); 782 } 783 784 void kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu) 785 { 786 spin_lock(&vcpu->arch.mp_state_lock); 787 __kvm_arm_vcpu_power_off(vcpu); 788 spin_unlock(&vcpu->arch.mp_state_lock); 789 } 790 791 bool kvm_arm_vcpu_stopped(struct kvm_vcpu *vcpu) 792 { 793 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_STOPPED; 794 } 795 796 static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu) 797 { 798 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_SUSPENDED); 799 kvm_make_request(KVM_REQ_SUSPEND, vcpu); 800 kvm_vcpu_kick(vcpu); 801 } 802 803 static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu) 804 { 805 return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED; 806 } 807 808 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, 809 struct kvm_mp_state *mp_state) 810 { 811 *mp_state = READ_ONCE(vcpu->arch.mp_state); 812 813 return 0; 814 } 815 816 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, 817 struct kvm_mp_state *mp_state) 818 { 819 int ret = 0; 820 821 spin_lock(&vcpu->arch.mp_state_lock); 822 823 switch (mp_state->mp_state) { 824 case KVM_MP_STATE_RUNNABLE: 825 WRITE_ONCE(vcpu->arch.mp_state, *mp_state); 826 break; 827 case KVM_MP_STATE_STOPPED: 828 __kvm_arm_vcpu_power_off(vcpu); 829 break; 830 case KVM_MP_STATE_SUSPENDED: 831 kvm_arm_vcpu_suspend(vcpu); 832 break; 833 default: 834 ret = -EINVAL; 835 } 836 837 spin_unlock(&vcpu->arch.mp_state_lock); 838 839 return ret; 840 } 841 842 /** 843 * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled 844 * @v: The VCPU pointer 845 * 846 * If the guest CPU is not waiting for interrupts or an interrupt line is 847 * asserted, the CPU is by definition runnable. 848 */ 849 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) 850 { 851 bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF | HCR_VSE); 852 853 irq_lines |= (!irqchip_in_kernel(v->kvm) && 854 (kvm_timer_should_notify_user(v) || 855 kvm_pmu_should_notify_user(v))); 856 857 return ((irq_lines || kvm_vgic_vcpu_pending_irq(v)) 858 && !kvm_arm_vcpu_stopped(v) && !v->arch.pause); 859 } 860 861 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) 862 { 863 return vcpu_mode_priv(vcpu); 864 } 865 866 #ifdef CONFIG_GUEST_PERF_EVENTS 867 unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu) 868 { 869 return *vcpu_pc(vcpu); 870 } 871 #endif 872 873 static void kvm_init_mpidr_data(struct kvm *kvm) 874 { 875 struct kvm_mpidr_data *data = NULL; 876 unsigned long c, mask, nr_entries; 877 u64 aff_set = 0, aff_clr = ~0UL; 878 struct kvm_vcpu *vcpu; 879 880 mutex_lock(&kvm->arch.config_lock); 881 882 if (rcu_access_pointer(kvm->arch.mpidr_data) || 883 atomic_read(&kvm->online_vcpus) == 1) 884 goto out; 885 886 kvm_for_each_vcpu(c, vcpu, kvm) { 887 u64 aff = kvm_vcpu_get_mpidr_aff(vcpu); 888 aff_set |= aff; 889 aff_clr &= aff; 890 } 891 892 /* 893 * A significant bit can be either 0 or 1, and will only appear in 894 * aff_set. Use aff_clr to weed out the useless stuff. 895 */ 896 mask = aff_set ^ aff_clr; 897 nr_entries = BIT_ULL(hweight_long(mask)); 898 899 /* 900 * Don't let userspace fool us. If we need more than a single page 901 * to describe the compressed MPIDR array, just fall back to the 902 * iterative method. Single vcpu VMs do not need this either. 903 */ 904 if (struct_size(data, cmpidr_to_idx, nr_entries) <= PAGE_SIZE) 905 data = kzalloc_flex(*data, cmpidr_to_idx, nr_entries, 906 GFP_KERNEL_ACCOUNT); 907 908 if (!data) 909 goto out; 910 911 data->mpidr_mask = mask; 912 913 kvm_for_each_vcpu(c, vcpu, kvm) { 914 u64 aff = kvm_vcpu_get_mpidr_aff(vcpu); 915 u16 index = kvm_mpidr_index(data, aff); 916 917 data->cmpidr_to_idx[index] = c; 918 } 919 920 rcu_assign_pointer(kvm->arch.mpidr_data, data); 921 out: 922 mutex_unlock(&kvm->arch.config_lock); 923 } 924 925 /* 926 * Handle both the initialisation that is being done when the vcpu is 927 * run for the first time, as well as the updates that must be 928 * performed each time we get a new thread dealing with this vcpu. 929 */ 930 int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) 931 { 932 struct kvm *kvm = vcpu->kvm; 933 int ret; 934 935 if (!kvm_vcpu_initialized(vcpu)) 936 return -ENOEXEC; 937 938 if (!kvm_arm_vcpu_is_finalized(vcpu)) 939 return -EPERM; 940 941 if (likely(vcpu_has_run_once(vcpu))) 942 return 0; 943 944 kvm_init_mpidr_data(kvm); 945 946 if (likely(irqchip_in_kernel(kvm))) { 947 /* 948 * Map the VGIC hardware resources before running a vcpu the 949 * first time on this VM. 950 */ 951 ret = kvm_vgic_map_resources(kvm); 952 if (ret) 953 return ret; 954 } 955 956 ret = kvm_finalize_sys_regs(vcpu); 957 if (ret) 958 return ret; 959 960 if (vcpu_has_nv(vcpu)) { 961 ret = kvm_vcpu_allocate_vncr_tlb(vcpu); 962 if (ret) 963 return ret; 964 965 ret = kvm_vgic_vcpu_nv_init(vcpu); 966 if (ret) 967 return ret; 968 } 969 970 /* 971 * This needs to happen after any restriction has been applied 972 * to the feature set. 973 */ 974 kvm_calculate_traps(vcpu); 975 976 ret = kvm_timer_enable(vcpu); 977 if (ret) 978 return ret; 979 980 if (kvm_vcpu_has_pmu(vcpu)) { 981 ret = kvm_arm_pmu_v3_enable(vcpu); 982 if (ret) 983 return ret; 984 } 985 986 ret = vgic_v5_finalize_ppi_state(kvm); 987 if (ret) 988 return ret; 989 990 if (is_protected_kvm_enabled()) { 991 /* Start with the vcpu in a dirty state */ 992 if (!kvm_vm_is_protected(vcpu->kvm)) 993 vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY); 994 ret = pkvm_create_hyp_vm(kvm); 995 if (ret) 996 return ret; 997 998 ret = pkvm_create_hyp_vcpu(vcpu); 999 if (ret) 1000 return ret; 1001 } 1002 1003 mutex_lock(&kvm->arch.config_lock); 1004 set_bit(KVM_ARCH_FLAG_HAS_RAN_ONCE, &kvm->arch.flags); 1005 mutex_unlock(&kvm->arch.config_lock); 1006 1007 return ret; 1008 } 1009 1010 bool kvm_arch_intc_initialized(struct kvm *kvm) 1011 { 1012 return vgic_initialized(kvm); 1013 } 1014 1015 void kvm_arm_halt_guest(struct kvm *kvm) 1016 { 1017 unsigned long i; 1018 struct kvm_vcpu *vcpu; 1019 1020 kvm_for_each_vcpu(i, vcpu, kvm) 1021 vcpu->arch.pause = true; 1022 kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP); 1023 } 1024 1025 void kvm_arm_resume_guest(struct kvm *kvm) 1026 { 1027 unsigned long i; 1028 struct kvm_vcpu *vcpu; 1029 1030 kvm_for_each_vcpu(i, vcpu, kvm) { 1031 vcpu->arch.pause = false; 1032 __kvm_vcpu_wake_up(vcpu); 1033 } 1034 } 1035 1036 static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu) 1037 { 1038 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); 1039 1040 rcuwait_wait_event(wait, 1041 (!kvm_arm_vcpu_stopped(vcpu)) && (!vcpu->arch.pause), 1042 TASK_INTERRUPTIBLE); 1043 1044 if (kvm_arm_vcpu_stopped(vcpu) || vcpu->arch.pause) { 1045 /* Awaken to handle a signal, request we sleep again later. */ 1046 kvm_make_request(KVM_REQ_SLEEP, vcpu); 1047 } 1048 1049 /* 1050 * Make sure we will observe a potential reset request if we've 1051 * observed a change to the power state. Pairs with the smp_wmb() in 1052 * kvm_psci_vcpu_on(). 1053 */ 1054 smp_rmb(); 1055 } 1056 1057 /** 1058 * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior 1059 * @vcpu: The VCPU pointer 1060 * 1061 * Suspend execution of a vCPU until a valid wake event is detected, i.e. until 1062 * the vCPU is runnable. The vCPU may or may not be scheduled out, depending 1063 * on when a wake event arrives, e.g. there may already be a pending wake event. 1064 */ 1065 void kvm_vcpu_wfi(struct kvm_vcpu *vcpu) 1066 { 1067 /* 1068 * Sync back the state of the GIC CPU interface so that we have 1069 * the latest PMR and group enables. This ensures that 1070 * kvm_arch_vcpu_runnable has up-to-date data to decide whether 1071 * we have pending interrupts, e.g. when determining if the 1072 * vCPU should block. 1073 * 1074 * For the same reason, we want to tell GICv4 that we need 1075 * doorbells to be signalled, should an interrupt become pending. 1076 */ 1077 preempt_disable(); 1078 vcpu_set_flag(vcpu, IN_WFI); 1079 kvm_vgic_put(vcpu); 1080 preempt_enable(); 1081 1082 kvm_vcpu_halt(vcpu); 1083 vcpu_clear_flag(vcpu, IN_WFIT); 1084 1085 preempt_disable(); 1086 vcpu_clear_flag(vcpu, IN_WFI); 1087 kvm_vgic_load(vcpu); 1088 preempt_enable(); 1089 } 1090 1091 static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu) 1092 { 1093 if (!kvm_arm_vcpu_suspended(vcpu)) 1094 return 1; 1095 1096 kvm_vcpu_wfi(vcpu); 1097 1098 /* 1099 * The suspend state is sticky; we do not leave it until userspace 1100 * explicitly marks the vCPU as runnable. Request that we suspend again 1101 * later. 1102 */ 1103 kvm_make_request(KVM_REQ_SUSPEND, vcpu); 1104 1105 /* 1106 * Check to make sure the vCPU is actually runnable. If so, exit to 1107 * userspace informing it of the wakeup condition. 1108 */ 1109 if (kvm_arch_vcpu_runnable(vcpu)) { 1110 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); 1111 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_WAKEUP; 1112 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; 1113 return 0; 1114 } 1115 1116 /* 1117 * Otherwise, we were unblocked to process a different event, such as a 1118 * pending signal. Return 1 and allow kvm_arch_vcpu_ioctl_run() to 1119 * process the event. 1120 */ 1121 return 1; 1122 } 1123 1124 /** 1125 * check_vcpu_requests - check and handle pending vCPU requests 1126 * @vcpu: the VCPU pointer 1127 * 1128 * Return: 1 if we should enter the guest 1129 * 0 if we should exit to userspace 1130 * < 0 if we should exit to userspace, where the return value indicates 1131 * an error 1132 */ 1133 static int check_vcpu_requests(struct kvm_vcpu *vcpu) 1134 { 1135 if (kvm_request_pending(vcpu)) { 1136 if (kvm_test_request(KVM_REQ_VM_DEAD, vcpu)) 1137 return -EIO; 1138 1139 if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) 1140 kvm_vcpu_sleep(vcpu); 1141 1142 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) 1143 kvm_reset_vcpu(vcpu); 1144 1145 /* 1146 * Clear IRQ_PENDING requests that were made to guarantee 1147 * that a VCPU sees new virtual interrupts. 1148 */ 1149 kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu); 1150 1151 /* Process interrupts deactivated through a trap */ 1152 if (kvm_check_request(KVM_REQ_VGIC_PROCESS_UPDATE, vcpu)) 1153 kvm_vgic_process_async_update(vcpu); 1154 1155 if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu)) 1156 kvm_update_stolen_time(vcpu); 1157 1158 if (kvm_check_request(KVM_REQ_RELOAD_GICv4, vcpu)) { 1159 /* The distributor enable bits were changed */ 1160 preempt_disable(); 1161 vgic_v4_put(vcpu); 1162 vgic_v4_load(vcpu); 1163 preempt_enable(); 1164 } 1165 1166 if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu)) 1167 kvm_vcpu_reload_pmu(vcpu); 1168 1169 if (kvm_check_request(KVM_REQ_RESYNC_PMU_EL0, vcpu)) 1170 kvm_vcpu_pmu_restore_guest(vcpu); 1171 1172 if (kvm_check_request(KVM_REQ_SUSPEND, vcpu)) 1173 return kvm_vcpu_suspend(vcpu); 1174 1175 if (kvm_dirty_ring_check_request(vcpu)) 1176 return 0; 1177 1178 check_nested_vcpu_requests(vcpu); 1179 } 1180 1181 return 1; 1182 } 1183 1184 static bool vcpu_mode_is_bad_32bit(struct kvm_vcpu *vcpu) 1185 { 1186 if (likely(!vcpu_mode_is_32bit(vcpu))) 1187 return false; 1188 1189 if (vcpu_has_nv(vcpu)) 1190 return true; 1191 1192 return !kvm_supports_32bit_el0(); 1193 } 1194 1195 static bool kvm_irq_update_run(struct kvm_vcpu *vcpu) 1196 { 1197 bool r; 1198 1199 r = kvm_timer_update_run(vcpu); 1200 r |= kvm_pmu_update_run(vcpu); 1201 return r; 1202 } 1203 1204 /** 1205 * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest 1206 * @vcpu: The VCPU pointer 1207 * @ret: Pointer to write optional return code 1208 * 1209 * Returns: true if the VCPU needs to return to a preemptible + interruptible 1210 * and skip guest entry. 1211 * 1212 * This function disambiguates between two different types of exits: exits to a 1213 * preemptible + interruptible kernel context and exits to userspace. For an 1214 * exit to userspace, this function will write the return code to ret and return 1215 * true. For an exit to preemptible + interruptible kernel context (i.e. check 1216 * for pending work and re-enter), return true without writing to ret. 1217 */ 1218 static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret) 1219 { 1220 struct kvm_run *run = vcpu->run; 1221 1222 /* 1223 * If we're using a userspace irqchip, then check if we need 1224 * to tell a userspace irqchip about timer or PMU level 1225 * changes and if so, exit to userspace while updating the run 1226 * state. 1227 */ 1228 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { 1229 if (unlikely(kvm_irq_update_run(vcpu))) { 1230 *ret = -EINTR; 1231 run->exit_reason = KVM_EXIT_INTR; 1232 return true; 1233 } 1234 } 1235 1236 if (unlikely(vcpu_on_unsupported_cpu(vcpu))) { 1237 run->exit_reason = KVM_EXIT_FAIL_ENTRY; 1238 run->fail_entry.hardware_entry_failure_reason = KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED; 1239 run->fail_entry.cpu = smp_processor_id(); 1240 *ret = 0; 1241 return true; 1242 } 1243 1244 return kvm_request_pending(vcpu) || 1245 xfer_to_guest_mode_work_pending(); 1246 } 1247 1248 /* 1249 * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while 1250 * the vCPU is running. 1251 * 1252 * This must be noinstr as instrumentation may make use of RCU, and this is not 1253 * safe during the EQS. 1254 */ 1255 static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu) 1256 { 1257 int ret; 1258 1259 guest_state_enter_irqoff(); 1260 ret = kvm_call_hyp_ret(__kvm_vcpu_run, vcpu); 1261 guest_state_exit_irqoff(); 1262 1263 return ret; 1264 } 1265 1266 /** 1267 * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code 1268 * @vcpu: The VCPU pointer 1269 * 1270 * This function is called through the VCPU_RUN ioctl called from user space. It 1271 * will execute VM code in a loop until the time slice for the process is used 1272 * or some emulation is needed from user space in which case the function will 1273 * return with return value 0 and with the kvm_run structure filled in with the 1274 * required data for the requested emulation. 1275 */ 1276 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) 1277 { 1278 struct kvm_run *run = vcpu->run; 1279 int ret; 1280 1281 if (run->exit_reason == KVM_EXIT_MMIO) { 1282 ret = kvm_handle_mmio_return(vcpu); 1283 if (ret <= 0) 1284 return ret; 1285 } 1286 1287 vcpu_load(vcpu); 1288 1289 if (!vcpu->wants_to_run) { 1290 ret = -EINTR; 1291 goto out; 1292 } 1293 1294 kvm_sigset_activate(vcpu); 1295 1296 ret = 1; 1297 run->exit_reason = KVM_EXIT_UNKNOWN; 1298 run->flags = 0; 1299 while (ret > 0) { 1300 /* 1301 * Check conditions before entering the guest 1302 */ 1303 ret = kvm_xfer_to_guest_mode_handle_work(vcpu); 1304 if (!ret) 1305 ret = 1; 1306 1307 if (ret > 0) 1308 ret = check_vcpu_requests(vcpu); 1309 1310 /* 1311 * Preparing the interrupts to be injected also 1312 * involves poking the GIC, which must be done in a 1313 * non-preemptible context. 1314 */ 1315 preempt_disable(); 1316 1317 kvm_nested_flush_hwstate(vcpu); 1318 1319 if (kvm_vcpu_has_pmu(vcpu)) 1320 kvm_pmu_flush_hwstate(vcpu); 1321 1322 local_irq_disable(); 1323 1324 kvm_vgic_flush_hwstate(vcpu); 1325 1326 kvm_pmu_update_vcpu_events(vcpu); 1327 1328 /* 1329 * Ensure we set mode to IN_GUEST_MODE after we disable 1330 * interrupts and before the final VCPU requests check. 1331 * See the comment in kvm_vcpu_exiting_guest_mode() and 1332 * Documentation/virt/kvm/vcpu-requests.rst 1333 */ 1334 smp_store_mb(vcpu->mode, IN_GUEST_MODE); 1335 1336 if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) { 1337 vcpu->mode = OUTSIDE_GUEST_MODE; 1338 isb(); /* Ensure work in x_flush_hwstate is committed */ 1339 if (kvm_vcpu_has_pmu(vcpu)) 1340 kvm_pmu_sync_hwstate(vcpu); 1341 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) 1342 kvm_timer_sync_user(vcpu); 1343 kvm_vgic_sync_hwstate(vcpu); 1344 local_irq_enable(); 1345 preempt_enable(); 1346 continue; 1347 } 1348 1349 kvm_arch_vcpu_ctxflush_fp(vcpu); 1350 1351 /************************************************************** 1352 * Enter the guest 1353 */ 1354 trace_kvm_entry(*vcpu_pc(vcpu)); 1355 guest_timing_enter_irqoff(); 1356 1357 ret = kvm_arm_vcpu_enter_exit(vcpu); 1358 1359 vcpu->mode = OUTSIDE_GUEST_MODE; 1360 vcpu->stat.exits++; 1361 /* 1362 * Back from guest 1363 *************************************************************/ 1364 1365 /* 1366 * We must sync the PMU state before the vgic state so 1367 * that the vgic can properly sample the updated state of the 1368 * interrupt line. 1369 */ 1370 if (kvm_vcpu_has_pmu(vcpu)) 1371 kvm_pmu_sync_hwstate(vcpu); 1372 1373 /* 1374 * Sync the vgic state before syncing the timer state because 1375 * the timer code needs to know if the virtual timer 1376 * interrupts are active. 1377 */ 1378 kvm_vgic_sync_hwstate(vcpu); 1379 1380 /* 1381 * Sync the timer hardware state before enabling interrupts as 1382 * we don't want vtimer interrupts to race with syncing the 1383 * timer virtual interrupt state. 1384 */ 1385 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) 1386 kvm_timer_sync_user(vcpu); 1387 1388 if (is_hyp_ctxt(vcpu)) 1389 kvm_timer_sync_nested(vcpu); 1390 1391 kvm_arch_vcpu_ctxsync_fp(vcpu); 1392 1393 /* 1394 * We must ensure that any pending interrupts are taken before 1395 * we exit guest timing so that timer ticks are accounted as 1396 * guest time. Transiently unmask interrupts so that any 1397 * pending interrupts are taken. 1398 * 1399 * Per ARM DDI 0487G.b section D1.13.4, an ISB (or other 1400 * context synchronization event) is necessary to ensure that 1401 * pending interrupts are taken. 1402 */ 1403 if (ARM_EXCEPTION_CODE(ret) == ARM_EXCEPTION_IRQ) { 1404 local_irq_enable(); 1405 isb(); 1406 local_irq_disable(); 1407 } 1408 1409 guest_timing_exit_irqoff(); 1410 1411 local_irq_enable(); 1412 1413 trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu)); 1414 1415 /* Exit types that need handling before we can be preempted */ 1416 handle_exit_early(vcpu, ret); 1417 1418 kvm_nested_sync_hwstate(vcpu); 1419 1420 preempt_enable(); 1421 1422 /* 1423 * The ARMv8 architecture doesn't give the hypervisor 1424 * a mechanism to prevent a guest from dropping to AArch32 EL0 1425 * if implemented by the CPU. If we spot the guest in such 1426 * state and that we decided it wasn't supposed to do so (like 1427 * with the asymmetric AArch32 case), return to userspace with 1428 * a fatal error. 1429 */ 1430 if (vcpu_mode_is_bad_32bit(vcpu)) { 1431 /* 1432 * As we have caught the guest red-handed, decide that 1433 * it isn't fit for purpose anymore by making the vcpu 1434 * invalid. The VMM can try and fix it by issuing a 1435 * KVM_ARM_VCPU_INIT if it really wants to. 1436 */ 1437 vcpu_clear_flag(vcpu, VCPU_INITIALIZED); 1438 ret = ARM_EXCEPTION_IL; 1439 } 1440 1441 ret = handle_exit(vcpu, ret); 1442 } 1443 1444 if (unlikely(!irqchip_in_kernel(vcpu->kvm))) 1445 kvm_irq_update_run(vcpu); 1446 1447 kvm_sigset_deactivate(vcpu); 1448 1449 out: 1450 /* 1451 * In the unlikely event that we are returning to userspace 1452 * with pending exceptions or PC adjustment, commit these 1453 * adjustments in order to give userspace a consistent view of 1454 * the vcpu state. Note that this relies on __kvm_adjust_pc() 1455 * being preempt-safe on VHE. 1456 */ 1457 if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) || 1458 vcpu_get_flag(vcpu, INCREMENT_PC))) 1459 kvm_call_hyp(__kvm_adjust_pc, vcpu); 1460 1461 vcpu_put(vcpu); 1462 return ret; 1463 } 1464 1465 static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) 1466 { 1467 int bit_index; 1468 bool set; 1469 unsigned long *hcr; 1470 1471 if (number == KVM_ARM_IRQ_CPU_IRQ) 1472 bit_index = __ffs(HCR_VI); 1473 else /* KVM_ARM_IRQ_CPU_FIQ */ 1474 bit_index = __ffs(HCR_VF); 1475 1476 hcr = vcpu_hcr(vcpu); 1477 if (level) 1478 set = test_and_set_bit(bit_index, hcr); 1479 else 1480 set = test_and_clear_bit(bit_index, hcr); 1481 1482 /* 1483 * If we didn't change anything, no need to wake up or kick other CPUs 1484 */ 1485 if (set == level) 1486 return 0; 1487 1488 /* 1489 * The vcpu irq_lines field was updated, wake up sleeping VCPUs and 1490 * trigger a world-switch round on the running physical CPU to set the 1491 * virtual IRQ/FIQ fields in the HCR appropriately. 1492 */ 1493 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); 1494 kvm_vcpu_kick(vcpu); 1495 1496 return 0; 1497 } 1498 1499 int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, 1500 bool line_status) 1501 { 1502 unsigned int irq_type, vcpu_id, irq_num; 1503 struct kvm_vcpu *vcpu = NULL; 1504 bool level = irq_level->level; 1505 u32 irq = irq_level->irq; 1506 unsigned long *mask; 1507 1508 irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; 1509 vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; 1510 vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1); 1511 irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; 1512 1513 trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level); 1514 1515 switch (irq_type) { 1516 case KVM_ARM_IRQ_TYPE_CPU: 1517 if (irqchip_in_kernel(kvm)) 1518 return -ENXIO; 1519 1520 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); 1521 if (!vcpu) 1522 return -EINVAL; 1523 1524 if (irq_num > KVM_ARM_IRQ_CPU_FIQ) 1525 return -EINVAL; 1526 1527 return vcpu_interrupt_line(vcpu, irq_num, level); 1528 case KVM_ARM_IRQ_TYPE_PPI: 1529 if (irqchip_in_kernel(kvm)) { 1530 int ret = vgic_lazy_init(kvm); 1531 if (ret) 1532 return ret; 1533 } else { 1534 return -ENXIO; 1535 } 1536 1537 vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); 1538 if (!vcpu) 1539 return -EINVAL; 1540 1541 if (vgic_is_v5(kvm)) { 1542 if (irq_num >= VGIC_V5_NR_PRIVATE_IRQS) 1543 return -EINVAL; 1544 1545 /* 1546 * Only allow PPIs that are explicitly exposed to 1547 * usespace to be driven via KVM_IRQ_LINE 1548 */ 1549 mask = kvm->arch.vgic.gicv5_vm.userspace_ppis; 1550 if (!test_bit(irq_num, mask)) 1551 return -EINVAL; 1552 1553 /* Build a GICv5-style IntID here */ 1554 irq_num = vgic_v5_make_ppi(irq_num); 1555 } else if (irq_num < VGIC_NR_SGIS || 1556 irq_num >= VGIC_NR_PRIVATE_IRQS) { 1557 return -EINVAL; 1558 } 1559 1560 return kvm_vgic_inject_irq(kvm, vcpu, irq_num, level, NULL); 1561 case KVM_ARM_IRQ_TYPE_SPI: 1562 if (irqchip_in_kernel(kvm)) { 1563 int ret = vgic_lazy_init(kvm); 1564 if (ret) 1565 return ret; 1566 } else { 1567 return -ENXIO; 1568 } 1569 1570 if (vgic_is_v5(kvm)) { 1571 /* Build a GICv5-style IntID here */ 1572 irq_num = vgic_v5_make_spi(irq_num); 1573 } else { 1574 if (irq_num < VGIC_NR_PRIVATE_IRQS) 1575 return -EINVAL; 1576 } 1577 1578 return kvm_vgic_inject_irq(kvm, NULL, irq_num, level, NULL); 1579 } 1580 1581 return -EINVAL; 1582 } 1583 1584 static unsigned long system_supported_vcpu_features(void) 1585 { 1586 unsigned long features = KVM_VCPU_VALID_FEATURES; 1587 1588 if (!cpus_have_final_cap(ARM64_HAS_32BIT_EL1)) 1589 clear_bit(KVM_ARM_VCPU_EL1_32BIT, &features); 1590 1591 if (!kvm_supports_guest_pmuv3()) { 1592 clear_bit(KVM_ARM_VCPU_PMU_V3, &features); 1593 clear_bit(KVM_ARM_VCPU_PMU_V3_STRICT, &features); 1594 } 1595 1596 if (!system_supports_sve()) 1597 clear_bit(KVM_ARM_VCPU_SVE, &features); 1598 1599 if (!kvm_has_full_ptr_auth()) { 1600 clear_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features); 1601 clear_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features); 1602 } 1603 1604 if (!cpus_have_final_cap(ARM64_HAS_NESTED_VIRT)) 1605 clear_bit(KVM_ARM_VCPU_HAS_EL2, &features); 1606 1607 return features; 1608 } 1609 1610 static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu, 1611 const struct kvm_vcpu_init *init) 1612 { 1613 unsigned long features = init->features[0]; 1614 int i; 1615 1616 if (features & ~KVM_VCPU_VALID_FEATURES) 1617 return -ENOENT; 1618 1619 for (i = 1; i < ARRAY_SIZE(init->features); i++) { 1620 if (init->features[i]) 1621 return -ENOENT; 1622 } 1623 1624 if (features & ~system_supported_vcpu_features()) 1625 return -EINVAL; 1626 1627 /* 1628 * For now make sure that both address/generic pointer authentication 1629 * features are requested by the userspace together. 1630 */ 1631 if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, &features) != 1632 test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features)) 1633 return -EINVAL; 1634 1635 /* Strict PMUv3 UAPI requires PMUv3. */ 1636 if (test_bit(KVM_ARM_VCPU_PMU_V3_STRICT, &features) && 1637 !test_bit(KVM_ARM_VCPU_PMU_V3, &features)) 1638 return -EINVAL; 1639 1640 if (!test_bit(KVM_ARM_VCPU_EL1_32BIT, &features)) 1641 return 0; 1642 1643 /* MTE is incompatible with AArch32 */ 1644 if (kvm_has_mte(vcpu->kvm)) 1645 return -EINVAL; 1646 1647 /* NV is incompatible with AArch32 */ 1648 if (test_bit(KVM_ARM_VCPU_HAS_EL2, &features)) 1649 return -EINVAL; 1650 1651 return 0; 1652 } 1653 1654 static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu, 1655 const struct kvm_vcpu_init *init) 1656 { 1657 unsigned long features = init->features[0]; 1658 1659 return !bitmap_equal(vcpu->kvm->arch.vcpu_features, &features, 1660 KVM_VCPU_MAX_FEATURES); 1661 } 1662 1663 static int kvm_setup_vcpu(struct kvm_vcpu *vcpu) 1664 { 1665 struct kvm *kvm = vcpu->kvm; 1666 int ret = 0; 1667 1668 /* 1669 * When the vCPU has a PMU, but no PMU is set for the guest yet, set 1670 * the default one. If KVM_ARM_VCPU_PMU_V3_STRICT is set, no default 1671 * PMU is created, and userspace must select a PMU via 1672 * KVM_ARM_VCPU_PMU_V3_SET_PMU. 1673 */ 1674 if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu && 1675 !kvm_vcpu_has_pmuv3_strict(vcpu)) 1676 ret = kvm_arm_set_default_pmu(kvm); 1677 1678 /* Prepare for nested if required */ 1679 if (!ret && vcpu_has_nv(vcpu)) 1680 ret = kvm_vcpu_init_nested(vcpu); 1681 1682 return ret; 1683 } 1684 1685 static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1686 const struct kvm_vcpu_init *init) 1687 { 1688 unsigned long features = init->features[0]; 1689 struct kvm *kvm = vcpu->kvm; 1690 int ret = -EINVAL; 1691 1692 mutex_lock(&kvm->arch.config_lock); 1693 1694 if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) && 1695 kvm_vcpu_init_changed(vcpu, init)) 1696 goto out_unlock; 1697 1698 bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES); 1699 1700 ret = kvm_setup_vcpu(vcpu); 1701 if (ret) 1702 goto out_unlock; 1703 1704 /* Now we know what it is, we can reset it. */ 1705 kvm_reset_vcpu(vcpu); 1706 1707 set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags); 1708 vcpu_set_flag(vcpu, VCPU_INITIALIZED); 1709 ret = 0; 1710 out_unlock: 1711 mutex_unlock(&kvm->arch.config_lock); 1712 return ret; 1713 } 1714 1715 static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 1716 const struct kvm_vcpu_init *init) 1717 { 1718 int ret; 1719 1720 if (init->target != KVM_ARM_TARGET_GENERIC_V8 && 1721 init->target != kvm_target_cpu()) 1722 return -EINVAL; 1723 1724 ret = kvm_vcpu_init_check_features(vcpu, init); 1725 if (ret) 1726 return ret; 1727 1728 if (!kvm_vcpu_initialized(vcpu)) 1729 return __kvm_vcpu_set_target(vcpu, init); 1730 1731 if (kvm_vcpu_init_changed(vcpu, init)) 1732 return -EINVAL; 1733 1734 kvm_reset_vcpu(vcpu); 1735 return 0; 1736 } 1737 1738 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, 1739 struct kvm_vcpu_init *init) 1740 { 1741 bool power_off = false; 1742 int ret; 1743 1744 /* 1745 * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid 1746 * reflecting it in the finalized feature set, thus limiting its scope 1747 * to a single KVM_ARM_VCPU_INIT call. 1748 */ 1749 if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) { 1750 init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF); 1751 power_off = true; 1752 } 1753 1754 ret = kvm_vcpu_set_target(vcpu, init); 1755 if (ret) 1756 return ret; 1757 1758 /* 1759 * Ensure a rebooted VM will fault in RAM pages and detect if the 1760 * guest MMU is turned off and flush the caches as needed. 1761 * 1762 * S2FWB enforces all memory accesses to RAM being cacheable, 1763 * ensuring that the data side is always coherent. We still 1764 * need to invalidate the I-cache though, as FWB does *not* 1765 * imply CTR_EL0.DIC. 1766 */ 1767 if (vcpu_has_run_once(vcpu)) { 1768 if (!cpus_have_final_cap(ARM64_HAS_STAGE2_FWB)) 1769 stage2_unmap_vm(vcpu->kvm); 1770 else 1771 icache_inval_all_pou(); 1772 } 1773 1774 vcpu_reset_hcr(vcpu); 1775 1776 /* 1777 * Handle the "start in power-off" case. 1778 */ 1779 spin_lock(&vcpu->arch.mp_state_lock); 1780 1781 if (power_off) 1782 __kvm_arm_vcpu_power_off(vcpu); 1783 else 1784 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE); 1785 1786 spin_unlock(&vcpu->arch.mp_state_lock); 1787 1788 return 0; 1789 } 1790 1791 static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, 1792 struct kvm_device_attr *attr) 1793 { 1794 int ret = -ENXIO; 1795 1796 switch (attr->group) { 1797 default: 1798 ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr); 1799 break; 1800 } 1801 1802 return ret; 1803 } 1804 1805 static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, 1806 struct kvm_device_attr *attr) 1807 { 1808 int ret = -ENXIO; 1809 1810 switch (attr->group) { 1811 default: 1812 ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr); 1813 break; 1814 } 1815 1816 return ret; 1817 } 1818 1819 static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, 1820 struct kvm_device_attr *attr) 1821 { 1822 int ret = -ENXIO; 1823 1824 switch (attr->group) { 1825 default: 1826 ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr); 1827 break; 1828 } 1829 1830 return ret; 1831 } 1832 1833 static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, 1834 struct kvm_vcpu_events *events) 1835 { 1836 memset(events, 0, sizeof(*events)); 1837 1838 return __kvm_arm_vcpu_get_events(vcpu, events); 1839 } 1840 1841 static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, 1842 struct kvm_vcpu_events *events) 1843 { 1844 int i; 1845 1846 /* check whether the reserved field is zero */ 1847 for (i = 0; i < ARRAY_SIZE(events->reserved); i++) 1848 if (events->reserved[i]) 1849 return -EINVAL; 1850 1851 /* check whether the pad field is zero */ 1852 for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++) 1853 if (events->exception.pad[i]) 1854 return -EINVAL; 1855 1856 return __kvm_arm_vcpu_set_events(vcpu, events); 1857 } 1858 1859 long kvm_arch_vcpu_ioctl(struct file *filp, 1860 unsigned int ioctl, unsigned long arg) 1861 { 1862 struct kvm_vcpu *vcpu = filp->private_data; 1863 void __user *argp = (void __user *)arg; 1864 struct kvm_device_attr attr; 1865 long r; 1866 1867 switch (ioctl) { 1868 case KVM_ARM_VCPU_INIT: { 1869 struct kvm_vcpu_init init; 1870 1871 r = -EFAULT; 1872 if (copy_from_user(&init, argp, sizeof(init))) 1873 break; 1874 1875 r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); 1876 break; 1877 } 1878 case KVM_SET_ONE_REG: 1879 case KVM_GET_ONE_REG: { 1880 struct kvm_one_reg reg; 1881 1882 r = -ENOEXEC; 1883 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1884 break; 1885 1886 r = -EFAULT; 1887 if (copy_from_user(®, argp, sizeof(reg))) 1888 break; 1889 1890 /* 1891 * We could owe a reset due to PSCI. Handle the pending reset 1892 * here to ensure userspace register accesses are ordered after 1893 * the reset. 1894 */ 1895 if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) 1896 kvm_reset_vcpu(vcpu); 1897 1898 if (ioctl == KVM_SET_ONE_REG) 1899 r = kvm_arm_set_reg(vcpu, ®); 1900 else 1901 r = kvm_arm_get_reg(vcpu, ®); 1902 break; 1903 } 1904 case KVM_GET_REG_LIST: { 1905 struct kvm_reg_list __user *user_list = argp; 1906 struct kvm_reg_list reg_list; 1907 unsigned n; 1908 1909 r = -ENOEXEC; 1910 if (unlikely(!kvm_vcpu_initialized(vcpu))) 1911 break; 1912 1913 r = -EPERM; 1914 if (!kvm_arm_vcpu_is_finalized(vcpu)) 1915 break; 1916 1917 r = -EFAULT; 1918 if (copy_from_user(®_list, user_list, sizeof(reg_list))) 1919 break; 1920 n = reg_list.n; 1921 reg_list.n = kvm_arm_num_regs(vcpu); 1922 if (copy_to_user(user_list, ®_list, sizeof(reg_list))) 1923 break; 1924 r = -E2BIG; 1925 if (n < reg_list.n) 1926 break; 1927 r = kvm_arm_copy_reg_indices(vcpu, user_list->reg); 1928 break; 1929 } 1930 case KVM_SET_DEVICE_ATTR: { 1931 r = -EFAULT; 1932 if (copy_from_user(&attr, argp, sizeof(attr))) 1933 break; 1934 r = kvm_arm_vcpu_set_attr(vcpu, &attr); 1935 break; 1936 } 1937 case KVM_GET_DEVICE_ATTR: { 1938 r = -EFAULT; 1939 if (copy_from_user(&attr, argp, sizeof(attr))) 1940 break; 1941 r = kvm_arm_vcpu_get_attr(vcpu, &attr); 1942 break; 1943 } 1944 case KVM_HAS_DEVICE_ATTR: { 1945 r = -EFAULT; 1946 if (copy_from_user(&attr, argp, sizeof(attr))) 1947 break; 1948 r = kvm_arm_vcpu_has_attr(vcpu, &attr); 1949 break; 1950 } 1951 case KVM_GET_VCPU_EVENTS: { 1952 struct kvm_vcpu_events events; 1953 1954 if (!kvm_vcpu_initialized(vcpu)) 1955 return -ENOEXEC; 1956 1957 if (kvm_arm_vcpu_get_events(vcpu, &events)) 1958 return -EINVAL; 1959 1960 if (copy_to_user(argp, &events, sizeof(events))) 1961 return -EFAULT; 1962 1963 return 0; 1964 } 1965 case KVM_SET_VCPU_EVENTS: { 1966 struct kvm_vcpu_events events; 1967 1968 if (!kvm_vcpu_initialized(vcpu)) 1969 return -ENOEXEC; 1970 1971 if (copy_from_user(&events, argp, sizeof(events))) 1972 return -EFAULT; 1973 1974 return kvm_arm_vcpu_set_events(vcpu, &events); 1975 } 1976 case KVM_ARM_VCPU_FINALIZE: { 1977 int what; 1978 1979 if (!kvm_vcpu_initialized(vcpu)) 1980 return -ENOEXEC; 1981 1982 if (get_user(what, (const int __user *)argp)) 1983 return -EFAULT; 1984 1985 return kvm_arm_vcpu_finalize(vcpu, what); 1986 } 1987 default: 1988 r = -EINVAL; 1989 } 1990 1991 return r; 1992 } 1993 1994 long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl, 1995 unsigned long arg) 1996 { 1997 return -ENOIOCTLCMD; 1998 } 1999 2000 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) 2001 { 2002 2003 } 2004 2005 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, 2006 struct kvm_arm_device_addr *dev_addr) 2007 { 2008 switch (FIELD_GET(KVM_ARM_DEVICE_ID_MASK, dev_addr->id)) { 2009 case KVM_ARM_DEVICE_VGIC_V2: 2010 if (!vgic_present) 2011 return -ENXIO; 2012 return kvm_set_legacy_vgic_v2_addr(kvm, dev_addr); 2013 default: 2014 return -ENODEV; 2015 } 2016 } 2017 2018 static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr) 2019 { 2020 switch (attr->group) { 2021 case KVM_ARM_VM_SMCCC_CTRL: 2022 return kvm_vm_smccc_has_attr(kvm, attr); 2023 default: 2024 return -ENXIO; 2025 } 2026 } 2027 2028 static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr) 2029 { 2030 switch (attr->group) { 2031 case KVM_ARM_VM_SMCCC_CTRL: 2032 return kvm_vm_smccc_set_attr(kvm, attr); 2033 default: 2034 return -ENXIO; 2035 } 2036 } 2037 2038 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) 2039 { 2040 struct kvm *kvm = filp->private_data; 2041 void __user *argp = (void __user *)arg; 2042 struct kvm_device_attr attr; 2043 2044 if (is_protected_kvm_enabled() && !kvm_pkvm_ioctl_allowed(kvm, ioctl)) 2045 return -EINVAL; 2046 2047 switch (ioctl) { 2048 case KVM_CREATE_IRQCHIP: { 2049 int ret; 2050 if (!vgic_present) 2051 return -ENXIO; 2052 mutex_lock(&kvm->lock); 2053 ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); 2054 mutex_unlock(&kvm->lock); 2055 return ret; 2056 } 2057 case KVM_ARM_SET_DEVICE_ADDR: { 2058 struct kvm_arm_device_addr dev_addr; 2059 2060 if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) 2061 return -EFAULT; 2062 return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); 2063 } 2064 case KVM_ARM_PREFERRED_TARGET: { 2065 struct kvm_vcpu_init init = { 2066 .target = KVM_ARM_TARGET_GENERIC_V8, 2067 }; 2068 2069 if (copy_to_user(argp, &init, sizeof(init))) 2070 return -EFAULT; 2071 2072 return 0; 2073 } 2074 case KVM_ARM_MTE_COPY_TAGS: { 2075 struct kvm_arm_copy_mte_tags copy_tags; 2076 2077 if (copy_from_user(©_tags, argp, sizeof(copy_tags))) 2078 return -EFAULT; 2079 return kvm_vm_ioctl_mte_copy_tags(kvm, ©_tags); 2080 } 2081 case KVM_ARM_SET_COUNTER_OFFSET: { 2082 struct kvm_arm_counter_offset offset; 2083 2084 if (copy_from_user(&offset, argp, sizeof(offset))) 2085 return -EFAULT; 2086 return kvm_vm_ioctl_set_counter_offset(kvm, &offset); 2087 } 2088 case KVM_HAS_DEVICE_ATTR: { 2089 if (copy_from_user(&attr, argp, sizeof(attr))) 2090 return -EFAULT; 2091 2092 return kvm_vm_has_attr(kvm, &attr); 2093 } 2094 case KVM_SET_DEVICE_ATTR: { 2095 if (copy_from_user(&attr, argp, sizeof(attr))) 2096 return -EFAULT; 2097 2098 return kvm_vm_set_attr(kvm, &attr); 2099 } 2100 case KVM_ARM_GET_REG_WRITABLE_MASKS: { 2101 struct reg_mask_range range; 2102 2103 if (copy_from_user(&range, argp, sizeof(range))) 2104 return -EFAULT; 2105 return kvm_vm_ioctl_get_reg_writable_masks(kvm, &range); 2106 } 2107 default: 2108 return -EINVAL; 2109 } 2110 } 2111 2112 static unsigned long nvhe_percpu_size(void) 2113 { 2114 return (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_end) - 2115 (unsigned long)CHOOSE_NVHE_SYM(__per_cpu_start); 2116 } 2117 2118 static unsigned long nvhe_percpu_order(void) 2119 { 2120 unsigned long size = nvhe_percpu_size(); 2121 2122 return size ? get_order(size) : 0; 2123 } 2124 2125 static size_t pkvm_host_sve_state_order(void) 2126 { 2127 return get_order(pkvm_host_sve_state_size()); 2128 } 2129 2130 /* A lookup table holding the hypervisor VA for each vector slot */ 2131 static void *hyp_spectre_vector_selector[BP_HARDEN_EL2_SLOTS]; 2132 2133 static void kvm_init_vector_slot(void *base, enum arm64_hyp_spectre_vector slot) 2134 { 2135 hyp_spectre_vector_selector[slot] = __kvm_vector_slot2addr(base, slot); 2136 } 2137 2138 static int kvm_init_vector_slots(void) 2139 { 2140 int err; 2141 void *base; 2142 2143 base = kern_hyp_va(kvm_ksym_ref(__kvm_hyp_vector)); 2144 kvm_init_vector_slot(base, HYP_VECTOR_DIRECT); 2145 2146 base = kern_hyp_va(kvm_ksym_ref(__bp_harden_hyp_vecs)); 2147 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_DIRECT); 2148 2149 if (kvm_system_needs_idmapped_vectors() && 2150 !is_protected_kvm_enabled()) { 2151 err = create_hyp_exec_mappings(__pa_symbol(__bp_harden_hyp_vecs), 2152 __BP_HARDEN_HYP_VECS_SZ, &base); 2153 if (err) 2154 return err; 2155 } 2156 2157 kvm_init_vector_slot(base, HYP_VECTOR_INDIRECT); 2158 kvm_init_vector_slot(base, HYP_VECTOR_SPECTRE_INDIRECT); 2159 return 0; 2160 } 2161 2162 static void __init cpu_prepare_hyp_mode(int cpu, u32 hyp_va_bits) 2163 { 2164 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 2165 unsigned long tcr; 2166 2167 /* 2168 * Calculate the raw per-cpu offset without a translation from the 2169 * kernel's mapping to the linear mapping, and store it in tpidr_el2 2170 * so that we can use adr_l to access per-cpu variables in EL2. 2171 * Also drop the KASAN tag which gets in the way... 2172 */ 2173 params->tpidr_el2 = (unsigned long)kasan_reset_tag(per_cpu_ptr_nvhe_sym(__per_cpu_start, cpu)) - 2174 (unsigned long)kvm_ksym_ref(CHOOSE_NVHE_SYM(__per_cpu_start)); 2175 2176 params->mair_el2 = read_sysreg(mair_el1); 2177 2178 tcr = read_sysreg(tcr_el1); 2179 if (cpus_have_final_cap(ARM64_KVM_HVHE)) { 2180 tcr &= ~(TCR_HD | TCR_HA | TCR_A1 | TCR_T0SZ_MASK); 2181 tcr |= TCR_EPD1_MASK; 2182 } else { 2183 unsigned long ips = FIELD_GET(TCR_IPS_MASK, tcr); 2184 2185 tcr &= TCR_EL2_MASK; 2186 tcr |= TCR_EL2_RES1 | FIELD_PREP(TCR_EL2_PS_MASK, ips); 2187 if (lpa2_is_enabled()) 2188 tcr |= TCR_EL2_DS; 2189 } 2190 tcr |= TCR_T0SZ(hyp_va_bits); 2191 params->tcr_el2 = tcr; 2192 2193 params->pgd_pa = kvm_mmu_get_httbr(); 2194 if (is_protected_kvm_enabled()) 2195 params->hcr_el2 = HCR_HOST_NVHE_PROTECTED_FLAGS; 2196 else 2197 params->hcr_el2 = HCR_HOST_NVHE_FLAGS; 2198 2199 if (system_supports_mte()) 2200 params->hcr_el2 |= HCR_ATA; 2201 else 2202 params->hcr_el2 |= HCR_TID5; 2203 2204 if (cpus_have_final_cap(ARM64_KVM_HVHE)) 2205 params->hcr_el2 |= HCR_E2H; 2206 params->vttbr = params->vtcr = 0; 2207 2208 /* 2209 * Flush the init params from the data cache because the struct will 2210 * be read while the MMU is off. 2211 */ 2212 kvm_flush_dcache_to_poc(params, sizeof(*params)); 2213 } 2214 2215 static void hyp_install_host_vector(void) 2216 { 2217 struct kvm_nvhe_init_params *params; 2218 struct arm_smccc_res res; 2219 2220 /* Switch from the HYP stub to our own HYP init vector */ 2221 __hyp_set_vectors(kvm_get_idmap_vector()); 2222 2223 /* 2224 * Call initialization code, and switch to the full blown HYP code. 2225 * If the cpucaps haven't been finalized yet, something has gone very 2226 * wrong, and hyp will crash and burn when it uses any 2227 * cpus_have_*_cap() wrapper. 2228 */ 2229 BUG_ON(!system_capabilities_finalized()); 2230 params = this_cpu_ptr_nvhe_sym(kvm_init_params); 2231 arm_smccc_1_1_hvc(KVM_HOST_SMCCC_FUNC(__kvm_hyp_init), virt_to_phys(params), &res); 2232 WARN_ON(res.a0 != SMCCC_RET_SUCCESS); 2233 } 2234 2235 static void cpu_init_hyp_mode(void) 2236 { 2237 hyp_install_host_vector(); 2238 2239 /* 2240 * Disabling SSBD on a non-VHE system requires us to enable SSBS 2241 * at EL2. 2242 */ 2243 if (this_cpu_has_cap(ARM64_SSBS) && 2244 arm64_get_spectre_v4_state() == SPECTRE_VULNERABLE) { 2245 kvm_call_hyp_nvhe(__kvm_enable_ssbs); 2246 } 2247 } 2248 2249 static void cpu_hyp_reset(void) 2250 { 2251 if (!is_kernel_in_hyp_mode()) 2252 __hyp_reset_vectors(); 2253 } 2254 2255 /* 2256 * EL2 vectors can be mapped and rerouted in a number of ways, 2257 * depending on the kernel configuration and CPU present: 2258 * 2259 * - If the CPU is affected by Spectre-v2, the hardening sequence is 2260 * placed in one of the vector slots, which is executed before jumping 2261 * to the real vectors. 2262 * 2263 * - If the CPU also has the ARM64_SPECTRE_V3A cap, the slot 2264 * containing the hardening sequence is mapped next to the idmap page, 2265 * and executed before jumping to the real vectors. 2266 * 2267 * - If the CPU only has the ARM64_SPECTRE_V3A cap, then an 2268 * empty slot is selected, mapped next to the idmap page, and 2269 * executed before jumping to the real vectors. 2270 * 2271 * Note that ARM64_SPECTRE_V3A is somewhat incompatible with 2272 * VHE, as we don't have hypervisor-specific mappings. If the system 2273 * is VHE and yet selects this capability, it will be ignored. 2274 */ 2275 static void cpu_set_hyp_vector(void) 2276 { 2277 struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data); 2278 void *vector = hyp_spectre_vector_selector[data->slot]; 2279 2280 if (!is_protected_kvm_enabled()) 2281 *this_cpu_ptr_hyp_sym(kvm_hyp_vector) = (unsigned long)vector; 2282 else 2283 kvm_call_hyp_nvhe(__pkvm_cpu_set_vector, data->slot); 2284 } 2285 2286 static void cpu_hyp_init_context(void) 2287 { 2288 kvm_init_host_cpu_context(host_data_ptr(host_ctxt)); 2289 kvm_init_host_debug_data(); 2290 2291 if (!is_kernel_in_hyp_mode()) 2292 cpu_init_hyp_mode(); 2293 } 2294 2295 static void cpu_hyp_init_features(void) 2296 { 2297 cpu_set_hyp_vector(); 2298 2299 if (is_kernel_in_hyp_mode()) { 2300 kvm_timer_init_vhe(); 2301 kvm_debug_init_vhe(); 2302 } 2303 2304 if (vgic_present) 2305 kvm_vgic_init_cpu_hardware(); 2306 } 2307 2308 static void cpu_hyp_reinit(void) 2309 { 2310 cpu_hyp_reset(); 2311 cpu_hyp_init_context(); 2312 cpu_hyp_init_features(); 2313 } 2314 2315 static void cpu_hyp_init(void *discard) 2316 { 2317 if (!__this_cpu_read(kvm_hyp_initialized)) { 2318 cpu_hyp_reinit(); 2319 __this_cpu_write(kvm_hyp_initialized, 1); 2320 } 2321 } 2322 2323 static void cpu_hyp_uninit(void *discard) 2324 { 2325 if (!is_protected_kvm_enabled() && __this_cpu_read(kvm_hyp_initialized)) { 2326 cpu_hyp_reset(); 2327 __this_cpu_write(kvm_hyp_initialized, 0); 2328 } 2329 } 2330 2331 int kvm_arch_enable_virtualization_cpu(void) 2332 { 2333 /* 2334 * Most calls to this function are made with migration 2335 * disabled, but not with preemption disabled. The former is 2336 * enough to ensure correctness, but most of the helpers 2337 * expect the later and will throw a tantrum otherwise. 2338 */ 2339 preempt_disable(); 2340 2341 cpu_hyp_init(NULL); 2342 2343 kvm_vgic_cpu_up(); 2344 kvm_timer_cpu_up(); 2345 2346 preempt_enable(); 2347 2348 return 0; 2349 } 2350 2351 void kvm_arch_disable_virtualization_cpu(void) 2352 { 2353 kvm_timer_cpu_down(); 2354 kvm_vgic_cpu_down(); 2355 2356 if (!is_protected_kvm_enabled()) 2357 cpu_hyp_uninit(NULL); 2358 } 2359 2360 #ifdef CONFIG_CPU_PM 2361 static int hyp_init_cpu_pm_notifier(struct notifier_block *self, 2362 unsigned long cmd, 2363 void *v) 2364 { 2365 /* 2366 * kvm_hyp_initialized is left with its old value over 2367 * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should 2368 * re-enable hyp. 2369 */ 2370 switch (cmd) { 2371 case CPU_PM_ENTER: 2372 if (__this_cpu_read(kvm_hyp_initialized)) 2373 /* 2374 * don't update kvm_hyp_initialized here 2375 * so that the hyp will be re-enabled 2376 * when we resume. See below. 2377 */ 2378 cpu_hyp_reset(); 2379 2380 return NOTIFY_OK; 2381 case CPU_PM_ENTER_FAILED: 2382 case CPU_PM_EXIT: 2383 if (__this_cpu_read(kvm_hyp_initialized)) 2384 /* The hyp was enabled before suspend. */ 2385 cpu_hyp_reinit(); 2386 2387 return NOTIFY_OK; 2388 2389 default: 2390 return NOTIFY_DONE; 2391 } 2392 } 2393 2394 static struct notifier_block hyp_init_cpu_pm_nb = { 2395 .notifier_call = hyp_init_cpu_pm_notifier, 2396 }; 2397 2398 static void __init hyp_cpu_pm_init(void) 2399 { 2400 if (!is_protected_kvm_enabled()) 2401 cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); 2402 } 2403 static void __init hyp_cpu_pm_exit(void) 2404 { 2405 if (!is_protected_kvm_enabled()) 2406 cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); 2407 } 2408 #else 2409 static inline void __init hyp_cpu_pm_init(void) 2410 { 2411 } 2412 static inline void __init hyp_cpu_pm_exit(void) 2413 { 2414 } 2415 #endif 2416 2417 static void __init init_cpu_logical_map(void) 2418 { 2419 unsigned int cpu; 2420 2421 /* 2422 * Copy the MPIDR <-> logical CPU ID mapping to hyp. 2423 * Only copy the set of online CPUs whose features have been checked 2424 * against the finalized system capabilities. The hypervisor will not 2425 * allow any other CPUs from the `possible` set to boot. 2426 */ 2427 for_each_online_cpu(cpu) 2428 hyp_cpu_logical_map[cpu] = cpu_logical_map(cpu); 2429 } 2430 2431 #define init_psci_0_1_impl_state(config, what) \ 2432 config.psci_0_1_ ## what ## _implemented = psci_ops.what 2433 2434 static bool __init init_psci_relay(void) 2435 { 2436 /* 2437 * If PSCI has not been initialized, protected KVM cannot install 2438 * itself on newly booted CPUs. 2439 */ 2440 if (!psci_ops.get_version) { 2441 kvm_err("Cannot initialize protected mode without PSCI\n"); 2442 return false; 2443 } 2444 2445 kvm_host_psci_config.version = psci_ops.get_version(); 2446 kvm_host_psci_config.smccc_version = arm_smccc_get_version(); 2447 2448 if (kvm_host_psci_config.version == PSCI_VERSION(0, 1)) { 2449 kvm_host_psci_config.function_ids_0_1 = get_psci_0_1_function_ids(); 2450 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_suspend); 2451 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_on); 2452 init_psci_0_1_impl_state(kvm_host_psci_config, cpu_off); 2453 init_psci_0_1_impl_state(kvm_host_psci_config, migrate); 2454 } 2455 return true; 2456 } 2457 2458 static int __init init_subsystems(void) 2459 { 2460 int err = 0; 2461 2462 /* 2463 * Enable hardware so that subsystem initialisation can access EL2. 2464 */ 2465 on_each_cpu(cpu_hyp_init, NULL, 1); 2466 2467 /* 2468 * Register CPU lower-power notifier 2469 */ 2470 hyp_cpu_pm_init(); 2471 2472 /* 2473 * Init HYP view of VGIC 2474 */ 2475 err = kvm_vgic_hyp_init(); 2476 switch (err) { 2477 case 0: 2478 vgic_present = true; 2479 if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif)) 2480 kvm_nvhe_sym(hyp_gicv3_nr_lr) = kvm_vgic_global_state.nr_lr; 2481 break; 2482 case -ENODEV: 2483 case -ENXIO: 2484 /* 2485 * No VGIC? No pKVM for you. 2486 * 2487 * Protected mode assumes that VGICv3 is present, so no point 2488 * in trying to hobble along if vgic initialization fails. 2489 */ 2490 if (is_protected_kvm_enabled()) 2491 goto out; 2492 2493 /* 2494 * Otherwise, userspace could choose to implement a GIC for its 2495 * guest on non-cooperative hardware. 2496 */ 2497 vgic_present = false; 2498 err = 0; 2499 break; 2500 default: 2501 goto out; 2502 } 2503 2504 if (kvm_mode == KVM_MODE_NV && 2505 !(vgic_present && (kvm_vgic_global_state.type == VGIC_V3 || 2506 kvm_vgic_global_state.has_gcie_v3_compat))) { 2507 kvm_err("NV support requires GICv3 or GICv5 with legacy support, giving up\n"); 2508 err = -EINVAL; 2509 goto out; 2510 } 2511 2512 /* 2513 * Init HYP architected timer support 2514 */ 2515 err = kvm_timer_hyp_init(vgic_present); 2516 if (err) 2517 goto out; 2518 2519 kvm_register_perf_callbacks(); 2520 2521 err = kvm_hyp_trace_init(); 2522 if (err) 2523 kvm_err("Failed to initialize Hyp tracing\n"); 2524 2525 out: 2526 if (err) 2527 hyp_cpu_pm_exit(); 2528 2529 if (err || !is_protected_kvm_enabled()) 2530 on_each_cpu(cpu_hyp_uninit, NULL, 1); 2531 2532 return err; 2533 } 2534 2535 static void __init teardown_subsystems(void) 2536 { 2537 kvm_unregister_perf_callbacks(); 2538 hyp_cpu_pm_exit(); 2539 } 2540 2541 static void __init teardown_hyp_mode(void) 2542 { 2543 bool free_sve = system_supports_sve() && is_protected_kvm_enabled(); 2544 int cpu; 2545 2546 free_hyp_pgds(); 2547 for_each_possible_cpu(cpu) { 2548 if (per_cpu(kvm_hyp_initialized, cpu)) 2549 continue; 2550 2551 free_pages(per_cpu(kvm_arm_hyp_stack_base, cpu), NVHE_STACK_SHIFT - PAGE_SHIFT); 2552 2553 if (!kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]) 2554 continue; 2555 2556 if (free_sve) { 2557 struct arm64_sve_state *sve_regs; 2558 2559 sve_regs = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_regs; 2560 free_pages((unsigned long) sve_regs, pkvm_host_sve_state_order()); 2561 } 2562 2563 free_pages(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu], nvhe_percpu_order()); 2564 2565 } 2566 } 2567 2568 static int __init do_pkvm_init(u32 hyp_va_bits) 2569 { 2570 void *per_cpu_base = kvm_ksym_ref(kvm_nvhe_sym(kvm_arm_hyp_percpu_base)); 2571 int ret; 2572 2573 preempt_disable(); 2574 cpu_hyp_init_context(); 2575 ret = kvm_call_hyp_nvhe(__pkvm_init, hyp_mem_base, hyp_mem_size, 2576 kern_hyp_va(per_cpu_base), 2577 hyp_va_bits); 2578 cpu_hyp_init_features(); 2579 2580 /* 2581 * The stub hypercalls are now disabled, so set our local flag to 2582 * prevent a later re-init attempt in kvm_arch_enable_virtualization_cpu(). 2583 */ 2584 __this_cpu_write(kvm_hyp_initialized, 1); 2585 preempt_enable(); 2586 2587 return ret; 2588 } 2589 2590 static u64 get_hyp_id_aa64pfr0_el1(void) 2591 { 2592 /* 2593 * Track whether the system isn't affected by spectre/meltdown in the 2594 * hypervisor's view of id_aa64pfr0_el1, used for protected VMs. 2595 * Although this is per-CPU, we make it global for simplicity, e.g., not 2596 * to have to worry about vcpu migration. 2597 * 2598 * Unlike for non-protected VMs, userspace cannot override this for 2599 * protected VMs. 2600 */ 2601 u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); 2602 2603 val &= ~(ID_AA64PFR0_EL1_CSV2 | 2604 ID_AA64PFR0_EL1_CSV3); 2605 2606 val |= FIELD_PREP(ID_AA64PFR0_EL1_CSV2, 2607 arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED); 2608 val |= FIELD_PREP(ID_AA64PFR0_EL1_CSV3, 2609 arm64_get_meltdown_state() == SPECTRE_UNAFFECTED); 2610 2611 return val; 2612 } 2613 2614 static void kvm_hyp_init_symbols(void) 2615 { 2616 kvm_nvhe_sym(id_aa64pfr0_el1_sys_val) = get_hyp_id_aa64pfr0_el1(); 2617 kvm_nvhe_sym(id_aa64pfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1); 2618 kvm_nvhe_sym(id_aa64pfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64PFR2_EL1); 2619 kvm_nvhe_sym(id_aa64isar0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR0_EL1); 2620 kvm_nvhe_sym(id_aa64isar1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1); 2621 kvm_nvhe_sym(id_aa64isar2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1); 2622 kvm_nvhe_sym(id_aa64mmfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); 2623 kvm_nvhe_sym(id_aa64mmfr1_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 2624 kvm_nvhe_sym(id_aa64mmfr2_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64MMFR2_EL1); 2625 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val) = read_sanitised_ftr_reg(SYS_ID_AA64SMFR0_EL1); 2626 kvm_nvhe_sym(__icache_flags) = __icache_flags; 2627 kvm_nvhe_sym(kvm_arm_vmid_bits) = kvm_arm_vmid_bits; 2628 2629 /* Propagate the FGT state to the nVHE side */ 2630 kvm_nvhe_sym(hfgrtr_masks) = hfgrtr_masks; 2631 kvm_nvhe_sym(hfgwtr_masks) = hfgwtr_masks; 2632 kvm_nvhe_sym(hfgitr_masks) = hfgitr_masks; 2633 kvm_nvhe_sym(hdfgrtr_masks) = hdfgrtr_masks; 2634 kvm_nvhe_sym(hdfgwtr_masks) = hdfgwtr_masks; 2635 kvm_nvhe_sym(hafgrtr_masks) = hafgrtr_masks; 2636 kvm_nvhe_sym(hfgrtr2_masks) = hfgrtr2_masks; 2637 kvm_nvhe_sym(hfgwtr2_masks) = hfgwtr2_masks; 2638 kvm_nvhe_sym(hfgitr2_masks) = hfgitr2_masks; 2639 kvm_nvhe_sym(hdfgrtr2_masks)= hdfgrtr2_masks; 2640 kvm_nvhe_sym(hdfgwtr2_masks)= hdfgwtr2_masks; 2641 kvm_nvhe_sym(ich_hfgrtr_masks) = ich_hfgrtr_masks; 2642 kvm_nvhe_sym(ich_hfgwtr_masks) = ich_hfgwtr_masks; 2643 kvm_nvhe_sym(ich_hfgitr_masks) = ich_hfgitr_masks; 2644 2645 /* 2646 * Flush entire BSS since part of its data containing init symbols is read 2647 * while the MMU is off. 2648 */ 2649 kvm_flush_dcache_to_poc(kvm_ksym_ref(__hyp_bss_start), 2650 kvm_ksym_ref(__hyp_bss_end) - kvm_ksym_ref(__hyp_bss_start)); 2651 } 2652 2653 static int __init kvm_hyp_init_protection(u32 hyp_va_bits) 2654 { 2655 void *addr = phys_to_virt(hyp_mem_base); 2656 int ret; 2657 2658 ret = create_hyp_mappings(addr, addr + hyp_mem_size, PAGE_HYP); 2659 if (ret) 2660 return ret; 2661 2662 ret = do_pkvm_init(hyp_va_bits); 2663 if (ret) 2664 return ret; 2665 2666 free_hyp_pgds(); 2667 2668 return 0; 2669 } 2670 2671 static int init_pkvm_host_sve_state(void) 2672 { 2673 int cpu; 2674 2675 if (!system_supports_sve()) 2676 return 0; 2677 2678 /* Allocate pages for host sve state in protected mode. */ 2679 for_each_possible_cpu(cpu) { 2680 struct page *page = alloc_pages(GFP_KERNEL, pkvm_host_sve_state_order()); 2681 2682 if (!page) 2683 return -ENOMEM; 2684 2685 per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_regs = page_address(page); 2686 } 2687 2688 /* 2689 * Don't map the pages in hyp since these are only used in protected 2690 * mode, which will (re)create its own mapping when initialized. 2691 */ 2692 2693 return 0; 2694 } 2695 2696 static int pkvm_check_sme_dvmsync_fw_call(void) 2697 { 2698 struct arm_smccc_res res; 2699 2700 if (!cpus_have_final_cap(ARM64_WORKAROUND_4193714)) 2701 return 0; 2702 2703 arm_smccc_1_1_smc(ARM_SMCCC_CPU_WORKAROUND_4193714, &res); 2704 if (res.a0) { 2705 kvm_err("pKVM requires firmware support for C1-Pro erratum 4193714\n"); 2706 return -ENODEV; 2707 } 2708 2709 return 0; 2710 } 2711 2712 /* 2713 * Finalizes the initialization of hyp mode, once everything else is initialized 2714 * and the initialziation process cannot fail. 2715 */ 2716 static void finalize_init_hyp_mode(void) 2717 { 2718 int cpu; 2719 2720 if (system_supports_sve() && is_protected_kvm_enabled()) { 2721 for_each_possible_cpu(cpu) { 2722 struct arm64_sve_state *sve_regs; 2723 2724 sve_regs = per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_regs; 2725 per_cpu_ptr_nvhe_sym(kvm_host_data, cpu)->sve_regs = 2726 kern_hyp_va(sve_regs); 2727 } 2728 } 2729 } 2730 2731 static void pkvm_hyp_init_ptrauth(void) 2732 { 2733 struct kvm_cpu_context *hyp_ctxt; 2734 int cpu; 2735 2736 for_each_possible_cpu(cpu) { 2737 hyp_ctxt = per_cpu_ptr_nvhe_sym(kvm_hyp_ctxt, cpu); 2738 hyp_ctxt->sys_regs[APIAKEYLO_EL1] = get_random_long(); 2739 hyp_ctxt->sys_regs[APIAKEYHI_EL1] = get_random_long(); 2740 hyp_ctxt->sys_regs[APIBKEYLO_EL1] = get_random_long(); 2741 hyp_ctxt->sys_regs[APIBKEYHI_EL1] = get_random_long(); 2742 hyp_ctxt->sys_regs[APDAKEYLO_EL1] = get_random_long(); 2743 hyp_ctxt->sys_regs[APDAKEYHI_EL1] = get_random_long(); 2744 hyp_ctxt->sys_regs[APDBKEYLO_EL1] = get_random_long(); 2745 hyp_ctxt->sys_regs[APDBKEYHI_EL1] = get_random_long(); 2746 hyp_ctxt->sys_regs[APGAKEYLO_EL1] = get_random_long(); 2747 hyp_ctxt->sys_regs[APGAKEYHI_EL1] = get_random_long(); 2748 } 2749 } 2750 2751 /* Inits Hyp-mode on all online CPUs */ 2752 static int __init init_hyp_mode(void) 2753 { 2754 u32 hyp_va_bits = kvm_hyp_va_bits(); 2755 int cpu; 2756 int err = -ENOMEM; 2757 2758 /* 2759 * The protected Hyp-mode cannot be initialized if the memory pool 2760 * allocation has failed. 2761 */ 2762 if (is_protected_kvm_enabled() && !hyp_mem_base) 2763 goto out_err; 2764 2765 /* 2766 * Allocate Hyp PGD and setup Hyp identity mapping 2767 */ 2768 err = kvm_mmu_init(hyp_va_bits); 2769 if (err) 2770 goto out_err; 2771 2772 /* 2773 * Allocate stack pages for Hypervisor-mode 2774 */ 2775 for_each_possible_cpu(cpu) { 2776 unsigned long stack_base; 2777 2778 stack_base = __get_free_pages(GFP_KERNEL, NVHE_STACK_SHIFT - PAGE_SHIFT); 2779 if (!stack_base) { 2780 err = -ENOMEM; 2781 goto out_err; 2782 } 2783 2784 per_cpu(kvm_arm_hyp_stack_base, cpu) = stack_base; 2785 } 2786 2787 /* 2788 * Allocate and initialize pages for Hypervisor-mode percpu regions. 2789 */ 2790 for_each_possible_cpu(cpu) { 2791 struct page *page; 2792 void *page_addr; 2793 2794 page = alloc_pages(GFP_KERNEL, nvhe_percpu_order()); 2795 if (!page) { 2796 err = -ENOMEM; 2797 goto out_err; 2798 } 2799 2800 page_addr = page_address(page); 2801 memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size()); 2802 kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr; 2803 } 2804 2805 kvm_nvhe_sym(hyp_nr_cpus) = num_possible_cpus(); 2806 2807 /* 2808 * Map the Hyp-code called directly from the host 2809 */ 2810 err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), 2811 kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); 2812 if (err) { 2813 kvm_err("Cannot map world-switch code\n"); 2814 goto out_err; 2815 } 2816 2817 err = create_hyp_mappings(kvm_ksym_ref(__hyp_data_start), 2818 kvm_ksym_ref(__hyp_data_end), PAGE_HYP); 2819 if (err) { 2820 kvm_err("Cannot map .hyp.data section\n"); 2821 goto out_err; 2822 } 2823 2824 err = create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start), 2825 kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO); 2826 if (err) { 2827 kvm_err("Cannot map .hyp.rodata section\n"); 2828 goto out_err; 2829 } 2830 2831 err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), 2832 kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); 2833 if (err) { 2834 kvm_err("Cannot map rodata section\n"); 2835 goto out_err; 2836 } 2837 2838 /* 2839 * .hyp.bss is guaranteed to be placed at the beginning of the .bss 2840 * section thanks to an assertion in the linker script. Map it RW and 2841 * the rest of .bss RO. 2842 */ 2843 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_start), 2844 kvm_ksym_ref(__hyp_bss_end), PAGE_HYP); 2845 if (err) { 2846 kvm_err("Cannot map hyp bss section: %d\n", err); 2847 goto out_err; 2848 } 2849 2850 err = create_hyp_mappings(kvm_ksym_ref(__hyp_bss_end), 2851 kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); 2852 if (err) { 2853 kvm_err("Cannot map bss section\n"); 2854 goto out_err; 2855 } 2856 2857 /* 2858 * Map the Hyp stack pages 2859 */ 2860 for_each_possible_cpu(cpu) { 2861 struct kvm_nvhe_init_params *params = per_cpu_ptr_nvhe_sym(kvm_init_params, cpu); 2862 char *stack_base = (char *)per_cpu(kvm_arm_hyp_stack_base, cpu); 2863 2864 err = create_hyp_stack(__pa(stack_base), ¶ms->stack_hyp_va); 2865 if (err) { 2866 kvm_err("Cannot map hyp stack\n"); 2867 goto out_err; 2868 } 2869 2870 /* 2871 * Save the stack PA in nvhe_init_params. This will be needed 2872 * to recreate the stack mapping in protected nVHE mode. 2873 * __hyp_pa() won't do the right thing there, since the stack 2874 * has been mapped in the flexible private VA space. 2875 */ 2876 params->stack_pa = __pa(stack_base); 2877 } 2878 2879 for_each_possible_cpu(cpu) { 2880 char *percpu_begin = (char *)kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu]; 2881 char *percpu_end = percpu_begin + nvhe_percpu_size(); 2882 2883 /* Map Hyp percpu pages */ 2884 err = create_hyp_mappings(percpu_begin, percpu_end, PAGE_HYP); 2885 if (err) { 2886 kvm_err("Cannot map hyp percpu region\n"); 2887 goto out_err; 2888 } 2889 2890 /* Prepare the CPU initialization parameters */ 2891 cpu_prepare_hyp_mode(cpu, hyp_va_bits); 2892 } 2893 2894 kvm_hyp_init_symbols(); 2895 2896 if (is_protected_kvm_enabled()) { 2897 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) && 2898 cpus_have_final_cap(ARM64_HAS_ADDRESS_AUTH)) 2899 pkvm_hyp_init_ptrauth(); 2900 2901 init_cpu_logical_map(); 2902 2903 if (!init_psci_relay()) { 2904 err = -ENODEV; 2905 goto out_err; 2906 } 2907 2908 err = init_pkvm_host_sve_state(); 2909 if (err) 2910 goto out_err; 2911 2912 err = pkvm_check_sme_dvmsync_fw_call(); 2913 if (err) 2914 goto out_err; 2915 2916 err = kvm_hyp_init_protection(hyp_va_bits); 2917 if (err) { 2918 kvm_err("Failed to init hyp memory protection\n"); 2919 goto out_err; 2920 } 2921 } 2922 2923 return 0; 2924 2925 out_err: 2926 teardown_hyp_mode(); 2927 kvm_err("error initializing Hyp mode: %d\n", err); 2928 return err; 2929 } 2930 2931 struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) 2932 { 2933 struct kvm_vcpu *vcpu = NULL; 2934 struct kvm_mpidr_data *data; 2935 unsigned long i; 2936 2937 mpidr &= MPIDR_HWID_BITMASK; 2938 2939 rcu_read_lock(); 2940 data = rcu_dereference(kvm->arch.mpidr_data); 2941 2942 if (data) { 2943 u16 idx = kvm_mpidr_index(data, mpidr); 2944 2945 vcpu = kvm_get_vcpu(kvm, data->cmpidr_to_idx[idx]); 2946 if (mpidr != kvm_vcpu_get_mpidr_aff(vcpu)) 2947 vcpu = NULL; 2948 } 2949 2950 rcu_read_unlock(); 2951 2952 if (vcpu) 2953 return vcpu; 2954 2955 kvm_for_each_vcpu(i, vcpu, kvm) { 2956 if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) 2957 return vcpu; 2958 } 2959 return NULL; 2960 } 2961 2962 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) 2963 { 2964 return irqchip_in_kernel(kvm); 2965 } 2966 2967 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, 2968 struct irq_bypass_producer *prod) 2969 { 2970 struct kvm_kernel_irqfd *irqfd = 2971 container_of(cons, struct kvm_kernel_irqfd, consumer); 2972 struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry; 2973 2974 /* 2975 * The only thing we have a chance of directly-injecting is LPIs. Maybe 2976 * one day... 2977 */ 2978 if (irq_entry->type != KVM_IRQ_ROUTING_MSI) 2979 return 0; 2980 2981 return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, 2982 &irqfd->irq_entry); 2983 } 2984 2985 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, 2986 struct irq_bypass_producer *prod) 2987 { 2988 struct kvm_kernel_irqfd *irqfd = 2989 container_of(cons, struct kvm_kernel_irqfd, consumer); 2990 struct kvm_kernel_irq_routing_entry *irq_entry = &irqfd->irq_entry; 2991 2992 if (irq_entry->type != KVM_IRQ_ROUTING_MSI) 2993 return; 2994 2995 kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq); 2996 } 2997 2998 void kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, 2999 struct kvm_kernel_irq_routing_entry *old, 3000 struct kvm_kernel_irq_routing_entry *new) 3001 { 3002 if (old->type == KVM_IRQ_ROUTING_MSI && 3003 new->type == KVM_IRQ_ROUTING_MSI && 3004 !memcmp(&old->msi, &new->msi, sizeof(new->msi))) 3005 return; 3006 3007 /* 3008 * Remapping the vLPI requires taking the its_lock mutex to resolve 3009 * the new translation. We're in spinlock land at this point, so no 3010 * chance of resolving the translation. 3011 * 3012 * Unmap the vLPI and fall back to software LPI injection. 3013 */ 3014 return kvm_vgic_v4_unset_forwarding(irqfd->kvm, irqfd->producer->irq); 3015 } 3016 3017 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) 3018 { 3019 struct kvm_kernel_irqfd *irqfd = 3020 container_of(cons, struct kvm_kernel_irqfd, consumer); 3021 3022 kvm_arm_halt_guest(irqfd->kvm); 3023 } 3024 3025 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) 3026 { 3027 struct kvm_kernel_irqfd *irqfd = 3028 container_of(cons, struct kvm_kernel_irqfd, consumer); 3029 3030 kvm_arm_resume_guest(irqfd->kvm); 3031 } 3032 3033 /* Initialize Hyp-mode and memory mappings on all CPUs */ 3034 static __init int kvm_arm_init(void) 3035 { 3036 int err; 3037 bool in_hyp_mode; 3038 3039 if (!is_hyp_mode_available()) { 3040 kvm_info("HYP mode not available\n"); 3041 return -ENODEV; 3042 } 3043 3044 if (kvm_get_mode() == KVM_MODE_NONE) { 3045 kvm_info("KVM disabled from command line\n"); 3046 return -ENODEV; 3047 } 3048 3049 err = kvm_sys_reg_table_init(); 3050 if (err) { 3051 kvm_info("Error initializing system register tables"); 3052 return err; 3053 } 3054 3055 in_hyp_mode = is_kernel_in_hyp_mode(); 3056 3057 if (cpus_have_final_cap(ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE) || 3058 cpus_have_final_cap(ARM64_WORKAROUND_1508412)) 3059 kvm_info("Guests without required CPU erratum workarounds can deadlock system!\n" \ 3060 "Only trusted guests should be used on this system.\n"); 3061 3062 err = kvm_set_ipa_limit(); 3063 if (err) 3064 return err; 3065 3066 err = kvm_arm_init_sve(); 3067 if (err) 3068 return err; 3069 3070 err = kvm_arm_vmid_alloc_init(); 3071 if (err) { 3072 kvm_err("Failed to initialize VMID allocator.\n"); 3073 return err; 3074 } 3075 3076 if (!in_hyp_mode) { 3077 err = init_hyp_mode(); 3078 if (err) 3079 goto out_err; 3080 } 3081 3082 err = kvm_init_vector_slots(); 3083 if (err) { 3084 kvm_err("Cannot initialise vector slots\n"); 3085 goto out_hyp; 3086 } 3087 3088 err = init_subsystems(); 3089 if (err) 3090 goto out_hyp; 3091 3092 kvm_info("%s%sVHE%s mode initialized successfully\n", 3093 in_hyp_mode ? "" : (is_protected_kvm_enabled() ? 3094 "Protected " : "Hyp "), 3095 in_hyp_mode ? "" : (cpus_have_final_cap(ARM64_KVM_HVHE) ? 3096 "h" : "n"), 3097 cpus_have_final_cap(ARM64_HAS_NESTED_VIRT) ? "+NV2": ""); 3098 3099 /* 3100 * FIXME: Do something reasonable if kvm_init() fails after pKVM 3101 * hypervisor protection is finalized. 3102 */ 3103 err = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE); 3104 if (err) 3105 goto out_subs; 3106 3107 /* 3108 * This should be called after initialization is done and failure isn't 3109 * possible anymore. 3110 */ 3111 if (!in_hyp_mode) 3112 finalize_init_hyp_mode(); 3113 3114 kvm_arm_initialised = true; 3115 3116 return 0; 3117 3118 out_subs: 3119 teardown_subsystems(); 3120 out_hyp: 3121 if (!in_hyp_mode) 3122 teardown_hyp_mode(); 3123 out_err: 3124 kvm_arm_vmid_alloc_free(); 3125 return err; 3126 } 3127 3128 static int __init early_kvm_mode_cfg(char *arg) 3129 { 3130 if (!arg) 3131 return -EINVAL; 3132 3133 if (strcmp(arg, "none") == 0) { 3134 kvm_mode = KVM_MODE_NONE; 3135 return 0; 3136 } 3137 3138 if (!is_hyp_mode_available()) { 3139 pr_warn_once("KVM is not available. Ignoring kvm-arm.mode\n"); 3140 return 0; 3141 } 3142 3143 if (strcmp(arg, "protected") == 0) { 3144 if (!is_kernel_in_hyp_mode()) 3145 kvm_mode = KVM_MODE_PROTECTED; 3146 else 3147 pr_warn_once("Protected KVM not available with VHE\n"); 3148 3149 return 0; 3150 } 3151 3152 if (strcmp(arg, "nvhe") == 0 && !WARN_ON(is_kernel_in_hyp_mode())) { 3153 kvm_mode = KVM_MODE_DEFAULT; 3154 return 0; 3155 } 3156 3157 if (strcmp(arg, "nested") == 0 && !WARN_ON(!is_kernel_in_hyp_mode())) { 3158 kvm_mode = KVM_MODE_NV; 3159 return 0; 3160 } 3161 3162 return -EINVAL; 3163 } 3164 early_param("kvm-arm.mode", early_kvm_mode_cfg); 3165 3166 static int __init early_kvm_wfx_trap_policy_cfg(char *arg, enum kvm_wfx_trap_policy *p) 3167 { 3168 if (!arg) 3169 return -EINVAL; 3170 3171 if (strcmp(arg, "trap") == 0) { 3172 *p = KVM_WFX_TRAP; 3173 return 0; 3174 } 3175 3176 if (strcmp(arg, "notrap") == 0) { 3177 *p = KVM_WFX_NOTRAP; 3178 return 0; 3179 } 3180 3181 return -EINVAL; 3182 } 3183 3184 static int __init early_kvm_wfi_trap_policy_cfg(char *arg) 3185 { 3186 return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfi_trap_policy); 3187 } 3188 early_param("kvm-arm.wfi_trap_policy", early_kvm_wfi_trap_policy_cfg); 3189 3190 static int __init early_kvm_wfe_trap_policy_cfg(char *arg) 3191 { 3192 return early_kvm_wfx_trap_policy_cfg(arg, &kvm_wfe_trap_policy); 3193 } 3194 early_param("kvm-arm.wfe_trap_policy", early_kvm_wfe_trap_policy_cfg); 3195 3196 enum kvm_mode kvm_get_mode(void) 3197 { 3198 return kvm_mode; 3199 } 3200 3201 module_init(kvm_arm_init); 3202