Skip to contentSkip to conversation

kvm/coresight: Support exclude guest and exclude host

PATCH Superseded James Clark 3 participants 20 messages Oct 19, 2023 – Dec 5, 2023

Subsystems (4)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover kvm/coresight: Support exclude guest and exclude host

This is a combination of the RFC for nVHE here [1] and v3 of VHE version here [2]. After a few of the review comments it seemed much simpler for both versions to use the same interface and be in the same patchset.

FEAT_TRF is a Coresight feature that allows trace capture to be completely filtered at different exception levels, unlike the existing TRCVICTLR controls which may still emit target addresses of branches, even if the following trace is filtered.

Without FEAT_TRF, it was possible to start a trace session on a host and also collect trace from the guest as TRCVICTLR was never programmed to exclude guests (and it could still emit target addresses even if it was).

With FEAT_TRF, the current behavior of trace in guests exists depends on whether nVHE or VHE are being used. Both of the examples below are from the host's point of view, as Coresight isn't accessible from guests. This patchset is only relevant to when FEAT_TRF exists, otherwise there is no change.

nVHE:

Because the host and the guest are both using TRFCR_EL1, trace will be generated in guests depending on the same filter rules the host is using. For example if the host is tracing userspace only, then guest userspace trace will also be collected.

(This is further limited by whether TRBE is used because an issue with TRBE means that it's completely disabled in nVHE guests, but it's possible to have other tracing components.)

VHE:

With VHE, the host filters will be in TRFCR_EL2, but the filters in TRFCR_EL1 will be active when the guest is running. Because we don't write to TRFCR_EL1, guest trace will be completely disabled.

With this change, the guest filtering rules from the Perf session are honored for both nVHE and VHE modes. This is done by either writing to TRFCR_EL12 at the start of the Perf session and doing nothing else further, or caching the guest value and writing it at guest switch for nVHE.

The first commit moves the register to sysreg because I add the EL12 version.

---
Changes since V2:

* Add a new iflag to signify presence of FEAT_TRF and keep the existing TRBE iflag. This fixes the issue where TRBLIMITR_EL1 was being accessed even if TRBE didn't exist
* Reword a commit message

Changes since V1:

* Squashed all the arm64/tools/sysreg changes into the first commit
* Add a new commit to move SPE and TRBE regs into the kvm sysreg array
* Add a comment above the TRFCR global that it's per host CPU rather than vcpu

Changes since nVHE RFC [1]:

* Re-write just in terms of the register value to be written for the host and the guest. This removes some logic from the hyp code and a value of kvm_vcpu_arch:trfcr_el1 = 0 no longer means "don't restore".
* Remove all the conditional compilation and new files.
* Change the kvm_etm_update_vcpu_events macro to a function.
* Re-use DEBUG_STATE_SAVE_TRFCR so iflags don't need to be expanded anymore.
* Expand the cover letter.

Changes since VHE v3 [2]:

* Use the same interface as nVHE mode so TRFCR_EL12 is now written by kvm.

[1]: https://lore.kernel.org/kvmarm/[email protected]/
[2]: https://lore.kernel.org/kvmarm/[email protected]/

James Clark (6):
arm64/sysreg: Move TRFCR definitions to sysreg
arm64: KVM: Move SPE and trace registers to the sysreg array
arm64: KVM: Add iflag for FEAT_TRF
arm64: KVM: Add interface to set guest value for TRFCR register
arm64: KVM: Write TRFCR value on guest switch with nVHE
coresight: Pass guest TRFCR value to KVM

 arch/arm64/include/asm/kvm_host.h             | 13 +--
 arch/arm64/include/asm/kvm_hyp.h              |  6 +-
 arch/arm64/include/asm/sysreg.h               | 12 ---
 arch/arm64/kvm/arm.c                          |  1 +
 arch/arm64/kvm/debug.c                        | 48 +++++++++-
 arch/arm64/kvm/hyp/nvhe/debug-sr.c            | 88 +++++++++++--------
 arch/arm64/kvm/hyp/nvhe/switch.c              |  4 +-
 arch/arm64/tools/sysreg                       | 41 +++++++++
 .../coresight/coresight-etm4x-core.c          | 42 +++++++--
 drivers/hwtracing/coresight/coresight-etm4x.h |  2 +-
 drivers/hwtracing/coresight/coresight-priv.h  |  3 +
 11 files changed, 192 insertions(+), 68 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/6 arm64/sysreg: Move TRFCR definitions to sysreg

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.

Also add TRFCR_EL12 which will start to be used in a later commit.

---
 arch/arm64/include/asm/sysreg.h | 12 ----------
 arch/arm64/tools/sysreg         | 41 +++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 12 deletions(-)
arch/arm64/include/asm/sysreg.h +0 -12
index 38296579a4fd..068dd6abe273 100644
@@ -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)
arch/arm64/tools/sysreg +41 -0

44 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 2/6 arm64: KVM: Move SPE and trace registers to the sysreg array

In reply to James Clark

pmscr_el1 and trfcr_el1 are currently special cased in the host_debug_state struct, but they're just registers after all so give them entries in the sysreg array and refer to them through the host context.

---
 arch/arm64/include/asm/kvm_host.h  |  6 ++--
 arch/arm64/include/asm/kvm_hyp.h   |  4 +--
 arch/arm64/kvm/hyp/nvhe/debug-sr.c | 44 +++++++++++++++---------------
 arch/arm64/kvm/hyp/nvhe/switch.c   |  4 +--
 4 files changed, 28 insertions(+), 30 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -4
index 4a966c0d7373..7c82927ddaf2 100644
@@ -437,6 +437,8 @@ enum vcpu_sysreg {
 	CNTHP_CVAL_EL2,
 	CNTHV_CTL_EL2,
 	CNTHV_CVAL_EL2,
+	PMSCR_EL1,	/* Statistical profiling extension */
+	TRFCR_EL1,	/* Self-hosted trace filters */
 
 	NR_SYS_REGS	/* Nothing after this line! */
 };
@@ -570,10 +572,6 @@ struct kvm_vcpu_arch {
 	struct {
 		/* {Break,watch}point registers */
 		struct kvm_guest_debug_arch regs;
-		/* Statistical profiling extension */
-		u64 pmscr_el1;
-		/* Self-hosted trace */
-		u64 trfcr_el1;
 	} host_debug_state;
 
 	/* VGIC state */
arch/arm64/include/asm/kvm_hyp.h +2 -2
index 66efd67ea7e8..52ac90d419e7 100644
@@ -103,8 +103,8 @@ void __debug_switch_to_guest(struct kvm_vcpu *vcpu);
 void __debug_switch_to_host(struct kvm_vcpu *vcpu);
 
 #ifdef __KVM_NVHE_HYPERVISOR__
-void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu);
-void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu);
+void __debug_save_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt);
+void __debug_restore_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt);
 #endif
 
 void __fpsimd_save_state(struct user_fpsimd_state *fp_regs);
arch/arm64/kvm/hyp/nvhe/debug-sr.c +22 -22

104 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/switch.c +2 -2
index c50f8459e4fc..6b4b24ae077f 100644
@@ -278,7 +278,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 	 * translation regime to EL2 (via MDCR_EL2_E2PB == 0) and
 	 * before we load guest Stage1.
 	 */
-	__debug_save_host_buffers_nvhe(vcpu);
+	__debug_save_host_buffers_nvhe(host_ctxt);
 
 	/*
 	 * We're about to restore some new MMU state. Make sure
@@ -345,7 +345,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 	 * This must come after restoring the host sysregs, since a non-VHE
 	 * system may enable SPE here and make use of the TTBRs.
 	 */
-	__debug_restore_host_buffers_nvhe(vcpu);
+	__debug_restore_host_buffers_nvhe(host_ctxt);
 
 	if (pmu_switch_needed)
 		__pmu_switch_to_host(vcpu);
Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH v3 2/6] arm64: KVM: Move SPE and trace registers to the sysreg array

Quoted 20 lines · James Clark

pmscr_el1 and trfcr_el1 are currently special cased in the host_debug_state struct, but they're just registers after all so give them entries in the sysreg array and refer to them through the host context.

---
 arch/arm64/include/asm/kvm_host.h  |  6 ++--
 arch/arm64/include/asm/kvm_hyp.h   |  4 +--
 arch/arm64/kvm/hyp/nvhe/debug-sr.c | 44 +++++++++++++++---------------
 arch/arm64/kvm/hyp/nvhe/switch.c   |  4 +--
 4 files changed, 28 insertions(+), 30 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 4a966c0d7373..7c82927ddaf2 100644

arch/arm64/include/asm/kvm_host.h enum vcpu_sysreg { · line 441

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -437,6 +437,8 @@ enum vcpu_sysreg {
 	CNTHP_CVAL_EL2,
 	CNTHV_CTL_EL2,
 	CNTHV_CVAL_EL2,
+	PMSCR_EL1,	/* Statistical profiling extension */
+	TRFCR_EL1,	/* Self-hosted trace filters */

Why this move? Are you also adding guest support for SPE?

Until you do, I don't see the need to pollute the guest's sysreg namespace.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#4 lore raw

Re: [PATCH v3 2/6] arm64: KVM: Move SPE and trace registers to the sysreg array

Quoted 30 lines · Marc Zyngier
Quoted 20 lines · James Clark

pmscr_el1 and trfcr_el1 are currently special cased in the host_debug_state struct, but they're just registers after all so give them entries in the sysreg array and refer to them through the host context.

---
 arch/arm64/include/asm/kvm_host.h  |  6 ++--
 arch/arm64/include/asm/kvm_hyp.h   |  4 +--
 arch/arm64/kvm/hyp/nvhe/debug-sr.c | 44 +++++++++++++++---------------
 arch/arm64/kvm/hyp/nvhe/switch.c   |  4 +--
 4 files changed, 28 insertions(+), 30 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 4a966c0d7373..7c82927ddaf2 100644

arch/arm64/include/asm/kvm_host.h enum vcpu_sysreg { · line 441

Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -437,6 +437,8 @@ enum vcpu_sysreg {
 	CNTHP_CVAL_EL2,
 	CNTHV_CTL_EL2,
 	CNTHV_CVAL_EL2,
+	PMSCR_EL1,	/* Statistical profiling extension */
+	TRFCR_EL1,	/* Self-hosted trace filters */

Why this move? Are you also adding guest support for SPE?

Quoted 5 lines

Until you do, I don't see the need to pollute the guest's sysreg namespace.

	M.

Ah ok yes, I think I misunderstood your previous review comment. You're right I don't touch SPE and it's only ever for the host so I can leave it where it was in struct host_debug_state.

James

#5 lore raw

PATCH v3 3/6 arm64: KVM: Add iflag for FEAT_TRF

In reply to James Clark

Add an extra iflag to signify if the TRFCR register is accessible. Because TRBE requires FEAT_TRF, DEBUG_STATE_SAVE_TRBE still has the same behavior even though it's only set when FEAT_TRF is present.

The following holes are left in struct kvm_vcpu_arch, but there aren't enough other 8 bit fields to rearrange it to leave any hole smaller than 7 bytes:

u8 cflags; /* 2292 1 */
/* XXX 1 byte hole, try to pack */

  u16                        iflags;               /*  2294     2 */
  u8                         sflags;               /*  2296     1 */
  bool                       pause;                /*  2297     1 */

/* XXX 6 bytes hole, try to pack */

---
 arch/arm64/include/asm/kvm_host.h |  4 +++-
 arch/arm64/kvm/debug.c            | 22 ++++++++++++++++++----
 2 files changed, 21 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -1
index 7c82927ddaf2..0f0bf8e641bd 100644
@@ -535,7 +535,7 @@ struct kvm_vcpu_arch {
 	u8 cflags;
 
 	/* Input flags to the hypervisor code, potentially cleared after use */
-	u8 iflags;
+	u16 iflags;
 
 	/* State flags for kernel bookkeeping, unused by the hypervisor code */
 	u8 sflags;
@@ -741,6 +741,8 @@ struct kvm_vcpu_arch {
 #define DEBUG_STATE_SAVE_TRBE	__vcpu_single_flag(iflags, BIT(6))
 /* vcpu running in HYP context */
 #define VCPU_HYP_CONTEXT	__vcpu_single_flag(iflags, BIT(7))
+/* Save trace filter controls */
+#define DEBUG_STATE_SAVE_TRFCR	__vcpu_single_flag(iflags, BIT(8))
 
 /* SVE enabled for host EL0 */
 #define HOST_SVE_ENABLED	__vcpu_single_flag(sflags, BIT(0))
arch/arm64/kvm/debug.c +18 -4
index 8725291cb00a..20cdd40b3c42 100644
@@ -331,14 +331,28 @@ 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 */
-	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);
+	/*
+	 * 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);
+		/*
+		 * 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. The architecture mandates FEAT_TRF with TRBE, so we
+		 * only need to check for TRBE after TRF.
+		 */
+		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);
+	}
 }
 
 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);
 }
Signature · 1 line
2.34.1
maintainer Reviewed-by on v3 3/6
#6 lore raw

Re: [PATCH v3 3/6] arm64: KVM: Add iflag for FEAT_TRF

Quoted 8 lines · James Clark

Add an extra iflag to signify if the TRFCR register is accessible. Because TRBE requires FEAT_TRF, DEBUG_STATE_SAVE_TRBE still has the same behavior even though it's only set when FEAT_TRF is present.

The following holes are left in struct kvm_vcpu_arch, but there aren't enough other 8 bit fields to rearrange it to leave any hole smaller than 7 bytes:

Quoted 8 lines

u8 cflags; /* 2292 1 */
/* XXX 1 byte hole, try to pack */

   u16                        iflags;               /*  2294     2 */
   u8                         sflags;               /*  2296     1 */
   bool                       pause;                /*  2297     1 */

/* XXX 6 bytes hole, try to pack */

Quoted 64 lines
---
  arch/arm64/include/asm/kvm_host.h |  4 +++-
  arch/arm64/kvm/debug.c            | 22 ++++++++++++++++++----
  2 files changed, 21 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -1
index 7c82927ddaf2..0f0bf8e641bd 100644
@@ -535,7 +535,7 @@ struct kvm_vcpu_arch {
  	u8 cflags;
 
  	/* Input flags to the hypervisor code, potentially cleared after use */
-	u8 iflags;
+	u16 iflags;
 
  	/* State flags for kernel bookkeeping, unused by the hypervisor code */
  	u8 sflags;
@@ -741,6 +741,8 @@ struct kvm_vcpu_arch {
  #define DEBUG_STATE_SAVE_TRBE	__vcpu_single_flag(iflags, BIT(6))
  /* vcpu running in HYP context */
  #define VCPU_HYP_CONTEXT	__vcpu_single_flag(iflags, BIT(7))
+/* Save trace filter controls */
+#define DEBUG_STATE_SAVE_TRFCR	__vcpu_single_flag(iflags, BIT(8))
 
  /* SVE enabled for host EL0 */
  #define HOST_SVE_ENABLED	__vcpu_single_flag(sflags, BIT(0))
arch/arm64/kvm/debug.c +18 -4
index 8725291cb00a..20cdd40b3c42 100644
@@ -331,14 +331,28 @@ 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 */
-	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);
+	/*
+	 * 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);
+		/*
+		 * 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. The architecture mandates FEAT_TRF with TRBE, so we
+		 * only need to check for TRBE after TRF.
+		 */
+		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);
+	}
  }
 
  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);
  }
maintainer
#7 lore raw

Re: [PATCH v3 3/6] arm64: KVM: Add iflag for FEAT_TRF

In reply to James Clark
Quoted 50 lines · James Clark

Add an extra iflag to signify if the TRFCR register is accessible. Because TRBE requires FEAT_TRF, DEBUG_STATE_SAVE_TRBE still has the same behavior even though it's only set when FEAT_TRF is present.

The following holes are left in struct kvm_vcpu_arch, but there aren't enough other 8 bit fields to rearrange it to leave any hole smaller than 7 bytes:

u8 cflags; /* 2292 1 */
/* XXX 1 byte hole, try to pack */

  u16                        iflags;               /*  2294     2 */
  u8                         sflags;               /*  2296     1 */
  bool                       pause;                /*  2297     1 */

/* XXX 6 bytes hole, try to pack */

---
 arch/arm64/include/asm/kvm_host.h |  4 +++-
 arch/arm64/kvm/debug.c            | 22 ++++++++++++++++++----
 2 files changed, 21 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -1
index 7c82927ddaf2..0f0bf8e641bd 100644
@@ -535,7 +535,7 @@ struct kvm_vcpu_arch {
 	u8 cflags;
 
 	/* Input flags to the hypervisor code, potentially cleared after use */
-	u8 iflags;
+	u16 iflags;
 
 	/* State flags for kernel bookkeeping, unused by the hypervisor code */
 	u8 sflags;
@@ -741,6 +741,8 @@ struct kvm_vcpu_arch {
 #define DEBUG_STATE_SAVE_TRBE	__vcpu_single_flag(iflags, BIT(6))
 /* vcpu running in HYP context */
 #define VCPU_HYP_CONTEXT	__vcpu_single_flag(iflags, BIT(7))
+/* Save trace filter controls */
+#define DEBUG_STATE_SAVE_TRFCR	__vcpu_single_flag(iflags, BIT(8))
 
 /* SVE enabled for host EL0 */
 #define HOST_SVE_ENABLED	__vcpu_single_flag(sflags, BIT(0))
arch/arm64/kvm/debug.c +0 -0
index 8725291cb00a..20cdd40b3c42 100644
@@ -331,14 +331,28 @@ void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu)
 	    !(read_sysreg_s(SYS_PMBIDR_EL1) & BIT(PMBIDR_EL1_P_SHIFT)))

arch/arm64/kvm/debug.c void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu) · line 337

Quoted 10 lines
arch/arm64/kvm/debug.c +4 -4
 		vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_SPE);
 
-	/* 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);
+	/*
+	 * 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.

This comment provides zero information. What will be done? Under which conditions? What are the rules?

arch/arm64/kvm/debug.c void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu) · line 350

Quoted 13 lines
arch/arm64/kvm/debug.c +13 -0
+	 */
+	if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_SHIFT)) {
+		vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
+		/*
+		 * 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. The architecture mandates FEAT_TRF with TRBE, so we
+		 * only need to check for TRBE after TRF.
+		 */
+		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);
+	}

Multiple questions:

- Why is it safe to trust the local CPU's capability rather than the consolidated view from the cpufeature infrastructure?

- Why defer the saving of the registers if there are no changes made to them in the interval?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#8 lore raw

Re: [PATCH v3 3/6] arm64: KVM: Add iflag for FEAT_TRF

Quoted 82 lines · Marc Zyngier
Quoted 50 lines · James Clark

Add an extra iflag to signify if the TRFCR register is accessible. Because TRBE requires FEAT_TRF, DEBUG_STATE_SAVE_TRBE still has the same behavior even though it's only set when FEAT_TRF is present.

The following holes are left in struct kvm_vcpu_arch, but there aren't enough other 8 bit fields to rearrange it to leave any hole smaller than 7 bytes:

u8 cflags; /* 2292 1 */
/* XXX 1 byte hole, try to pack */

   u16                        iflags;               /*  2294     2 */
   u8                         sflags;               /*  2296     1 */
   bool                       pause;                /*  2297     1 */

/* XXX 6 bytes hole, try to pack */

---
  arch/arm64/include/asm/kvm_host.h |  4 +++-
  arch/arm64/kvm/debug.c            | 22 ++++++++++++++++++----
  2 files changed, 21 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -1
index 7c82927ddaf2..0f0bf8e641bd 100644
@@ -535,7 +535,7 @@ struct kvm_vcpu_arch {
  	u8 cflags;
 
  	/* Input flags to the hypervisor code, potentially cleared after use */
-	u8 iflags;
+	u16 iflags;
 
  	/* State flags for kernel bookkeeping, unused by the hypervisor code */
  	u8 sflags;
@@ -741,6 +741,8 @@ struct kvm_vcpu_arch {
  #define DEBUG_STATE_SAVE_TRBE	__vcpu_single_flag(iflags, BIT(6))
  /* vcpu running in HYP context */
  #define VCPU_HYP_CONTEXT	__vcpu_single_flag(iflags, BIT(7))
+/* Save trace filter controls */
+#define DEBUG_STATE_SAVE_TRFCR	__vcpu_single_flag(iflags, BIT(8))
 
  /* SVE enabled for host EL0 */
  #define HOST_SVE_ENABLED	__vcpu_single_flag(sflags, BIT(0))
arch/arm64/kvm/debug.c +0 -0
index 8725291cb00a..20cdd40b3c42 100644
@@ -331,14 +331,28 @@ void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu)
  	    !(read_sysreg_s(SYS_PMBIDR_EL1) & BIT(PMBIDR_EL1_P_SHIFT)))

arch/arm64/kvm/debug.c void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu) · line 337

Quoted 10 lines
arch/arm64/kvm/debug.c +4 -4
  		vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_SPE);
 
-	/* 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);
+	/*
+	 * 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.

This comment provides zero information. What will be done? Under which conditions? What are the rules?

arch/arm64/kvm/debug.c void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu) · line 350

Quoted 13 lines
arch/arm64/kvm/debug.c +13 -0
+	 */
+	if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_SHIFT)) {
+		vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
+		/*
+		 * 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. The architecture mandates FEAT_TRF with TRBE, so we
+		 * only need to check for TRBE after TRF.
+		 */
+		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);
+	}

Multiple questions:

Quoted 2 lines

- Why is it safe to trust the local CPU's capability rather than the consolidated view from the cpufeature infrastructure?

The coresight driver is capable of handling heterogeneous systems. i.e., some CPUs may not have FEAT_TRF or FEAT_TRBE. This could be for various reasons (e.g., CPU Erratum disabling TRBE, though not used now). It already needs to treat each CPU differently (due to the capabilities of the ETM).

That said, we could reject events with exclude_guest/exclude_host flags on CPUs that do not support FEAT_TRF. But that doesn't solve checking the local capability.

Suzuki

Quoted 8 lines

- Why defer the saving of the registers if there are no changes made to them in the interval?

Thanks,

	M.
#9 lore raw

PATCH v3 4/6 arm64: KVM: Add interface to set guest value for TRFCR register

In reply to James Clark

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, the host trace is controlled by TRFCR_EL2 and thus we can write to the TRFCR_EL1 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.

---
 arch/arm64/include/asm/kvm_host.h |  3 +++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/debug.c            | 26 ++++++++++++++++++++++++++
 3 files changed, 30 insertions(+)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f0bf8e641bd..e1852102550d 100644
@@ -1125,6 +1125,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) {}
@@ -1132,6 +1134,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);
arch/arm64/kvm/arm.c +1 -0
index 0f717b6a9151..e4d846f2f665 100644
@@ -1015,6 +1015,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
arch/arm64/kvm/debug.c +26 -0
index 20cdd40b3c42..2ab41b954512 100644
@@ -23,6 +23,12 @@
 
 static DEFINE_PER_CPU(u64, mdcr_el2);
 
+/*
+ * Per CPU value for TRFCR that should be applied to any guest vcpu that may
+ * run on that core in the future.
+ */
+static DEFINE_PER_CPU(u64, guest_trfcr);
+
 /**
  * save/restore_guest_debug_regs
  *
@@ -356,3 +362,23 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
 	vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
 	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))
+		ctxt_sys_reg(&vcpu->arch.ctxt, TRFCR_EL1) = *this_cpu_ptr(&guest_trfcr);
+}
Signature · 1 line
2.34.1
maintainer Reviewed-by on v3 4/6
#10 lore raw

Re: [PATCH v3 4/6] arm64: KVM: Add interface to set guest value for TRFCR register

Quoted 8 lines · James Clark

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, the host trace is controlled by TRFCR_EL2 and thus we can write to the TRFCR_EL1 immediately. Because guest writes to the register are trapped, the value will persist and can't be modified.

This could also be added below to make it easier for the code reader.

Quoted 63 lines

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.

---
  arch/arm64/include/asm/kvm_host.h |  3 +++
  arch/arm64/kvm/arm.c              |  1 +
  arch/arm64/kvm/debug.c            | 26 ++++++++++++++++++++++++++
  3 files changed, 30 insertions(+)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f0bf8e641bd..e1852102550d 100644
@@ -1125,6 +1125,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) {}
@@ -1132,6 +1134,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);
arch/arm64/kvm/arm.c +1 -0
index 0f717b6a9151..e4d846f2f665 100644
@@ -1015,6 +1015,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
arch/arm64/kvm/debug.c +6 -0
index 20cdd40b3c42..2ab41b954512 100644
@@ -23,6 +23,12 @@
 
  static DEFINE_PER_CPU(u64, mdcr_el2);
 
+/*
+ * Per CPU value for TRFCR that should be applied to any guest vcpu that may
+ * run on that core in the future.
+ */
+static DEFINE_PER_CPU(u64, guest_trfcr);
+
  /**
   * save/restore_guest_debug_regs
   *

arch/arm64/kvm/debug.c void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) · line 365

Quoted 4 lines
arch/arm64/kvm/debug.c +1 -0
@@ -356,3 +362,23 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
  	vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
  	vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
  }
+

The comment in the description could be helpful here.

Quoted 9 lines
arch/arm64/kvm/debug.c +9 -0
+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);
+

arch/arm64/kvm/debug.c void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) · line 384

Quoted 10 lines
arch/arm64/kvm/debug.c +10 -0
+/*
+ * 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))
+		ctxt_sys_reg(&vcpu->arch.ctxt, TRFCR_EL1) = *this_cpu_ptr(&guest_trfcr);
+}

Either way,

maintainer
#11 lore raw

Re: [PATCH v3 4/6] arm64: KVM: Add interface to set guest value for TRFCR register

Quoted 80 lines · Suzuki K Poulose
Quoted 8 lines · James Clark

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, the host trace is controlled by TRFCR_EL2 and thus we can write to the TRFCR_EL1 immediately. Because guest writes to the register are trapped, the value will persist and can't be modified.

This could also be added below to make it easier for the code reader.

Quoted 63 lines

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.

---
  arch/arm64/include/asm/kvm_host.h |  3 +++ arch/arm64/kvm/arm.c              |  1 + arch/arm64/kvm/debug.c            | 26 ++++++++++++++++++++++++++

  3 files changed, 30 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
index 0f0bf8e641bd..e1852102550d 100644
@@ -1125,6 +1125,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) {}

arch/arm64/include/asm/kvm_host.h +1 -0
@@ -1132,6 +1134,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);
arch/arm64/kvm/arm.c +1 -0
index 0f717b6a9151..e4d846f2f665 100644
@@ -1015,6 +1015,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
arch/arm64/kvm/debug.c +2 -0
index 20cdd40b3c42..2ab41b954512 100644
@@ -23,6 +23,12 @@
   static DEFINE_PER_CPU(u64, mdcr_el2);
+/*
+ * Per CPU value for TRFCR that should be applied to any guest vcpu 

that may
+ * run on that core in the future.
+ */

arch/arm64/kvm/debug.c +2 -0
+static DEFINE_PER_CPU(u64, guest_trfcr);
+

  /**
   * save/restore_guest_debug_regs
   *

arch/arm64/kvm/debug.c +0 -0
@@ -356,3 +362,23 @@ void kvm_arch_vcpu_put_debug_state_flags(struct 
 kvm_vcpu *vcpu)
       vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
       vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
Quoted 2 lines

  }
+

The comment in the description could be helpful here.

Quoted 3 lines

arch/arm64/kvm/debug.c void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) · line 368

Quoted 3 lines
arch/arm64/kvm/debug.c +3 -0
+void kvm_etm_set_guest_trfcr(u64 trfcr_guest)
+{
+    if (has_vhe())

I am wondering if "kvm" should do the extra safety check of making sure that the FEAT_TRF is available on the CPU, before actually writing to the register ? coresight calls this only when the FEAT_TRF is implemented. But given this is an exported function, may be we should be extra careful ?

Suzuki

Quoted 21 lines
Quoted 15 lines

+        write_sysreg_s(trfcr_guest, SYS_TRFCR_EL12); +    else
+        *this_cpu_ptr(&guest_trfcr) = trfcr_guest;

arch/arm64/kvm/debug.c +12 -0
+}
+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))
+        ctxt_sys_reg(&vcpu->arch.ctxt, TRFCR_EL1) = 
Quoted 2 lines

*this_cpu_ptr(&guest_trfcr);
+}

Either way,

#12 lore raw

Re: [PATCH v3 4/6] arm64: KVM: Add interface to set guest value for TRFCR register

In reply to Suzuki K Poulose
Quoted 73 lines · Suzuki K Poulose
Quoted 8 lines · James Clark

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, the host trace is controlled by TRFCR_EL2 and thus we can write to the TRFCR_EL1 immediately. Because guest writes to the register are trapped, the value will persist and can't be modified.

This could also be added below to make it easier for the code reader.

Quoted 61 lines

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.

---
  arch/arm64/include/asm/kvm_host.h |  3 +++ arch/arm64/kvm/arm.c              |  1 + arch/arm64/kvm/debug.c            | 26 ++++++++++++++++++++++++++

  3 files changed, 30 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
index 0f0bf8e641bd..e1852102550d 100644
@@ -1125,6 +1125,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) {}

arch/arm64/include/asm/kvm_host.h +1 -0
@@ -1132,6 +1134,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);
arch/arm64/kvm/arm.c +1 -0
index 0f717b6a9151..e4d846f2f665 100644
@@ -1015,6 +1015,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
arch/arm64/kvm/debug.c +0 -0
index 20cdd40b3c42..2ab41b954512 100644
@@ -23,6 +23,12 @@
     static DEFINE_PER_CPU(u64, mdcr_el2);

  +/*
+ * Per CPU value for TRFCR that should be applied to any guest vcpu that may
+ * run on that core in the future.
+ */

arch/arm64/kvm/debug.c +2 -0
+static DEFINE_PER_CPU(u64, guest_trfcr);
+

  /**
   * save/restore_guest_debug_regs
   *

Quoted 9 lines
Quoted 6 lines
arch/arm64/kvm/debug.c +0 -0
@@ -356,3 +362,23 @@ void kvm_arch_vcpu_put_debug_state_flags(struct
 kvm_vcpu *vcpu)
       vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
       vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);

  }
+

The comment in the description could be helpful here.

Yep I agree, I'll add it.

Quoted 25 lines
Quoted 18 lines
arch/arm64/kvm/debug.c +18 -0
+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))
+        ctxt_sys_reg(&vcpu->arch.ctxt, TRFCR_EL1) =
Quoted 2 lines

*this_cpu_ptr(&guest_trfcr);
+}

Either way,

maintainer
#13 lore raw

Re: [PATCH v3 4/6] arm64: KVM: Add interface to set guest value for TRFCR register

In reply to James Clark
Quoted 10 lines · James Clark

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, the host trace is controlled by TRFCR_EL2 and thus we can write to the TRFCR_EL1 immediately. Because guest writes to the register are trapped, the value will persist and can't be modified.

Quoted 3 lines

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.

Then maybe we should look at a better way of sharing global data between EL1 and EL2 instead of copying stuff ad-nauseam?

Quoted 61 lines
---
 arch/arm64/include/asm/kvm_host.h |  3 +++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/debug.c            | 26 ++++++++++++++++++++++++++
 3 files changed, 30 insertions(+)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f0bf8e641bd..e1852102550d 100644
@@ -1125,6 +1125,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) {}
@@ -1132,6 +1134,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);
arch/arm64/kvm/arm.c +1 -0
index 0f717b6a9151..e4d846f2f665 100644
@@ -1015,6 +1015,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
arch/arm64/kvm/debug.c +6 -0
index 20cdd40b3c42..2ab41b954512 100644
@@ -23,6 +23,12 @@
 
 static DEFINE_PER_CPU(u64, mdcr_el2);
 
+/*
+ * Per CPU value for TRFCR that should be applied to any guest vcpu that may
+ * run on that core in the future.
+ */
+static DEFINE_PER_CPU(u64, guest_trfcr);
+
 /**
  * save/restore_guest_debug_regs
  *
@@ -356,3 +362,23 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
 	vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
 	vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);

arch/arm64/kvm/debug.c void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) · line 373

Quoted 10 lines
arch/arm64/kvm/debug.c +9 -0
 }
+
+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);

How does the ETM code know what guests it impacts? Don't you have some per-process context already?

arch/arm64/kvm/debug.c void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) · line 384

Quoted 11 lines
arch/arm64/kvm/debug.c +11 -0
+
+/*
+ * 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))
+		ctxt_sys_reg(&vcpu->arch.ctxt, TRFCR_EL1) = *this_cpu_ptr(&guest_trfcr);
+}

Why this requirement of updating it at all times? Why can't this be done in a more lazy way, using the flags to instruct the hypervisor what and when to load it?

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#14 lore raw

Re: [PATCH v3 4/6] arm64: KVM: Add interface to set guest value for TRFCR register

Quoted 12 lines · Marc Zyngier
Quoted 10 lines · James Clark

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, the host trace is controlled by TRFCR_EL2 and thus we can write to the TRFCR_EL1 immediately. Because guest writes to the register are trapped, the value will persist and can't be modified.

Quoted 7 lines
Quoted 3 lines

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.

Then maybe we should look at a better way of sharing global data between EL1 and EL2 instead of copying stuff ad-nauseam?

That probably makes sense, I can have a look into that.

Quoted 66 lines
Quoted 66 lines
---
 arch/arm64/include/asm/kvm_host.h |  3 +++
 arch/arm64/kvm/arm.c              |  1 +
 arch/arm64/kvm/debug.c            | 26 ++++++++++++++++++++++++++
 3 files changed, 30 insertions(+)
arch/arm64/include/asm/kvm_host.h +3 -0
index 0f0bf8e641bd..e1852102550d 100644
@@ -1125,6 +1125,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) {}
@@ -1132,6 +1134,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);
arch/arm64/kvm/arm.c +1 -0
index 0f717b6a9151..e4d846f2f665 100644
@@ -1015,6 +1015,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
arch/arm64/kvm/debug.c +10 -0
index 20cdd40b3c42..2ab41b954512 100644
@@ -23,6 +23,12 @@
 
 static DEFINE_PER_CPU(u64, mdcr_el2);
 
+/*
+ * Per CPU value for TRFCR that should be applied to any guest vcpu that may
+ * run on that core in the future.
+ */
+static DEFINE_PER_CPU(u64, guest_trfcr);
+
 /**
  * save/restore_guest_debug_regs
  *
@@ -356,3 +362,23 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
 	vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
 	vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRFCR);
 }
+
+void kvm_etm_set_guest_trfcr(u64 trfcr_guest)
+{
+	if (has_vhe())
Quoted 9 lines

arch/arm64/kvm/debug.c void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) · line 373

Quoted 5 lines
arch/arm64/kvm/debug.c +5 -0
+		write_sysreg_s(trfcr_guest, SYS_TRFCR_EL12);
+	else
+		*this_cpu_ptr(&guest_trfcr) = trfcr_guest;
+}
+EXPORT_SYMBOL_GPL(kvm_etm_set_guest_trfcr);

How does the ETM code know what guests it impacts? Don't you have some per-process context already?

It doesn't know what guests it impacts, it just does it blindly based on host CPU and whatever guest might run on the CPU in the future. PMU events are the same.

We do have per-process context for per-process sessions, so if that was the VM process we might have been able to do something with that info. But we also have per-cpu sessions that would trace anything that runs on that CPU, so to be able to support that mode I think it has to be done without knowing about any guest.

Quoted 16 lines

arch/arm64/kvm/debug.c void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu) · line 384

Quoted 11 lines
arch/arm64/kvm/debug.c +11 -0
+
+/*
+ * 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))
+		ctxt_sys_reg(&vcpu->arch.ctxt, TRFCR_EL1) = *this_cpu_ptr(&guest_trfcr);
+}

Why this requirement of updating it at all times? Why can't this be done in a more lazy way, using the flags to instruct the hypervisor what and when to load it?

Quoted 2 lines
	M.

I could probably add a flag that gets set if the guest value should be different to the host value. I was just trying to keep it simple and in terms of just what the registers should be.

The PMU one has something similar where it doesn't write anything if kvm_pmu_switch_needed() is false, but that's only on the path where the host sets the events, it still always does the copy in kvm_pmu_update_vcpu_events() before the guest switch.

I suppose if I make the change to have the shared global data then the copy isn't needed and this function and kvm_pmu_update_vcpu_events() will just get deleted.

Thanks
James

#15 lore raw

PATCH v3 5/6 arm64: KVM: Write TRFCR value on guest switch with nVHE

In reply to James Clark

The guest value for TRFCR requested by the Coresight driver is saved in sysregs[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.

---
 arch/arm64/include/asm/kvm_hyp.h   |  6 ++-
 arch/arm64/kvm/hyp/nvhe/debug-sr.c | 68 ++++++++++++++++++------------
 arch/arm64/kvm/hyp/nvhe/switch.c   |  4 +-
 3 files changed, 48 insertions(+), 30 deletions(-)
arch/arm64/include/asm/kvm_hyp.h +4 -2
index 52ac90d419e7..6286e580696e 100644
@@ -103,8 +103,10 @@ void __debug_switch_to_guest(struct kvm_vcpu *vcpu);
 void __debug_switch_to_host(struct kvm_vcpu *vcpu);
 
 #ifdef __KVM_NVHE_HYPERVISOR__
-void __debug_save_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt);
-void __debug_restore_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt);
+void __debug_save_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt,
+				    struct kvm_cpu_context *guest_ctxt);
+void __debug_restore_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt,
+				       struct kvm_cpu_context *guest_ctxt);
 #endif
 
 void __fpsimd_save_state(struct user_fpsimd_state *fp_regs);
arch/arm64/kvm/hyp/nvhe/debug-sr.c +42 -26

96 lines not on this page · raw message

arch/arm64/kvm/hyp/nvhe/switch.c +2 -2
index 6b4b24ae077f..c7bea5cf672d 100644
@@ -278,7 +278,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 	 * translation regime to EL2 (via MDCR_EL2_E2PB == 0) and
 	 * before we load guest Stage1.
 	 */
-	__debug_save_host_buffers_nvhe(host_ctxt);
+	__debug_save_host_buffers_nvhe(host_ctxt, guest_ctxt);
 
 	/*
 	 * We're about to restore some new MMU state. Make sure
@@ -345,7 +345,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
 	 * This must come after restoring the host sysregs, since a non-VHE
 	 * system may enable SPE here and make use of the TTBRs.
 	 */
-	__debug_restore_host_buffers_nvhe(host_ctxt);
+	__debug_restore_host_buffers_nvhe(host_ctxt, guest_ctxt);
 
 	if (pmu_switch_needed)
 		__pmu_switch_to_host(vcpu);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v3 5/6
#16 lore raw

Re: [PATCH v3 5/6] arm64: KVM: Write TRFCR value on guest switch with nVHE

Quoted 8 lines · James Clark

The guest value for TRFCR requested by the Coresight driver is saved in sysregs[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.

Quoted 6 lines

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.

Looks good to me.

Quoted 147 lines
---
  arch/arm64/include/asm/kvm_hyp.h   |  6 ++-
  arch/arm64/kvm/hyp/nvhe/debug-sr.c | 68 ++++++++++++++++++------------
  arch/arm64/kvm/hyp/nvhe/switch.c   |  4 +-
  3 files changed, 48 insertions(+), 30 deletions(-)
arch/arm64/include/asm/kvm_hyp.h +4 -2
index 52ac90d419e7..6286e580696e 100644
@@ -103,8 +103,10 @@ void __debug_switch_to_guest(struct kvm_vcpu *vcpu);
  void __debug_switch_to_host(struct kvm_vcpu *vcpu);
 
  #ifdef __KVM_NVHE_HYPERVISOR__
-void __debug_save_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt);
-void __debug_restore_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt);
+void __debug_save_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt,
+				    struct kvm_cpu_context *guest_ctxt);
+void __debug_restore_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt,
+				       struct kvm_cpu_context *guest_ctxt);
  #endif
 
  void __fpsimd_save_state(struct user_fpsimd_state *fp_regs);
arch/arm64/kvm/hyp/nvhe/debug-sr.c +42 -26
index f389ee59788c..6174f710948e 100644
@@ -51,42 +51,57 @@ static void __debug_restore_spe(struct kvm_cpu_context *host_ctxt)
  	write_sysreg_s(ctxt_sys_reg(host_ctxt, PMSCR_EL1), SYS_PMSCR_EL1);
  }
 
-static void __debug_save_trace(struct kvm_cpu_context *host_ctxt)
+/*
+ * Save TRFCR and disable trace completely if TRBE is being used, otherwise
+ * apply required guest TRFCR value.
+ */
+static void __debug_save_trace(struct kvm_cpu_context *host_ctxt,
+			       struct kvm_cpu_context *guest_ctxt)
  {
-	ctxt_sys_reg(host_ctxt, TRFCR_EL1) = 0;
+	ctxt_sys_reg(host_ctxt, 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.
-	 */
-	ctxt_sys_reg(host_ctxt, 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 (vcpu_get_flag(host_ctxt->__hyp_running_vcpu, DEBUG_STATE_SAVE_TRBE) &&
+	    (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.
+		 */
+		ctxt_sys_reg(guest_ctxt, 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 (ctxt_sys_reg(host_ctxt, TRFCR_EL1) !=
+		    ctxt_sys_reg(guest_ctxt, TRFCR_EL1))
+			write_sysreg_s(ctxt_sys_reg(guest_ctxt, TRFCR_EL1),
+				       SYS_TRFCR_EL1);
+	}
  }
 
-static void __debug_restore_trace(struct kvm_cpu_context *host_ctxt)
+static void __debug_restore_trace(struct kvm_cpu_context *host_ctxt,
+				  struct kvm_cpu_context *guest_ctxt)
  {
-	if (!ctxt_sys_reg(host_ctxt, TRFCR_EL1))
-		return;
-
  	/* Restore trace filter controls */
-	write_sysreg_s(ctxt_sys_reg(host_ctxt, TRFCR_EL1), SYS_TRFCR_EL1);
+	if (ctxt_sys_reg(host_ctxt, TRFCR_EL1) != ctxt_sys_reg(guest_ctxt, TRFCR_EL1))
+		write_sysreg_s(ctxt_sys_reg(host_ctxt, TRFCR_EL1), SYS_TRFCR_EL1);
  }
 
-void __debug_save_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt)
+void __debug_save_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt,
+				    struct kvm_cpu_context *guest_ctxt)
  {
  	/* Disable and flush SPE data generation */
  	if (vcpu_get_flag(host_ctxt->__hyp_running_vcpu, DEBUG_STATE_SAVE_SPE))
  		__debug_save_spe(host_ctxt);
-	/* Disable and flush Self-Hosted Trace generation */
-	if (vcpu_get_flag(host_ctxt->__hyp_running_vcpu, DEBUG_STATE_SAVE_TRBE))
-		__debug_save_trace(host_ctxt);
+
+	if (vcpu_get_flag(host_ctxt->__hyp_running_vcpu, DEBUG_STATE_SAVE_TRFCR))
+		__debug_save_trace(host_ctxt, guest_ctxt);
  }
 
  void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
@@ -94,12 +109,13 @@ void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
  	__debug_switch_to_guest_common(vcpu);
  }
 
-void __debug_restore_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt)
+void __debug_restore_host_buffers_nvhe(struct kvm_cpu_context *host_ctxt,
+				       struct kvm_cpu_context *guest_ctxt)
  {
  	if (vcpu_get_flag(host_ctxt->__hyp_running_vcpu, DEBUG_STATE_SAVE_SPE))
  		__debug_restore_spe(host_ctxt);
-	if (vcpu_get_flag(host_ctxt->__hyp_running_vcpu, DEBUG_STATE_SAVE_TRBE))
-		__debug_restore_trace(host_ctxt);
+	if (vcpu_get_flag(host_ctxt->__hyp_running_vcpu, DEBUG_STATE_SAVE_TRFCR))
+		__debug_restore_trace(host_ctxt, guest_ctxt);
  }
 
  void __debug_switch_to_host(struct kvm_vcpu *vcpu)
arch/arm64/kvm/hyp/nvhe/switch.c +2 -2
index 6b4b24ae077f..c7bea5cf672d 100644
@@ -278,7 +278,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
  	 * translation regime to EL2 (via MDCR_EL2_E2PB == 0) and
  	 * before we load guest Stage1.
  	 */
-	__debug_save_host_buffers_nvhe(host_ctxt);
+	__debug_save_host_buffers_nvhe(host_ctxt, guest_ctxt);
 
  	/*
  	 * We're about to restore some new MMU state. Make sure
@@ -345,7 +345,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
  	 * This must come after restoring the host sysregs, since a non-VHE
  	 * system may enable SPE here and make use of the TTBRs.
  	 */
-	__debug_restore_host_buffers_nvhe(host_ctxt);
+	__debug_restore_host_buffers_nvhe(host_ctxt, guest_ctxt);
 
  	if (pmu_switch_needed)
  		__pmu_switch_to_host(vcpu);
#17 lore raw

PATCH v3 6/6 coresight: Pass guest TRFCR value to KVM

In reply to James Clark

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
---
 .../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(-)
drivers/hwtracing/coresight/coresight-etm4x-core.c +36 -6
index 77b0271ce6eb..292f9da6aeaf 100644
@@ -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);
 
drivers/hwtracing/coresight/coresight-etm4x.h +1 -1
index 20e2e4cb7614..3f170599822f 100644
@@ -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;
drivers/hwtracing/coresight/coresight-priv.h +3 -0
index 767076e07970..727dd27ba800 100644
@@ -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;
Signature · 1 line
2.34.1
maintainer
#18 lore raw

Re: [PATCH v3 6/6] coresight: Pass guest TRFCR value to KVM

Quoted 104 lines · James Clark

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
---
  .../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(-)
drivers/hwtracing/coresight/coresight-etm4x-core.c +20 -6
index 77b0271ce6eb..292f9da6aeaf 100644
@@ -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);
 

drivers/hwtracing/coresight/coresight-etm4x-core.c static void etm4x_prohibit_trace(struct etmv4_drvdata *drvdata) · line 323

Quoted 10 lines
drivers/hwtracing/coresight/coresight-etm4x-core.c +9 -0
  	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;

Here we are using the Guest "Virtual time" for the trace, which could be different from that of the "host" (if the trace covers both). Thus correlating the trace could be difficult if someone wants to do that.

We could switch to using Physical time, for the guest trace always, matching the host time for correlating the events. (Assuming we are not a Guest Hypervisor).

Rest looks fine to me.

Quoted 35 lines
drivers/hwtracing/coresight/coresight-etm4x-core.c +7 -0
+	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);
 
drivers/hwtracing/coresight/coresight-etm4x.h +1 -1
index 20e2e4cb7614..3f170599822f 100644
@@ -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;
drivers/hwtracing/coresight/coresight-priv.h +0 -0
index 767076e07970..727dd27ba800 100644

drivers/hwtracing/coresight/coresight-priv.h line 44

Quoted 6 lines
drivers/hwtracing/coresight/coresight-priv.h +3 -0
@@ -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)
+

nit: Some day, we should move to using the "event->attrs" directly, than translating them to these values.

Suzuki

#19 lore raw

Re: [PATCH v3 6/6] coresight: Pass guest TRFCR value to KVM

Quoted 110 lines · Suzuki K Poulose
Quoted 109 lines · James Clark

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

---
  .../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(-)
drivers/hwtracing/coresight/coresight-etm4x-core.c +13 -0
index 77b0271ce6eb..292f9da6aeaf 100644
@@ -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

drivers/hwtracing/coresight/coresight-etm4x-core.c +11 -5
@@ -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
Quoted 9 lines

drivers/hwtracing/coresight/coresight-etm4x-core.c static void etm4x_prohibit_trace(struct etmv4_drvdata *drvdata) · line 323

Quoted 4 lines
drivers/hwtracing/coresight/coresight-etm4x-core.c +4 -0
+        trfcr = etm4x_get_kern_user_filter(drvdata);
+
+    /* TRFCR_EL1 doesn't have CX so mask it out. */
+    trfcr &= ~TRFCR_EL2_CX;

Here we are using the Guest "Virtual time" for the trace, which could be different from that of the "host" (if the trace covers both). Thus correlating the trace could be difficult if someone wants to do that.

I don't think it would be difficult, you need to have a perf recording from inside the guest anyway to capture the DSO mappings and switch events. So that recording would have the guest's time conversion event in it.

It might require a perf change to handle two different time conversion packets, or you'd probably just want to use the guest one anyway which might already work. I haven't really tested it in that much detail.

Quoted 4 lines

We could switch to using Physical time, for the guest trace always, matching the host time for correlating the events. (Assuming we are not a Guest Hypervisor).

I think Physical time would be less useful, because eventually we want to be able to handle things like overlapping mmaps around execs etc so we need the virtual time.

Physical time is only good for ordering events, and the virtual time can do that too.

One thing that we might want to do is make TRFCR_ELx_TS_VIRTUAL configurable via a config option if you want to be able to correlate host and guest trace together with physical time. But I think that would be an extra change because it's already hard coded to on, and you can already trace nVHE guests. We should probably keep this change just about the filtering only.

Quoted 37 lines

Rest looks fine to me.

Quoted 35 lines
drivers/hwtracing/coresight/coresight-etm4x-core.c +6 -0
+    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

drivers/hwtracing/coresight/coresight-etm4x.h +1 -1
@@ -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;
drivers/hwtracing/coresight/coresight-priv.h +0 -0
index 767076e07970..727dd27ba800 100644
@@ -39,6 +39,9 @@
Quoted 9 lines

drivers/hwtracing/coresight/coresight-priv.h line 44

Quoted 5 lines
drivers/hwtracing/coresight/coresight-priv.h +3 -0
     #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)
+

nit: Some day, we should move to using the "event->attrs" directly, than translating them to these values.

Yeah we could do that, that would be a good cleanup.

Quoted 3 lines

Suzuki