* [PATCH v1 1/5] arm64/sysreg: Move TRFCR definitions to sysreg
2023-09-28 15:16 [PATCH v1 0/5] kvm/coresight: Support exclude guest and exclude host James Clark
@ 2023-09-28 15:16 ` James Clark
2023-09-28 15:16 ` [PATCH v1 2/5] arm64: KVM: Rename DEBUG_STATE_SAVE_TRBE to DEBUG_STATE_SAVE_TRFCR James Clark
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: James Clark @ 2023-09-28 15:16 UTC (permalink / raw)
To: coresight, linux-arm-kernel, kvmarm, broonie, maz, suzuki.poulose
Cc: James Clark, Oliver Upton, James Morse, Zenghui Yu,
Catalin Marinas, Will Deacon, Mike Leach, Leo Yan,
Alexander Shishkin, Anshuman Khandual, Rob Herring, Jintack Lim,
Joey Gouly, linux-kernel
Add separate definitions for ELx and EL2 as TRFCR_EL1 doesn't have CX.
This also mirrors the previous definition so no code change is required.
Reviewed-by: Mark Brown <broonie@kernel.org>
Signed-off-by: James Clark <james.clark@arm.com>
---
arch/arm64/include/asm/sysreg.h | 12 -----------
arch/arm64/tools/sysreg | 37 +++++++++++++++++++++++++++++++++
2 files changed, 37 insertions(+), 12 deletions(-)
diff --git a/arch/arm64/include/asm/sysreg.h b/arch/arm64/include/asm/sysreg.h
index 38296579a4fd..068dd6abe273 100644
--- a/arch/arm64/include/asm/sysreg.h
+++ b/arch/arm64/include/asm/sysreg.h
@@ -278,8 +278,6 @@
#define SYS_RGSR_EL1 sys_reg(3, 0, 1, 0, 5)
#define SYS_GCR_EL1 sys_reg(3, 0, 1, 0, 6)
-#define SYS_TRFCR_EL1 sys_reg(3, 0, 1, 2, 1)
-
#define SYS_TCR_EL1 sys_reg(3, 0, 2, 0, 2)
#define SYS_APIAKEYLO_EL1 sys_reg(3, 0, 2, 1, 0)
@@ -496,7 +494,6 @@
#define SYS_VTTBR_EL2 sys_reg(3, 4, 2, 1, 0)
#define SYS_VTCR_EL2 sys_reg(3, 4, 2, 1, 2)
-#define SYS_TRFCR_EL2 sys_reg(3, 4, 1, 2, 1)
#define SYS_HAFGRTR_EL2 sys_reg(3, 4, 3, 1, 6)
#define SYS_SPSR_EL2 sys_reg(3, 4, 4, 0, 0)
#define SYS_ELR_EL2 sys_reg(3, 4, 4, 0, 1)
@@ -904,15 +901,6 @@
/* Safe value for MPIDR_EL1: Bit31:RES1, Bit30:U:0, Bit24:MT:0 */
#define SYS_MPIDR_SAFE_VAL (BIT(31))
-#define TRFCR_ELx_TS_SHIFT 5
-#define TRFCR_ELx_TS_MASK ((0x3UL) << TRFCR_ELx_TS_SHIFT)
-#define TRFCR_ELx_TS_VIRTUAL ((0x1UL) << TRFCR_ELx_TS_SHIFT)
-#define TRFCR_ELx_TS_GUEST_PHYSICAL ((0x2UL) << TRFCR_ELx_TS_SHIFT)
-#define TRFCR_ELx_TS_PHYSICAL ((0x3UL) << TRFCR_ELx_TS_SHIFT)
-#define TRFCR_EL2_CX BIT(3)
-#define TRFCR_ELx_ExTRE BIT(1)
-#define TRFCR_ELx_E0TRE BIT(0)
-
/* GIC Hypervisor interface registers */
/* ICH_MISR_EL2 bit definitions */
#define ICH_MISR_EOI (1 << 0)
diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg
index 2517ef7c21cf..992722c0c23b 100644
--- a/arch/arm64/tools/sysreg
+++ b/arch/arm64/tools/sysreg
@@ -2624,3 +2624,40 @@ Field 5 F
Field 4 P
Field 3:0 Align
EndSysreg
+
+SysregFields TRFCR_EL2
+Res0 63:7
+UnsignedEnum 6:5 TS
+ 0b0000 USE_TRFCR_EL1_TS
+ 0b0001 VIRTUAL
+ 0b0010 GUEST_PHYSICAL
+ 0b0011 PHYSICAL
+EndEnum
+Res0 4
+Field 3 CX
+Res0 2
+Field 1 E2TRE
+Field 0 E0HTRE
+EndSysregFields
+
+# TRFCR_EL1 doesn't have the CX bit so redefine it without CX instead of
+# using a shared definition between TRFCR_EL2 and TRFCR_EL1
+SysregFields TRFCR_ELx
+Res0 63:7
+UnsignedEnum 6:5 TS
+ 0b0001 VIRTUAL
+ 0b0010 GUEST_PHYSICAL
+ 0b0011 PHYSICAL
+EndEnum
+Res0 4:2
+Field 1 ExTRE
+Field 0 E0TRE
+EndSysregFields
+
+Sysreg TRFCR_EL1 3 0 1 2 1
+Fields TRFCR_ELx
+EndSysreg
+
+Sysreg TRFCR_EL2 3 4 1 2 1
+Fields TRFCR_EL2
+EndSysreg
--
2.34.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v1 2/5] arm64: KVM: Rename DEBUG_STATE_SAVE_TRBE to DEBUG_STATE_SAVE_TRFCR
2023-09-28 15:16 [PATCH v1 0/5] kvm/coresight: Support exclude guest and exclude host James Clark
2023-09-28 15:16 ` [PATCH v1 1/5] arm64/sysreg: Move TRFCR definitions to sysreg James Clark
@ 2023-09-28 15:16 ` James Clark
2023-09-28 15:16 ` [PATCH v1 3/5] arm64: KVM: Add interface to set guest value for TRFCR register James Clark
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: James Clark @ 2023-09-28 15:16 UTC (permalink / raw)
To: coresight, linux-arm-kernel, kvmarm, broonie, maz, suzuki.poulose
Cc: James Clark, Oliver Upton, James Morse, Zenghui Yu,
Catalin Marinas, Will Deacon, Mike Leach, Leo Yan,
Alexander Shishkin, Anshuman Khandual, Rob Herring, Jintack Lim,
Joey Gouly, linux-kernel
This flag actually causes the TRFCR register to be saved, so rename it
to that effect.
Currently it only happens when TRBE is used, but in a later commit
TRFCR will be saved and restored even if TRBE isn't used, so the new
name will be more accurate.
Signed-off-by: James Clark <james.clark@arm.com>
---
arch/arm64/include/asm/kvm_host.h | 4 ++--
arch/arm64/kvm/debug.c | 4 ++--
arch/arm64/kvm/hyp/nvhe/debug-sr.c | 4 ++--
3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index c3a17888f183..498f922f4f41 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -713,8 +713,8 @@ struct kvm_vcpu_arch {
#define DEBUG_DIRTY __vcpu_single_flag(iflags, BIT(4))
/* Save SPE context if active */
#define DEBUG_STATE_SAVE_SPE __vcpu_single_flag(iflags, BIT(5))
-/* Save TRBE context if active */
-#define DEBUG_STATE_SAVE_TRBE __vcpu_single_flag(iflags, BIT(6))
+/* Save TRFCR and disable TRBE if necessary */
+#define DEBUG_STATE_SAVE_TRFCR __vcpu_single_flag(iflags, BIT(6))
/* vcpu running in HYP context */
#define VCPU_HYP_CONTEXT __vcpu_single_flag(iflags, BIT(7))
diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
index 8725291cb00a..6a1bad1a921b 100644
--- a/arch/arm64/kvm/debug.c
+++ b/arch/arm64/kvm/debug.c
@@ -334,11 +334,11 @@ void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu)
/* Check if we have TRBE implemented and available at the host */
if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffer_SHIFT) &&
!(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_EL1_P))
- vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
+ vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
}
void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
{
vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE);
- vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
+ vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
}
diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
index 4558c02eb352..89c208112eb7 100644
--- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c
+++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
@@ -85,7 +85,7 @@ void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
__debug_save_spe(&vcpu->arch.host_debug_state.pmscr_el1);
/* Disable and flush Self-Hosted Trace generation */
- if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRBE))
+ if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
__debug_save_trace(&vcpu->arch.host_debug_state.trfcr_el1);
}
@@ -98,7 +98,7 @@ void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
{
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
__debug_restore_spe(vcpu->arch.host_debug_state.pmscr_el1);
- if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRBE))
+ if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
__debug_restore_trace(vcpu->arch.host_debug_state.trfcr_el1);
}
--
2.34.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v1 3/5] arm64: KVM: Add interface to set guest value for TRFCR register
2023-09-28 15:16 [PATCH v1 0/5] kvm/coresight: Support exclude guest and exclude host James Clark
2023-09-28 15:16 ` [PATCH v1 1/5] arm64/sysreg: Move TRFCR definitions to sysreg James Clark
2023-09-28 15:16 ` [PATCH v1 2/5] arm64: KVM: Rename DEBUG_STATE_SAVE_TRBE to DEBUG_STATE_SAVE_TRFCR James Clark
@ 2023-09-28 15:16 ` James Clark
2023-09-29 10:17 ` Marc Zyngier
2023-09-28 15:16 ` [PATCH v1 4/5] arm64: KVM: Write TRFCR value on guest switch with nVHE James Clark
2023-09-28 15:16 ` [PATCH v1 5/5] coresight: Pass guest TRFCR value to KVM James Clark
4 siblings, 1 reply; 8+ messages in thread
From: James Clark @ 2023-09-28 15:16 UTC (permalink / raw)
To: coresight, linux-arm-kernel, kvmarm, broonie, maz, suzuki.poulose
Cc: James Clark, Oliver Upton, James Morse, Zenghui Yu,
Catalin Marinas, Will Deacon, Mike Leach, Leo Yan,
Alexander Shishkin, Anshuman Khandual, Rob Herring, Jintack Lim,
Joey Gouly, linux-kernel
Add an interface for the Coresight driver to use to set the value of the
TRFCR register for the guest. This register controls the exclude
settings for trace at different exception levels, and is used to
honor the exclude_host and exclude_guest parameters from the Perf
session. This will be used to later write TRFCR_EL1 on nVHE at guest
switch. For VHE, TRFCR_EL1 is written immediately. Because guest writes
to the register are trapped, the value will persist and can't be
modified.
The settings must be copied to the vCPU before each run in the same
way that PMU events are because the per-cpu struct isn't accessible in
protected mode.
Now that both guest and host values are saved, rename trfcr_el1 to
host_trfcr_el1 to make it clear that's the value that should be restored
on return to the host.
Reviewed-by: Mark Brown <broonie@kernel.org> (sysreg)
Signed-off-by: James Clark <james.clark@arm.com>
---
arch/arm64/include/asm/kvm_host.h | 6 +++++-
arch/arm64/kvm/arm.c | 1 +
arch/arm64/kvm/debug.c | 21 +++++++++++++++++++++
arch/arm64/kvm/hyp/nvhe/debug-sr.c | 4 ++--
arch/arm64/tools/sysreg | 4 ++++
5 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 498f922f4f41..0e57827a0cf2 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -547,7 +547,8 @@ struct kvm_vcpu_arch {
/* Statistical profiling extension */
u64 pmscr_el1;
/* Self-hosted trace */
- u64 trfcr_el1;
+ u64 host_trfcr_el1;
+ u64 guest_trfcr_el1;
} host_debug_state;
/* VGIC state */
@@ -1097,6 +1098,8 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr);
void kvm_clr_pmu_events(u32 clr);
bool kvm_set_pmuserenr(u64 val);
+void kvm_etm_set_guest_trfcr(u64 trfcr_guest);
+void kvm_etm_update_vcpu_events(struct kvm_vcpu *vcpu);
#else
static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
static inline void kvm_clr_pmu_events(u32 clr) {}
@@ -1104,6 +1107,7 @@ static inline bool kvm_set_pmuserenr(u64 val)
{
return false;
}
+static inline void kvm_etm_set_guest_trfcr(u64 trfcr_guest) {}
#endif
void kvm_vcpu_load_sysregs_vhe(struct kvm_vcpu *vcpu);
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 1bfdd583b261..65e805dc1d7a 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -958,6 +958,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
kvm_vgic_flush_hwstate(vcpu);
kvm_pmu_update_vcpu_events(vcpu);
+ kvm_etm_update_vcpu_events(vcpu);
/*
* Ensure we set mode to IN_GUEST_MODE after we disable
diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
index 6a1bad1a921b..379d2677961f 100644
--- a/arch/arm64/kvm/debug.c
+++ b/arch/arm64/kvm/debug.c
@@ -22,6 +22,7 @@
DBG_MDSCR_MDE)
static DEFINE_PER_CPU(u64, mdcr_el2);
+static DEFINE_PER_CPU(u64, guest_trfcr);
/**
* save/restore_guest_debug_regs
@@ -342,3 +343,23 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE);
vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
}
+
+void kvm_etm_set_guest_trfcr(u64 trfcr_guest)
+{
+ if (has_vhe())
+ write_sysreg_s(trfcr_guest, SYS_TRFCR_EL12);
+ else
+ *this_cpu_ptr(&guest_trfcr) = trfcr_guest;
+}
+EXPORT_SYMBOL_GPL(kvm_etm_set_guest_trfcr);
+
+/*
+ * Updates the vcpu's view of the etm events for this cpu. Must be
+ * called before every vcpu run after disabling interrupts, to ensure
+ * that an interrupt cannot fire and update the structure.
+ */
+void kvm_etm_update_vcpu_events(struct kvm_vcpu *vcpu)
+{
+ if (!has_vhe() && vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
+ vcpu->arch.host_debug_state.guest_trfcr_el1 = *this_cpu_ptr(&guest_trfcr);
+}
diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
index 89c208112eb7..55bc01e9808f 100644
--- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c
+++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
@@ -86,7 +86,7 @@ void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
__debug_save_spe(&vcpu->arch.host_debug_state.pmscr_el1);
/* Disable and flush Self-Hosted Trace generation */
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
- __debug_save_trace(&vcpu->arch.host_debug_state.trfcr_el1);
+ __debug_save_trace(&vcpu->arch.host_debug_state.host_trfcr_el1);
}
void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
@@ -99,7 +99,7 @@ void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
__debug_restore_spe(vcpu->arch.host_debug_state.pmscr_el1);
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
- __debug_restore_trace(vcpu->arch.host_debug_state.trfcr_el1);
+ __debug_restore_trace(vcpu->arch.host_debug_state.host_trfcr_el1);
}
void __debug_switch_to_host(struct kvm_vcpu *vcpu)
diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg
index 992722c0c23b..295a4a625b8b 100644
--- a/arch/arm64/tools/sysreg
+++ b/arch/arm64/tools/sysreg
@@ -2661,3 +2661,7 @@ EndSysreg
Sysreg TRFCR_EL2 3 4 1 2 1
Fields TRFCR_EL2
EndSysreg
+
+Sysreg TRFCR_EL12 3 5 1 2 1
+Fields TRFCR_ELx
+EndSysreg
--
2.34.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH v1 3/5] arm64: KVM: Add interface to set guest value for TRFCR register
2023-09-28 15:16 ` [PATCH v1 3/5] arm64: KVM: Add interface to set guest value for TRFCR register James Clark
@ 2023-09-29 10:17 ` Marc Zyngier
2023-09-29 12:09 ` James Clark
0 siblings, 1 reply; 8+ messages in thread
From: Marc Zyngier @ 2023-09-29 10:17 UTC (permalink / raw)
To: James Clark
Cc: coresight, linux-arm-kernel, kvmarm, broonie, suzuki.poulose,
Oliver Upton, James Morse, Zenghui Yu, Catalin Marinas,
Will Deacon, Mike Leach, Leo Yan, Alexander Shishkin,
Anshuman Khandual, Rob Herring, Jintack Lim, Joey Gouly,
linux-kernel
On Thu, 28 Sep 2023 16:16:07 +0100,
James Clark <james.clark@arm.com> wrote:
>
> Add an interface for the Coresight driver to use to set the value of the
> TRFCR register for the guest. This register controls the exclude
> settings for trace at different exception levels, and is used to
> honor the exclude_host and exclude_guest parameters from the Perf
> session. This will be used to later write TRFCR_EL1 on nVHE at guest
> switch. For VHE, TRFCR_EL1 is written immediately. Because guest writes
> to the register are trapped, the value will persist and can't be
> modified.
>
> The settings must be copied to the vCPU before each run in the same
> way that PMU events are because the per-cpu struct isn't accessible in
> protected mode.
>
> Now that both guest and host values are saved, rename trfcr_el1 to
> host_trfcr_el1 to make it clear that's the value that should be restored
> on return to the host.
>
> Reviewed-by: Mark Brown <broonie@kernel.org> (sysreg)
> Signed-off-by: James Clark <james.clark@arm.com>
> ---
> arch/arm64/include/asm/kvm_host.h | 6 +++++-
> arch/arm64/kvm/arm.c | 1 +
> arch/arm64/kvm/debug.c | 21 +++++++++++++++++++++
> arch/arm64/kvm/hyp/nvhe/debug-sr.c | 4 ++--
> arch/arm64/tools/sysreg | 4 ++++
> 5 files changed, 33 insertions(+), 3 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
> index 498f922f4f41..0e57827a0cf2 100644
> --- a/arch/arm64/include/asm/kvm_host.h
> +++ b/arch/arm64/include/asm/kvm_host.h
> @@ -547,7 +547,8 @@ struct kvm_vcpu_arch {
> /* Statistical profiling extension */
> u64 pmscr_el1;
> /* Self-hosted trace */
> - u64 trfcr_el1;
> + u64 host_trfcr_el1;
> + u64 guest_trfcr_el1;
> } host_debug_state;
I think it is high time we stop having *guest* state in a structure
that is obviously for the host, starting by moving the breakpoint and
watchpoint out, and into the sysreg array.
And then TRFCR_EL1 can join the fun. But it is pretty unclear whether
that's actually the guest state.
>
> /* VGIC state */
> @@ -1097,6 +1098,8 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
> void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr);
> void kvm_clr_pmu_events(u32 clr);
> bool kvm_set_pmuserenr(u64 val);
> +void kvm_etm_set_guest_trfcr(u64 trfcr_guest);
> +void kvm_etm_update_vcpu_events(struct kvm_vcpu *vcpu);
> #else
> static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
> static inline void kvm_clr_pmu_events(u32 clr) {}
> @@ -1104,6 +1107,7 @@ static inline bool kvm_set_pmuserenr(u64 val)
> {
> return false;
> }
> +static inline void kvm_etm_set_guest_trfcr(u64 trfcr_guest) {}
> #endif
>
> void kvm_vcpu_load_sysregs_vhe(struct kvm_vcpu *vcpu);
> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
> index 1bfdd583b261..65e805dc1d7a 100644
> --- a/arch/arm64/kvm/arm.c
> +++ b/arch/arm64/kvm/arm.c
> @@ -958,6 +958,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
> kvm_vgic_flush_hwstate(vcpu);
>
> kvm_pmu_update_vcpu_events(vcpu);
> + kvm_etm_update_vcpu_events(vcpu);
>
> /*
> * Ensure we set mode to IN_GUEST_MODE after we disable
> diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
> index 6a1bad1a921b..379d2677961f 100644
> --- a/arch/arm64/kvm/debug.c
> +++ b/arch/arm64/kvm/debug.c
> @@ -22,6 +22,7 @@
> DBG_MDSCR_MDE)
>
> static DEFINE_PER_CPU(u64, mdcr_el2);
> +static DEFINE_PER_CPU(u64, guest_trfcr);
Hold on a sec. Why is that global? I'd expect the tracing to be
specific to a vcpu, and not to affect *everything*.
>
> /**
> * save/restore_guest_debug_regs
> @@ -342,3 +343,23 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
> vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE);
> vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
> }
> +
> +void kvm_etm_set_guest_trfcr(u64 trfcr_guest)
> +{
> + if (has_vhe())
> + write_sysreg_s(trfcr_guest, SYS_TRFCR_EL12);
> + else
> + *this_cpu_ptr(&guest_trfcr) = trfcr_guest;
> +}
> +EXPORT_SYMBOL_GPL(kvm_etm_set_guest_trfcr);
In what context can this be called? What if we want to expose TRFCR to
guests instead? It looks to me that this ultimately precludes such
use.
> +
> +/*
> + * Updates the vcpu's view of the etm events for this cpu. Must be
> + * called before every vcpu run after disabling interrupts, to ensure
> + * that an interrupt cannot fire and update the structure.
> + */
> +void kvm_etm_update_vcpu_events(struct kvm_vcpu *vcpu)
> +{
> + if (!has_vhe() && vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
> + vcpu->arch.host_debug_state.guest_trfcr_el1 = *this_cpu_ptr(&guest_trfcr);
But what is the point of this per-vcpu field if all you care about is
some per-CPU data?
> +}
> diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
> index 89c208112eb7..55bc01e9808f 100644
> --- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c
> +++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
> @@ -86,7 +86,7 @@ void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
> __debug_save_spe(&vcpu->arch.host_debug_state.pmscr_el1);
> /* Disable and flush Self-Hosted Trace generation */
> if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
> - __debug_save_trace(&vcpu->arch.host_debug_state.trfcr_el1);
> + __debug_save_trace(&vcpu->arch.host_debug_state.host_trfcr_el1);
> }
`<>
> void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
> @@ -99,7 +99,7 @@ void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
> if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
> __debug_restore_spe(vcpu->arch.host_debug_state.pmscr_el1);
> if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
> - __debug_restore_trace(vcpu->arch.host_debug_state.trfcr_el1);
> + __debug_restore_trace(vcpu->arch.host_debug_state.host_trfcr_el1);
> }
>
> void __debug_switch_to_host(struct kvm_vcpu *vcpu)
> diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg
> index 992722c0c23b..295a4a625b8b 100644
> --- a/arch/arm64/tools/sysreg
> +++ b/arch/arm64/tools/sysreg
> @@ -2661,3 +2661,7 @@ EndSysreg
> Sysreg TRFCR_EL2 3 4 1 2 1
> Fields TRFCR_EL2
> EndSysreg
> +
> +Sysreg TRFCR_EL12 3 5 1 2 1
> +Fields TRFCR_ELx
> +EndSysreg
Please move this to the first patch.
M.
--
Without deviation from the norm, progress is not possible.
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH v1 3/5] arm64: KVM: Add interface to set guest value for TRFCR register
2023-09-29 10:17 ` Marc Zyngier
@ 2023-09-29 12:09 ` James Clark
0 siblings, 0 replies; 8+ messages in thread
From: James Clark @ 2023-09-29 12:09 UTC (permalink / raw)
To: Marc Zyngier
Cc: coresight, linux-arm-kernel, kvmarm, broonie, suzuki.poulose,
Oliver Upton, James Morse, Zenghui Yu, Catalin Marinas,
Will Deacon, Mike Leach, Leo Yan, Alexander Shishkin,
Anshuman Khandual, Rob Herring, Jintack Lim, Joey Gouly,
linux-kernel
On 29/09/2023 11:17, Marc Zyngier wrote:
> On Thu, 28 Sep 2023 16:16:07 +0100,
> James Clark <james.clark@arm.com> wrote:
>>
>> Add an interface for the Coresight driver to use to set the value of the
>> TRFCR register for the guest. This register controls the exclude
>> settings for trace at different exception levels, and is used to
>> honor the exclude_host and exclude_guest parameters from the Perf
>> session. This will be used to later write TRFCR_EL1 on nVHE at guest
>> switch. For VHE, TRFCR_EL1 is written immediately. Because guest writes
>> to the register are trapped, the value will persist and can't be
>> modified.
>>
>> The settings must be copied to the vCPU before each run in the same
>> way that PMU events are because the per-cpu struct isn't accessible in
>> protected mode.
>>
>> Now that both guest and host values are saved, rename trfcr_el1 to
>> host_trfcr_el1 to make it clear that's the value that should be restored
>> on return to the host.
>>
>> Reviewed-by: Mark Brown <broonie@kernel.org> (sysreg)
>> Signed-off-by: James Clark <james.clark@arm.com>
>> ---
>> arch/arm64/include/asm/kvm_host.h | 6 +++++-
>> arch/arm64/kvm/arm.c | 1 +
>> arch/arm64/kvm/debug.c | 21 +++++++++++++++++++++
>> arch/arm64/kvm/hyp/nvhe/debug-sr.c | 4 ++--
>> arch/arm64/tools/sysreg | 4 ++++
>> 5 files changed, 33 insertions(+), 3 deletions(-)
>>
>> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
>> index 498f922f4f41..0e57827a0cf2 100644
>> --- a/arch/arm64/include/asm/kvm_host.h
>> +++ b/arch/arm64/include/asm/kvm_host.h
>> @@ -547,7 +547,8 @@ struct kvm_vcpu_arch {
>> /* Statistical profiling extension */
>> u64 pmscr_el1;
>> /* Self-hosted trace */
>> - u64 trfcr_el1;
>> + u64 host_trfcr_el1;
>> + u64 guest_trfcr_el1;
>> } host_debug_state;
>
> I think it is high time we stop having *guest* state in a structure
> that is obviously for the host, starting by moving the breakpoint and
> watchpoint out, and into the sysreg array.
>
It looks like host_debug_state::regs is actually the host debug state,
despite the type being called "struct kvm_guest_debug_arch".
But yeah I can move pmscr_el1 and trfcr_el1 to the guest and host sysreg
arrays, that would make sense.
host_debug_state::regs seems to be a bit more intertwined with the
kvm_arch_vcpu_ioctl_set_guest_debug() ioctl and the pattern to support
two different sets of guest debug state, so I'm not sure if it's worth
touching that at this point:
* We maintain more than a single set of debug registers to support
* debugging the guest
...
* debug_ptr points to the set of debug registers that should be loaded
* onto the hardware when running the guest.
*/
struct kvm_guest_debug_arch *debug_ptr;
struct kvm_guest_debug_arch vcpu_debug_state;
struct kvm_guest_debug_arch external_debug_state;
> And then TRFCR_EL1 can join the fun. But it is pretty unclear whether
> that's actually the guest state.
>
It has an effect on the guest, but can never actually be read or written
by the guest. Not sure if that pattern exists elsewhere...
>>
>> /* VGIC state */
>> @@ -1097,6 +1098,8 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
>> void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr);
>> void kvm_clr_pmu_events(u32 clr);
>> bool kvm_set_pmuserenr(u64 val);
>> +void kvm_etm_set_guest_trfcr(u64 trfcr_guest);
>> +void kvm_etm_update_vcpu_events(struct kvm_vcpu *vcpu);
>> #else
>> static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
>> static inline void kvm_clr_pmu_events(u32 clr) {}
>> @@ -1104,6 +1107,7 @@ static inline bool kvm_set_pmuserenr(u64 val)
>> {
>> return false;
>> }
>> +static inline void kvm_etm_set_guest_trfcr(u64 trfcr_guest) {}
>> #endif
>>
>> void kvm_vcpu_load_sysregs_vhe(struct kvm_vcpu *vcpu);
>> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
>> index 1bfdd583b261..65e805dc1d7a 100644
>> --- a/arch/arm64/kvm/arm.c
>> +++ b/arch/arm64/kvm/arm.c
>> @@ -958,6 +958,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
>> kvm_vgic_flush_hwstate(vcpu);
>>
>> kvm_pmu_update_vcpu_events(vcpu);
>> + kvm_etm_update_vcpu_events(vcpu);
>>
>> /*
>> * Ensure we set mode to IN_GUEST_MODE after we disable
>> diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
>> index 6a1bad1a921b..379d2677961f 100644
>> --- a/arch/arm64/kvm/debug.c
>> +++ b/arch/arm64/kvm/debug.c
>> @@ -22,6 +22,7 @@
>> DBG_MDSCR_MDE)
>>
>> static DEFINE_PER_CPU(u64, mdcr_el2);
>> +static DEFINE_PER_CPU(u64, guest_trfcr);
>
> Hold on a sec. Why is that global? I'd expect the tracing to be
> specific to a vcpu, and not to affect *everything*.
>
I suppose because it is a property of the host CPU rather than the vcpu.
The vcpu might not even exist yet when the Perf session starts, and Perf
sessions only care about the host CPU's perspective. It's defining the
guest trfcr value for any vcpu that might or might not run on this core
in the future, rather than any one specific vcpu.
I copied the pattern from the existing Perf PMU settings in
arch/arm64/kvm/pmu.c:
static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);
We could flip it around and make KVM say "what's the guest trfcr value
for this core?" to the Coresight driver at the point of guest switch,
but all that would mean is that the global would be in the Coresight
driver instead.
>>
>> /**
>> * save/restore_guest_debug_regs
>> @@ -342,3 +343,23 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
>> vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE);
>> vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
>> }
>> +
>> +void kvm_etm_set_guest_trfcr(u64 trfcr_guest)
>> +{
>> + if (has_vhe())
>> + write_sysreg_s(trfcr_guest, SYS_TRFCR_EL12);
>> + else
>> + *this_cpu_ptr(&guest_trfcr) = trfcr_guest;
>> +}
>> +EXPORT_SYMBOL_GPL(kvm_etm_set_guest_trfcr);
>
> In what context can this be called? What if we want to expose TRFCR to
> guests instead? It looks to me that this ultimately precludes such
> use.
>
Yes it does, but currently Coresight isn't exposed to guests at all so
it's not a problem.
In the future if we did decide to do it, there would be significant work
to somehow share the components between the host and the guest. But even
if they were shared, in the case where the guest isn't using trace the
trfcr and exclude guest settings would still need to work, so something
of this form would still be required.
>> +
>> +/*
>> + * Updates the vcpu's view of the etm events for this cpu. Must be
>> + * called before every vcpu run after disabling interrupts, to ensure
>> + * that an interrupt cannot fire and update the structure.
>> + */
>> +void kvm_etm_update_vcpu_events(struct kvm_vcpu *vcpu)
>> +{
>> + if (!has_vhe() && vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
>> + vcpu->arch.host_debug_state.guest_trfcr_el1 = *this_cpu_ptr(&guest_trfcr);
>
> But what is the point of this per-vcpu field if all you care about is
> some per-CPU data?
>
This was the only way I could find to share data between the host and
have it available to EL2 on the guest switch. I saw it was also done
that way for PMUs in kvm_pmu_update_vcpu_events(). Maybe it could be
done by exposing the guest_trfcr global to both sides with some
annotation? But I assumed that wasn't idomatic as it wasn't done for PMUs.
>> +}
>> diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
>> index 89c208112eb7..55bc01e9808f 100644
>> --- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c
>> +++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
>> @@ -86,7 +86,7 @@ void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
>> __debug_save_spe(&vcpu->arch.host_debug_state.pmscr_el1);
>> /* Disable and flush Self-Hosted Trace generation */
>> if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
>> - __debug_save_trace(&vcpu->arch.host_debug_state.trfcr_el1);
>> + __debug_save_trace(&vcpu->arch.host_debug_state.host_trfcr_el1);
>> }
> `<>
>> void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
>> @@ -99,7 +99,7 @@ void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
>> if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
>> __debug_restore_spe(vcpu->arch.host_debug_state.pmscr_el1);
>> if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
>> - __debug_restore_trace(vcpu->arch.host_debug_state.trfcr_el1);
>> + __debug_restore_trace(vcpu->arch.host_debug_state.host_trfcr_el1);
>> }
>>
>> void __debug_switch_to_host(struct kvm_vcpu *vcpu)
>> diff --git a/arch/arm64/tools/sysreg b/arch/arm64/tools/sysreg
>> index 992722c0c23b..295a4a625b8b 100644
>> --- a/arch/arm64/tools/sysreg
>> +++ b/arch/arm64/tools/sysreg
>> @@ -2661,3 +2661,7 @@ EndSysreg
>> Sysreg TRFCR_EL2 3 4 1 2 1
>> Fields TRFCR_EL2
>> EndSysreg
>> +
>> +Sysreg TRFCR_EL12 3 5 1 2 1
>> +Fields TRFCR_ELx
>> +EndSysreg
>
> Please move this to the first patch.
>
> M.
>
Will do.
James
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v1 4/5] arm64: KVM: Write TRFCR value on guest switch with nVHE
2023-09-28 15:16 [PATCH v1 0/5] kvm/coresight: Support exclude guest and exclude host James Clark
` (2 preceding siblings ...)
2023-09-28 15:16 ` [PATCH v1 3/5] arm64: KVM: Add interface to set guest value for TRFCR register James Clark
@ 2023-09-28 15:16 ` James Clark
2023-09-28 15:16 ` [PATCH v1 5/5] coresight: Pass guest TRFCR value to KVM James Clark
4 siblings, 0 replies; 8+ messages in thread
From: James Clark @ 2023-09-28 15:16 UTC (permalink / raw)
To: coresight, linux-arm-kernel, kvmarm, broonie, maz, suzuki.poulose
Cc: James Clark, Oliver Upton, James Morse, Zenghui Yu,
Catalin Marinas, Will Deacon, Mike Leach, Leo Yan,
Alexander Shishkin, Anshuman Khandual, Rob Herring, Jintack Lim,
Joey Gouly, linux-kernel
The guest value for TRFCR requested by the Coresight driver is saved
in guest_trfcr_el1. On guest switch this value needs to be written to
the register. Currently TRFCR is only modified when we want to disable
trace completely in guests due to an issue with TRBE. Expand the
__debug_save_trace() function to always write to the register if a
different value for guests is required, but also keep the existing TRBE
disable behavior if that's required.
The TRFCR restore function remains functionally the same, except a value
of 0 doesn't mean "don't restore" anymore. Now that we save both guest
and host values the register is restored any time the guest and host
values differ.
Signed-off-by: James Clark <james.clark@arm.com>
---
arch/arm64/kvm/debug.c | 13 ++++++-
arch/arm64/kvm/hyp/nvhe/debug-sr.c | 56 ++++++++++++++++++------------
2 files changed, 46 insertions(+), 23 deletions(-)
diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
index 379d2677961f..e39f2d535b25 100644
--- a/arch/arm64/kvm/debug.c
+++ b/arch/arm64/kvm/debug.c
@@ -332,10 +332,21 @@ void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu)
!(read_sysreg_s(SYS_PMBIDR_EL1) & BIT(PMBIDR_EL1_P_SHIFT)))
vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_SPE);
- /* Check if we have TRBE implemented and available at the host */
+ /*
+ * Check if we have TRBE implemented and available at the host. If it's
+ * in use at the time of guest switch it will need to be disabled and
+ * then restored.
+ */
if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffer_SHIFT) &&
!(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_EL1_P))
vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
+ /*
+ * Also save TRFCR on nVHE if FEAT_TRF (TraceFilt) exists. This will be
+ * done in cases where use of TRBE doesn't completely disable trace and
+ * handles the exclude_host/exclude_guest rules of the trace session.
+ */
+ if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_SHIFT))
+ vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
}
void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
index 55bc01e9808f..7ceb16be14cd 100644
--- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c
+++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c
@@ -51,32 +51,44 @@ static void __debug_restore_spe(u64 pmscr_el1)
write_sysreg_s(pmscr_el1, SYS_PMSCR_EL1);
}
-static void __debug_save_trace(u64 *trfcr_el1)
+/*
+ * Save TRFCR and disable trace completely if TRBE is being used, otherwise
+ * apply required guest TRFCR value.
+ */
+static void __debug_save_trace(struct kvm_vcpu *vcpu)
{
- *trfcr_el1 = 0;
+ vcpu->arch.host_debug_state.host_trfcr_el1 = read_sysreg_s(SYS_TRFCR_EL1);
/* Check if the TRBE is enabled */
- if (!(read_sysreg_s(SYS_TRBLIMITR_EL1) & TRBLIMITR_EL1_E))
- return;
- /*
- * Prohibit trace generation while we are in guest.
- * Since access to TRFCR_EL1 is trapped, the guest can't
- * modify the filtering set by the host.
- */
- *trfcr_el1 = read_sysreg_s(SYS_TRFCR_EL1);
- write_sysreg_s(0, SYS_TRFCR_EL1);
- isb();
- /* Drain the trace buffer to memory */
- tsb_csync();
+ if (read_sysreg_s(SYS_TRBLIMITR_EL1) & TRBLIMITR_EL1_E) {
+ /*
+ * Prohibit trace generation while we are in guest. Since access
+ * to TRFCR_EL1 is trapped, the guest can't modify the filtering
+ * set by the host.
+ */
+ vcpu->arch.host_debug_state.guest_trfcr_el1 = 0;
+ write_sysreg_s(0, SYS_TRFCR_EL1);
+ isb();
+ /* Drain the trace buffer to memory */
+ tsb_csync();
+ } else {
+ /*
+ * Not using TRBE, so guest trace works. Apply the guest filters
+ * provided by the Coresight driver, if different.
+ */
+ if (vcpu->arch.host_debug_state.guest_trfcr_el1 !=
+ vcpu->arch.host_debug_state.host_trfcr_el1)
+ write_sysreg_s(vcpu->arch.host_debug_state.guest_trfcr_el1,
+ SYS_TRFCR_EL1);
+ }
}
-static void __debug_restore_trace(u64 trfcr_el1)
+static void __debug_restore_trace(struct kvm_vcpu *vcpu)
{
- if (!trfcr_el1)
- return;
-
/* Restore trace filter controls */
- write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1);
+ if (vcpu->arch.host_debug_state.host_trfcr_el1 !=
+ vcpu->arch.host_debug_state.guest_trfcr_el1)
+ write_sysreg_s(vcpu->arch.host_debug_state.host_trfcr_el1, SYS_TRFCR_EL1);
}
void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
@@ -84,9 +96,9 @@ void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
/* Disable and flush SPE data generation */
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
__debug_save_spe(&vcpu->arch.host_debug_state.pmscr_el1);
- /* Disable and flush Self-Hosted Trace generation */
+
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
- __debug_save_trace(&vcpu->arch.host_debug_state.host_trfcr_el1);
+ __debug_save_trace(vcpu);
}
void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
@@ -99,7 +111,7 @@ void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_SPE))
__debug_restore_spe(vcpu->arch.host_debug_state.pmscr_el1);
if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
- __debug_restore_trace(vcpu->arch.host_debug_state.host_trfcr_el1);
+ __debug_restore_trace(vcpu);
}
void __debug_switch_to_host(struct kvm_vcpu *vcpu)
--
2.34.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH v1 5/5] coresight: Pass guest TRFCR value to KVM
2023-09-28 15:16 [PATCH v1 0/5] kvm/coresight: Support exclude guest and exclude host James Clark
` (3 preceding siblings ...)
2023-09-28 15:16 ` [PATCH v1 4/5] arm64: KVM: Write TRFCR value on guest switch with nVHE James Clark
@ 2023-09-28 15:16 ` James Clark
4 siblings, 0 replies; 8+ messages in thread
From: James Clark @ 2023-09-28 15:16 UTC (permalink / raw)
To: coresight, linux-arm-kernel, kvmarm, broonie, maz, suzuki.poulose
Cc: James Clark, Oliver Upton, James Morse, Zenghui Yu,
Catalin Marinas, Will Deacon, Mike Leach, Leo Yan,
Alexander Shishkin, Anshuman Khandual, Rob Herring, Jintack Lim,
Joey Gouly, linux-kernel
Currently the userspace and kernel filters for guests are never set, so
no trace will be generated for them. Add support for tracing guests by
passing the desired TRFCR value to KVM so it can be applied to the
guest.
By writing either E1TRE or E0TRE, filtering on either guest kernel or
guest userspace is also supported. And if both E1TRE and E0TRE are
cleared when exclude_guest is set, that option is supported too. This
change also brings exclude_host support which is difficult to add as a
separate commit without excess churn and resulting in no trace at all.
Testing
=======
The addresses were counted with the following:
$ perf report -D | grep -Eo 'EL2|EL1|EL0' | sort | uniq -c
Guest kernel only:
$ perf record -e cs_etm//Gk -a -- true
535 EL1
1 EL2
Guest user only (only 5 addresses because the guest runs slowly in the
model):
$ perf record -e cs_etm//Gu -a -- true
5 EL0
Host kernel only:
$ perf record -e cs_etm//Hk -a -- true
3501 EL2
Host userspace only:
$ perf record -e cs_etm//Hu -a -- true
408 EL0
1 EL2
Signed-off-by: James Clark <james.clark@arm.com>
---
.../coresight/coresight-etm4x-core.c | 42 ++++++++++++++++---
drivers/hwtracing/coresight/coresight-etm4x.h | 2 +-
drivers/hwtracing/coresight/coresight-priv.h | 3 ++
3 files changed, 40 insertions(+), 7 deletions(-)
diff --git a/drivers/hwtracing/coresight/coresight-etm4x-core.c b/drivers/hwtracing/coresight/coresight-etm4x-core.c
index 77b0271ce6eb..292f9da6aeaf 100644
--- a/drivers/hwtracing/coresight/coresight-etm4x-core.c
+++ b/drivers/hwtracing/coresight/coresight-etm4x-core.c
@@ -6,6 +6,7 @@
#include <linux/acpi.h>
#include <linux/bitops.h>
#include <linux/kernel.h>
+#include <linux/kvm_host.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/types.h>
@@ -271,9 +272,22 @@ static void etm4x_prohibit_trace(struct etmv4_drvdata *drvdata)
/* If the CPU doesn't support FEAT_TRF, nothing to do */
if (!drvdata->trfcr)
return;
+ kvm_etm_set_guest_trfcr(0);
cpu_prohibit_trace();
}
+static u64 etm4x_get_kern_user_filter(struct etmv4_drvdata *drvdata)
+{
+ u64 trfcr = drvdata->trfcr;
+
+ if (drvdata->config.mode & ETM_MODE_EXCL_KERN)
+ trfcr &= ~TRFCR_ELx_ExTRE;
+ if (drvdata->config.mode & ETM_MODE_EXCL_USER)
+ trfcr &= ~TRFCR_ELx_E0TRE;
+
+ return trfcr;
+}
+
/*
* etm4x_allow_trace - Allow CPU tracing in the respective ELs,
* as configured by the drvdata->config.mode for the current
@@ -286,18 +300,28 @@ static void etm4x_prohibit_trace(struct etmv4_drvdata *drvdata)
*/
static void etm4x_allow_trace(struct etmv4_drvdata *drvdata)
{
- u64 trfcr = drvdata->trfcr;
+ u64 trfcr;
/* If the CPU doesn't support FEAT_TRF, nothing to do */
- if (!trfcr)
+ if (!drvdata->trfcr)
return;
- if (drvdata->config.mode & ETM_MODE_EXCL_KERN)
- trfcr &= ~TRFCR_ELx_ExTRE;
- if (drvdata->config.mode & ETM_MODE_EXCL_USER)
- trfcr &= ~TRFCR_ELx_E0TRE;
+ if (drvdata->config.mode & ETM_MODE_EXCL_HOST)
+ trfcr = drvdata->trfcr & ~(TRFCR_ELx_ExTRE | TRFCR_ELx_E0TRE);
+ else
+ trfcr = etm4x_get_kern_user_filter(drvdata);
write_trfcr(trfcr);
+
+ /* Set filters for guests and pass to KVM */
+ if (drvdata->config.mode & ETM_MODE_EXCL_GUEST)
+ trfcr = drvdata->trfcr & ~(TRFCR_ELx_ExTRE | TRFCR_ELx_E0TRE);
+ else
+ trfcr = etm4x_get_kern_user_filter(drvdata);
+
+ /* TRFCR_EL1 doesn't have CX so mask it out. */
+ trfcr &= ~TRFCR_EL2_CX;
+ kvm_etm_set_guest_trfcr(trfcr);
}
#ifdef CONFIG_ETM4X_IMPDEF_FEATURE
@@ -655,6 +679,12 @@ static int etm4_parse_event_config(struct coresight_device *csdev,
if (attr->exclude_user)
config->mode = ETM_MODE_EXCL_USER;
+ if (attr->exclude_host)
+ config->mode |= ETM_MODE_EXCL_HOST;
+
+ if (attr->exclude_guest)
+ config->mode |= ETM_MODE_EXCL_GUEST;
+
/* Always start from the default config */
etm4_set_default_config(config);
diff --git a/drivers/hwtracing/coresight/coresight-etm4x.h b/drivers/hwtracing/coresight/coresight-etm4x.h
index 20e2e4cb7614..3f170599822f 100644
--- a/drivers/hwtracing/coresight/coresight-etm4x.h
+++ b/drivers/hwtracing/coresight/coresight-etm4x.h
@@ -841,7 +841,7 @@ enum etm_impdef_type {
* @s_ex_level: Secure ELs where tracing is supported.
*/
struct etmv4_config {
- u32 mode;
+ u64 mode;
u32 pe_sel;
u32 cfg;
u32 eventctrl0;
diff --git a/drivers/hwtracing/coresight/coresight-priv.h b/drivers/hwtracing/coresight/coresight-priv.h
index 767076e07970..727dd27ba800 100644
--- a/drivers/hwtracing/coresight/coresight-priv.h
+++ b/drivers/hwtracing/coresight/coresight-priv.h
@@ -39,6 +39,9 @@
#define ETM_MODE_EXCL_KERN BIT(30)
#define ETM_MODE_EXCL_USER BIT(31)
+#define ETM_MODE_EXCL_HOST BIT(32)
+#define ETM_MODE_EXCL_GUEST BIT(33)
+
struct cs_pair_attribute {
struct device_attribute attr;
u32 lo_off;
--
2.34.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread