Skip to content

Commit d7567e9

Browse files
Anshuman Khandualwilldeacon
authored andcommitted
KVM: arm64: nvhe: Disable branch generation in nVHE guests
While BRBE can record branches within guests, the host recording branches in guests is not supported by perf (though events are). Support for BRBE in guests will supported by providing direct access to BRBE within the guests. That is how x86 LBR works for guests. Therefore, BRBE needs to be disabled on guest entry and restored on exit. For nVHE, this requires explicit handling for guests. Before entering a guest, save the BRBE state and disable the it. When returning to the host, restore the state. For VHE, it is not necessary. We initialize BRBCR_EL1.{E1BRE,E0BRE}=={0,0} at boot time, and HCR_EL2.TGE==1 while running in the host. We configure BRBCR_EL2.{E2BRE,E0HBRE} to enable branch recording in the host. When entering the guest, we set HCR_EL2.TGE==0 which means BRBCR_EL1 is used instead of BRBCR_EL2. Consequently for VHE, BRBE recording is disabled at EL1 and EL0 when running a guest. Should recording in guests (by the host) ever be desired, the perf ABI will need to be extended to distinguish guest addresses (struct perf_branch_entry.priv) for starters. BRBE records would also need to be invalidated on guest entry/exit as guest/host EL1 and EL0 records can't be distinguished. Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com> Signed-off-by: Mark Rutland <mark.rutland@arm.com> Co-developed-by: Rob Herring (Arm) <robh@kernel.org> Signed-off-by: Rob Herring (Arm) <robh@kernel.org> Tested-by: James Clark <james.clark@linaro.org> Reviewed-by: Leo Yan <leo.yan@arm.com> Reviewed-by: Suzuki K Poulose <suzuki.poulose@arm.com> Acked-by: Marc Zyngier <maz@kernel.org> Acked-by: Mark Rutland <mark.rutland@arm.com> Link: https://lore.kernel.org/r/20250611-arm-brbe-v19-v23-3-e7775563036e@kernel.org Signed-off-by: Will Deacon <will@kernel.org>
1 parent ae344bc commit d7567e9

4 files changed

Lines changed: 39 additions & 1 deletion

File tree

arch/arm64/include/asm/kvm_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ struct kvm_host_data {
704704
#define KVM_HOST_DATA_FLAG_EL1_TRACING_CONFIGURED 5
705705
#define KVM_HOST_DATA_FLAG_VCPU_IN_HYP_CONTEXT 6
706706
#define KVM_HOST_DATA_FLAG_L1_VNCR_MAPPED 7
707+
#define KVM_HOST_DATA_FLAG_HAS_BRBE 8
707708
unsigned long flags;
708709

709710
struct kvm_cpu_context host_ctxt;
@@ -737,6 +738,7 @@ struct kvm_host_data {
737738
u64 trfcr_el1;
738739
/* Values of trap registers for the host before guest entry. */
739740
u64 mdcr_el2;
741+
u64 brbcr_el1;
740742
} host_debug_state;
741743

742744
/* Guest trace filter value */

arch/arm64/kvm/debug.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ void kvm_init_host_debug_data(void)
8181
!(read_sysreg_s(SYS_PMBIDR_EL1) & PMBIDR_EL1_P))
8282
host_data_set_flag(HAS_SPE);
8383

84+
/* Check if we have BRBE implemented and available at the host */
85+
if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_BRBE_SHIFT))
86+
host_data_set_flag(HAS_BRBE);
87+
8488
if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_SHIFT)) {
8589
/* Force disable trace in protected mode in case of no TRBE */
8690
if (is_protected_kvm_enabled())

arch/arm64/kvm/hyp/nvhe/debug-sr.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,42 @@ static void __trace_switch_to_host(void)
9292
*host_data_ptr(host_debug_state.trfcr_el1));
9393
}
9494

95+
static void __debug_save_brbe(u64 *brbcr_el1)
96+
{
97+
*brbcr_el1 = 0;
98+
99+
/* Check if the BRBE is enabled */
100+
if (!(read_sysreg_el1(SYS_BRBCR) & (BRBCR_ELx_E0BRE | BRBCR_ELx_ExBRE)))
101+
return;
102+
103+
/*
104+
* Prohibit branch record generation while we are in guest.
105+
* Since access to BRBCR_EL1 is trapped, the guest can't
106+
* modify the filtering set by the host.
107+
*/
108+
*brbcr_el1 = read_sysreg_el1(SYS_BRBCR);
109+
write_sysreg_el1(0, SYS_BRBCR);
110+
}
111+
112+
static void __debug_restore_brbe(u64 brbcr_el1)
113+
{
114+
if (!brbcr_el1)
115+
return;
116+
117+
/* Restore BRBE controls */
118+
write_sysreg_el1(brbcr_el1, SYS_BRBCR);
119+
}
120+
95121
void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
96122
{
97123
/* Disable and flush SPE data generation */
98124
if (host_data_test_flag(HAS_SPE))
99125
__debug_save_spe(host_data_ptr(host_debug_state.pmscr_el1));
100126

127+
/* Disable BRBE branch records */
128+
if (host_data_test_flag(HAS_BRBE))
129+
__debug_save_brbe(host_data_ptr(host_debug_state.brbcr_el1));
130+
101131
if (__trace_needs_switch())
102132
__trace_switch_to_guest();
103133
}
@@ -111,6 +141,8 @@ void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
111141
{
112142
if (host_data_test_flag(HAS_SPE))
113143
__debug_restore_spe(*host_data_ptr(host_debug_state.pmscr_el1));
144+
if (host_data_test_flag(HAS_BRBE))
145+
__debug_restore_brbe(*host_data_ptr(host_debug_state.brbcr_el1));
114146
if (__trace_needs_switch())
115147
__trace_switch_to_host();
116148
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
331331
* We're about to restore some new MMU state. Make sure
332332
* ongoing page-table walks that have started before we
333333
* trapped to EL2 have completed. This also synchronises the
334-
* above disabling of SPE and TRBE.
334+
* above disabling of BRBE, SPE and TRBE.
335335
*
336336
* See DDI0487I.a D8.1.5 "Out-of-context translation regimes",
337337
* rule R_LFHQG and subsequent information statements.

0 commit comments

Comments
 (0)