Skip to content

Commit a246562

Browse files
chazyMarc Zyngier
authored andcommitted
KVM: arm64: Configure c15, PMU, and debug register traps on cpu load/put for VHE
We do not have to change the c15 trap setting on each switch to/from the guest on VHE systems, because this setting only affects guest EL1/EL0 (and therefore not the VHE host). The PMU and debug trap configuration can also be done on vcpu load/put instead, because they don't affect how the VHE host kernel can access the debug registers while executing KVM kernel code. Reviewed-by: Marc Zyngier <marc.zyngier@arm.com> Reviewed-by: Andrew Jones <drjones@redhat.com> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent c16c113 commit a246562

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

‎arch/arm64/include/asm/kvm_hyp.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ void __fpsimd_save_state(struct user_fpsimd_state *fp_regs);
147147
void __fpsimd_restore_state(struct user_fpsimd_state *fp_regs);
148148
bool __fpsimd_enabled(void);
149149

150+
void activate_traps_vhe_load(struct kvm_vcpu *vcpu);
151+
void deactivate_traps_vhe_put(void);
152+
150153
u64 __guest_enter(struct kvm_vcpu *vcpu, struct kvm_cpu_context *host_ctxt);
151154
void __noreturn __hyp_do_panic(unsigned long, ...);
152155

‎arch/arm64/kvm/hyp/switch.c‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ static void __hyp_text __activate_traps_nvhe(struct kvm_vcpu *vcpu)
102102
{
103103
u64 val;
104104

105+
__activate_traps_common(vcpu);
106+
105107
val = CPTR_EL2_DEFAULT;
106108
val |= CPTR_EL2_TTA | CPTR_EL2_TFP | CPTR_EL2_TZ;
107109
write_sysreg(val, cptr_el2);
@@ -121,20 +123,12 @@ static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
121123
write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2);
122124

123125
__activate_traps_fpsimd32(vcpu);
124-
__activate_traps_common(vcpu);
125126
__activate_traps_arch()(vcpu);
126127
}
127128

128129
static void __hyp_text __deactivate_traps_vhe(void)
129130
{
130131
extern char vectors[]; /* kernel exception vectors */
131-
u64 mdcr_el2 = read_sysreg(mdcr_el2);
132-
133-
mdcr_el2 &= MDCR_EL2_HPMN_MASK |
134-
MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
135-
MDCR_EL2_TPMS;
136-
137-
write_sysreg(mdcr_el2, mdcr_el2);
138132
write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
139133
write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
140134
write_sysreg(vectors, vbar_el1);
@@ -144,6 +138,8 @@ static void __hyp_text __deactivate_traps_nvhe(void)
144138
{
145139
u64 mdcr_el2 = read_sysreg(mdcr_el2);
146140

141+
__deactivate_traps_common();
142+
147143
mdcr_el2 &= MDCR_EL2_HPMN_MASK;
148144
mdcr_el2 |= MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT;
149145

@@ -167,10 +163,27 @@ static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
167163
if (vcpu->arch.hcr_el2 & HCR_VSE)
168164
vcpu->arch.hcr_el2 = read_sysreg(hcr_el2);
169165

170-
__deactivate_traps_common();
171166
__deactivate_traps_arch()();
172167
}
173168

169+
void activate_traps_vhe_load(struct kvm_vcpu *vcpu)
170+
{
171+
__activate_traps_common(vcpu);
172+
}
173+
174+
void deactivate_traps_vhe_put(void)
175+
{
176+
u64 mdcr_el2 = read_sysreg(mdcr_el2);
177+
178+
mdcr_el2 &= MDCR_EL2_HPMN_MASK |
179+
MDCR_EL2_E2PB_MASK << MDCR_EL2_E2PB_SHIFT |
180+
MDCR_EL2_TPMS;
181+
182+
write_sysreg(mdcr_el2, mdcr_el2);
183+
184+
__deactivate_traps_common();
185+
}
186+
174187
static void __hyp_text __activate_vm(struct kvm *kvm)
175188
{
176189
write_sysreg(kvm->arch.vttbr, vttbr_el2);

‎arch/arm64/kvm/hyp/sysreg-sr.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu)
254254
__sysreg_restore_el1_state(guest_ctxt);
255255

256256
vcpu->arch.sysregs_loaded_on_cpu = true;
257+
258+
activate_traps_vhe_load(vcpu);
257259
}
258260

259261
/**
@@ -275,6 +277,8 @@ void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu)
275277
if (!has_vhe())
276278
return;
277279

280+
deactivate_traps_vhe_put();
281+
278282
__sysreg_save_el1_state(guest_ctxt);
279283
__sysreg_save_user_state(guest_ctxt);
280284
__sysreg32_save_state(vcpu);

0 commit comments

Comments
 (0)