Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'kvm-arm64/writable-midr' into kvmarm/next

* kvm-arm64/writable-midr:
: Writable implementation ID registers, courtesy of Sebastian Ott
:
: Introduce a new capability that allows userspace to set the
: ID registers that identify a CPU implementation: MIDR_EL1, REVIDR_EL1,
: and AIDR_EL1. Also plug a hole in KVM's trap configuration where
: SMIDR_EL1 was readable at EL1, despite the fact that KVM does not
: support SME.
KVM: arm64: Fix documentation for KVM_CAP_ARM_WRITABLE_IMP_ID_REGS
KVM: arm64: Copy MIDR_EL1 into hyp VM when it is writable
KVM: arm64: Copy guest CTR_EL0 into hyp VM
KVM: selftests: arm64: Test writes to MIDR,REVIDR,AIDR
KVM: arm64: Allow userspace to change the implementation ID registers
KVM: arm64: Load VPIDR_EL2 with the VM's MIDR_EL1 value
KVM: arm64: Maintain per-VM copy of implementation ID regs
KVM: arm64: Set HCR_EL2.TID1 unconditionally

Signed-off-by: Oliver Upton <oliver.upton@linux.dev>

+244 -119
+18
Documentation/virt/kvm/api.rst
··· 8258 8258 depending on which executed at the time of an exit. Userspace must 8259 8259 take care to differentiate between these cases. 8260 8260 8261 + 7.37 KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 8262 + ------------------------------------- 8263 + 8264 + :Architectures: arm64 8265 + :Target: VM 8266 + :Parameters: None 8267 + :Returns: 0 on success, -EINVAL if vCPUs have been created before enabling this 8268 + capability. 8269 + 8270 + This capability changes the behavior of the registers that identify a PE 8271 + implementation of the Arm architecture: MIDR_EL1, REVIDR_EL1, and AIDR_EL1. 8272 + By default, these registers are visible to userspace but treated as invariant. 8273 + 8274 + When this capability is enabled, KVM allows userspace to change the 8275 + aforementioned registers before the first KVM_RUN. These registers are VM 8276 + scoped, meaning that the same set of values are presented on all vCPUs in a 8277 + given VM. 8278 + 8261 8279 8. Other capabilities. 8262 8280 ====================== 8263 8281
+2 -2
arch/arm64/include/asm/kvm_arm.h
··· 92 92 * SWIO: Turn set/way invalidates into set/way clean+invalidate 93 93 * PTW: Take a stage2 fault if a stage1 walk steps in device memory 94 94 * TID3: Trap EL1 reads of group 3 ID registers 95 - * TID2: Trap CTR_EL0, CCSIDR2_EL1, CLIDR_EL1, and CSSELR_EL1 95 + * TID1: Trap REVIDR_EL1, AIDR_EL1, and SMIDR_EL1 96 96 */ 97 97 #define HCR_GUEST_FLAGS (HCR_TSC | HCR_TSW | HCR_TWE | HCR_TWI | HCR_VM | \ 98 98 HCR_BSU_IS | HCR_FB | HCR_TACR | \ 99 99 HCR_AMO | HCR_SWIO | HCR_TIDCP | HCR_RW | HCR_TLOR | \ 100 - HCR_FMO | HCR_IMO | HCR_PTW | HCR_TID3) 100 + HCR_FMO | HCR_IMO | HCR_PTW | HCR_TID3 | HCR_TID1) 101 101 #define HCR_HOST_NVHE_FLAGS (HCR_RW | HCR_API | HCR_APK | HCR_ATA) 102 102 #define HCR_HOST_NVHE_PROTECTED_FLAGS (HCR_HOST_NVHE_FLAGS | HCR_TSC) 103 103 #define HCR_HOST_VHE_FLAGS (HCR_RW | HCR_TGE | HCR_E2H)
+11
arch/arm64/include/asm/kvm_host.h
··· 336 336 #define KVM_ARCH_FLAG_FGU_INITIALIZED 8 337 337 /* SVE exposed to guest */ 338 338 #define KVM_ARCH_FLAG_GUEST_HAS_SVE 9 339 + /* MIDR_EL1, REVIDR_EL1, and AIDR_EL1 are writable from userspace */ 340 + #define KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS 10 339 341 unsigned long flags; 340 342 341 343 /* VM-wide vCPU feature set */ ··· 377 375 #define KVM_ARM_ID_REG_NUM (IDREG_IDX(sys_reg(3, 0, 0, 7, 7)) + 1) 378 376 u64 id_regs[KVM_ARM_ID_REG_NUM]; 379 377 378 + u64 midr_el1; 379 + u64 revidr_el1; 380 + u64 aidr_el1; 380 381 u64 ctr_el0; 381 382 382 383 /* Masks for VNCR-backed and general EL2 sysregs */ ··· 1494 1489 return &ka->id_regs[IDREG_IDX(reg)]; 1495 1490 case SYS_CTR_EL0: 1496 1491 return &ka->ctr_el0; 1492 + case SYS_MIDR_EL1: 1493 + return &ka->midr_el1; 1494 + case SYS_REVIDR_EL1: 1495 + return &ka->revidr_el1; 1496 + case SYS_AIDR_EL1: 1497 + return &ka->aidr_el1; 1497 1498 default: 1498 1499 WARN_ON_ONCE(1); 1499 1500 return NULL;
+9
arch/arm64/kvm/arm.c
··· 125 125 } 126 126 mutex_unlock(&kvm->slots_lock); 127 127 break; 128 + case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS: 129 + mutex_lock(&kvm->lock); 130 + if (!kvm->created_vcpus) { 131 + r = 0; 132 + set_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &kvm->arch.flags); 133 + } 134 + mutex_unlock(&kvm->lock); 135 + break; 128 136 default: 129 137 break; 130 138 } ··· 321 313 case KVM_CAP_ARM_SYSTEM_SUSPEND: 322 314 case KVM_CAP_IRQFD_RESAMPLE: 323 315 case KVM_CAP_COUNTER_OFFSET: 316 + case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS: 324 317 r = 1; 325 318 break; 326 319 case KVM_CAP_SET_GUEST_DEBUG2:
+13 -1
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
··· 43 43 return &ctxt_sys_reg(ctxt, MDSCR_EL1); 44 44 } 45 45 46 + static inline u64 ctxt_midr_el1(struct kvm_cpu_context *ctxt) 47 + { 48 + struct kvm *kvm = kern_hyp_va(ctxt_to_vcpu(ctxt)->kvm); 49 + 50 + if (!(ctxt_is_guest(ctxt) && 51 + test_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &kvm->arch.flags))) 52 + return read_cpuid_id(); 53 + 54 + return kvm_read_vm_id_reg(kvm, SYS_MIDR_EL1); 55 + } 56 + 46 57 static inline void __sysreg_save_common_state(struct kvm_cpu_context *ctxt) 47 58 { 48 59 *ctxt_mdscr_el1(ctxt) = read_sysreg(mdscr_el1); ··· 179 168 } 180 169 181 170 static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt, 182 - u64 mpidr) 171 + u64 midr, u64 mpidr) 183 172 { 173 + write_sysreg(midr, vpidr_el2); 184 174 write_sysreg(mpidr, vmpidr_el2); 185 175 186 176 if (has_vhe() ||
+9 -1
arch/arm64/kvm/hyp/nvhe/pkvm.c
··· 46 46 vcpu->arch.hcr_el2 |= HCR_FWB; 47 47 48 48 if (cpus_have_final_cap(ARM64_HAS_EVT) && 49 - !cpus_have_final_cap(ARM64_MISMATCHED_CACHE_TYPE)) 49 + !cpus_have_final_cap(ARM64_MISMATCHED_CACHE_TYPE) && 50 + kvm_read_vm_id_reg(vcpu->kvm, SYS_CTR_EL0) == read_cpuid(CTR_EL0)) 50 51 vcpu->arch.hcr_el2 |= HCR_TID4; 51 52 else 52 53 vcpu->arch.hcr_el2 |= HCR_TID2; ··· 316 315 unsigned long host_arch_flags = READ_ONCE(host_kvm->arch.flags); 317 316 DECLARE_BITMAP(allowed_features, KVM_VCPU_MAX_FEATURES); 318 317 318 + /* CTR_EL0 is always under host control, even for protected VMs. */ 319 + hyp_vm->kvm.arch.ctr_el0 = host_kvm->arch.ctr_el0; 320 + 319 321 if (test_bit(KVM_ARCH_FLAG_MTE_ENABLED, &host_kvm->arch.flags)) 320 322 set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags); 321 323 ··· 329 325 bitmap_copy(kvm->arch.vcpu_features, 330 326 host_kvm->arch.vcpu_features, 331 327 KVM_VCPU_MAX_FEATURES); 328 + 329 + if (test_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &host_arch_flags)) 330 + hyp_vm->kvm.arch.midr_el1 = host_kvm->arch.midr_el1; 331 + 332 332 return; 333 333 } 334 334
+3 -1
arch/arm64/kvm/hyp/nvhe/sysreg-sr.c
··· 28 28 29 29 void __sysreg_restore_state_nvhe(struct kvm_cpu_context *ctxt) 30 30 { 31 - __sysreg_restore_el1_state(ctxt, ctxt_sys_reg(ctxt, MPIDR_EL1)); 31 + u64 midr = ctxt_midr_el1(ctxt); 32 + 33 + __sysreg_restore_el1_state(ctxt, midr, ctxt_sys_reg(ctxt, MPIDR_EL1)); 32 34 __sysreg_restore_common_state(ctxt); 33 35 __sysreg_restore_user_state(ctxt); 34 36 __sysreg_restore_el2_return_state(ctxt);
+10 -18
arch/arm64/kvm/hyp/vhe/sysreg-sr.c
··· 87 87 write_sysreg(__vcpu_sys_reg(vcpu, PAR_EL1), par_el1); 88 88 write_sysreg(__vcpu_sys_reg(vcpu, TPIDR_EL1), tpidr_el1); 89 89 90 - write_sysreg(__vcpu_sys_reg(vcpu, MPIDR_EL1), vmpidr_el2); 91 - write_sysreg_el1(__vcpu_sys_reg(vcpu, MAIR_EL2), SYS_MAIR); 92 - write_sysreg_el1(__vcpu_sys_reg(vcpu, VBAR_EL2), SYS_VBAR); 93 - write_sysreg_el1(__vcpu_sys_reg(vcpu, CONTEXTIDR_EL2), SYS_CONTEXTIDR); 94 - write_sysreg_el1(__vcpu_sys_reg(vcpu, AMAIR_EL2), SYS_AMAIR); 90 + write_sysreg(ctxt_midr_el1(&vcpu->arch.ctxt), vpidr_el2); 91 + write_sysreg(__vcpu_sys_reg(vcpu, MPIDR_EL1), vmpidr_el2); 92 + write_sysreg_el1(__vcpu_sys_reg(vcpu, MAIR_EL2), SYS_MAIR); 93 + write_sysreg_el1(__vcpu_sys_reg(vcpu, VBAR_EL2), SYS_VBAR); 94 + write_sysreg_el1(__vcpu_sys_reg(vcpu, CONTEXTIDR_EL2), SYS_CONTEXTIDR); 95 + write_sysreg_el1(__vcpu_sys_reg(vcpu, AMAIR_EL2), SYS_AMAIR); 95 96 96 97 if (vcpu_el2_e2h_is_set(vcpu)) { 97 98 /* ··· 192 191 { 193 192 struct kvm_cpu_context *guest_ctxt = &vcpu->arch.ctxt; 194 193 struct kvm_cpu_context *host_ctxt; 195 - u64 mpidr; 194 + u64 midr, mpidr; 196 195 197 196 host_ctxt = host_data_ptr(host_ctxt); 198 197 __sysreg_save_user_state(host_ctxt); ··· 222 221 } else { 223 222 if (vcpu_has_nv(vcpu)) { 224 223 /* 225 - * Use the guest hypervisor's VPIDR_EL2 when in a 226 - * nested state. The hardware value of MIDR_EL1 gets 227 - * restored on put. 228 - */ 229 - write_sysreg(ctxt_sys_reg(guest_ctxt, VPIDR_EL2), vpidr_el2); 230 - 231 - /* 232 224 * As we're restoring a nested guest, set the value 233 225 * provided by the guest hypervisor. 234 226 */ 227 + midr = ctxt_sys_reg(guest_ctxt, VPIDR_EL2); 235 228 mpidr = ctxt_sys_reg(guest_ctxt, VMPIDR_EL2); 236 229 } else { 230 + midr = ctxt_midr_el1(guest_ctxt); 237 231 mpidr = ctxt_sys_reg(guest_ctxt, MPIDR_EL1); 238 232 } 239 233 240 - __sysreg_restore_el1_state(guest_ctxt, mpidr); 234 + __sysreg_restore_el1_state(guest_ctxt, midr, mpidr); 241 235 } 242 236 243 237 vcpu_set_flag(vcpu, SYSREGS_ON_CPU); ··· 266 270 267 271 /* Restore host user state */ 268 272 __sysreg_restore_user_state(host_ctxt); 269 - 270 - /* If leaving a nesting guest, restore MIDR_EL1 default view */ 271 - if (vcpu_has_nv(vcpu)) 272 - write_sysreg(read_cpuid_id(), vpidr_el2); 273 273 274 274 vcpu_clear_flag(vcpu, SYSREGS_ON_CPU); 275 275 }
+138 -89
arch/arm64/kvm/sys_regs.c
··· 1674 1674 * Return true if the register's (Op0, Op1, CRn, CRm, Op2) is 1675 1675 * (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8, which is the range of ID 1676 1676 * registers KVM maintains on a per-VM basis. 1677 + * 1678 + * Additionally, the implementation ID registers and CTR_EL0 are handled as 1679 + * per-VM registers. 1677 1680 */ 1678 1681 static inline bool is_vm_ftr_id_reg(u32 id) 1679 1682 { 1680 - if (id == SYS_CTR_EL0) 1683 + switch (id) { 1684 + case SYS_CTR_EL0: 1685 + case SYS_MIDR_EL1: 1686 + case SYS_REVIDR_EL1: 1687 + case SYS_AIDR_EL1: 1681 1688 return true; 1689 + default: 1690 + return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 && 1691 + sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 && 1692 + sys_reg_CRm(id) < 8); 1682 1693 1683 - return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 && 1684 - sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 && 1685 - sys_reg_CRm(id) < 8); 1694 + } 1686 1695 } 1687 1696 1688 1697 static inline bool is_vcpu_ftr_id_reg(u32 id) ··· 2579 2570 return true; 2580 2571 } 2581 2572 2573 + /* 2574 + * For historical (ahem ABI) reasons, KVM treated MIDR_EL1, REVIDR_EL1, and 2575 + * AIDR_EL1 as "invariant" registers, meaning userspace cannot change them. 2576 + * The values made visible to userspace were the register values of the boot 2577 + * CPU. 2578 + * 2579 + * At the same time, reads from these registers at EL1 previously were not 2580 + * trapped, allowing the guest to read the actual hardware value. On big-little 2581 + * machines, this means the VM can see different values depending on where a 2582 + * given vCPU got scheduled. 2583 + * 2584 + * These registers are now trapped as collateral damage from SME, and what 2585 + * follows attempts to give a user / guest view consistent with the existing 2586 + * ABI. 2587 + */ 2588 + static bool access_imp_id_reg(struct kvm_vcpu *vcpu, 2589 + struct sys_reg_params *p, 2590 + const struct sys_reg_desc *r) 2591 + { 2592 + if (p->is_write) 2593 + return write_to_read_only(vcpu, p, r); 2594 + 2595 + /* 2596 + * Return the VM-scoped implementation ID register values if userspace 2597 + * has made them writable. 2598 + */ 2599 + if (test_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &vcpu->kvm->arch.flags)) 2600 + return access_id_reg(vcpu, p, r); 2601 + 2602 + /* 2603 + * Otherwise, fall back to the old behavior of returning the value of 2604 + * the current CPU. 2605 + */ 2606 + switch (reg_to_encoding(r)) { 2607 + case SYS_REVIDR_EL1: 2608 + p->regval = read_sysreg(revidr_el1); 2609 + break; 2610 + case SYS_AIDR_EL1: 2611 + p->regval = read_sysreg(aidr_el1); 2612 + break; 2613 + default: 2614 + WARN_ON_ONCE(1); 2615 + } 2616 + 2617 + return true; 2618 + } 2619 + 2620 + static u64 __ro_after_init boot_cpu_midr_val; 2621 + static u64 __ro_after_init boot_cpu_revidr_val; 2622 + static u64 __ro_after_init boot_cpu_aidr_val; 2623 + 2624 + static void init_imp_id_regs(void) 2625 + { 2626 + boot_cpu_midr_val = read_sysreg(midr_el1); 2627 + boot_cpu_revidr_val = read_sysreg(revidr_el1); 2628 + boot_cpu_aidr_val = read_sysreg(aidr_el1); 2629 + } 2630 + 2631 + static u64 reset_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) 2632 + { 2633 + switch (reg_to_encoding(r)) { 2634 + case SYS_MIDR_EL1: 2635 + return boot_cpu_midr_val; 2636 + case SYS_REVIDR_EL1: 2637 + return boot_cpu_revidr_val; 2638 + case SYS_AIDR_EL1: 2639 + return boot_cpu_aidr_val; 2640 + default: 2641 + KVM_BUG_ON(1, vcpu->kvm); 2642 + return 0; 2643 + } 2644 + } 2645 + 2646 + static int set_imp_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, 2647 + u64 val) 2648 + { 2649 + struct kvm *kvm = vcpu->kvm; 2650 + u64 expected; 2651 + 2652 + guard(mutex)(&kvm->arch.config_lock); 2653 + 2654 + expected = read_id_reg(vcpu, r); 2655 + if (expected == val) 2656 + return 0; 2657 + 2658 + if (!test_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &kvm->arch.flags)) 2659 + return -EINVAL; 2660 + 2661 + /* 2662 + * Once the VM has started the ID registers are immutable. Reject the 2663 + * write if userspace tries to change it. 2664 + */ 2665 + if (kvm_vm_has_ran_once(kvm)) 2666 + return -EBUSY; 2667 + 2668 + /* 2669 + * Any value is allowed for the implementation ID registers so long as 2670 + * it is within the writable mask. 2671 + */ 2672 + if ((val & r->val) != val) 2673 + return -EINVAL; 2674 + 2675 + kvm_set_vm_id_reg(kvm, reg_to_encoding(r), val); 2676 + return 0; 2677 + } 2678 + 2679 + #define IMPLEMENTATION_ID(reg, mask) { \ 2680 + SYS_DESC(SYS_##reg), \ 2681 + .access = access_imp_id_reg, \ 2682 + .get_user = get_id_reg, \ 2683 + .set_user = set_imp_id_reg, \ 2684 + .reset = reset_imp_id_reg, \ 2685 + .val = mask, \ 2686 + } 2582 2687 2583 2688 /* 2584 2689 * Architected system registers. ··· 2742 2619 2743 2620 { SYS_DESC(SYS_DBGVCR32_EL2), undef_access, reset_val, DBGVCR32_EL2, 0 }, 2744 2621 2622 + IMPLEMENTATION_ID(MIDR_EL1, GENMASK_ULL(31, 0)), 2745 2623 { SYS_DESC(SYS_MPIDR_EL1), NULL, reset_mpidr, MPIDR_EL1 }, 2624 + IMPLEMENTATION_ID(REVIDR_EL1, GENMASK_ULL(63, 0)), 2746 2625 2747 2626 /* 2748 2627 * ID regs: all ID_SANITISED() entries here must have corresponding ··· 3015 2890 .set_user = set_clidr, .val = ~CLIDR_EL1_RES0 }, 3016 2891 { SYS_DESC(SYS_CCSIDR2_EL1), undef_access }, 3017 2892 { SYS_DESC(SYS_SMIDR_EL1), undef_access }, 2893 + IMPLEMENTATION_ID(AIDR_EL1, GENMASK_ULL(63, 0)), 3018 2894 { SYS_DESC(SYS_CSSELR_EL1), access_csselr, reset_unknown, CSSELR_EL1 }, 3019 2895 ID_FILTERED(CTR_EL0, ctr_el0, 3020 2896 CTR_EL0_DIC_MASK | ··· 4507 4381 * Certain AArch32 ID registers are handled by rerouting to the AArch64 4508 4382 * system register table. Registers in the ID range where CRm=0 are 4509 4383 * excluded from this scheme as they do not trivially map into AArch64 4510 - * system register encodings. 4384 + * system register encodings, except for AIDR/REVIDR. 4511 4385 */ 4512 - if (params.Op1 == 0 && params.CRn == 0 && params.CRm) 4386 + if (params.Op1 == 0 && params.CRn == 0 && 4387 + (params.CRm || params.Op2 == 6 /* REVIDR */)) 4388 + return kvm_emulate_cp15_id_reg(vcpu, &params); 4389 + if (params.Op1 == 1 && params.CRn == 0 && 4390 + params.CRm == 0 && params.Op2 == 7 /* AIDR */) 4513 4391 return kvm_emulate_cp15_id_reg(vcpu, &params); 4514 4392 4515 4393 return kvm_handle_cp_32(vcpu, &params, cp15_regs, ARRAY_SIZE(cp15_regs)); ··· 4817 4687 return r; 4818 4688 } 4819 4689 4820 - /* 4821 - * These are the invariant sys_reg registers: we let the guest see the 4822 - * host versions of these, so they're part of the guest state. 4823 - * 4824 - * A future CPU may provide a mechanism to present different values to 4825 - * the guest, or a future kvm may trap them. 4826 - */ 4827 - 4828 - #define FUNCTION_INVARIANT(reg) \ 4829 - static u64 reset_##reg(struct kvm_vcpu *v, \ 4830 - const struct sys_reg_desc *r) \ 4831 - { \ 4832 - ((struct sys_reg_desc *)r)->val = read_sysreg(reg); \ 4833 - return ((struct sys_reg_desc *)r)->val; \ 4834 - } 4835 - 4836 - FUNCTION_INVARIANT(midr_el1) 4837 - FUNCTION_INVARIANT(revidr_el1) 4838 - FUNCTION_INVARIANT(aidr_el1) 4839 - 4840 - /* ->val is filled in by kvm_sys_reg_table_init() */ 4841 - static struct sys_reg_desc invariant_sys_regs[] __ro_after_init = { 4842 - { SYS_DESC(SYS_MIDR_EL1), NULL, reset_midr_el1 }, 4843 - { SYS_DESC(SYS_REVIDR_EL1), NULL, reset_revidr_el1 }, 4844 - { SYS_DESC(SYS_AIDR_EL1), NULL, reset_aidr_el1 }, 4845 - }; 4846 - 4847 - static int get_invariant_sys_reg(u64 id, u64 __user *uaddr) 4848 - { 4849 - const struct sys_reg_desc *r; 4850 - 4851 - r = get_reg_by_id(id, invariant_sys_regs, 4852 - ARRAY_SIZE(invariant_sys_regs)); 4853 - if (!r) 4854 - return -ENOENT; 4855 - 4856 - return put_user(r->val, uaddr); 4857 - } 4858 - 4859 - static int set_invariant_sys_reg(u64 id, u64 __user *uaddr) 4860 - { 4861 - const struct sys_reg_desc *r; 4862 - u64 val; 4863 - 4864 - r = get_reg_by_id(id, invariant_sys_regs, 4865 - ARRAY_SIZE(invariant_sys_regs)); 4866 - if (!r) 4867 - return -ENOENT; 4868 - 4869 - if (get_user(val, uaddr)) 4870 - return -EFAULT; 4871 - 4872 - /* This is what we mean by invariant: you can't change it. */ 4873 - if (r->val != val) 4874 - return -EINVAL; 4875 - 4876 - return 0; 4877 - } 4878 - 4879 4690 static int demux_c15_get(struct kvm_vcpu *vcpu, u64 id, void __user *uaddr) 4880 4691 { 4881 4692 u32 val; ··· 4898 4827 int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) 4899 4828 { 4900 4829 void __user *uaddr = (void __user *)(unsigned long)reg->addr; 4901 - int err; 4902 4830 4903 4831 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX) 4904 4832 return demux_c15_get(vcpu, reg->id, uaddr); 4905 - 4906 - err = get_invariant_sys_reg(reg->id, uaddr); 4907 - if (err != -ENOENT) 4908 - return err; 4909 4833 4910 4834 return kvm_sys_reg_get_user(vcpu, reg, 4911 4835 sys_reg_descs, ARRAY_SIZE(sys_reg_descs)); ··· 4937 4871 int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) 4938 4872 { 4939 4873 void __user *uaddr = (void __user *)(unsigned long)reg->addr; 4940 - int err; 4941 4874 4942 4875 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX) 4943 4876 return demux_c15_set(vcpu, reg->id, uaddr); 4944 - 4945 - err = set_invariant_sys_reg(reg->id, uaddr); 4946 - if (err != -ENOENT) 4947 - return err; 4948 4877 4949 4878 return kvm_sys_reg_set_user(vcpu, reg, 4950 4879 sys_reg_descs, ARRAY_SIZE(sys_reg_descs)); ··· 5029 4968 5030 4969 unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu) 5031 4970 { 5032 - return ARRAY_SIZE(invariant_sys_regs) 5033 - + num_demux_regs() 4971 + return num_demux_regs() 5034 4972 + walk_sys_regs(vcpu, (u64 __user *)NULL); 5035 4973 } 5036 4974 5037 4975 int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) 5038 4976 { 5039 - unsigned int i; 5040 4977 int err; 5041 - 5042 - /* Then give them all the invariant registers' indices. */ 5043 - for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) { 5044 - if (put_user(sys_reg_to_index(&invariant_sys_regs[i]), uindices)) 5045 - return -EFAULT; 5046 - uindices++; 5047 - } 5048 4978 5049 4979 err = walk_sys_regs(vcpu, uindices); 5050 4980 if (err < 0) ··· 5262 5210 valid &= check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs), true); 5263 5211 valid &= check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs), true); 5264 5212 valid &= check_sysreg_table(cp15_64_regs, ARRAY_SIZE(cp15_64_regs), true); 5265 - valid &= check_sysreg_table(invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs), false); 5266 5213 valid &= check_sysreg_table(sys_insn_descs, ARRAY_SIZE(sys_insn_descs), false); 5267 5214 5268 5215 if (!valid) 5269 5216 return -EINVAL; 5270 5217 5271 - /* We abuse the reset function to overwrite the table itself. */ 5272 - for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) 5273 - invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]); 5218 + init_imp_id_regs(); 5274 5219 5275 5220 ret = populate_nv_trap_config(); 5276 5221
+1
include/uapi/linux/kvm.h
··· 929 929 #define KVM_CAP_PRE_FAULT_MEMORY 236 930 930 #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237 931 931 #define KVM_CAP_X86_GUEST_MODE 238 932 + #define KVM_CAP_ARM_WRITABLE_IMP_ID_REGS 239 932 933 933 934 struct kvm_irq_routing_irqchip { 934 935 __u32 irqchip;
+30 -7
tools/testing/selftests/kvm/arm64/set_id_regs.c
··· 233 233 GUEST_REG_SYNC(SYS_ID_AA64MMFR2_EL1); 234 234 GUEST_REG_SYNC(SYS_ID_AA64ZFR0_EL1); 235 235 GUEST_REG_SYNC(SYS_CTR_EL0); 236 + GUEST_REG_SYNC(SYS_MIDR_EL1); 237 + GUEST_REG_SYNC(SYS_REVIDR_EL1); 238 + GUEST_REG_SYNC(SYS_AIDR_EL1); 236 239 237 240 GUEST_DONE(); 238 241 } ··· 615 612 test_reg_vals[encoding_to_range_idx(SYS_CTR_EL0)] = ctr; 616 613 } 617 614 618 - static void test_vcpu_ftr_id_regs(struct kvm_vcpu *vcpu) 615 + static void test_id_reg(struct kvm_vcpu *vcpu, u32 id) 619 616 { 620 617 u64 val; 621 618 619 + val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(id)); 620 + val++; 621 + vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(id), val); 622 + test_reg_vals[encoding_to_range_idx(id)] = val; 623 + } 624 + 625 + static void test_vcpu_ftr_id_regs(struct kvm_vcpu *vcpu) 626 + { 622 627 test_clidr(vcpu); 623 628 test_ctr(vcpu); 624 629 625 - val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1)); 626 - val++; 627 - vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1), val); 630 + test_id_reg(vcpu, SYS_MPIDR_EL1); 631 + ksft_test_result_pass("%s\n", __func__); 632 + } 628 633 629 - test_reg_vals[encoding_to_range_idx(SYS_MPIDR_EL1)] = val; 634 + static void test_vcpu_non_ftr_id_regs(struct kvm_vcpu *vcpu) 635 + { 636 + test_id_reg(vcpu, SYS_MIDR_EL1); 637 + test_id_reg(vcpu, SYS_REVIDR_EL1); 638 + test_id_reg(vcpu, SYS_AIDR_EL1); 639 + 630 640 ksft_test_result_pass("%s\n", __func__); 631 641 } 632 642 ··· 666 650 test_assert_id_reg_unchanged(vcpu, SYS_MPIDR_EL1); 667 651 test_assert_id_reg_unchanged(vcpu, SYS_CLIDR_EL1); 668 652 test_assert_id_reg_unchanged(vcpu, SYS_CTR_EL0); 653 + test_assert_id_reg_unchanged(vcpu, SYS_MIDR_EL1); 654 + test_assert_id_reg_unchanged(vcpu, SYS_REVIDR_EL1); 655 + test_assert_id_reg_unchanged(vcpu, SYS_AIDR_EL1); 669 656 670 657 ksft_test_result_pass("%s\n", __func__); 671 658 } ··· 682 663 int test_cnt; 683 664 684 665 TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES)); 666 + TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_WRITABLE_IMP_ID_REGS)); 685 667 686 - vm = vm_create_with_one_vcpu(&vcpu, guest_code); 668 + vm = vm_create(1); 669 + vm_enable_cap(vm, KVM_CAP_ARM_WRITABLE_IMP_ID_REGS, 0); 670 + vcpu = vm_vcpu_add(vm, 0, guest_code); 687 671 688 672 /* Check for AARCH64 only system */ 689 673 val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64PFR0_EL1)); ··· 700 678 ARRAY_SIZE(ftr_id_aa64isar2_el1) + ARRAY_SIZE(ftr_id_aa64pfr0_el1) + 701 679 ARRAY_SIZE(ftr_id_aa64pfr1_el1) + ARRAY_SIZE(ftr_id_aa64mmfr0_el1) + 702 680 ARRAY_SIZE(ftr_id_aa64mmfr1_el1) + ARRAY_SIZE(ftr_id_aa64mmfr2_el1) + 703 - ARRAY_SIZE(ftr_id_aa64zfr0_el1) - ARRAY_SIZE(test_regs) + 2 + 681 + ARRAY_SIZE(ftr_id_aa64zfr0_el1) - ARRAY_SIZE(test_regs) + 3 + 704 682 MPAM_IDREG_TEST; 705 683 706 684 ksft_set_plan(test_cnt); 707 685 708 686 test_vm_ftr_id_regs(vcpu, aarch64_only); 709 687 test_vcpu_ftr_id_regs(vcpu); 688 + test_vcpu_non_ftr_id_regs(vcpu); 710 689 test_user_set_mpam_reg(vcpu); 711 690 712 691 test_guest_reg_read(vcpu);