KVM: arm64: nv: Add CPTR_EL2 handling
Marc Zyngier
maintainercover KVM: arm64: nv: Add CPTR_EL2 handling
Building on top of Oliver's NV+SVE series[1], I've put together a small set of patches handling CPTR_EL2, as the two are pretty closely related.
This series adds:
- the description on the CPTR_EL2-driven traps in the exception routing tables
- the merging of the L1 guest's CPTR_EL2 traps with the host's
- the handling of L1's CPTR_EL2 access via the CPACR_EL1 accessor as a trap so that we can emulate writing to the TCPAC/TTA bits
Overall, this is pretty straightforward (for NV, that is...).
[1] https://lore.kernel.org/r/[email protected]
Marc Zyngier (4):
KVM: arm64: nv: Add TCPAC/TTA to CPTR->CPACR conversion helper
KVM: arm64: nv: Add trap description for CPTR_EL2
KVM: arm64: nv: Add additional trap setup for CPTR_EL2
KVM: arm64: nv: Handle CPACR_EL1 traps
arch/arm64/include/asm/kvm_emulate.h | 2 +- arch/arm64/include/asm/kvm_nested.h | 4 +- arch/arm64/kvm/emulate-nested.c | 91 ++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/vhe/switch.c | 55 ++++++++++++++++- 4 files changed, 148 insertions(+), 4 deletions(-)
Signature · 1 line
2.39.2
Marc Zyngier
maintainerPATCH 1/4 KVM: arm64: nv: Add TCPAC/TTA to CPTR->CPACR conversion helper
We are missing the propagation of CPTR_EL2.{TCPAC,TTA} into the CPACR format. Make sure we preserve these bits.
--- arch/arm64/include/asm/kvm_nested.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_nested.h +3 -1
@@ -32,7 +32,7 @@ static inline u64 translate_tcr_el2_to_tcr_el1(u64 tcr)static inline u64 translate_cptr_el2_to_cpacr_el1 (u64 cptr_el2) { -u64 cpacr_el1 =0 ; +u64 cpacr_el1 = CPACR_ELx_RES1;if (cptr_el2 &CPTR_EL2_TTA ) cpacr_el1 |= CPACR_ELx_TTA;
@@ -41,6 +41,8 @@ static inline u64 translate_cptr_el2_to_cpacr_el1(u64 cptr_el2)if (!(cptr_el2 &CPTR_EL2_TZ )) cpacr_el1 |= CPACR_ELx_ZEN; + cpacr_el1 |= cptr_el2 & (CPTR_EL2_TCPAC |CPTR_EL2_TAM ); +return cpacr_el1; }
Signature · 1 line
2.39.2
Marc Zyngier
maintainerPATCH 2/4 KVM: arm64: nv: Add trap description for CPTR_EL2
In reply to Marc ZyngierAdd trap description for CPTR_EL2.{TCPAC,TAM,E0POE,TTA}.
TTA is a bit annoying as it changes location depending on E2H. This forces us to add yet another "complex" trap condition.
--- arch/arm64/kvm/emulate-nested.c | 91 +++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+)
arch/arm64/kvm/emulate-nested.c +91 -0
@@ -79,6 +79,10 @@ enum cgt_group_id {CGT_MDCR_E2TB ,CGT_MDCR_TDCC , +CGT_CPACR_E0POE , +CGT_CPTR_TAM , +CGT_CPTR_TCPAC , +/* * Anything after this point is a combination of coarse trap * controls, which must all be evaluated to decide what to do.
@@ -106,6 +110,8 @@ enum cgt_group_id {CGT_CNTHCTL_EL1PCTEN = __COMPLEX_CONDITIONS__,CGT_CNTHCTL_EL1PTEN , +CGT_CPTR_TTA , +/* Must be last */ __NR_CGT_GROUP_IDS__ };
@@ -345,6 +351,24 @@ static const struct trap_bits coarse_trap_bits[] = { .mask = MDCR_EL2_TDCC, .behaviour = BEHAVE_FORWARD_ANY, }, + [CGT_CPACR_E0POE] = { + .index = CPTR_EL2, + .value = CPACR_ELx_E0POE, + .mask = CPACR_ELx_E0POE, + .behaviour = BEHAVE_FORWARD_ANY, + }, + [CGT_CPTR_TAM] = { + .index = CPTR_EL2, + .value = CPTR_EL2_TAM, + .mask = CPTR_EL2_TAM, + .behaviour = BEHAVE_FORWARD_ANY, + }, + [CGT_CPTR_TCPAC] = { + .index = CPTR_EL2, + .value = CPTR_EL2_TCPAC, + .mask = CPTR_EL2_TCPAC, + .behaviour = BEHAVE_FORWARD_ANY, + }, };#define MCB (id, ...) \
@@ -410,12 +434,26 @@ static enum trap_behaviour check_cnthctl_el1pten(struct kvm_vcpu *vcpu)return BEHAVE_FORWARD_ANY ; } +static enum trap_behaviour check_cptr_tta (struct kvm_vcpu *vcpu) +{ +u64 val =__vcpu_sys_reg (vcpu,CPTR_EL2 ); + +if (!vcpu_el2_e2h_is_set (vcpu)) + val =translate_cptr_el2_to_cpacr_el1 (val); + +if (val & CPACR_ELx_TTA) +return BEHAVE_FORWARD_ANY ; + +return BEHAVE_HANDLE_LOCALLY ; +} +#define CCC (id, fn) \ [id - __COMPLEX_CONDITIONS__] = fnstatic const complex_condition_check ccc[] = {CCC (CGT_CNTHCTL_EL1PCTEN , check_cnthctl_el1pcten),CCC (CGT_CNTHCTL_EL1PTEN , check_cnthctl_el1pten), +CCC (CGT_CPTR_TTA , check_cptr_tta), };/*
@@ -1000,6 +1038,59 @@ static const struct encoding_to_trap_config encoding_to_cgt[] __initconst = {SR_TRAP (SYS_TRBPTR_EL1 ,CGT_MDCR_E2TB ),SR_TRAP (SYS_TRBSR_EL1 ,CGT_MDCR_E2TB ),SR_TRAP (SYS_TRBTRG_EL1 ,CGT_MDCR_E2TB ), +SR_TRAP (SYS_CPACR_EL1 ,CGT_CPTR_TCPAC ), +SR_TRAP (SYS_AMUSERENR_EL0 ,CGT_CPTR_TAM ), +SR_TRAP (SYS_AMCFGR_EL0 ,CGT_CPTR_TAM ), +SR_TRAP (SYS_AMCGCR_EL0 ,CGT_CPTR_TAM ), +SR_TRAP (SYS_AMCNTENCLR0_EL0 ,CGT_CPTR_TAM ), +SR_TRAP (SYS_AMCNTENCLR1_EL0 ,CGT_CPTR_TAM ), +SR_TRAP (SYS_AMCNTENSET0_EL0 ,CGT_CPTR_TAM ), +SR_TRAP (SYS_AMCNTENSET1_EL0 ,CGT_CPTR_TAM ), +SR_TRAP (SYS_AMCR_EL0 ,CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVCNTR0_EL0 (0 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVCNTR0_EL0 (1 ),CGT_CPTR_TAM ), +SR_TRAP (SY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EL0 (0 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (1 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (2 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (3 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (4 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (5 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (6 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (7 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (8 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (9 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (10 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (11 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (12 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (13 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (14 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_AMEVTYPER1_EL0 (15 ),CGT_CPTR_TAM ), +SR_TRAP (SYS_POR_EL0 ,CGT_CPACR_E0POE ), +/* op0=2, op1=1, and CRn<0b1000 */ +SR_RANGE_TRAP (sys_reg (2 ,1 ,0 ,0 ,0 ), +sys_reg (2 ,1 ,7 ,15 ,7 ),CGT_CPTR_TTA ),SR_TRAP (SYS_CNTP_TVAL_EL0 ,CGT_CNTHCTL_EL1PTEN ),SR_TRAP (SYS_CNTP_CVAL_EL0 ,CGT_CNTHCTL_EL1PTEN ), SR_TRAP(SYS_CNTP_CTL_EL0, CGT_CNTHCTL_EL1PTEN),
Signature · 1 line
2.39.2
Marc Zyngier
maintainerPATCH 3/4 KVM: arm64: nv: Add additional trap setup for CPTR_EL2
In reply to Marc ZyngierWe need to teach KVM a couple of new tricks. CPTR_EL2 and its VHE accessor CPACR_EL1 need to be handled specially:
- CPACR_EL1 is trapped on VHE so that we can track the TCPAC and TTA bits
- CPTR_EL2.{TCPAC,E0POE} are propagated from L1 to L2
As a consequence of CPTR_EL2 being always trapped, we update vcpu_sanitised_cptr_el2() so that it doesn't try to read from the CPU registers, but from the shadow copy (ensuring that we always have up-to-date TCPAC and TTA bits).
This helper will also be used when handling the CPTR_EL1 trap.
--- arch/arm64/include/asm/kvm_emulate.h | 2 +- arch/arm64/kvm/hyp/vhe/switch.c | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +1 -1
@@ -607,7 +607,7 @@ static __always_inline void kvm_reset_cptr_el2(struct kvm_vcpu *vcpu)*/ static inline u64 vcpu_sanitised_cptr_el2 (const struct kvm_vcpu *vcpu) { -u64 cptr =vcpu_read_sys_reg (vcpu,CPTR_EL2 ); +u64 cptr =__vcpu_sys_reg (vcpu,CPTR_EL2 );if (!vcpu_el2_e2h_is_set (vcpu)) cptr =translate_cptr_el2_to_cpacr_el1 (cptr);
arch/arm64/kvm/hyp/vhe/switch.c +22 -1
@@ -67,6 +67,8 @@ static u64 __compute_hcr(struct kvm_vcpu *vcpu)static void __activate_cptr_traps (struct kvm_vcpu *vcpu) { +u64 cptr; +/* * With VHE (HCR.E2H == 1), accesses to CPACR_EL1 are routed to * CPTR_EL2. In general, CPACR_EL1 has the same layout as CPTR_EL2,
@@ -85,11 +87,23 @@ static void __activate_cptr_traps(struct kvm_vcpu *vcpu)__activate_traps_fpsimd32 (vcpu); } +if (!vcpu_has_nv (vcpu)) + goto write; + +/* +* The architecture is a bit crap (what a surprise): an EL2 guest +* writing to CPTR_EL2 via CPACR_EL1 can't set any of TCPAC or TTA, +* as they are RES0 in the guest's view. To work around it, trap the +* sucker using the very same bit it can't set... +*/ +if (vcpu_el2_e2h_is_set (vcpu) &&is_hyp_ctxt (vcpu)) + val |=CPTR_EL2_TCPAC ; +/* * Layer the guest hypervisor's trap configuration on top of our own if * we're in a nested context. */ -if (!vcpu_has_nv (vcpu) ||is_hyp_ctxt (vcpu)) +if (is_hyp_ctxt (vcpu)) goto write;if (guest_hyp_fpsimd_traps_enabled (vcpu))
@@ -97,6 +111,13 @@ static void __activate_cptr_traps(struct kvm_vcpu *vcpu)if (guest_hyp_sve_traps_enabled (vcpu)) val &= ~CPACR_ELx_ZEN; + cptr =vcpu_sanitised_cptr_el2 (vcpu); + +if (kvm_has_feat (vcpu->kvm,ID_AA64MMFR3_EL1 ,S2POE ,IMP )) + val |= cptr & CPACR_ELx_E0POE; + + val |= cptr &CPTR_EL2_TCPAC ; + write:write_sysreg (val, cpacr_el1); }
Signature · 1 line
2.39.2
Marc Zyngier
maintainerPATCH 4/4 KVM: arm64: nv: Handle CPACR_EL1 traps
In reply to Marc ZyngierHandle CPACR_EL1 accesses when running a VHE guest. In order to limit the cost of the emulation, implement it ass a shallow exit.
In the other cases:
- this is a nVHE L1 which will write to memory, and we don't trap
- this is a L2 guest:
* the L1 has CPTR_EL2.TCPAC==0, and the L2 has direct register access
* the L1 has CPTR_EL2.TCPAC==1, and the L2 will trap, but the handling is defered to the general handling for forwarding
--- arch/arm64/kvm/hyp/vhe/switch.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/vhe/switch.c +31 -1
@@ -295,10 +295,40 @@ static bool kvm_hyp_handle_eret(struct kvm_vcpu *vcpu, u64 *exit_code)return true; } +static bool kvm_hyp_handle_cpacr_el1 (struct kvm_vcpu *vcpu,u64 *exit_code) +{ +u64 esr =kvm_vcpu_get_esr (vcpu); +int rt; + +if (!is_hyp_ctxt (vcpu) ||esr_sys64_to_sysreg (esr) !=SYS_CPACR_EL1 ) +return false; + + rt =kvm_vcpu_sys_get_rt (vcpu); + +if ((esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_READ) { +vcpu_set_reg (vcpu, rt,__vcpu_sys_reg (vcpu,CPTR_EL2 )); + }else { +vcpu_write_sys_reg (vcpu,vcpu_get_reg (vcpu, rt),CPTR_EL2 ); +__activate_cptr_traps (vcpu); + } + +__kvm_skip_instr (vcpu); + +return true; +} + +static bool kvm_hyp_handle_sysreg_vhe (struct kvm_vcpu *vcpu,u64 *exit_code) +{ +if (kvm_hyp_handle_cpacr_el1 (vcpu, exit_code)) +return true; + +return kvm_hyp_handle_sysreg (vcpu, exit_code); +} +static const exit_handler_fn hyp_exit_handlers[] = { [0 ... ESR_ELx_EC_MAX] =NULL , [ESR_ELx_EC_CP15_32] = kvm_hyp_handle_cp15_32, - [ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg, + [ESR_ELx_EC_SYS64] = kvm_hyp_handle_sysreg_vhe, [ESR_ELx_EC_SVE] = kvm_hyp_handle_fpsimd, [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd, [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low,
Signature · 1 line
2.39.2
Quoted 3 lines · Marc Zyngier
Building on top of Oliver's NV+SVE series[1], I've put together a small set of patches handling
CPTR_EL2, as the two are pretty closely related.
Thanks for posting this, overall looks good. I'll probably pick this up and repost a combined series that eliminates some of the incidental churn that I've forced upon your patches.
Signature · 2 lines
Best, Oliver