Skip to content

Commit 38131c0

Browse files
ouptonMarc Zyngier
authored andcommitted
KVM: arm64: Track presence of SPE/TRBE in kvm_host_data instead of vCPU
Add flags to kvm_host_data to track if SPE/TRBE is present + programmable on a per-CPU basis. Set the flags up at init rather than vcpu_load() as the programmability of these buffers is unlikely to change. Reviewed-by: James Clark <[email protected]> Tested-by: James Clark <[email protected]> Signed-off-by: Oliver Upton <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Marc Zyngier <[email protected]>
1 parent 2417218 commit 38131c0

File tree

4 files changed

+24
-42
lines changed

4 files changed

+24
-42
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,10 @@ struct cpu_sve_state {
610610
* field.
611611
*/
612612
struct kvm_host_data {
613+
#define KVM_HOST_DATA_FLAG_HAS_SPE 0
614+
#define KVM_HOST_DATA_FLAG_HAS_TRBE 1
615+
unsigned long flags;
616+
613617
struct kvm_cpu_context host_ctxt;
614618

615619
/*
@@ -911,10 +915,6 @@ struct kvm_vcpu_arch {
911915
#define EXCEPT_AA64_EL2_SERR __vcpu_except_flags(7)
912916
/* Guest debug is live */
913917
#define DEBUG_DIRTY __vcpu_single_flag(iflags, BIT(4))
914-
/* Save SPE context if active */
915-
#define DEBUG_STATE_SAVE_SPE __vcpu_single_flag(iflags, BIT(5))
916-
/* Save TRBE context if active */
917-
#define DEBUG_STATE_SAVE_TRBE __vcpu_single_flag(iflags, BIT(6))
918918

919919
/* SVE enabled for host EL0 */
920920
#define HOST_SVE_ENABLED __vcpu_single_flag(sflags, BIT(0))
@@ -1310,6 +1310,13 @@ DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data);
13101310
&this_cpu_ptr_hyp_sym(kvm_host_data)->f)
13111311
#endif
13121312

1313+
#define host_data_test_flag(flag) \
1314+
(test_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags)))
1315+
#define host_data_set_flag(flag) \
1316+
set_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags))
1317+
#define host_data_clear_flag(flag) \
1318+
clear_bit(KVM_HOST_DATA_FLAG_##flag, host_data_ptr(flags))
1319+
13131320
/* Check whether the FP regs are owned by the guest */
13141321
static inline bool guest_owns_fp_regs(void)
13151322
{
@@ -1370,10 +1377,6 @@ static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
13701377
return (!has_vhe() && attr->exclude_host);
13711378
}
13721379

1373-
/* Flags for host debug state */
1374-
void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu);
1375-
void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
1376-
13771380
#ifdef CONFIG_KVM
13781381
void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr);
13791382
void kvm_clr_pmu_events(u64 clr);

arch/arm64/kvm/arm.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,12 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
617617

618618
vcpu_set_pauth_traps(vcpu);
619619

620-
kvm_arch_vcpu_load_debug_state_flags(vcpu);
621-
622620
if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus))
623621
vcpu_set_on_unsupported_cpu(vcpu);
624622
}
625623

626624
void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
627625
{
628-
kvm_arch_vcpu_put_debug_state_flags(vcpu);
629626
kvm_arch_vcpu_put_fp(vcpu);
630627
if (has_vhe())
631628
kvm_vcpu_put_vhe(vcpu);

arch/arm64/kvm/debug.c

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -294,40 +294,22 @@ void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
294294
}
295295
}
296296

297-
void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu)
297+
void kvm_init_host_debug_data(void)
298298
{
299-
u64 dfr0;
299+
u64 dfr0 = read_sysreg(id_aa64dfr0_el1);
300+
301+
if (cpuid_feature_extract_signed_field(dfr0, ID_AA64DFR0_EL1_PMUVer_SHIFT) > 0)
302+
*host_data_ptr(nr_event_counters) = FIELD_GET(ARMV8_PMU_PMCR_N,
303+
read_sysreg(pmcr_el0));
300304

301-
/* For VHE, there is nothing to do */
302305
if (has_vhe())
303306
return;
304307

305-
dfr0 = read_sysreg(id_aa64dfr0_el1);
306-
/*
307-
* If SPE is present on this CPU and is available at current EL,
308-
* we may need to check if the host state needs to be saved.
309-
*/
310308
if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_PMSVer_SHIFT) &&
311-
!(read_sysreg_s(SYS_PMBIDR_EL1) & BIT(PMBIDR_EL1_P_SHIFT)))
312-
vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_SPE);
309+
!(read_sysreg_s(SYS_PMBIDR_EL1) & PMBIDR_EL1_P))
310+
host_data_set_flag(HAS_SPE);
313311

314-
/* Check if we have TRBE implemented and available at the host */
315312
if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffer_SHIFT) &&
316313
!(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_EL1_P))
317-
vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
318-
}
319-
320-
void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
321-
{
322-
vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE);
323-
vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
324-
}
325-
326-
void kvm_init_host_debug_data(void)
327-
{
328-
u64 dfr0 = read_sysreg(id_aa64dfr0_el1);
329-
330-
if (cpuid_feature_extract_signed_field(dfr0, ID_AA64DFR0_EL1_PMUVer_SHIFT) > 0)
331-
*host_data_ptr(nr_event_counters) = FIELD_GET(ARMV8_PMU_PMCR_N,
332-
read_sysreg(pmcr_el0));
314+
host_data_set_flag(HAS_TRBE);
333315
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ static void __debug_restore_trace(u64 trfcr_el1)
8282
void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
8383
{
8484
/* Disable and flush SPE data generation */
85-
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
85+
if (host_data_test_flag(HAS_SPE))
8686
__debug_save_spe(host_data_ptr(host_debug_state.pmscr_el1));
8787
/* Disable and flush Self-Hosted Trace generation */
88-
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRBE))
88+
if (host_data_test_flag(HAS_TRBE))
8989
__debug_save_trace(host_data_ptr(host_debug_state.trfcr_el1));
9090
}
9191

@@ -96,9 +96,9 @@ void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
9696

9797
void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
9898
{
99-
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
99+
if (host_data_test_flag(HAS_SPE))
100100
__debug_restore_spe(*host_data_ptr(host_debug_state.pmscr_el1));
101-
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRBE))
101+
if (host_data_test_flag(HAS_TRBE))
102102
__debug_restore_trace(*host_data_ptr(host_debug_state.trfcr_el1));
103103
}
104104

0 commit comments

Comments
 (0)