Skip to contentSkip to conversation

kvm/coresight: Support exclude guest and exclude host

PATCH Superseded James Clark 4 participants 28 messages Jan 4, 2024 – Feb 5, 2024

Subsystems (4)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 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.

---

Changes since V3:
* Create a new shared area to store the host state instead of copying it before each VCPU run
* Drop commit that moved SPE and trace registers from host_debug_state into the kvm sysregs array because the guest values were never used
* Document kvm_etm_set_guest_trfcr()
* Guard kvm_etm_set_guest_trfcr() with a feature check
* Drop Mark B and Suzuki's review tags on the sysreg patch because it turned out that broke the Perf build and needed some unconventional changes to fix it (as in: to update the tools copy of the headers in the same commit as the kernel changes)

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 (7):
arm64: KVM: Fix renamed function in comment
arm64: KVM: Use shared area to pass PMU event state to hypervisor arm64/sysreg/tools: Move TRFCR definitions to sysreg
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             |  15 +-
 arch/arm64/include/asm/sysreg.h               |  12 -
 arch/arm64/kernel/image-vars.h                |   1 +
 arch/arm64/kvm/arm.c                          |  16 +-
 arch/arm64/kvm/debug.c                        |  48 ++-
 arch/arm64/kvm/hyp/nvhe/debug-sr.c            |  55 +--
 arch/arm64/kvm/hyp/nvhe/setup.c               |  13 +-
 arch/arm64/kvm/hyp/nvhe/switch.c              |   9 +-
 arch/arm64/kvm/pmu.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 +
 include/kvm/arm_pmu.h                         |  17 -
 tools/arch/arm64/include/asm/sysreg.h         | 345 +++++++++++++++++-
 tools/include/linux/kasan-tags.h              |  15 +
 16 files changed, 554 insertions(+), 84 deletions(-)
 create mode 100644 tools/include/linux/kasan-tags.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/7 arm64: KVM: Fix renamed function in comment

finalise_host_mappings() became fix_host_ownership() in commit 0d16d12eb26e ("KVM: arm64: Fix-up hyp stage-1 refcounts for all pages mapped at EL2") so update the comment.

---
 arch/arm64/kvm/hyp/nvhe/setup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/setup.c +1 -1
index cd2f39388251..b5452e58c49a 100644
@@ -150,7 +150,7 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
 	 * can't be donated or shared with another entity.
 	 *
 	 * The ownership transition requires matching changes in the host
-	 * stage-2. This will be done later (see finalize_host_mappings()) once
+	 * stage-2. This will be done later (see fix_host_ownership()) once
 	 * the hyp_vmemmap is addressable.
 	 */
 	prot = pkvm_mkstate(PAGE_HYP_RO, PKVM_PAGE_SHARED_OWNED);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v4 1/7
#2 lore raw

Re: [PATCH v4 1/7] arm64: KVM: Fix renamed function in comment

Quoted 5 lines · James Clark

finalise_host_mappings() became fix_host_ownership() in commit 0d16d12eb26e ("KVM: arm64: Fix-up hyp stage-1 refcounts for all pages mapped at EL2") so update the comment.

Quoted 17 lines
---
  arch/arm64/kvm/hyp/nvhe/setup.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/setup.c +1 -1
index cd2f39388251..b5452e58c49a 100644
@@ -150,7 +150,7 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
  	 * can't be donated or shared with another entity.
  	 *
  	 * The ownership transition requires matching changes in the host
-	 * stage-2. This will be done later (see finalize_host_mappings()) once
+	 * stage-2. This will be done later (see fix_host_ownership()) once
  	 * the hyp_vmemmap is addressable.
  	 */
  	prot = pkvm_mkstate(PAGE_HYP_RO, PKVM_PAGE_SHARED_OWNED);
#3 lore raw

PATCH v4 2/7 arm64: KVM: Use shared area to pass PMU event state to hypervisor

In reply to James Clark

Currently the state of the PMU events is copied into the VCPU struct before every VCPU run. This isn't scalable if more data for other features needs to be added too. So make a writable area that's shared between the host and the hypervisor to store this state.

Normal per-cpu constructs can't be used because although the framework exists for the host to write to the hypervisor's per-cpu structs, this only works until the protection is enabled. And for the other way around, no framework exists for the hypervisor to access the host's size and layout of per-cpu data. Instead of making a new framework for the hypervisor to access the host's per-cpu data that would only be used once, just define the new shared area as an array with NR_CPUS elements. This also reduces the amount of sharing that needs to be done, because unlike this array, the per-cpu data isn't contiguous.

---
 arch/arm64/include/asm/kvm_host.h |  8 ++++++++
 arch/arm64/kernel/image-vars.h    |  1 +
 arch/arm64/kvm/arm.c              | 16 ++++++++++++++--
 arch/arm64/kvm/hyp/nvhe/setup.c   | 11 +++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c  |  9 +++++++--
 arch/arm64/kvm/pmu.c              |  4 +---
 include/kvm/arm_pmu.h             | 17 -----------------
 7 files changed, 42 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +8 -0
index 824f29f04916..93d38ad257ed 100644
@@ -466,6 +466,14 @@ struct kvm_cpu_context {
 	struct kvm_vcpu *__hyp_running_vcpu;
 };
 
+struct kvm_host_global_state {
+	struct kvm_pmu_events {
+		u32 events_host;
+		u32 events_guest;
+	} pmu_events;
+} ____cacheline_aligned;
+extern struct kvm_host_global_state kvm_host_global_state[NR_CPUS];
+
 struct kvm_host_data {
 	struct kvm_cpu_context host_ctxt;
 };
arch/arm64/kernel/image-vars.h +1 -0
index 119ca121b5f8..1a9dbb02bb4a 100644
@@ -59,6 +59,7 @@ KVM_NVHE_ALIAS(alt_cb_patch_nops);
 
 /* Global kernel state accessed by nVHE hyp code. */
 KVM_NVHE_ALIAS(kvm_vgic_global_state);
+KVM_NVHE_ALIAS(kvm_host_global_state);
 
 /* Kernel symbols used to call panic() from nVHE hyp code (via ERET). */
 KVM_NVHE_ALIAS(nvhe_hyp_panic_handler);
arch/arm64/kvm/arm.c +14 -2
index 4796104c4471..bd6b2eda5f4f 100644
@@ -47,6 +47,20 @@
 
 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
 
+/*
+ * Host state that isn't associated with any VCPU, but will affect any VCPU
+ * running on a host CPU in the future. This remains writable from the host and
+ * readable in the hyp.
+ *
+ * PER_CPU constructs aren't compatible between the hypervisor and the host so
+ * just define it as a NR_CPUS array. DECLARE_KVM_NVHE_PER_CPU works in both
+ * places, but not after the hypervisor protection is initialised. After that,
+ * kvm_arm_hyp_percpu_base isn't accessible from the host, so even if the
+ * kvm_host_global_state struct was shared with the host, the per-cpu offset
+ * can't be calculated without sharing even more data with the host.
+ */
+struct kvm_host_global_state kvm_host_global_state[NR_CPUS];
+
 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
 
 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
@@ -1016,8 +1030,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
 		kvm_vgic_flush_hwstate(vcpu);
 
-		kvm_pmu_update_vcpu_events(vcpu);
-
 		/*
 		 * Ensure we set mode to IN_GUEST_MODE after we disable
 		 * interrupts and before the final VCPU requests check.
arch/arm64/kvm/hyp/nvhe/setup.c +11 -0
index b5452e58c49a..3e45cc10ba96 100644
@@ -159,6 +159,17 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
 	if (ret)
 		return ret;
 
+	/*
+	 * Similar to kvm_vgic_global_state, but this one remains writable by
+	 * the host rather than read-only. Used to store per-cpu state about the
+	 * host that isn't associated with any particular VCPU.
+	 */
+	prot = pkvm_mkstate(KVM_PGTABLE_PROT_RW, PKVM_PAGE_SHARED_OWNED);
+	ret = pkvm_create_mappings(&kvm_host_global_state,
+				   &kvm_host_global_state + 1, prot);
+	if (ret)
+		return ret;
+
 	ret = create_hyp_debug_uart_mapping();
 	if (ret)
 		return ret;
arch/arm64/kvm/hyp/nvhe/switch.c +7 -2
index c50f8459e4fc..89147a9dc38c 100644
@@ -130,13 +130,18 @@ static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
 	}
 }
 
+static struct kvm_pmu_events *kvm_nvhe_get_pmu_events(struct kvm_vcpu *vcpu)
+{
+	return &kvm_host_global_state[vcpu->cpu].pmu_events;
+}
+
 /*
  * Disable host events, enable guest events
  */
 #ifdef CONFIG_HW_PERF_EVENTS
 static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
 {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
 	if (pmu->events_host)
 		write_sysreg(pmu->events_host, pmcntenclr_el0);
@@ -152,7 +157,7 @@ static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
  */
 static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
 {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
 	if (pmu->events_guest)
 		write_sysreg(pmu->events_guest, pmcntenclr_el0);
arch/arm64/kvm/pmu.c +1 -3
index a243934c5568..136d5c6c1916 100644
@@ -6,8 +6,6 @@
 #include <linux/kvm_host.h>
 #include <linux/perf_event.h>
 
-static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);
-
 /*
  * Given the perf event attributes and system type, determine
  * if we are going to need to switch counters at guest entry/exit.
@@ -28,7 +26,7 @@ static bool kvm_pmu_switch_needed(struct perf_event_attr *attr)
 
 struct kvm_pmu_events *kvm_get_pmu_events(void)
 {
-	return this_cpu_ptr(&kvm_pmu_events);
+	return &kvm_host_global_state[smp_processor_id()].pmu_events;
 }
 
 /*
include/kvm/arm_pmu.h +0 -17
index 4b9d8fb393a8..71a835970ab5 100644
@@ -18,14 +18,8 @@ struct kvm_pmc {
 	struct perf_event *perf_event;
 };
 
-struct kvm_pmu_events {
-	u32 events_host;
-	u32 events_guest;
-};
-
 struct kvm_pmu {
 	struct irq_work overflow_work;
-	struct kvm_pmu_events events;
 	struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS];
 	int irq_num;
 	bool created;
@@ -79,17 +73,6 @@ void kvm_vcpu_pmu_resync_el0(void);
 #define kvm_vcpu_has_pmu(vcpu)					\
 	(vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))
 
-/*
- * Updates the vcpu's view of the pmu 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.
- */
-#define kvm_pmu_update_vcpu_events(vcpu)				\
-	do {								\
-		if (!has_vhe() && kvm_vcpu_has_pmu(vcpu))		\
-			vcpu->arch.pmu.events = *kvm_get_pmu_events();	\
-	} while (0)
-
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
  */
Signature · 1 line
2.34.1
maintainer
#4 lore raw

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 31 lines · James Clark

Currently the state of the PMU events is copied into the VCPU struct before every VCPU run. This isn't scalable if more data for other features needs to be added too. So make a writable area that's shared between the host and the hypervisor to store this state.

Normal per-cpu constructs can't be used because although the framework exists for the host to write to the hypervisor's per-cpu structs, this only works until the protection is enabled. And for the other way around, no framework exists for the hypervisor to access the host's size and layout of per-cpu data. Instead of making a new framework for the hypervisor to access the host's per-cpu data that would only be used once, just define the new shared area as an array with NR_CPUS elements. This also reduces the amount of sharing that needs to be done, because unlike this array, the per-cpu data isn't contiguous.

---
  arch/arm64/include/asm/kvm_host.h |  8 ++++++++
  arch/arm64/kernel/image-vars.h    |  1 +
  arch/arm64/kvm/arm.c              | 16 ++++++++++++++--
  arch/arm64/kvm/hyp/nvhe/setup.c   | 11 +++++++++++
  arch/arm64/kvm/hyp/nvhe/switch.c  |  9 +++++++--
  arch/arm64/kvm/pmu.c              |  4 +---
  include/kvm/arm_pmu.h             | 17 -----------------
  7 files changed, 42 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -0
index 824f29f04916..93d38ad257ed 100644

arch/arm64/include/asm/kvm_host.h struct kvm_cpu_context { · line 475

Quoted 10 lines
arch/arm64/include/asm/kvm_host.h +7 -0
@@ -466,6 +466,14 @@ struct kvm_cpu_context {
  	struct kvm_vcpu *__hyp_running_vcpu;
  };
 
+struct kvm_host_global_state {
+	struct kvm_pmu_events {
+		u32 events_host;
+		u32 events_guest;
+	} pmu_events;
+} ____cacheline_aligned;
+extern struct kvm_host_global_state kvm_host_global_state[NR_CPUS];

With this in place, we could also optimize the VCPU debug state flags (DEBUG_STATE_SAVE_{TRBE,SPE}). i.e., right now, we always check the for SPE and TRBE availability on the CPU, by reading the ID registers. This could hold the per-cpu flags for the Physical CPU and the VCPU could use this for making the decisions, rather than reading the two ID registers per feature everytime.

This can come later though, in a separate series.

Suzuki

Quoted 164 lines
arch/arm64/include/asm/kvm_host.h +1 -0
+
  struct kvm_host_data {
  	struct kvm_cpu_context host_ctxt;
  };
arch/arm64/kernel/image-vars.h +1 -0
index 119ca121b5f8..1a9dbb02bb4a 100644
@@ -59,6 +59,7 @@ KVM_NVHE_ALIAS(alt_cb_patch_nops);
 
  /* Global kernel state accessed by nVHE hyp code. */
  KVM_NVHE_ALIAS(kvm_vgic_global_state);
+KVM_NVHE_ALIAS(kvm_host_global_state);
 
  /* Kernel symbols used to call panic() from nVHE hyp code (via ERET). */
  KVM_NVHE_ALIAS(nvhe_hyp_panic_handler);
arch/arm64/kvm/arm.c +14 -2
index 4796104c4471..bd6b2eda5f4f 100644
@@ -47,6 +47,20 @@
 
  static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
 
+/*
+ * Host state that isn't associated with any VCPU, but will affect any VCPU
+ * running on a host CPU in the future. This remains writable from the host and
+ * readable in the hyp.
+ *
+ * PER_CPU constructs aren't compatible between the hypervisor and the host so
+ * just define it as a NR_CPUS array. DECLARE_KVM_NVHE_PER_CPU works in both
+ * places, but not after the hypervisor protection is initialised. After that,
+ * kvm_arm_hyp_percpu_base isn't accessible from the host, so even if the
+ * kvm_host_global_state struct was shared with the host, the per-cpu offset
+ * can't be calculated without sharing even more data with the host.
+ */
+struct kvm_host_global_state kvm_host_global_state[NR_CPUS];
+
  DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
 
  DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
@@ -1016,8 +1030,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
  		kvm_vgic_flush_hwstate(vcpu);
 
-		kvm_pmu_update_vcpu_events(vcpu);
-
  		/*
  		 * Ensure we set mode to IN_GUEST_MODE after we disable
  		 * interrupts and before the final VCPU requests check.
arch/arm64/kvm/hyp/nvhe/setup.c +11 -0
index b5452e58c49a..3e45cc10ba96 100644
@@ -159,6 +159,17 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
  	if (ret)
  		return ret;
 
+	/*
+	 * Similar to kvm_vgic_global_state, but this one remains writable by
+	 * the host rather than read-only. Used to store per-cpu state about the
+	 * host that isn't associated with any particular VCPU.
+	 */
+	prot = pkvm_mkstate(KVM_PGTABLE_PROT_RW, PKVM_PAGE_SHARED_OWNED);
+	ret = pkvm_create_mappings(&kvm_host_global_state,
+				   &kvm_host_global_state + 1, prot);
+	if (ret)
+		return ret;
+
  	ret = create_hyp_debug_uart_mapping();
  	if (ret)
  		return ret;
arch/arm64/kvm/hyp/nvhe/switch.c +7 -2
index c50f8459e4fc..89147a9dc38c 100644
@@ -130,13 +130,18 @@ static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
  	}
  }
 
+static struct kvm_pmu_events *kvm_nvhe_get_pmu_events(struct kvm_vcpu *vcpu)
+{
+	return &kvm_host_global_state[vcpu->cpu].pmu_events;
+}
+
  /*
   * Disable host events, enable guest events
   */
  #ifdef CONFIG_HW_PERF_EVENTS
  static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
  {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
  	if (pmu->events_host)
  		write_sysreg(pmu->events_host, pmcntenclr_el0);
@@ -152,7 +157,7 @@ static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
   */
  static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
  {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
  	if (pmu->events_guest)
  		write_sysreg(pmu->events_guest, pmcntenclr_el0);
arch/arm64/kvm/pmu.c +1 -3
index a243934c5568..136d5c6c1916 100644
@@ -6,8 +6,6 @@
  #include <linux/kvm_host.h>
  #include <linux/perf_event.h>
 
-static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);
-
  /*
   * Given the perf event attributes and system type, determine
   * if we are going to need to switch counters at guest entry/exit.
@@ -28,7 +26,7 @@ static bool kvm_pmu_switch_needed(struct perf_event_attr *attr)
 
  struct kvm_pmu_events *kvm_get_pmu_events(void)
  {
-	return this_cpu_ptr(&kvm_pmu_events);
+	return &kvm_host_global_state[smp_processor_id()].pmu_events;
  }
 
  /*
include/kvm/arm_pmu.h +0 -17
index 4b9d8fb393a8..71a835970ab5 100644
@@ -18,14 +18,8 @@ struct kvm_pmc {
  	struct perf_event *perf_event;
  };
 
-struct kvm_pmu_events {
-	u32 events_host;
-	u32 events_guest;
-};
-
  struct kvm_pmu {
  	struct irq_work overflow_work;
-	struct kvm_pmu_events events;
  	struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS];
  	int irq_num;
  	bool created;
@@ -79,17 +73,6 @@ void kvm_vcpu_pmu_resync_el0(void);
  #define kvm_vcpu_has_pmu(vcpu)					\
  	(vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))
 
-/*
- * Updates the vcpu's view of the pmu 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.
- */
-#define kvm_pmu_update_vcpu_events(vcpu)				\
-	do {								\
-		if (!has_vhe() && kvm_vcpu_has_pmu(vcpu))		\
-			vcpu->arch.pmu.events = *kvm_get_pmu_events();	\
-	} while (0)
-
  /*
   * Evaluates as true when emulating PMUv3p5, and false otherwise.
   */
#5 lore raw

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

In reply to James Clark
Quoted 15 lines · James Clark

Currently the state of the PMU events is copied into the VCPU struct before every VCPU run. This isn't scalable if more data for other features needs to be added too. So make a writable area that's shared between the host and the hypervisor to store this state.

Normal per-cpu constructs can't be used because although the framework exists for the host to write to the hypervisor's per-cpu structs, this only works until the protection is enabled. And for the other way around, no framework exists for the hypervisor to access the host's size and layout of per-cpu data. Instead of making a new framework for the hypervisor to access the host's per-cpu data that would only be used once, just define the new shared area as an array with NR_CPUS elements. This also reduces the amount of sharing that needs to be done, because unlike this array, the per-cpu data isn't contiguous.

Quoted 1 line

Hi Marc,

Do you have any feedback about whether this what you were thinking of in your comment here:
https://lore.kernel.org/kvmarm/[email protected]/

Thanks
James

Quoted 189 lines
---
 arch/arm64/include/asm/kvm_host.h |  8 ++++++++
 arch/arm64/kernel/image-vars.h    |  1 +
 arch/arm64/kvm/arm.c              | 16 ++++++++++++++--
 arch/arm64/kvm/hyp/nvhe/setup.c   | 11 +++++++++++
 arch/arm64/kvm/hyp/nvhe/switch.c  |  9 +++++++--
 arch/arm64/kvm/pmu.c              |  4 +---
 include/kvm/arm_pmu.h             | 17 -----------------
 7 files changed, 42 insertions(+), 24 deletions(-)
arch/arm64/include/asm/kvm_host.h +8 -0
index 824f29f04916..93d38ad257ed 100644
@@ -466,6 +466,14 @@ struct kvm_cpu_context {
 	struct kvm_vcpu *__hyp_running_vcpu;
 };
 
+struct kvm_host_global_state {
+	struct kvm_pmu_events {
+		u32 events_host;
+		u32 events_guest;
+	} pmu_events;
+} ____cacheline_aligned;
+extern struct kvm_host_global_state kvm_host_global_state[NR_CPUS];
+
 struct kvm_host_data {
 	struct kvm_cpu_context host_ctxt;
 };
arch/arm64/kernel/image-vars.h +1 -0
index 119ca121b5f8..1a9dbb02bb4a 100644
@@ -59,6 +59,7 @@ KVM_NVHE_ALIAS(alt_cb_patch_nops);
 
 /* Global kernel state accessed by nVHE hyp code. */
 KVM_NVHE_ALIAS(kvm_vgic_global_state);
+KVM_NVHE_ALIAS(kvm_host_global_state);
 
 /* Kernel symbols used to call panic() from nVHE hyp code (via ERET). */
 KVM_NVHE_ALIAS(nvhe_hyp_panic_handler);
arch/arm64/kvm/arm.c +14 -2
index 4796104c4471..bd6b2eda5f4f 100644
@@ -47,6 +47,20 @@
 
 static enum kvm_mode kvm_mode = KVM_MODE_DEFAULT;
 
+/*
+ * Host state that isn't associated with any VCPU, but will affect any VCPU
+ * running on a host CPU in the future. This remains writable from the host and
+ * readable in the hyp.
+ *
+ * PER_CPU constructs aren't compatible between the hypervisor and the host so
+ * just define it as a NR_CPUS array. DECLARE_KVM_NVHE_PER_CPU works in both
+ * places, but not after the hypervisor protection is initialised. After that,
+ * kvm_arm_hyp_percpu_base isn't accessible from the host, so even if the
+ * kvm_host_global_state struct was shared with the host, the per-cpu offset
+ * can't be calculated without sharing even more data with the host.
+ */
+struct kvm_host_global_state kvm_host_global_state[NR_CPUS];
+
 DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector);
 
 DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);
@@ -1016,8 +1030,6 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 
 		kvm_vgic_flush_hwstate(vcpu);
 
-		kvm_pmu_update_vcpu_events(vcpu);
-
 		/*
 		 * Ensure we set mode to IN_GUEST_MODE after we disable
 		 * interrupts and before the final VCPU requests check.
arch/arm64/kvm/hyp/nvhe/setup.c +11 -0
index b5452e58c49a..3e45cc10ba96 100644
@@ -159,6 +159,17 @@ static int recreate_hyp_mappings(phys_addr_t phys, unsigned long size,
 	if (ret)
 		return ret;
 
+	/*
+	 * Similar to kvm_vgic_global_state, but this one remains writable by
+	 * the host rather than read-only. Used to store per-cpu state about the
+	 * host that isn't associated with any particular VCPU.
+	 */
+	prot = pkvm_mkstate(KVM_PGTABLE_PROT_RW, PKVM_PAGE_SHARED_OWNED);
+	ret = pkvm_create_mappings(&kvm_host_global_state,
+				   &kvm_host_global_state + 1, prot);
+	if (ret)
+		return ret;
+
 	ret = create_hyp_debug_uart_mapping();
 	if (ret)
 		return ret;
arch/arm64/kvm/hyp/nvhe/switch.c +7 -2
index c50f8459e4fc..89147a9dc38c 100644
@@ -130,13 +130,18 @@ static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
 	}
 }
 
+static struct kvm_pmu_events *kvm_nvhe_get_pmu_events(struct kvm_vcpu *vcpu)
+{
+	return &kvm_host_global_state[vcpu->cpu].pmu_events;
+}
+
 /*
  * Disable host events, enable guest events
  */
 #ifdef CONFIG_HW_PERF_EVENTS
 static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
 {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
 	if (pmu->events_host)
 		write_sysreg(pmu->events_host, pmcntenclr_el0);
@@ -152,7 +157,7 @@ static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
  */
 static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
 {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
 	if (pmu->events_guest)
 		write_sysreg(pmu->events_guest, pmcntenclr_el0);
arch/arm64/kvm/pmu.c +1 -3
index a243934c5568..136d5c6c1916 100644
@@ -6,8 +6,6 @@
 #include <linux/kvm_host.h>
 #include <linux/perf_event.h>
 
-static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);
-
 /*
  * Given the perf event attributes and system type, determine
  * if we are going to need to switch counters at guest entry/exit.
@@ -28,7 +26,7 @@ static bool kvm_pmu_switch_needed(struct perf_event_attr *attr)
 
 struct kvm_pmu_events *kvm_get_pmu_events(void)
 {
-	return this_cpu_ptr(&kvm_pmu_events);
+	return &kvm_host_global_state[smp_processor_id()].pmu_events;
 }
 
 /*
include/kvm/arm_pmu.h +0 -17
index 4b9d8fb393a8..71a835970ab5 100644
@@ -18,14 +18,8 @@ struct kvm_pmc {
 	struct perf_event *perf_event;
 };
 
-struct kvm_pmu_events {
-	u32 events_host;
-	u32 events_guest;
-};
-
 struct kvm_pmu {
 	struct irq_work overflow_work;
-	struct kvm_pmu_events events;
 	struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS];
 	int irq_num;
 	bool created;
@@ -79,17 +73,6 @@ void kvm_vcpu_pmu_resync_el0(void);
 #define kvm_vcpu_has_pmu(vcpu)					\
 	(vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))
 
-/*
- * Updates the vcpu's view of the pmu 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.
- */
-#define kvm_pmu_update_vcpu_events(vcpu)				\
-	do {								\
-		if (!has_vhe() && kvm_vcpu_has_pmu(vcpu))		\
-			vcpu->arch.pmu.events = *kvm_get_pmu_events();	\
-	} while (0)
-
 /*
  * Evaluates as true when emulating PMUv3p5, and false otherwise.
  */
#6 lore raw

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

In reply to James Clark

On Thu, Jan 04, 2024 at 04:27:02PM +0000, James Clark wrote:

[...]

Quoted 25 lines
arch/arm64/kvm/hyp/nvhe/switch.c +6 -1
index c50f8459e4fc..89147a9dc38c 100644
@@ -130,13 +130,18 @@ static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
 	}
 }
 
+static struct kvm_pmu_events *kvm_nvhe_get_pmu_events(struct kvm_vcpu *vcpu)
+{
+	return &kvm_host_global_state[vcpu->cpu].pmu_events;
+}
+
 /*
  * Disable host events, enable guest events
  */
 #ifdef CONFIG_HW_PERF_EVENTS
 static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
 {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
 	if (pmu->events_host)
 		write_sysreg(pmu->events_host, pmcntenclr_el0);

arch/arm64/kvm/hyp/nvhe/switch.c static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu) · line 163

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
@@ -152,7 +157,7 @@ static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
  */
 static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
 {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
 	if (pmu->events_guest)
 		write_sysreg(pmu->events_guest, pmcntenclr_el0);

This now allows the host to program event counters for a protected guest. That _might_ be a useful feature behind some debug option, but is most definitely *not* something we want to do for pVMs generally.

Do we even need to make this shared data work at all for pKVM? The rest of the shared data between pKVM and the kernel is system information, which (importantly) doesn't have any guest context in it.

I'm perfectly happy leaving these sorts of features broken for pKVM and using the 'normal' way of getting percpu data to the nVHE hypervisor otherwise.

Signature · 2 lines
Thanks,
Oliver
#7 lore raw

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 35 lines · Oliver Upton

On Thu, Jan 04, 2024 at 04:27:02PM +0000, James Clark wrote:

[...]

Quoted 31 lines
arch/arm64/kvm/hyp/nvhe/switch.c +7 -2
index c50f8459e4fc..89147a9dc38c 100644
@@ -130,13 +130,18 @@ static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
 	}
 }
 
+static struct kvm_pmu_events *kvm_nvhe_get_pmu_events(struct kvm_vcpu *vcpu)
+{
+	return &kvm_host_global_state[vcpu->cpu].pmu_events;
+}
+
 /*
  * Disable host events, enable guest events
  */
 #ifdef CONFIG_HW_PERF_EVENTS
 static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
 {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
 	if (pmu->events_host)
 		write_sysreg(pmu->events_host, pmcntenclr_el0);
@@ -152,7 +157,7 @@ static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
  */
 static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
 {
-	struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
+	struct kvm_pmu_events *pmu = kvm_nvhe_get_pmu_events(vcpu);
 
Quoted 6 lines

arch/arm64/kvm/hyp/nvhe/switch.c static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu) · line 163

Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/switch.c +0 -0
 	if (pmu->events_guest)
 		write_sysreg(pmu->events_guest, pmcntenclr_el0);

This now allows the host to program event counters for a protected guest. That _might_ be a useful feature behind some debug option, but is most definitely *not* something we want to do for pVMs generally.

Unless I'm missing something, using PMUs on protected guests was added by 722625c6f4c5b ("KVM: arm64: Reenable pmu in Protected Mode"). This change is just a refactor that will allow us to add the same behavior for a similar feature (tracing) without adding yet another copy of some state before the guest switch.

Quoted 5 lines

Do we even need to make this shared data work at all for pKVM? The rest of the shared data between pKVM and the kernel is system information, which (importantly) doesn't have any guest context in it.

Probably not, Marc actually mentioned on one of the first versions of that this could be hidden behind a debug flag. To be honest one of the reasons I didn't do that was because I wasn't sure what the appropriate debug setting was. NVHE_EL2_DEBUG didn't seem quite right. DEBUG_KERNEL maybe? Or a new one?

And then I suppose I got distracted by trying to make it have feature parity with PMUs and forgot about the debug only thing.

Quoted 4 lines

I'm perfectly happy leaving these sorts of features broken for pKVM and using the 'normal' way of getting percpu data to the nVHE hypervisor otherwise.

I can do that. But do I also disable PMU at the same time in a new commit? Now that both PMU and tracing is working maybe it would be a waste to throw that away and hiding it behind an option is better. Or I can leave the PMU as it is and just keep tracing disabled in pKVM.

I don't mind either way, my main goal was to get exclude/include guest tracing working for normal VMs. For pKVM I don't have a strong opinion.

#8 lore raw

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 9 lines · James Clark
Quoted 3 lines

This now allows the host to program event counters for a protected guest. That _might_ be a useful feature behind some debug option, but is most definitely *not* something we want to do for pVMs generally.

Unless I'm missing something, using PMUs on protected guests was added by 722625c6f4c5b ("KVM: arm64: Reenable pmu in Protected Mode"). This change is just a refactor that will allow us to add the same behavior for a similar feature (tracing) without adding yet another copy of some state before the guest switch.

Ha, I had forgotten about that patch (and I had reviewed it!)

My interpretation of the intent for that change was to enable the usage of vPMU for non-protected VMs. The situation has changed since then, as we use the shadow state for vCPUs unconditionally in protected mode as of commit be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")

Protected mode is well understood at this point to be a WIP feature, and that not all things are expected to work with it. Eventually we will need a way to distinguish between 'normal' VMs and true pVMs (i.e. the VMM selected full isolation) in nVHE, but right now what we have enables testing of some isolation features.

Quoted 5 lines
Quoted 4 lines

I'm perfectly happy leaving these sorts of features broken for pKVM and using the 'normal' way of getting percpu data to the nVHE hypervisor otherwise.

Quoted 7 lines

I can do that. But do I also disable PMU at the same time in a new commit? Now that both PMU and tracing is working maybe it would be a waste to throw that away and hiding it behind an option is better. Or I can leave the PMU as it is and just keep tracing disabled in pKVM.

I don't mind either way, my main goal was to get exclude/include guest tracing working for normal VMs. For pKVM I don't have a strong opinion.

Unless someone has strong opinions about making this work in protected mode, I am happy to see tracing support limited to the 'normal' nVHE configuration. The protected feature as a whole is just baggage until upstream support is completed.

Signature · 2 lines
Thanks,
Oliver
maintainer
#9 lore raw

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 5 lines · Oliver Upton

Unless someone has strong opinions about making this work in protected mode, I am happy to see tracing support limited to the 'normal' nVHE configuration. The protected feature as a whole is just baggage until upstream support is completed.

Limiting tracing to non-protected mode is a must IMO. Allowing tracing when pKVM is enabled is a sure way to expose secrets that should stay... secret. The only exception I can think of is when CONFIG_NVHE_EL2_DEBUG is enabled, at which point all bets are off.

Thanks,

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

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 8 lines · Marc Zyngier
Quoted 5 lines · Oliver Upton

Unless someone has strong opinions about making this work in protected mode, I am happy to see tracing support limited to the 'normal' nVHE configuration. The protected feature as a whole is just baggage until upstream support is completed.

Quoted 4 lines

Limiting tracing to non-protected mode is a must IMO. Allowing tracing when pKVM is enabled is a sure way to expose secrets that should stay... secret. The only exception I can think of is when CONFIG_NVHE_EL2_DEBUG is enabled, at which point all bets are off.

Zero argument there :) I left off the "and PMU" part of what I was saying, because that was a feature that semi-worked in protected mode before VM/VCPU shadowing support landed.

Signature · 2 lines
Thanks,
Oliver
maintainer
#11 lore raw

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 10 lines · Oliver Upton
Quoted 8 lines · Marc Zyngier
Quoted 5 lines · Oliver Upton

Unless someone has strong opinions about making this work in protected mode, I am happy to see tracing support limited to the 'normal' nVHE configuration. The protected feature as a whole is just baggage until upstream support is completed.

Quoted 8 lines
Quoted 4 lines

Limiting tracing to non-protected mode is a must IMO. Allowing tracing when pKVM is enabled is a sure way to expose secrets that should stay... secret. The only exception I can think of is when CONFIG_NVHE_EL2_DEBUG is enabled, at which point all bets are off.

Zero argument there :) I left off the "and PMU" part of what I was saying, because that was a feature that semi-worked in protected mode before VM/VCPU shadowing support landed.

Indeed. The goal is that as far as userspace is concerned, the host running in protected mode shouldn't impair the ability to run non-protected VMs, and it should all be hunky-dory, unless you explicitly ask for a protected guest (at which point you are facing a lot of restrictions).

PMU definitely falls into that last bucket, although I would hope that we eventually get some support by context-switching the whole of the PMU state. Don't worry, it's going to be cheap...

Thanks,

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

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

In reply to Oliver Upton
Quoted 9 lines · Oliver Upton
Quoted 8 lines · Marc Zyngier
Quoted 5 lines · Oliver Upton

Unless someone has strong opinions about making this work in protected mode, I am happy to see tracing support limited to the 'normal' nVHE configuration. The protected feature as a whole is just baggage until upstream support is completed.

Quoted 9 lines
Quoted 4 lines

Limiting tracing to non-protected mode is a must IMO. Allowing tracing when pKVM is enabled is a sure way to expose secrets that should stay... secret. The only exception I can think of is when CONFIG_NVHE_EL2_DEBUG is enabled, at which point all bets are off.

Zero argument there :) I left off the "and PMU" part of what I was saying, because that was a feature that semi-worked in protected mode before VM/VCPU shadowing support landed.

In that case I can hide all this behind CONFIG_NVHE_EL2_DEBUG for pKVM. This will also have the effect of disabling PMU again for pKVM because I moved that into this new shared area.

The same place will be used to store the state for normal nVHE and at least then there is some code re-use and flexibility to use trace and PMU for debugging if needed. And the copy on every switch gets deleted.

maintainer
#13 lore raw

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 16 lines · James Clark
Quoted 14 lines · Oliver Upton
Quoted 12 lines · Marc Zyngier
Quoted 5 lines · Oliver Upton

Unless someone has strong opinions about making this work in protected mode, I am happy to see tracing support limited to the 'normal' nVHE configuration. The protected feature as a whole is just baggage until upstream support is completed.

Limiting tracing to non-protected mode is a must IMO. Allowing tracing when pKVM is enabled is a sure way to expose secrets that should stay... secret. The only exception I can think of is when CONFIG_NVHE_EL2_DEBUG is enabled, at which point all bets are off.

Quoted 8 lines
Quoted 4 lines

Zero argument there :) I left off the "and PMU" part of what I was saying, because that was a feature that semi-worked in protected mode before VM/VCPU shadowing support landed.

In that case I can hide all this behind CONFIG_NVHE_EL2_DEBUG for pKVM. This will also have the effect of disabling PMU again for pKVM because I moved that into this new shared area.

I'm not sure what you have in mind, but dropping PMU support for non-protected guests when protected-mode is enabled is not an acceptable outcome.

Hiding the trace behind a debug option is fine as this is a global setting that has no userspace impact, but impacting guests isn't.

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

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 27 lines · Marc Zyngier
Quoted 16 lines · James Clark
Quoted 14 lines · Oliver Upton
Quoted 12 lines · Marc Zyngier
Quoted 5 lines · Oliver Upton

Unless someone has strong opinions about making this work in protected mode, I am happy to see tracing support limited to the 'normal' nVHE configuration. The protected feature as a whole is just baggage until upstream support is completed.

Limiting tracing to non-protected mode is a must IMO. Allowing tracing when pKVM is enabled is a sure way to expose secrets that should stay... secret. The only exception I can think of is when CONFIG_NVHE_EL2_DEBUG is enabled, at which point all bets are off.

Quoted 8 lines
Quoted 4 lines

Zero argument there :) I left off the "and PMU" part of what I was saying, because that was a feature that semi-worked in protected mode before VM/VCPU shadowing support landed.

In that case I can hide all this behind CONFIG_NVHE_EL2_DEBUG for pKVM. This will also have the effect of disabling PMU again for pKVM because I moved that into this new shared area.

Quoted 9 lines

I'm not sure what you have in mind, but dropping PMU support for non-protected guests when protected-mode is enabled is not an acceptable outcome.

Hiding the trace behind a debug option is fine as this is a global setting that has no userspace impact, but impacting guests isn't.

	M.

Hmmm in that case if there's currently no way to distinguish between normal VMs and pVMs in protected-mode then what I was thinking of probably won't work.

I'll actually just leave PMU as it is and only have tracing disabled in protected-mode.

My only question now is whether to:

* Keep this new shared area and use it for both PMU and trace status (well, for PMU only in protected mode as trace would always be disabled and doesn't actually need any state)

* Delete patch 2, add a new normal per-cpu struct just for trace status that's only used in non-protected mode and revert to copying the PMU status into the vCPU on guest switch as it was previously.

maintainer
#15 lore raw

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 38 lines · James Clark
Quoted 34 lines · Marc Zyngier
Quoted 16 lines · James Clark
Quoted 14 lines · Oliver Upton
Quoted 12 lines · Marc Zyngier
Quoted 5 lines · Oliver Upton

Unless someone has strong opinions about making this work in protected mode, I am happy to see tracing support limited to the 'normal' nVHE configuration. The protected feature as a whole is just baggage until upstream support is completed.

Limiting tracing to non-protected mode is a must IMO. Allowing tracing when pKVM is enabled is a sure way to expose secrets that should stay... secret. The only exception I can think of is when CONFIG_NVHE_EL2_DEBUG is enabled, at which point all bets are off.

Quoted 8 lines
Quoted 4 lines

Zero argument there :) I left off the "and PMU" part of what I was saying, because that was a feature that semi-worked in protected mode before VM/VCPU shadowing support landed.

In that case I can hide all this behind CONFIG_NVHE_EL2_DEBUG for pKVM. This will also have the effect of disabling PMU again for pKVM because I moved that into this new shared area.

I'm not sure what you have in mind, but dropping PMU support for non-protected guests when protected-mode is enabled is not an acceptable outcome.

Hiding the trace behind a debug option is fine as this is a global setting that has no userspace impact, but impacting guests isn't.

Quoted 6 lines
Quoted 2 lines
	M.

Hmmm in that case if there's currently no way to distinguish between normal VMs and pVMs in protected-mode then what I was thinking of probably won't work.

Have you looked? kvm_vm_is_protected() has been in for a while, even if that's not a lot. The upcoming code will flesh this helper out,

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

Re: [PATCH v4 2/7] arm64: KVM: Use shared area to pass PMU event state to hypervisor

Quoted 9 lines · Marc Zyngier
Quoted 4 lines · James Clark

Hmmm in that case if there's currently no way to distinguish between normal VMs and pVMs in protected-mode then what I was thinking of probably won't work.

Have you looked? kvm_vm_is_protected() has been in for a while, even if that's not a lot. The upcoming code will flesh this helper out,

Blame me for the bad intel. What I was mentioning earlier is that (1) we use the hyp's shadowed vCPUs when running in protected mode and (2) we don't sync PMU state into the shadow vCPU. So really PMU support for non-protected guests has been broken since commit be66e67f1750 ("KVM:
arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()").

Fixing PMU support for non-protected guests implies the hypervisor will conditionally trust data coming from the host based on the type of VM that it is running.

For protected guests the hypervisor will need a private location to do save/restore of the enable regs since I'm certain we will not trust whatever the host tells us in these circumstances.

Both of these reasons has me feeling like the PMU context still needs to be associated with the vCPU, though the tracing stuff can be percpu.

Signature · 2 lines
Thanks,
Oliver
#17 lore raw

PATCH v4 3/7 arm64/sysreg/tools: Move TRFCR definitions to sysreg

In reply to James Clark

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.

Unfortunately, to avoid breaking the Perf build with duplicate definition errors, the tools copy of the sysreg.h header needs to be updated at the same time rather than the usual second commit. This is because the generated version of sysreg (arch/arm64/include/generated/asm/sysreg-defs.h), is currently shared between the kernel and tools and not copied.

Because the new version of sysreg.h includes kasan-tags.h, that file also now needs to be copied into tools.

---
 arch/arm64/include/asm/sysreg.h       |  12 -
 arch/arm64/tools/sysreg               |  41 +++
 tools/arch/arm64/include/asm/sysreg.h | 345 ++++++++++++++++++++++++--
 tools/include/linux/kasan-tags.h      |  15 ++
 4 files changed, 387 insertions(+), 26 deletions(-)
 create mode 100644 tools/include/linux/kasan-tags.h
arch/arm64/include/asm/sysreg.h +0 -12
index 5e65f51c10d2..92dfb41af018 100644
@@ -280,8 +280,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)
@@ -499,7 +497,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_VNCR_EL2			sys_reg(3, 4, 2, 2, 0)
 #define SYS_HAFGRTR_EL2			sys_reg(3, 4, 3, 1, 6)
 #define SYS_SPSR_EL2			sys_reg(3, 4, 4, 0, 0)
@@ -949,15 +946,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

tools/arch/arm64/include/asm/sysreg.h +331 -14

462 lines not on this page · raw message

tools/include/linux/kasan-tags.h new file +15 -0
new file mode 100644
index 000000000000..4f85f562512c
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_KASAN_TAGS_H
+#define _LINUX_KASAN_TAGS_H
+
+#define KASAN_TAG_KERNEL	0xFF /* native kernel pointers tag */
+#define KASAN_TAG_INVALID	0xFE /* inaccessible memory tag */
+#define KASAN_TAG_MAX		0xFD /* maximum value for random tags */
+
+#ifdef CONFIG_KASAN_HW_TAGS
+#define KASAN_TAG_MIN		0xF0 /* minimum value for random tags */
+#else
+#define KASAN_TAG_MIN		0x00 /* minimum value for random tags */
+#endif
+
+#endif /* LINUX_KASAN_TAGS_H */
Signature · 1 line
2.34.1
maintainer
#18 lore raw

Re: [PATCH v4 3/7] arm64/sysreg/tools: Move TRFCR definitions to sysreg

Hi James

Quoted 12 lines · James Clark

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.

Unfortunately, to avoid breaking the Perf build with duplicate definition errors, the tools copy of the sysreg.h header needs to be updated at the same time rather than the usual second commit. This is because the generated version of sysreg (arch/arm64/include/generated/asm/sysreg-defs.h), is currently shared between the kernel and tools and not copied.

Quoted 2 lines

Because the new version of sysreg.h includes kasan-tags.h, that file also now needs to be copied into tools.

Is it possible to split the patch such that, tools/sysreg is updated to match the existing kernel headers first and then add the TRFCR changes ? This looks like a lot of unrelated changes.

Suzuki

Quoted 597 lines
---
  arch/arm64/include/asm/sysreg.h       |  12 -
  arch/arm64/tools/sysreg               |  41 +++
  tools/arch/arm64/include/asm/sysreg.h | 345 ++++++++++++++++++++++++--
  tools/include/linux/kasan-tags.h      |  15 ++
  4 files changed, 387 insertions(+), 26 deletions(-)

create mode 100644 tools/include/linux/kasan-tags.h

arch/arm64/include/asm/sysreg.h +0 -12
index 5e65f51c10d2..92dfb41af018 100644
@@ -280,8 +280,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)
@@ -499,7 +497,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_VNCR_EL2			sys_reg(3, 4, 2, 2, 0)
  #define SYS_HAFGRTR_EL2			sys_reg(3, 4, 3, 1, 6)
  #define SYS_SPSR_EL2			sys_reg(3, 4, 4, 0, 0)
@@ -949,15 +946,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
index 96cbeeab4eec..8fe23eac910f 100644
@@ -2634,3 +2634,44 @@ 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
+
+Sysreg	TRFCR_EL12	3	5	1	2	1
+Fields	TRFCR_ELx
+EndSysreg
tools/arch/arm64/include/asm/sysreg.h +331 -14
index ccc13e991376..92dfb41af018 100644
@@ -11,6 +11,7 @@
 
  #include <linux/bits.h>
  #include <linux/stringify.h>
+#include <linux/kasan-tags.h>
 
  #include <asm/gpr-num.h>
 
@@ -123,6 +124,37 @@
  #define SYS_DC_CIGSW			sys_insn(1, 0, 7, 14, 4)
  #define SYS_DC_CIGDSW			sys_insn(1, 0, 7, 14, 6)
 
+#define SYS_IC_IALLUIS			sys_insn(1, 0, 7, 1, 0)
+#define SYS_IC_IALLU			sys_insn(1, 0, 7, 5, 0)
+#define SYS_IC_IVAU			sys_insn(1, 3, 7, 5, 1)
+
+#define SYS_DC_IVAC			sys_insn(1, 0, 7, 6, 1)
+#define SYS_DC_IGVAC			sys_insn(1, 0, 7, 6, 3)
+#define SYS_DC_IGDVAC			sys_insn(1, 0, 7, 6, 5)
+
+#define SYS_DC_CVAC			sys_insn(1, 3, 7, 10, 1)
+#define SYS_DC_CGVAC			sys_insn(1, 3, 7, 10, 3)
+#define SYS_DC_CGDVAC			sys_insn(1, 3, 7, 10, 5)
+
+#define SYS_DC_CVAU			sys_insn(1, 3, 7, 11, 1)
+
+#define SYS_DC_CVAP			sys_insn(1, 3, 7, 12, 1)
+#define SYS_DC_CGVAP			sys_insn(1, 3, 7, 12, 3)
+#define SYS_DC_CGDVAP			sys_insn(1, 3, 7, 12, 5)
+
+#define SYS_DC_CVADP			sys_insn(1, 3, 7, 13, 1)
+#define SYS_DC_CGVADP			sys_insn(1, 3, 7, 13, 3)
+#define SYS_DC_CGDVADP			sys_insn(1, 3, 7, 13, 5)
+
+#define SYS_DC_CIVAC			sys_insn(1, 3, 7, 14, 1)
+#define SYS_DC_CIGVAC			sys_insn(1, 3, 7, 14, 3)
+#define SYS_DC_CIGDVAC			sys_insn(1, 3, 7, 14, 5)
+
+/* Data cache zero operations */
+#define SYS_DC_ZVA			sys_insn(1, 3, 7, 4, 1)
+#define SYS_DC_GVA			sys_insn(1, 3, 7, 4, 3)
+#define SYS_DC_GZVA			sys_insn(1, 3, 7, 4, 4)
+
  /*
   * Automatically generated definitions for system registers, the
   * manual encodings below are in the process of being converted to
@@ -162,6 +194,84 @@
  #define SYS_DBGDTRTX_EL0		sys_reg(2, 3, 0, 5, 0)
  #define SYS_DBGVCR32_EL2		sys_reg(2, 4, 0, 7, 0)
 
+#define SYS_BRBINF_EL1(n)		sys_reg(2, 1, 8, (n & 15), (((n & 16) >> 2) | 0))
+#define SYS_BRBINFINJ_EL1		sys_reg(2, 1, 9, 1, 0)
+#define SYS_BRBSRC_EL1(n)		sys_reg(2, 1, 8, (n & 15), (((n & 16) >> 2) | 1))
+#define SYS_BRBSRCINJ_EL1		sys_reg(2, 1, 9, 1, 1)
+#define SYS_BRBTGT_EL1(n)		sys_reg(2, 1, 8, (n & 15), (((n & 16) >> 2) | 2))
+#define SYS_BRBTGTINJ_EL1		sys_reg(2, 1, 9, 1, 2)
+#define SYS_BRBTS_EL1			sys_reg(2, 1, 9, 0, 2)
+
+#define SYS_BRBCR_EL1			sys_reg(2, 1, 9, 0, 0)
+#define SYS_BRBFCR_EL1			sys_reg(2, 1, 9, 0, 1)
+#define SYS_BRBIDR0_EL1			sys_reg(2, 1, 9, 2, 0)
+
+#define SYS_TRCITECR_EL1		sys_reg(3, 0, 1, 2, 3)
+#define SYS_TRCACATR(m)			sys_reg(2, 1, 2, ((m & 7) << 1), (2 | (m >> 3)))
+#define SYS_TRCACVR(m)			sys_reg(2, 1, 2, ((m & 7) << 1), (0 | (m >> 3)))
+#define SYS_TRCAUTHSTATUS		sys_reg(2, 1, 7, 14, 6)
+#define SYS_TRCAUXCTLR			sys_reg(2, 1, 0, 6, 0)
+#define SYS_TRCBBCTLR			sys_reg(2, 1, 0, 15, 0)
+#define SYS_TRCCCCTLR			sys_reg(2, 1, 0, 14, 0)
+#define SYS_TRCCIDCCTLR0		sys_reg(2, 1, 3, 0, 2)
+#define SYS_TRCCIDCCTLR1		sys_reg(2, 1, 3, 1, 2)
+#define SYS_TRCCIDCVR(m)		sys_reg(2, 1, 3, ((m & 7) << 1), 0)
+#define SYS_TRCCLAIMCLR			sys_reg(2, 1, 7, 9, 6)
+#define SYS_TRCCLAIMSET			sys_reg(2, 1, 7, 8, 6)
+#define SYS_TRCCNTCTLR(m)		sys_reg(2, 1, 0, (4 | (m & 3)), 5)
+#define SYS_TRCCNTRLDVR(m)		sys_reg(2, 1, 0, (0 | (m & 3)), 5)
+#define SYS_TRCCNTVR(m)			sys_reg(2, 1, 0, (8 | (m & 3)), 5)
+#define SYS_TRCCONFIGR			sys_reg(2, 1, 0, 4, 0)
+#define SYS_TRCDEVARCH			sys_reg(2, 1, 7, 15, 6)
+#define SYS_TRCDEVID			sys_reg(2, 1, 7, 2, 7)
+#define SYS_TRCEVENTCTL0R		sys_reg(2, 1, 0, 8, 0)
+#define SYS_TRCEVENTCTL1R		sys_reg(2, 1, 0, 9, 0)
+#define SYS_TRCEXTINSELR(m)		sys_reg(2, 1, 0, (8 | (m & 3)), 4)
+#define SYS_TRCIDR0			sys_reg(2, 1, 0, 8, 7)
+#define SYS_TRCIDR10			sys_reg(2, 1, 0, 2, 6)
+#define SYS_TRCIDR11			sys_reg(2, 1, 0, 3, 6)
+#define SYS_TRCIDR12			sys_reg(2, 1, 0, 4, 6)
+#define SYS_TRCIDR13			sys_reg(2, 1, 0, 5, 6)
+#define SYS_TRCIDR1			sys_reg(2, 1, 0, 9, 7)
+#define SYS_TRCIDR2			sys_reg(2, 1, 0, 10, 7)
+#define SYS_TRCIDR3			sys_reg(2, 1, 0, 11, 7)
+#define SYS_TRCIDR4			sys_reg(2, 1, 0, 12, 7)
+#define SYS_TRCIDR5			sys_reg(2, 1, 0, 13, 7)
+#define SYS_TRCIDR6			sys_reg(2, 1, 0, 14, 7)
+#define SYS_TRCIDR7			sys_reg(2, 1, 0, 15, 7)
+#define SYS_TRCIDR8			sys_reg(2, 1, 0, 0, 6)
+#define SYS_TRCIDR9			sys_reg(2, 1, 0, 1, 6)
+#define SYS_TRCIMSPEC(m)		sys_reg(2, 1, 0, (m & 7), 7)
+#define SYS_TRCITEEDCR			sys_reg(2, 1, 0, 2, 1)
+#define SYS_TRCOSLSR			sys_reg(2, 1, 1, 1, 4)
+#define SYS_TRCPRGCTLR			sys_reg(2, 1, 0, 1, 0)
+#define SYS_TRCQCTLR			sys_reg(2, 1, 0, 1, 1)
+#define SYS_TRCRSCTLR(m)		sys_reg(2, 1, 1, (m & 15), (0 | (m >> 4)))
+#define SYS_TRCRSR			sys_reg(2, 1, 0, 10, 0)
+#define SYS_TRCSEQEVR(m)		sys_reg(2, 1, 0, (m & 3), 4)
+#define SYS_TRCSEQRSTEVR		sys_reg(2, 1, 0, 6, 4)
+#define SYS_TRCSEQSTR			sys_reg(2, 1, 0, 7, 4)
+#define SYS_TRCSSCCR(m)			sys_reg(2, 1, 1, (m & 7), 2)
+#define SYS_TRCSSCSR(m)			sys_reg(2, 1, 1, (8 | (m & 7)), 2)
+#define SYS_TRCSSPCICR(m)		sys_reg(2, 1, 1, (m & 7), 3)
+#define SYS_TRCSTALLCTLR		sys_reg(2, 1, 0, 11, 0)
+#define SYS_TRCSTATR			sys_reg(2, 1, 0, 3, 0)
+#define SYS_TRCSYNCPR			sys_reg(2, 1, 0, 13, 0)
+#define SYS_TRCTRACEIDR			sys_reg(2, 1, 0, 0, 1)
+#define SYS_TRCTSCTLR			sys_reg(2, 1, 0, 12, 0)
+#define SYS_TRCVICTLR			sys_reg(2, 1, 0, 0, 2)
+#define SYS_TRCVIIECTLR			sys_reg(2, 1, 0, 1, 2)
+#define SYS_TRCVIPCSSCTLR		sys_reg(2, 1, 0, 3, 2)
+#define SYS_TRCVISSCTLR			sys_reg(2, 1, 0, 2, 2)
+#define SYS_TRCVMIDCCTLR0		sys_reg(2, 1, 3, 2, 2)
+#define SYS_TRCVMIDCCTLR1		sys_reg(2, 1, 3, 3, 2)
+#define SYS_TRCVMIDCVR(m)		sys_reg(2, 1, 3, ((m & 7) << 1), 1)
+
+/* ETM */
+#define SYS_TRCOSLAR			sys_reg(2, 1, 1, 0, 4)
+
+#define SYS_BRBCR_EL2			sys_reg(2, 4, 9, 0, 0)
+
  #define SYS_MIDR_EL1			sys_reg(3, 0, 0, 0, 0)
  #define SYS_MPIDR_EL1			sys_reg(3, 0, 0, 0, 5)
  #define SYS_REVIDR_EL1			sys_reg(3, 0, 0, 0, 6)
@@ -170,8 +280,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)
@@ -202,8 +310,13 @@
  #define SYS_ERXCTLR_EL1			sys_reg(3, 0, 5, 4, 1)
  #define SYS_ERXSTATUS_EL1		sys_reg(3, 0, 5, 4, 2)
  #define SYS_ERXADDR_EL1			sys_reg(3, 0, 5, 4, 3)
+#define SYS_ERXPFGF_EL1			sys_reg(3, 0, 5, 4, 4)
+#define SYS_ERXPFGCTL_EL1		sys_reg(3, 0, 5, 4, 5)
+#define SYS_ERXPFGCDN_EL1		sys_reg(3, 0, 5, 4, 6)
  #define SYS_ERXMISC0_EL1		sys_reg(3, 0, 5, 5, 0)
  #define SYS_ERXMISC1_EL1		sys_reg(3, 0, 5, 5, 1)
+#define SYS_ERXMISC2_EL1		sys_reg(3, 0, 5, 5, 2)
+#define SYS_ERXMISC3_EL1		sys_reg(3, 0, 5, 5, 3)
  #define SYS_TFSR_EL1			sys_reg(3, 0, 5, 6, 0)
  #define SYS_TFSRE0_EL1			sys_reg(3, 0, 5, 6, 1)
 
@@ -274,6 +387,8 @@
  #define SYS_ICC_IGRPEN0_EL1		sys_reg(3, 0, 12, 12, 6)
  #define SYS_ICC_IGRPEN1_EL1		sys_reg(3, 0, 12, 12, 7)
 
+#define SYS_ACCDATA_EL1			sys_reg(3, 0, 13, 0, 5)
+
  #define SYS_CNTKCTL_EL1			sys_reg(3, 0, 14, 1, 0)
 
  #define SYS_AIDR_EL1			sys_reg(3, 1, 0, 0, 7)
@@ -369,6 +484,7 @@
 
  #define SYS_SCTLR_EL2			sys_reg(3, 4, 1, 0, 0)
  #define SYS_ACTLR_EL2			sys_reg(3, 4, 1, 0, 1)
+#define SYS_SCTLR2_EL2			sys_reg(3, 4, 1, 0, 3)
  #define SYS_HCR_EL2			sys_reg(3, 4, 1, 1, 0)
  #define SYS_MDCR_EL2			sys_reg(3, 4, 1, 1, 1)
  #define SYS_CPTR_EL2			sys_reg(3, 4, 1, 1, 2)
@@ -381,13 +497,15 @@
  #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_HDFGRTR_EL2			sys_reg(3, 4, 3, 1, 4)
-#define SYS_HDFGWTR_EL2			sys_reg(3, 4, 3, 1, 5)
+#define SYS_VNCR_EL2			sys_reg(3, 4, 2, 2, 0)
  #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)
  #define SYS_SP_EL1			sys_reg(3, 4, 4, 1, 0)
+#define SYS_SPSR_irq			sys_reg(3, 4, 4, 3, 0)
+#define SYS_SPSR_abt			sys_reg(3, 4, 4, 3, 1)
+#define SYS_SPSR_und			sys_reg(3, 4, 4, 3, 2)
+#define SYS_SPSR_fiq			sys_reg(3, 4, 4, 3, 3)
  #define SYS_IFSR32_EL2			sys_reg(3, 4, 5, 0, 1)
  #define SYS_AFSR0_EL2			sys_reg(3, 4, 5, 1, 0)
  #define SYS_AFSR1_EL2			sys_reg(3, 4, 5, 1, 1)
@@ -401,6 +519,18 @@
 
  #define SYS_MAIR_EL2			sys_reg(3, 4, 10, 2, 0)
  #define SYS_AMAIR_EL2			sys_reg(3, 4, 10, 3, 0)
+#define SYS_MPAMHCR_EL2			sys_reg(3, 4, 10, 4, 0)
+#define SYS_MPAMVPMV_EL2		sys_reg(3, 4, 10, 4, 1)
+#define SYS_MPAM2_EL2			sys_reg(3, 4, 10, 5, 0)
+#define __SYS__MPAMVPMx_EL2(x)		sys_reg(3, 4, 10, 6, x)
+#define SYS_MPAMVPM0_EL2		__SYS__MPAMVPMx_EL2(0)
+#define SYS_MPAMVPM1_EL2		__SYS__MPAMVPMx_EL2(1)
+#define SYS_MPAMVPM2_EL2		__SYS__MPAMVPMx_EL2(2)
+#define SYS_MPAMVPM3_EL2		__SYS__MPAMVPMx_EL2(3)
+#define SYS_MPAMVPM4_EL2		__SYS__MPAMVPMx_EL2(4)
+#define SYS_MPAMVPM5_EL2		__SYS__MPAMVPMx_EL2(5)
+#define SYS_MPAMVPM6_EL2		__SYS__MPAMVPMx_EL2(6)
+#define SYS_MPAMVPM7_EL2		__SYS__MPAMVPMx_EL2(7)
 
  #define SYS_VBAR_EL2			sys_reg(3, 4, 12, 0, 0)
  #define SYS_RVBAR_EL2			sys_reg(3, 4, 12, 0, 1)
@@ -449,24 +579,49 @@
 
  #define SYS_CONTEXTIDR_EL2		sys_reg(3, 4, 13, 0, 1)
  #define SYS_TPIDR_EL2			sys_reg(3, 4, 13, 0, 2)
+#define SYS_SCXTNUM_EL2			sys_reg(3, 4, 13, 0, 7)
+
+#define __AMEV_op2(m)			(m & 0x7)
+#define __AMEV_CRm(n, m)		(n | ((m & 0x8) >> 3))
+#define __SYS__AMEVCNTVOFF0n_EL2(m)	sys_reg(3, 4, 13, __AMEV_CRm(0x8, m), __AMEV_op2(m))
+#define SYS_AMEVCNTVOFF0n_EL2(m)	__SYS__AMEVCNTVOFF0n_EL2(m)
+#define __SYS__AMEVCNTVOFF1n_EL2(m)	sys_reg(3, 4, 13, __AMEV_CRm(0xA, m), __AMEV_op2(m))
+#define SYS_AMEVCNTVOFF1n_EL2(m)	__SYS__AMEVCNTVOFF1n_EL2(m)
 
  #define SYS_CNTVOFF_EL2			sys_reg(3, 4, 14, 0, 3)
  #define SYS_CNTHCTL_EL2			sys_reg(3, 4, 14, 1, 0)
+#define SYS_CNTHP_TVAL_EL2		sys_reg(3, 4, 14, 2, 0)
+#define SYS_CNTHP_CTL_EL2		sys_reg(3, 4, 14, 2, 1)
+#define SYS_CNTHP_CVAL_EL2		sys_reg(3, 4, 14, 2, 2)
+#define SYS_CNTHV_TVAL_EL2		sys_reg(3, 4, 14, 3, 0)
+#define SYS_CNTHV_CTL_EL2		sys_reg(3, 4, 14, 3, 1)
+#define SYS_CNTHV_CVAL_EL2		sys_reg(3, 4, 14, 3, 2)
 
  /* VHE encodings for architectural EL0/1 system registers */
+#define SYS_BRBCR_EL12			sys_reg(2, 5, 9, 0, 0)
  #define SYS_SCTLR_EL12			sys_reg(3, 5, 1, 0, 0)
+#define SYS_CPACR_EL12			sys_reg(3, 5, 1, 0, 2)
+#define SYS_SCTLR2_EL12			sys_reg(3, 5, 1, 0, 3)
+#define SYS_ZCR_EL12			sys_reg(3, 5, 1, 2, 0)
+#define SYS_TRFCR_EL12			sys_reg(3, 5, 1, 2, 1)
+#define SYS_SMCR_EL12			sys_reg(3, 5, 1, 2, 6)
  #define SYS_TTBR0_EL12			sys_reg(3, 5, 2, 0, 0)
  #define SYS_TTBR1_EL12			sys_reg(3, 5, 2, 0, 1)
  #define SYS_TCR_EL12			sys_reg(3, 5, 2, 0, 2)
+#define SYS_TCR2_EL12			sys_reg(3, 5, 2, 0, 3)
  #define SYS_SPSR_EL12			sys_reg(3, 5, 4, 0, 0)
  #define SYS_ELR_EL12			sys_reg(3, 5, 4, 0, 1)
  #define SYS_AFSR0_EL12			sys_reg(3, 5, 5, 1, 0)
  #define SYS_AFSR1_EL12			sys_reg(3, 5, 5, 1, 1)
  #define SYS_ESR_EL12			sys_reg(3, 5, 5, 2, 0)
  #define SYS_TFSR_EL12			sys_reg(3, 5, 5, 6, 0)
+#define SYS_FAR_EL12			sys_reg(3, 5, 6, 0, 0)
+#define SYS_PMSCR_EL12			sys_reg(3, 5, 9, 9, 0)
  #define SYS_MAIR_EL12			sys_reg(3, 5, 10, 2, 0)
  #define SYS_AMAIR_EL12			sys_reg(3, 5, 10, 3, 0)
  #define SYS_VBAR_EL12			sys_reg(3, 5, 12, 0, 0)
+#define SYS_CONTEXTIDR_EL12		sys_reg(3, 5, 13, 0, 1)
+#define SYS_SCXTNUM_EL12		sys_reg(3, 5, 13, 0, 7)
  #define SYS_CNTKCTL_EL12		sys_reg(3, 5, 14, 1, 0)
  #define SYS_CNTP_TVAL_EL02		sys_reg(3, 5, 14, 2, 0)
  #define SYS_CNTP_CTL_EL02		sys_reg(3, 5, 14, 2, 1)
@@ -477,6 +632,158 @@
 
  #define SYS_SP_EL2			sys_reg(3, 6,  4, 1, 0)
 
+/* AT instructions */
+#define AT_Op0 1
+#define AT_CRn 7
+
+#define OP_AT_S1E1R	sys_insn(AT_Op0, 0, AT_CRn, 8, 0)
+#define OP_AT_S1E1W	sys_insn(AT_Op0, 0, AT_CRn, 8, 1)
+#define OP_AT_S1E0R	sys_insn(AT_Op0, 0, AT_CRn, 8, 2)
+#define OP_AT_S1E0W	sys_insn(AT_Op0, 0, AT_CRn, 8, 3)
+#define OP_AT_S1E1RP	sys_insn(AT_Op0, 0, AT_CRn, 9, 0)
+#define OP_AT_S1E1WP	sys_insn(AT_Op0, 0, AT_CRn, 9, 1)
+#define OP_AT_S1E2R	sys_insn(AT_Op0, 4, AT_CRn, 8, 0)
+#define OP_AT_S1E2W	sys_insn(AT_Op0, 4, AT_CRn, 8, 1)
+#define OP_AT_S12E1R	sys_insn(AT_Op0, 4, AT_CRn, 8, 4)
+#define OP_AT_S12E1W	sys_insn(AT_Op0, 4, AT_CRn, 8, 5)
+#define OP_AT_S12E0R	sys_insn(AT_Op0, 4, AT_CRn, 8, 6)
+#define OP_AT_S12E0W	sys_insn(AT_Op0, 4, AT_CRn, 8, 7)
+
+/* TLBI instructions */
+#define OP_TLBI_VMALLE1OS		sys_insn(1, 0, 8, 1, 0)
+#define OP_TLBI_VAE1OS			sys_insn(1, 0, 8, 1, 1)
+#define OP_TLBI_ASIDE1OS		sys_insn(1, 0, 8, 1, 2)
+#define OP_TLBI_VAAE1OS			sys_insn(1, 0, 8, 1, 3)
+#define OP_TLBI_VALE1OS			sys_insn(1, 0, 8, 1, 5)
+#define OP_TLBI_VAALE1OS		sys_insn(1, 0, 8, 1, 7)
+#define OP_TLBI_RVAE1IS			sys_insn(1, 0, 8, 2, 1)
+#define OP_TLBI_RVAAE1IS		sys_insn(1, 0, 8, 2, 3)
+#define OP_TLBI_RVALE1IS		sys_insn(1, 0, 8, 2, 5)
+#define OP_TLBI_RVAALE1IS		sys_insn(1, 0, 8, 2, 7)
+#define OP_TLBI_VMALLE1IS		sys_insn(1, 0, 8, 3, 0)
+#define OP_TLBI_VAE1IS			sys_insn(1, 0, 8, 3, 1)
+#define OP_TLBI_ASIDE1IS		sys_insn(1, 0, 8, 3, 2)
+#define OP_TLBI_VAAE1IS			sys_insn(1, 0, 8, 3, 3)
+#define OP_TLBI_VALE1IS			sys_insn(1, 0, 8, 3, 5)
+#define OP_TLBI_VAALE1IS		sys_insn(1, 0, 8, 3, 7)
+#define OP_TLBI_RVAE1OS			sys_insn(1, 0, 8, 5, 1)
+#define OP_TLBI_RVAAE1OS		sys_insn(1, 0, 8, 5, 3)
+#define OP_TLBI_RVALE1OS		sys_insn(1, 0, 8, 5, 5)
+#define OP_TLBI_RVAALE1OS		sys_insn(1, 0, 8, 5, 7)
+#define OP_TLBI_RVAE1			sys_insn(1, 0, 8, 6, 1)
+#define OP_TLBI_RVAAE1			sys_insn(1, 0, 8, 6, 3)
+#define OP_TLBI_RVALE1			sys_insn(1, 0, 8, 6, 5)
+#define OP_TLBI_RVAALE1			sys_insn(1, 0, 8, 6, 7)
+#define OP_TLBI_VMALLE1			sys_insn(1, 0, 8, 7, 0)
+#define OP_TLBI_VAE1			sys_insn(1, 0, 8, 7, 1)
+#define OP_TLBI_ASIDE1			sys_insn(1, 0, 8, 7, 2)
+#define OP_TLBI_VAAE1			sys_insn(1, 0, 8, 7, 3)
+#define OP_TLBI_VALE1			sys_insn(1, 0, 8, 7, 5)
+#define OP_TLBI_VAALE1			sys_insn(1, 0, 8, 7, 7)
+#define OP_TLBI_VMALLE1OSNXS		sys_insn(1, 0, 9, 1, 0)
+#define OP_TLBI_VAE1OSNXS		sys_insn(1, 0, 9, 1, 1)
+#define OP_TLBI_ASIDE1OSNXS		sys_insn(1, 0, 9, 1, 2)
+#define OP_TLBI_VAAE1OSNXS		sys_insn(1, 0, 9, 1, 3)
+#define OP_TLBI_VALE1OSNXS		sys_insn(1, 0, 9, 1, 5)
+#define OP_TLBI_VAALE1OSNXS		sys_insn(1, 0, 9, 1, 7)
+#define OP_TLBI_RVAE1ISNXS		sys_insn(1, 0, 9, 2, 1)
+#define OP_TLBI_RVAAE1ISNXS		sys_insn(1, 0, 9, 2, 3)
+#define OP_TLBI_RVALE1ISNXS		sys_insn(1, 0, 9, 2, 5)
+#define OP_TLBI_RVAALE1ISNXS		sys_insn(1, 0, 9, 2, 7)
+#define OP_TLBI_VMALLE1ISNXS		sys_insn(1, 0, 9, 3, 0)
+#define OP_TLBI_VAE1ISNXS		sys_insn(1, 0, 9, 3, 1)
+#define OP_TLBI_ASIDE1ISNXS		sys_insn(1, 0, 9, 3, 2)
+#define OP_TLBI_VAAE1ISNXS		sys_insn(1, 0, 9, 3, 3)
+#define OP_TLBI_VALE1ISNXS		sys_insn(1, 0, 9, 3, 5)
+#define OP_TLBI_VAALE1ISNXS		sys_insn(1, 0, 9, 3, 7)
+#define OP_TLBI_RVAE1OSNXS		sys_insn(1, 0, 9, 5, 1)
+#define OP_TLBI_RVAAE1OSNXS		sys_insn(1, 0, 9, 5, 3)
+#define OP_TLBI_RVALE1OSNXS		sys_insn(1, 0, 9, 5, 5)
+#define OP_TLBI_RVAALE1OSNXS		sys_insn(1, 0, 9, 5, 7)
+#define OP_TLBI_RVAE1NXS		sys_insn(1, 0, 9, 6, 1)
+#define OP_TLBI_RVAAE1NXS		sys_insn(1, 0, 9, 6, 3)
+#define OP_TLBI_RVALE1NXS		sys_insn(1, 0, 9, 6, 5)
+#define OP_TLBI_RVAALE1NXS		sys_insn(1, 0, 9, 6, 7)
+#define OP_TLBI_VMALLE1NXS		sys_insn(1, 0, 9, 7, 0)
+#define OP_TLBI_VAE1NXS			sys_insn(1, 0, 9, 7, 1)
+#define OP_TLBI_ASIDE1NXS		sys_insn(1, 0, 9, 7, 2)
+#define OP_TLBI_VAAE1NXS		sys_insn(1, 0, 9, 7, 3)
+#define OP_TLBI_VALE1NXS		sys_insn(1, 0, 9, 7, 5)
+#define OP_TLBI_VAALE1NXS		sys_insn(1, 0, 9, 7, 7)
+#define OP_TLBI_IPAS2E1IS		sys_insn(1, 4, 8, 0, 1)
+#define OP_TLBI_RIPAS2E1IS		sys_insn(1, 4, 8, 0, 2)
+#define OP_TLBI_IPAS2LE1IS		sys_insn(1, 4, 8, 0, 5)
+#define OP_TLBI_RIPAS2LE1IS		sys_insn(1, 4, 8, 0, 6)
+#define OP_TLBI_ALLE2OS			sys_insn(1, 4, 8, 1, 0)
+#define OP_TLBI_VAE2OS			sys_insn(1, 4, 8, 1, 1)
+#define OP_TLBI_ALLE1OS			sys_insn(1, 4, 8, 1, 4)
+#define OP_TLBI_VALE2OS			sys_insn(1, 4, 8, 1, 5)
+#define OP_TLBI_VMALLS12E1OS		sys_insn(1, 4, 8, 1, 6)
+#define OP_TLBI_RVAE2IS			sys_insn(1, 4, 8, 2, 1)
+#define OP_TLBI_RVALE2IS		sys_insn(1, 4, 8, 2, 5)
+#define OP_TLBI_ALLE2IS			sys_insn(1, 4, 8, 3, 0)
+#define OP_TLBI_VAE2IS			sys_insn(1, 4, 8, 3, 1)
+#define OP_TLBI_ALLE1IS			sys_insn(1, 4, 8, 3, 4)
+#define OP_TLBI_VALE2IS			sys_insn(1, 4, 8, 3, 5)
+#define OP_TLBI_VMALLS12E1IS		sys_insn(1, 4, 8, 3, 6)
+#define OP_TLBI_IPAS2E1OS		sys_insn(1, 4, 8, 4, 0)
+#define OP_TLBI_IPAS2E1			sys_insn(1, 4, 8, 4, 1)
+#define OP_TLBI_RIPAS2E1		sys_insn(1, 4, 8, 4, 2)
+#define OP_TLBI_RIPAS2E1OS		sys_insn(1, 4, 8, 4, 3)
+#define OP_TLBI_IPAS2LE1OS		sys_insn(1, 4, 8, 4, 4)
+#define OP_TLBI_IPAS2LE1		sys_insn(1, 4, 8, 4, 5)
+#define OP_TLBI_RIPAS2LE1		sys_insn(1, 4, 8, 4, 6)
+#define OP_TLBI_RIPAS2LE1OS		sys_insn(1, 4, 8, 4, 7)
+#define OP_TLBI_RVAE2OS			sys_insn(1, 4, 8, 5, 1)
+#define OP_TLBI_RVALE2OS		sys_insn(1, 4, 8, 5, 5)
+#define OP_TLBI_RVAE2			sys_insn(1, 4, 8, 6, 1)
+#define OP_TLBI_RVALE2			sys_insn(1, 4, 8, 6, 5)
+#define OP_TLBI_ALLE2			sys_insn(1, 4, 8, 7, 0)
+#define OP_TLBI_VAE2			sys_insn(1, 4, 8, 7, 1)
+#define OP_TLBI_ALLE1			sys_insn(1, 4, 8, 7, 4)
+#define OP_TLBI_VALE2			sys_insn(1, 4, 8, 7, 5)
+#define OP_TLBI_VMALLS12E1		sys_insn(1, 4, 8, 7, 6)
+#define OP_TLBI_IPAS2E1ISNXS		sys_insn(1, 4, 9, 0, 1)
+#define OP_TLBI_RIPAS2E1ISNXS		sys_insn(1, 4, 9, 0, 2)
+#define OP_TLBI_IPAS2LE1ISNXS		sys_insn(1, 4, 9, 0, 5)
+#define OP_TLBI_RIPAS2LE1ISNXS		sys_insn(1, 4, 9, 0, 6)
+#define OP_TLBI_ALLE2OSNXS		sys_insn(1, 4, 9, 1, 0)
+#define OP_TLBI_VAE2OSNXS		sys_insn(1, 4, 9, 1, 1)
+#define OP_TLBI_ALLE1OSNXS		sys_insn(1, 4, 9, 1, 4)
+#define OP_TLBI_VALE2OSNXS		sys_insn(1, 4, 9, 1, 5)
+#define OP_TLBI_VMALLS12E1OSNXS		sys_insn(1, 4, 9, 1, 6)
+#define OP_TLBI_RVAE2ISNXS		sys_insn(1, 4, 9, 2, 1)
+#define OP_TLBI_RVALE2ISNXS		sys_insn(1, 4, 9, 2, 5)
+#define OP_TLBI_ALLE2ISNXS		sys_insn(1, 4, 9, 3, 0)
+#define OP_TLBI_VAE2ISNXS		sys_insn(1, 4, 9, 3, 1)
+#define OP_TLBI_ALLE1ISNXS		sys_insn(1, 4, 9, 3, 4)
+#define OP_TLBI_VALE2ISNXS		sys_insn(1, 4, 9, 3, 5)
+#define OP_TLBI_VMALLS12E1ISNXS		sys_insn(1, 4, 9, 3, 6)
+#define OP_TLBI_IPAS2E1OSNXS		sys_insn(1, 4, 9, 4, 0)
+#define OP_TLBI_IPAS2E1NXS		sys_insn(1, 4, 9, 4, 1)
+#define OP_TLBI_RIPAS2E1NXS		sys_insn(1, 4, 9, 4, 2)
+#define OP_TLBI_RIPAS2E1OSNXS		sys_insn(1, 4, 9, 4, 3)
+#define OP_TLBI_IPAS2LE1OSNXS		sys_insn(1, 4, 9, 4, 4)
+#define OP_TLBI_IPAS2LE1NXS		sys_insn(1, 4, 9, 4, 5)
+#define OP_TLBI_RIPAS2LE1NXS		sys_insn(1, 4, 9, 4, 6)
+#define OP_TLBI_RIPAS2LE1OSNXS		sys_insn(1, 4, 9, 4, 7)
+#define OP_TLBI_RVAE2OSNXS		sys_insn(1, 4, 9, 5, 1)
+#define OP_TLBI_RVALE2OSNXS		sys_insn(1, 4, 9, 5, 5)
+#define OP_TLBI_RVAE2NXS		sys_insn(1, 4, 9, 6, 1)
+#define OP_TLBI_RVALE2NXS		sys_insn(1, 4, 9, 6, 5)
+#define OP_TLBI_ALLE2NXS		sys_insn(1, 4, 9, 7, 0)
+#define OP_TLBI_VAE2NXS			sys_insn(1, 4, 9, 7, 1)
+#define OP_TLBI_ALLE1NXS		sys_insn(1, 4, 9, 7, 4)
+#define OP_TLBI_VALE2NXS		sys_insn(1, 4, 9, 7, 5)
+#define OP_TLBI_VMALLS12E1NXS		sys_insn(1, 4, 9, 7, 6)
+
+/* Misc instructions */
+#define OP_BRB_IALL			sys_insn(1, 1, 7, 2, 4)
+#define OP_BRB_INJ			sys_insn(1, 1, 7, 2, 5)
+#define OP_CFP_RCTX			sys_insn(1, 3, 7, 3, 4)
+#define OP_DVP_RCTX			sys_insn(1, 3, 7, 3, 5)
+#define OP_CPP_RCTX			sys_insn(1, 3, 7, 3, 7)
+
  /* Common SCTLR_ELx flags. */
  #define SCTLR_ELx_ENTP2	(BIT(60))
  #define SCTLR_ELx_DSSBS	(BIT(44))
@@ -610,6 +917,19 @@
  #define SYS_GCR_EL1_RRND	(BIT(16))
  #define SYS_GCR_EL1_EXCL_MASK	0xffffUL
 
+#ifdef CONFIG_KASAN_HW_TAGS
+/*
+ * KASAN always uses a whole byte for its tags. With CONFIG_KASAN_HW_TAGS it
+ * only uses tags in the range 0xF0-0xFF, which we map to MTE tags 0x0-0xF.
+ */
+#define __MTE_TAG_MIN		(KASAN_TAG_MIN & 0xf)
+#define __MTE_TAG_MAX		(KASAN_TAG_MAX & 0xf)
+#define __MTE_TAG_INCL		GENMASK(__MTE_TAG_MAX, __MTE_TAG_MIN)
+#define KERNEL_GCR_EL1_EXCL	(SYS_GCR_EL1_EXCL_MASK & ~__MTE_TAG_INCL)
+#else
+#define KERNEL_GCR_EL1_EXCL	SYS_GCR_EL1_EXCL_MASK
+#endif
+
  #define KERNEL_GCR_EL1		(SYS_GCR_EL1_RRND | KERNEL_GCR_EL1_EXCL)
 
  /* RGSR_EL1 Definitions */
@@ -626,15 +946,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)
@@ -789,15 +1100,21 @@
  /*
   * For registers without architectural names, or simply unsupported by
   * GAS.
+ *
+ * __check_r forces warnings to be generated by the compiler when
+ * evaluating r which wouldn't normally happen due to being passed to
+ * the assembler via __stringify(r).
   */
  #define read_sysreg_s(r) ({						\
  	u64 __val;							\
+	u32 __maybe_unused __check_r = (u32)(r);			\
  	asm volatile(__mrs_s("%0", r) : "=r" (__val));			\
  	__val;								\
  })
 
  #define write_sysreg_s(v, r) do {					\
  	u64 __val = (u64)(v);						\
+	u32 __maybe_unused __check_r = (u32)(r);			\
  	asm volatile(__msr_s(r, "%x0") : : "rZ" (__val));		\
  } while (0)
 
tools/include/linux/kasan-tags.h new file +15 -0
new file mode 100644
index 000000000000..4f85f562512c
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_KASAN_TAGS_H
+#define _LINUX_KASAN_TAGS_H
+
+#define KASAN_TAG_KERNEL	0xFF /* native kernel pointers tag */
+#define KASAN_TAG_INVALID	0xFE /* inaccessible memory tag */
+#define KASAN_TAG_MAX		0xFD /* maximum value for random tags */
+
+#ifdef CONFIG_KASAN_HW_TAGS
+#define KASAN_TAG_MIN		0xF0 /* minimum value for random tags */
+#else
+#define KASAN_TAG_MIN		0x00 /* minimum value for random tags */
+#endif
+
+#endif /* LINUX_KASAN_TAGS_H */
#19 lore raw

Re: [PATCH v4 3/7] arm64/sysreg/tools: Move TRFCR definitions to sysreg

Quoted 15 lines · Suzuki K Poulose

Hi James

Quoted 12 lines · James Clark

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.

Unfortunately, to avoid breaking the Perf build with duplicate definition errors, the tools copy of the sysreg.h header needs to be updated at the same time rather than the usual second commit. This is because the generated version of sysreg (arch/arm64/include/generated/asm/sysreg-defs.h), is currently shared between the kernel and tools and not copied.

Quoted 10 lines
Quoted 2 lines

Because the new version of sysreg.h includes kasan-tags.h, that file also now needs to be copied into tools.

Is it possible to split the patch such that, tools/sysreg is updated to match the existing kernel headers first and then add the TRFCR changes ? This looks like a lot of unrelated changes.

Suzuki

Yes that's not a bad idea I can split it into two.

Quoted 584 lines
Quoted 582 lines

---
  arch/arm64/include/asm/sysreg.h       |  12 - arch/arm64/tools/sysreg               |  41 +++ tools/arch/arm64/include/asm/sysreg.h | 345 ++++++++++++++++++++++++-- tools/include/linux/kasan-tags.h      |  15 ++

  4 files changed, 387 insertions(+), 26 deletions(-)

  create mode 100644 tools/include/linux/kasan-tags.h

arch/arm64/include/asm/sysreg.h +0 -1
index 5e65f51c10d2..92dfb41af018 100644
@@ -280,8 +280,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)
@@ -499,7 +497,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_VNCR_EL2            sys_reg(3, 4, 2, 2, 0)
   #define SYS_HAFGRTR_EL2            sys_reg(3, 4, 3, 1, 6)
   #define SYS_SPSR_EL2            sys_reg(3, 4, 4, 0, 0)
@@ -949,15 +946,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

arch/arm64/include/asm/sysreg.h +0 -8
-#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 +0 -0
index 96cbeeab4eec..8fe23eac910f 100644
@@ -2634,3 +2634,44 @@ Field    5    F

  Field    4    P
  Field    3:0    Align
  EndSysreg

arch/arm64/tools/sysreg +41 -0
+
+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
+
+Sysreg    TRFCR_EL12    3    5    1    2    1
+Fields    TRFCR_ELx
+EndSysreg
tools/arch/arm64/include/asm/sysreg.h +1 -0
index ccc13e991376..92dfb41af018 100644
@@ -11,6 +11,7 @@
     #include <linux/bits.h>
   #include <linux/stringify.h>
+#include <linux/kasan-tags.h>
     #include <asm/gpr-num.h>

  @@ -123,6 +124,37 @@
  #define SYS_DC_CIGSW            sys_insn(1, 0, 7, 14, 4) #define SYS_DC_CIGDSW            sys_insn(1, 0, 7, 14, 6) +#define SYS_IC_IALLUIS            sys_insn(1, 0, 7, 1, 0)

tools/arch/arm64/include/asm/sysreg.h +30 -0
+#define SYS_IC_IALLU            sys_insn(1, 0, 7, 5, 0)
+#define SYS_IC_IVAU            sys_insn(1, 3, 7, 5, 1)
+
+#define SYS_DC_IVAC            sys_insn(1, 0, 7, 6, 1)
+#define SYS_DC_IGVAC            sys_insn(1, 0, 7, 6, 3)
+#define SYS_DC_IGDVAC            sys_insn(1, 0, 7, 6, 5)
+
+#define SYS_DC_CVAC            sys_insn(1, 3, 7, 10, 1)
+#define SYS_DC_CGVAC            sys_insn(1, 3, 7, 10, 3)
+#define SYS_DC_CGDVAC            sys_insn(1, 3, 7, 10, 5)
+
+#define SYS_DC_CVAU            sys_insn(1, 3, 7, 11, 1)
+
+#define SYS_DC_CVAP            sys_insn(1, 3, 7, 12, 1)
+#define SYS_DC_CGVAP            sys_insn(1, 3, 7, 12, 3)
+#define SYS_DC_CGDVAP            sys_insn(1, 3, 7, 12, 5)
+
+#define SYS_DC_CVADP            sys_insn(1, 3, 7, 13, 1)
+#define SYS_DC_CGVADP            sys_insn(1, 3, 7, 13, 3)
+#define SYS_DC_CGDVADP            sys_insn(1, 3, 7, 13, 5)
+
+#define SYS_DC_CIVAC            sys_insn(1, 3, 7, 14, 1)
+#define SYS_DC_CIGVAC            sys_insn(1, 3, 7, 14, 3)
+#define SYS_DC_CIGDVAC            sys_insn(1, 3, 7, 14, 5)
+
+/* Data cache zero operations */
+#define SYS_DC_ZVA            sys_insn(1, 3, 7, 4, 1)
+#define SYS_DC_GVA            sys_insn(1, 3, 7, 4, 3)
+#define SYS_DC_GZVA            sys_insn(1, 3, 7, 4, 4)
+

  /*
   * Automatically generated definitions for system registers, the
   * manual encodings below are in the process of being converted to

tools/arch/arm64/include/asm/sysreg.h +0 -0
@@ -162,6 +194,84 @@
   #define SYS_DBGDTRTX_EL0        sys_reg(2, 3, 0, 5, 0)
   #define SYS_DBGVCR32_EL2        sys_reg(2, 4, 0, 7, 0)

  +#define SYS_BRBINF_EL1(n)        sys_reg(2, 1, 8, (n & 15), (((n &
16) >> 2) | 0))

tools/arch/arm64/include/asm/sysreg.h +2 -0
+#define SYS_BRBINFINJ_EL1        sys_reg(2, 1, 9, 1, 0)
+#define SYS_BRBSRC_EL1(n)        sys_reg(2, 1, 8, (n & 15), (((n &

16) >> 2) | 1))

tools/arch/arm64/include/asm/sysreg.h +2 -0
+#define SYS_BRBSRCINJ_EL1        sys_reg(2, 1, 9, 1, 1)
+#define SYS_BRBTGT_EL1(n)        sys_reg(2, 1, 8, (n & 15), (((n &

16) >> 2) | 2))

tools/arch/arm64/include/asm/sysreg.h +9 -0
+#define SYS_BRBTGTINJ_EL1        sys_reg(2, 1, 9, 1, 2)
+#define SYS_BRBTS_EL1            sys_reg(2, 1, 9, 0, 2)
+
+#define SYS_BRBCR_EL1            sys_reg(2, 1, 9, 0, 0)
+#define SYS_BRBFCR_EL1            sys_reg(2, 1, 9, 0, 1)
+#define SYS_BRBIDR0_EL1            sys_reg(2, 1, 9, 2, 0)
+
+#define SYS_TRCITECR_EL1        sys_reg(3, 0, 1, 2, 3)
+#define SYS_TRCACATR(m)            sys_reg(2, 1, 2, ((m & 7) << 1),

(2 | (m >> 3)))
+#define SYS_TRCACVR(m)            sys_reg(2, 1, 2, ((m & 7) << 1), (0 | (m >> 3)))

tools/arch/arm64/include/asm/sysreg.h +38 -0
+#define SYS_TRCAUTHSTATUS        sys_reg(2, 1, 7, 14, 6)
+#define SYS_TRCAUXCTLR            sys_reg(2, 1, 0, 6, 0)
+#define SYS_TRCBBCTLR            sys_reg(2, 1, 0, 15, 0)
+#define SYS_TRCCCCTLR            sys_reg(2, 1, 0, 14, 0)
+#define SYS_TRCCIDCCTLR0        sys_reg(2, 1, 3, 0, 2)
+#define SYS_TRCCIDCCTLR1        sys_reg(2, 1, 3, 1, 2)
+#define SYS_TRCCIDCVR(m)        sys_reg(2, 1, 3, ((m & 7) << 1), 0)
+#define SYS_TRCCLAIMCLR            sys_reg(2, 1, 7, 9, 6)
+#define SYS_TRCCLAIMSET            sys_reg(2, 1, 7, 8, 6)
+#define SYS_TRCCNTCTLR(m)        sys_reg(2, 1, 0, (4 | (m & 3)), 5)
+#define SYS_TRCCNTRLDVR(m)        sys_reg(2, 1, 0, (0 | (m & 3)), 5)
+#define SYS_TRCCNTVR(m)            sys_reg(2, 1, 0, (8 | (m & 3)), 5)
+#define SYS_TRCCONFIGR            sys_reg(2, 1, 0, 4, 0)
+#define SYS_TRCDEVARCH            sys_reg(2, 1, 7, 15, 6)
+#define SYS_TRCDEVID            sys_reg(2, 1, 7, 2, 7)
+#define SYS_TRCEVENTCTL0R        sys_reg(2, 1, 0, 8, 0)
+#define SYS_TRCEVENTCTL1R        sys_reg(2, 1, 0, 9, 0)
+#define SYS_TRCEXTINSELR(m)        sys_reg(2, 1, 0, (8 | (m & 3)), 4)
+#define SYS_TRCIDR0            sys_reg(2, 1, 0, 8, 7)
+#define SYS_TRCIDR10            sys_reg(2, 1, 0, 2, 6)
+#define SYS_TRCIDR11            sys_reg(2, 1, 0, 3, 6)
+#define SYS_TRCIDR12            sys_reg(2, 1, 0, 4, 6)
+#define SYS_TRCIDR13            sys_reg(2, 1, 0, 5, 6)
+#define SYS_TRCIDR1            sys_reg(2, 1, 0, 9, 7)
+#define SYS_TRCIDR2            sys_reg(2, 1, 0, 10, 7)
+#define SYS_TRCIDR3            sys_reg(2, 1, 0, 11, 7)
+#define SYS_TRCIDR4            sys_reg(2, 1, 0, 12, 7)
+#define SYS_TRCIDR5            sys_reg(2, 1, 0, 13, 7)
+#define SYS_TRCIDR6            sys_reg(2, 1, 0, 14, 7)
+#define SYS_TRCIDR7            sys_reg(2, 1, 0, 15, 7)
+#define SYS_TRCIDR8            sys_reg(2, 1, 0, 0, 6)
+#define SYS_TRCIDR9            sys_reg(2, 1, 0, 1, 6)
+#define SYS_TRCIMSPEC(m)        sys_reg(2, 1, 0, (m & 7), 7)
+#define SYS_TRCITEEDCR            sys_reg(2, 1, 0, 2, 1)
+#define SYS_TRCOSLSR            sys_reg(2, 1, 1, 1, 4)
+#define SYS_TRCPRGCTLR            sys_reg(2, 1, 0, 1, 0)
+#define SYS_TRCQCTLR            sys_reg(2, 1, 0, 1, 1)
+#define SYS_TRCRSCTLR(m)        sys_reg(2, 1, 1, (m & 15), (0 | (m >>

4)))

tools/arch/arm64/include/asm/sysreg.h +25 -0
+#define SYS_TRCRSR            sys_reg(2, 1, 0, 10, 0)
+#define SYS_TRCSEQEVR(m)        sys_reg(2, 1, 0, (m & 3), 4)
+#define SYS_TRCSEQRSTEVR        sys_reg(2, 1, 0, 6, 4)
+#define SYS_TRCSEQSTR            sys_reg(2, 1, 0, 7, 4)
+#define SYS_TRCSSCCR(m)            sys_reg(2, 1, 1, (m & 7), 2)
+#define SYS_TRCSSCSR(m)            sys_reg(2, 1, 1, (8 | (m & 7)), 2)
+#define SYS_TRCSSPCICR(m)        sys_reg(2, 1, 1, (m & 7), 3)
+#define SYS_TRCSTALLCTLR        sys_reg(2, 1, 0, 11, 0)
+#define SYS_TRCSTATR            sys_reg(2, 1, 0, 3, 0)
+#define SYS_TRCSYNCPR            sys_reg(2, 1, 0, 13, 0)
+#define SYS_TRCTRACEIDR            sys_reg(2, 1, 0, 0, 1)
+#define SYS_TRCTSCTLR            sys_reg(2, 1, 0, 12, 0)
+#define SYS_TRCVICTLR            sys_reg(2, 1, 0, 0, 2)
+#define SYS_TRCVIIECTLR            sys_reg(2, 1, 0, 1, 2)
+#define SYS_TRCVIPCSSCTLR        sys_reg(2, 1, 0, 3, 2)
+#define SYS_TRCVISSCTLR            sys_reg(2, 1, 0, 2, 2)
+#define SYS_TRCVMIDCCTLR0        sys_reg(2, 1, 3, 2, 2)
+#define SYS_TRCVMIDCCTLR1        sys_reg(2, 1, 3, 3, 2)
+#define SYS_TRCVMIDCVR(m)        sys_reg(2, 1, 3, ((m & 7) << 1), 1)
+
+/* ETM */
+#define SYS_TRCOSLAR            sys_reg(2, 1, 1, 0, 4)
+
+#define SYS_BRBCR_EL2            sys_reg(2, 4, 9, 0, 0)
+

  #define SYS_MIDR_EL1            sys_reg(3, 0, 0, 0, 0) #define SYS_MPIDR_EL1            sys_reg(3, 0, 0, 0, 5) #define SYS_REVIDR_EL1            sys_reg(3, 0, 0, 0, 6)

tools/arch/arm64/include/asm/sysreg.h +5 -1
@@ -170,8 +280,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)
@@ -202,8 +310,13 @@
   #define SYS_ERXCTLR_EL1            sys_reg(3, 0, 5, 4, 1)
   #define SYS_ERXSTATUS_EL1        sys_reg(3, 0, 5, 4, 2)
   #define SYS_ERXADDR_EL1            sys_reg(3, 0, 5, 4, 3)
+#define SYS_ERXPFGF_EL1            sys_reg(3, 0, 5, 4, 4)
+#define SYS_ERXPFGCTL_EL1        sys_reg(3, 0, 5, 4, 5)
+#define SYS_ERXPFGCDN_EL1        sys_reg(3, 0, 5, 4, 6)
   #define SYS_ERXMISC0_EL1        sys_reg(3, 0, 5, 5, 0)
   #define SYS_ERXMISC1_EL1        sys_reg(3, 0, 5, 5, 1)
+#define SYS_ERXMISC2_EL1        sys_reg(3, 0, 5, 5, 2)
+#define SYS_ERXMISC3_EL1        sys_reg(3, 0, 5, 5, 3)
   #define SYS_TFSR_EL1            sys_reg(3, 0, 5, 6, 0)
   #define SYS_TFSRE0_EL1            sys_reg(3, 0, 5, 6, 1)

  @@ -274,6 +387,8 @@
  #define SYS_ICC_IGRPEN0_EL1        sys_reg(3, 0, 12, 12, 6) #define SYS_ICC_IGRPEN1_EL1        sys_reg(3, 0, 12, 12, 7) +#define SYS_ACCDATA_EL1            sys_reg(3, 0, 13, 0, 5) +
  #define SYS_CNTKCTL_EL1            sys_reg(3, 0, 14, 1, 0) #define SYS_AIDR_EL1            sys_reg(3, 1, 0, 0, 7)

tools/arch/arm64/include/asm/sysreg.h +43 -2
@@ -369,6 +484,7 @@
     #define SYS_SCTLR_EL2            sys_reg(3, 4, 1, 0, 0)
   #define SYS_ACTLR_EL2            sys_reg(3, 4, 1, 0, 1)
+#define SYS_SCTLR2_EL2            sys_reg(3, 4, 1, 0, 3)
   #define SYS_HCR_EL2            sys_reg(3, 4, 1, 1, 0)
   #define SYS_MDCR_EL2            sys_reg(3, 4, 1, 1, 1)
   #define SYS_CPTR_EL2            sys_reg(3, 4, 1, 1, 2)
@@ -381,13 +497,15 @@
   #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_HDFGRTR_EL2            sys_reg(3, 4, 3, 1, 4)
-#define SYS_HDFGWTR_EL2            sys_reg(3, 4, 3, 1, 5)
+#define SYS_VNCR_EL2            sys_reg(3, 4, 2, 2, 0)
   #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)
   #define SYS_SP_EL1            sys_reg(3, 4, 4, 1, 0)
+#define SYS_SPSR_irq            sys_reg(3, 4, 4, 3, 0)
+#define SYS_SPSR_abt            sys_reg(3, 4, 4, 3, 1)
+#define SYS_SPSR_und            sys_reg(3, 4, 4, 3, 2)
+#define SYS_SPSR_fiq            sys_reg(3, 4, 4, 3, 3)
   #define SYS_IFSR32_EL2            sys_reg(3, 4, 5, 0, 1)
   #define SYS_AFSR0_EL2            sys_reg(3, 4, 5, 1, 0)
   #define SYS_AFSR1_EL2            sys_reg(3, 4, 5, 1, 1)
@@ -401,6 +519,18 @@
     #define SYS_MAIR_EL2            sys_reg(3, 4, 10, 2, 0)
   #define SYS_AMAIR_EL2            sys_reg(3, 4, 10, 3, 0)
+#define SYS_MPAMHCR_EL2            sys_reg(3, 4, 10, 4, 0)
+#define SYS_MPAMVPMV_EL2        sys_reg(3, 4, 10, 4, 1)
+#define SYS_MPAM2_EL2            sys_reg(3, 4, 10, 5, 0)
+#define __SYS__MPAMVPMx_EL2(x)        sys_reg(3, 4, 10, 6, x)
+#define SYS_MPAMVPM0_EL2        __SYS__MPAMVPMx_EL2(0)
+#define SYS_MPAMVPM1_EL2        __SYS__MPAMVPMx_EL2(1)
+#define SYS_MPAMVPM2_EL2        __SYS__MPAMVPMx_EL2(2)
+#define SYS_MPAMVPM3_EL2        __SYS__MPAMVPMx_EL2(3)
+#define SYS_MPAMVPM4_EL2        __SYS__MPAMVPMx_EL2(4)
+#define SYS_MPAMVPM5_EL2        __SYS__MPAMVPMx_EL2(5)
+#define SYS_MPAMVPM6_EL2        __SYS__MPAMVPMx_EL2(6)
+#define SYS_MPAMVPM7_EL2        __SYS__MPAMVPMx_EL2(7)
     #define SYS_VBAR_EL2            sys_reg(3, 4, 12, 0, 0)
   #define SYS_RVBAR_EL2            sys_reg(3, 4, 12, 0, 1)
@@ -449,24 +579,49 @@
     #define SYS_CONTEXTIDR_EL2        sys_reg(3, 4, 13, 0, 1)
   #define SYS_TPIDR_EL2            sys_reg(3, 4, 13, 0, 2)
+#define SYS_SCXTNUM_EL2            sys_reg(3, 4, 13, 0, 7)
+
+#define __AMEV_op2(m)            (m & 0x7)
+#define __AMEV_CRm(n, m)        (n | ((m & 0x8) >> 3))
+#define __SYS__AMEVCNTVOFF0n_EL2(m)    sys_reg(3, 4, 13,
 __AMEV_CRm(0x8, m), __AMEV_op2(m))
+#define SYS_AMEVCNTVOFF0n_EL2(m)    __SYS__AMEVCNTVOFF0n_EL2(m)
+#define __SYS__AMEVCNTVOFF1n_EL2(m)    sys_reg(3, 4, 13,
 __AMEV_CRm(0xA, m), __AMEV_op2(m))
+#define SYS_AMEVCNTVOFF1n_EL2(m)    __SYS__AMEVCNTVOFF1n_EL2(m)
     #define SYS_CNTVOFF_EL2            sys_reg(3, 4, 14, 0, 3)
   #define SYS_CNTHCTL_EL2            sys_reg(3, 4, 14, 1, 0)
+#define SYS_CNTHP_TVAL_EL2        sys_reg(3, 4, 14, 2, 0)
+#define SYS_CNTHP_CTL_EL2        sys_reg(3, 4, 14, 2, 1)
+#define SYS_CNTHP_CVAL_EL2        sys_reg(3, 4, 14, 2, 2)
+#define SYS_CNTHV_TVAL_EL2        sys_reg(3, 4, 14, 3, 0)
+#define SYS_CNTHV_CTL_EL2        sys_reg(3, 4, 14, 3, 1)
+#define SYS_CNTHV_CVAL_EL2        sys_reg(3, 4, 14, 3, 2)
     /* VHE encodings for architectural EL0/1 system registers */
+#define SYS_BRBCR_EL12            sys_reg(2, 5, 9, 0, 0)
   #define SYS_SCTLR_EL12            sys_reg(3, 5, 1, 0, 0)
+#define SYS_CPACR_EL12            sys_reg(3, 5, 1, 0, 2)
+#define SYS_SCTLR2_EL12            sys_reg(3, 5, 1, 0, 3)
+#define SYS_ZCR_EL12            sys_reg(3, 5, 1, 2, 0)
+#define SYS_TRFCR_EL12            sys_reg(3, 5, 1, 2, 1)
+#define SYS_SMCR_EL12            sys_reg(3, 5, 1, 2, 6)
   #define SYS_TTBR0_EL12            sys_reg(3, 5, 2, 0, 0)
   #define SYS_TTBR1_EL12            sys_reg(3, 5, 2, 0, 1)
   #define SYS_TCR_EL12            sys_reg(3, 5, 2, 0, 2)
+#define SYS_TCR2_EL12            sys_reg(3, 5, 2, 0, 3)
   #define SYS_SPSR_EL12            sys_reg(3, 5, 4, 0, 0)
   #define SYS_ELR_EL12            sys_reg(3, 5, 4, 0, 1)
   #define SYS_AFSR0_EL12            sys_reg(3, 5, 5, 1, 0)
   #define SYS_AFSR1_EL12            sys_reg(3, 5, 5, 1, 1)
   #define SYS_ESR_EL12            sys_reg(3, 5, 5, 2, 0)
   #define SYS_TFSR_EL12            sys_reg(3, 5, 5, 6, 0)
+#define SYS_FAR_EL12            sys_reg(3, 5, 6, 0, 0)
+#define SYS_PMSCR_EL12            sys_reg(3, 5, 9, 9, 0)
   #define SYS_MAIR_EL12            sys_reg(3, 5, 10, 2, 0)
   #define SYS_AMAIR_EL12            sys_reg(3, 5, 10, 3, 0)
   #define SYS_VBAR_EL12            sys_reg(3, 5, 12, 0, 0)
+#define SYS_CONTEXTIDR_EL12        sys_reg(3, 5, 13, 0, 1)
+#define SYS_SCXTNUM_EL12        sys_reg(3, 5, 13, 0, 7)
   #define SYS_CNTKCTL_EL12        sys_reg(3, 5, 14, 1, 0)
   #define SYS_CNTP_TVAL_EL02        sys_reg(3, 5, 14, 2, 0)
   #define SYS_CNTP_CTL_EL02        sys_reg(3, 5, 14, 2, 1)
@@ -477,6 +632,158 @@
     #define SYS_SP_EL2            sys_reg(3, 6,  4, 1, 0)

  +/* AT instructions */

tools/arch/arm64/include/asm/sysreg.h +151 -0
+#define AT_Op0 1
+#define AT_CRn 7
+
+#define OP_AT_S1E1R    sys_insn(AT_Op0, 0, AT_CRn, 8, 0)
+#define OP_AT_S1E1W    sys_insn(AT_Op0, 0, AT_CRn, 8, 1)
+#define OP_AT_S1E0R    sys_insn(AT_Op0, 0, AT_CRn, 8, 2)
+#define OP_AT_S1E0W    sys_insn(AT_Op0, 0, AT_CRn, 8, 3)
+#define OP_AT_S1E1RP    sys_insn(AT_Op0, 0, AT_CRn, 9, 0)
+#define OP_AT_S1E1WP    sys_insn(AT_Op0, 0, AT_CRn, 9, 1)
+#define OP_AT_S1E2R    sys_insn(AT_Op0, 4, AT_CRn, 8, 0)
+#define OP_AT_S1E2W    sys_insn(AT_Op0, 4, AT_CRn, 8, 1)
+#define OP_AT_S12E1R    sys_insn(AT_Op0, 4, AT_CRn, 8, 4)
+#define OP_AT_S12E1W    sys_insn(AT_Op0, 4, AT_CRn, 8, 5)
+#define OP_AT_S12E0R    sys_insn(AT_Op0, 4, AT_CRn, 8, 6)
+#define OP_AT_S12E0W    sys_insn(AT_Op0, 4, AT_CRn, 8, 7)
+
+/* TLBI instructions */
+#define OP_TLBI_VMALLE1OS        sys_insn(1, 0, 8, 1, 0)
+#define OP_TLBI_VAE1OS            sys_insn(1, 0, 8, 1, 1)
+#define OP_TLBI_ASIDE1OS        sys_insn(1, 0, 8, 1, 2)
+#define OP_TLBI_VAAE1OS            sys_insn(1, 0, 8, 1, 3)
+#define OP_TLBI_VALE1OS            sys_insn(1, 0, 8, 1, 5)
+#define OP_TLBI_VAALE1OS        sys_insn(1, 0, 8, 1, 7)
+#define OP_TLBI_RVAE1IS            sys_insn(1, 0, 8, 2, 1)
+#define OP_TLBI_RVAAE1IS        sys_insn(1, 0, 8, 2, 3)
+#define OP_TLBI_RVALE1IS        sys_insn(1, 0, 8, 2, 5)
+#define OP_TLBI_RVAALE1IS        sys_insn(1, 0, 8, 2, 7)
+#define OP_TLBI_VMALLE1IS        sys_insn(1, 0, 8, 3, 0)
+#define OP_TLBI_VAE1IS            sys_insn(1, 0, 8, 3, 1)
+#define OP_TLBI_ASIDE1IS        sys_insn(1, 0, 8, 3, 2)
+#define OP_TLBI_VAAE1IS            sys_insn(1, 0, 8, 3, 3)
+#define OP_TLBI_VALE1IS            sys_insn(1, 0, 8, 3, 5)
+#define OP_TLBI_VAALE1IS        sys_insn(1, 0, 8, 3, 7)
+#define OP_TLBI_RVAE1OS            sys_insn(1, 0, 8, 5, 1)
+#define OP_TLBI_RVAAE1OS        sys_insn(1, 0, 8, 5, 3)
+#define OP_TLBI_RVALE1OS        sys_insn(1, 0, 8, 5, 5)
+#define OP_TLBI_RVAALE1OS        sys_insn(1, 0, 8, 5, 7)
+#define OP_TLBI_RVAE1            sys_insn(1, 0, 8, 6, 1)
+#define OP_TLBI_RVAAE1            sys_insn(1, 0, 8, 6, 3)
+#define OP_TLBI_RVALE1            sys_insn(1, 0, 8, 6, 5)
+#define OP_TLBI_RVAALE1            sys_insn(1, 0, 8, 6, 7)
+#define OP_TLBI_VMALLE1            sys_insn(1, 0, 8, 7, 0)
+#define OP_TLBI_VAE1            sys_insn(1, 0, 8, 7, 1)
+#define OP_TLBI_ASIDE1            sys_insn(1, 0, 8, 7, 2)
+#define OP_TLBI_VAAE1            sys_insn(1, 0, 8, 7, 3)
+#define OP_TLBI_VALE1            sys_insn(1, 0, 8, 7, 5)
+#define OP_TLBI_VAALE1            sys_insn(1, 0, 8, 7, 7)
+#define OP_TLBI_VMALLE1OSNXS        sys_insn(1, 0, 9, 1, 0)
+#define OP_TLBI_VAE1OSNXS        sys_insn(1, 0, 9, 1, 1)
+#define OP_TLBI_ASIDE1OSNXS        sys_insn(1, 0, 9, 1, 2)
+#define OP_TLBI_VAAE1OSNXS        sys_insn(1, 0, 9, 1, 3)
+#define OP_TLBI_VALE1OSNXS        sys_insn(1, 0, 9, 1, 5)
+#define OP_TLBI_VAALE1OSNXS        sys_insn(1, 0, 9, 1, 7)
+#define OP_TLBI_RVAE1ISNXS        sys_insn(1, 0, 9, 2, 1)
+#define OP_TLBI_RVAAE1ISNXS        sys_insn(1, 0, 9, 2, 3)
+#define OP_TLBI_RVALE1ISNXS        sys_insn(1, 0, 9, 2, 5)
+#define OP_TLBI_RVAALE1ISNXS        sys_insn(1, 0, 9, 2, 7)
+#define OP_TLBI_VMALLE1ISNXS        sys_insn(1, 0, 9, 3, 0)
+#define OP_TLBI_VAE1ISNXS        sys_insn(1, 0, 9, 3, 1)
+#define OP_TLBI_ASIDE1ISNXS        sys_insn(1, 0, 9, 3, 2)
+#define OP_TLBI_VAAE1ISNXS        sys_insn(1, 0, 9, 3, 3)
+#define OP_TLBI_VALE1ISNXS        sys_insn(1, 0, 9, 3, 5)
+#define OP_TLBI_VAALE1ISNXS        sys_insn(1, 0, 9, 3, 7)
+#define OP_TLBI_RVAE1OSNXS        sys_insn(1, 0, 9, 5, 1)
+#define OP_TLBI_RVAAE1OSNXS        sys_insn(1, 0, 9, 5, 3)
+#define OP_TLBI_RVALE1OSNXS        sys_insn(1, 0, 9, 5, 5)
+#define OP_TLBI_RVAALE1OSNXS        sys_insn(1, 0, 9, 5, 7)
+#define OP_TLBI_RVAE1NXS        sys_insn(1, 0, 9, 6, 1)
+#define OP_TLBI_RVAAE1NXS        sys_insn(1, 0, 9, 6, 3)
+#define OP_TLBI_RVALE1NXS        sys_insn(1, 0, 9, 6, 5)
+#define OP_TLBI_RVAALE1NXS        sys_insn(1, 0, 9, 6, 7)
+#define OP_TLBI_VMALLE1NXS        sys_insn(1, 0, 9, 7, 0)
+#define OP_TLBI_VAE1NXS            sys_insn(1, 0, 9, 7, 1)
+#define OP_TLBI_ASIDE1NXS        sys_insn(1, 0, 9, 7, 2)
+#define OP_TLBI_VAAE1NXS        sys_insn(1, 0, 9, 7, 3)
+#define OP_TLBI_VALE1NXS        sys_insn(1, 0, 9, 7, 5)
+#define OP_TLBI_VAALE1NXS        sys_insn(1, 0, 9, 7, 7)
+#define OP_TLBI_IPAS2E1IS        sys_insn(1, 4, 8, 0, 1)
+#define OP_TLBI_RIPAS2E1IS        sys_insn(1, 4, 8, 0, 2)
+#define OP_TLBI_IPAS2LE1IS        sys_insn(1, 4, 8, 0, 5)
+#define OP_TLBI_RIPAS2LE1IS        sys_insn(1, 4, 8, 0, 6)
+#define OP_TLBI_ALLE2OS            sys_insn(1, 4, 8, 1, 0)
+#define OP_TLBI_VAE2OS            sys_insn(1, 4, 8, 1, 1)
+#define OP_TLBI_ALLE1OS            sys_insn(1, 4, 8, 1, 4)
+#define OP_TLBI_VALE2OS            sys_insn(1, 4, 8, 1, 5)
+#define OP_TLBI_VMALLS12E1OS        sys_insn(1, 4, 8, 1, 6)
+#define OP_TLBI_RVAE2IS            sys_insn(1, 4, 8, 2, 1)
+#define OP_TLBI_RVALE2IS        sys_insn(1, 4, 8, 2, 5)
+#define OP_TLBI_ALLE2IS            sys_insn(1, 4, 8, 3, 0)
+#define OP_TLBI_VAE2IS            sys_insn(1, 4, 8, 3, 1)
+#define OP_TLBI_ALLE1IS            sys_insn(1, 4, 8, 3, 4)
+#define OP_TLBI_VALE2IS            sys_insn(1, 4, 8, 3, 5)
+#define OP_TLBI_VMALLS12E1IS        sys_insn(1, 4, 8, 3, 6)
+#define OP_TLBI_IPAS2E1OS        sys_insn(1, 4, 8, 4, 0)
+#define OP_TLBI_IPAS2E1            sys_insn(1, 4, 8, 4, 1)
+#define OP_TLBI_RIPAS2E1        sys_insn(1, 4, 8, 4, 2)
+#define OP_TLBI_RIPAS2E1OS        sys_insn(1, 4, 8, 4, 3)
+#define OP_TLBI_IPAS2LE1OS        sys_insn(1, 4, 8, 4, 4)
+#define OP_TLBI_IPAS2LE1        sys_insn(1, 4, 8, 4, 5)
+#define OP_TLBI_RIPAS2LE1        sys_insn(1, 4, 8, 4, 6)
+#define OP_TLBI_RIPAS2LE1OS        sys_insn(1, 4, 8, 4, 7)
+#define OP_TLBI_RVAE2OS            sys_insn(1, 4, 8, 5, 1)
+#define OP_TLBI_RVALE2OS        sys_insn(1, 4, 8, 5, 5)
+#define OP_TLBI_RVAE2            sys_insn(1, 4, 8, 6, 1)
+#define OP_TLBI_RVALE2            sys_insn(1, 4, 8, 6, 5)
+#define OP_TLBI_ALLE2            sys_insn(1, 4, 8, 7, 0)
+#define OP_TLBI_VAE2            sys_insn(1, 4, 8, 7, 1)
+#define OP_TLBI_ALLE1            sys_insn(1, 4, 8, 7, 4)
+#define OP_TLBI_VALE2            sys_insn(1, 4, 8, 7, 5)
+#define OP_TLBI_VMALLS12E1        sys_insn(1, 4, 8, 7, 6)
+#define OP_TLBI_IPAS2E1ISNXS        sys_insn(1, 4, 9, 0, 1)
+#define OP_TLBI_RIPAS2E1ISNXS        sys_insn(1, 4, 9, 0, 2)
+#define OP_TLBI_IPAS2LE1ISNXS        sys_insn(1, 4, 9, 0, 5)
+#define OP_TLBI_RIPAS2LE1ISNXS        sys_insn(1, 4, 9, 0, 6)
+#define OP_TLBI_ALLE2OSNXS        sys_insn(1, 4, 9, 1, 0)
+#define OP_TLBI_VAE2OSNXS        sys_insn(1, 4, 9, 1, 1)
+#define OP_TLBI_ALLE1OSNXS        sys_insn(1, 4, 9, 1, 4)
+#define OP_TLBI_VALE2OSNXS        sys_insn(1, 4, 9, 1, 5)
+#define OP_TLBI_VMALLS12E1OSNXS        sys_insn(1, 4, 9, 1, 6)
+#define OP_TLBI_RVAE2ISNXS        sys_insn(1, 4, 9, 2, 1)
+#define OP_TLBI_RVALE2ISNXS        sys_insn(1, 4, 9, 2, 5)
+#define OP_TLBI_ALLE2ISNXS        sys_insn(1, 4, 9, 3, 0)
+#define OP_TLBI_VAE2ISNXS        sys_insn(1, 4, 9, 3, 1)
+#define OP_TLBI_ALLE1ISNXS        sys_insn(1, 4, 9, 3, 4)
+#define OP_TLBI_VALE2ISNXS        sys_insn(1, 4, 9, 3, 5)
+#define OP_TLBI_VMALLS12E1ISNXS        sys_insn(1, 4, 9, 3, 6)
+#define OP_TLBI_IPAS2E1OSNXS        sys_insn(1, 4, 9, 4, 0)
+#define OP_TLBI_IPAS2E1NXS        sys_insn(1, 4, 9, 4, 1)
+#define OP_TLBI_RIPAS2E1NXS        sys_insn(1, 4, 9, 4, 2)
+#define OP_TLBI_RIPAS2E1OSNXS        sys_insn(1, 4, 9, 4, 3)
+#define OP_TLBI_IPAS2LE1OSNXS        sys_insn(1, 4, 9, 4, 4)
+#define OP_TLBI_IPAS2LE1NXS        sys_insn(1, 4, 9, 4, 5)
+#define OP_TLBI_RIPAS2LE1NXS        sys_insn(1, 4, 9, 4, 6)
+#define OP_TLBI_RIPAS2LE1OSNXS        sys_insn(1, 4, 9, 4, 7)
+#define OP_TLBI_RVAE2OSNXS        sys_insn(1, 4, 9, 5, 1)
+#define OP_TLBI_RVALE2OSNXS        sys_insn(1, 4, 9, 5, 5)
+#define OP_TLBI_RVAE2NXS        sys_insn(1, 4, 9, 6, 1)
+#define OP_TLBI_RVALE2NXS        sys_insn(1, 4, 9, 6, 5)
+#define OP_TLBI_ALLE2NXS        sys_insn(1, 4, 9, 7, 0)
+#define OP_TLBI_VAE2NXS            sys_insn(1, 4, 9, 7, 1)
+#define OP_TLBI_ALLE1NXS        sys_insn(1, 4, 9, 7, 4)
+#define OP_TLBI_VALE2NXS        sys_insn(1, 4, 9, 7, 5)
+#define OP_TLBI_VMALLS12E1NXS        sys_insn(1, 4, 9, 7, 6)
+
+/* Misc instructions */
+#define OP_BRB_IALL            sys_insn(1, 1, 7, 2, 4)
+#define OP_BRB_INJ            sys_insn(1, 1, 7, 2, 5)
+#define OP_CFP_RCTX            sys_insn(1, 3, 7, 3, 4)
+#define OP_DVP_RCTX            sys_insn(1, 3, 7, 3, 5)
+#define OP_CPP_RCTX            sys_insn(1, 3, 7, 3, 7)
+

  /* Common SCTLR_ELx flags. */
  #define SCTLR_ELx_ENTP2    (BIT(60))
  #define SCTLR_ELx_DSSBS    (BIT(44))

tools/arch/arm64/include/asm/sysreg.h +0 -0
@@ -610,6 +917,19 @@
   #define SYS_GCR_EL1_RRND    (BIT(16))
   #define SYS_GCR_EL1_EXCL_MASK    0xffffUL

  +#ifdef CONFIG_KASAN_HW_TAGS
+/*
+ * KASAN always uses a whole byte for its tags. With CONFIG_KASAN_HW_TAGS it
+ * only uses tags in the range 0xF0-0xFF, which we map to MTE tags 0x0-0xF.
+ */

tools/arch/arm64/include/asm/sysreg.h +8 -0
+#define __MTE_TAG_MIN        (KASAN_TAG_MIN & 0xf)
+#define __MTE_TAG_MAX        (KASAN_TAG_MAX & 0xf)
+#define __MTE_TAG_INCL        GENMASK(__MTE_TAG_MAX, __MTE_TAG_MIN)
+#define KERNEL_GCR_EL1_EXCL    (SYS_GCR_EL1_EXCL_MASK & ~__MTE_TAG_INCL)
+#else
+#define KERNEL_GCR_EL1_EXCL    SYS_GCR_EL1_EXCL_MASK
+#endif
+

  #define KERNEL_GCR_EL1        (SYS_GCR_EL1_RRND | KERNEL_GCR_EL1_EXCL) /* RGSR_EL1 Definitions */

tools/arch/arm64/include/asm/sysreg.h +0 -0
@@ -626,15 +946,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

tools/arch/arm64/include/asm/sysreg.h +0 -8
-#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)

tools/arch/arm64/include/asm/sysreg.h +6 -0
@@ -789,15 +1100,21 @@
   /*
    * For registers without architectural names, or simply unsupported by
    * GAS.
+ *
+ * __check_r forces warnings to be generated by the compiler when
+ * evaluating r which wouldn't normally happen due to being passed to
+ * the assembler via __stringify(r).
    */
   #define read_sysreg_s(r) ({                        \
       u64 __val;                            \
+    u32 __maybe_unused __check_r = (u32)(r);            \
       asm volatile(__mrs_s("%0", r) : "=r" (__val));            \
       __val;                                \
   })
     #define write_sysreg_s(v, r) do {                    \
       u64 __val = (u64)(v);                        \
+    u32 __maybe_unused __check_r = (u32)(r);            \
       asm volatile(__msr_s(r, "%x0") : : "rZ" (__val));        \
   } while (0)

  diff --git a/tools/include/linux/kasan-tags.h
b/tools/include/linux/kasan-tags.h
new file mode 100644
index 000000000000..4f85f562512c

tools/include/linux/kasan-tags.h new file +15 -0
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_KASAN_TAGS_H
+#define _LINUX_KASAN_TAGS_H
+
+#define KASAN_TAG_KERNEL    0xFF /* native kernel pointers tag */
+#define KASAN_TAG_INVALID    0xFE /* inaccessible memory tag */
+#define KASAN_TAG_MAX        0xFD /* maximum value for random tags */
+
+#ifdef CONFIG_KASAN_HW_TAGS
+#define KASAN_TAG_MIN        0xF0 /* minimum value for random tags */
+#else
+#define KASAN_TAG_MIN        0x00 /* minimum value for random tags */
+#endif
+
+#endif /* LINUX_KASAN_TAGS_H */
#20 lore raw

PATCH v4 4/7 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            | 24 ++++++++++++++++++++----
 2 files changed, 23 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -1
index 93d38ad257ed..4864a1fcdf89 100644
@@ -551,7 +551,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;
@@ -761,6 +761,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 +20 -4
index 8725291cb00a..f86cbfae60f3 100644
@@ -331,14 +331,30 @@ 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);
+	/*
+	 * Set SAVE_TRFCR flag if FEAT_TRF (TraceFilt) exists. This flag
+	 * signifies that the exclude_host/exclude_guest settings of any active
+	 * host Perf session on a core running a VCPU can be written into
+	 * TRFCR_EL1 on guest switch.
+	 */
+	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 then trace will
+		 * need to be completely disabled. 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
#21 lore raw

PATCH v4 5/7 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.

---
 arch/arm64/include/asm/kvm_host.h |  3 +++
 arch/arm64/kvm/debug.c            | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
arch/arm64/include/asm/kvm_host.h +3 -0
index 4864a1fcdf89..ee6cba7ee6ee 100644
@@ -471,6 +471,7 @@ struct kvm_host_global_state {
 		u32 events_host;
 		u32 events_guest;
 	} pmu_events;
+	u64 guest_trfcr_el1;
 } ____cacheline_aligned;
 extern struct kvm_host_global_state kvm_host_global_state[NR_CPUS];
 
@@ -1145,6 +1146,7 @@ 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);
 #else
 static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
 static inline void kvm_clr_pmu_events(u32 clr) {}
@@ -1152,6 +1154,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_vhe(struct kvm_vcpu *vcpu);
arch/arm64/kvm/debug.c +24 -0
index f86cbfae60f3..d69a0b9d9575 100644
@@ -358,3 +358,27 @@ 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);
 }
+
+/*
+ * 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.
+ */
+void kvm_etm_set_guest_trfcr(u64 trfcr_guest)
+{
+	if (!cpuid_feature_extract_unsigned_field(read_sysreg(id_aa64dfr0_el1),
+						  ID_AA64DFR0_EL1_TraceFilt_SHIFT))
+		return;
+
+	if (has_vhe())
+		write_sysreg_s(trfcr_guest, SYS_TRFCR_EL12);
+	else
+		kvm_host_global_state[smp_processor_id()].guest_trfcr_el1 = trfcr_guest;
+}
+EXPORT_SYMBOL_GPL(kvm_etm_set_guest_trfcr);
Signature · 1 line
2.34.1
maintainer Reviewed-by on v4 5/7
#22 lore raw

Re: [PATCH v4 5/7] arm64: KVM: Add interface to set guest value for TRFCR register

Quoted 57 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.

---
  arch/arm64/include/asm/kvm_host.h |  3 +++
  arch/arm64/kvm/debug.c            | 24 ++++++++++++++++++++++++
  2 files changed, 27 insertions(+)
arch/arm64/include/asm/kvm_host.h +3 -0
index 4864a1fcdf89..ee6cba7ee6ee 100644
@@ -471,6 +471,7 @@ struct kvm_host_global_state {
  		u32 events_host;
  		u32 events_guest;
  	} pmu_events;
+	u64 guest_trfcr_el1;
  } ____cacheline_aligned;
  extern struct kvm_host_global_state kvm_host_global_state[NR_CPUS];
 
@@ -1145,6 +1146,7 @@ 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);
  #else
  static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
  static inline void kvm_clr_pmu_events(u32 clr) {}
@@ -1152,6 +1154,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_vhe(struct kvm_vcpu *vcpu);
arch/arm64/kvm/debug.c +6 -0
index f86cbfae60f3..d69a0b9d9575 100644
@@ -358,3 +358,27 @@ 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);
  }
+
+/*
+ * 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.

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

Quoted 10 lines
arch/arm64/kvm/debug.c +10 -0
+ *
+ * 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.
+ */
+void kvm_etm_set_guest_trfcr(u64 trfcr_guest)
+{
+	if (!cpuid_feature_extract_unsigned_field(read_sysreg(id_aa64dfr0_el1),
+						  ID_AA64DFR0_EL1_TraceFilt_SHIFT))

Perhaps WARN_ON_ONCE() ?

Otherwise,

Quoted 8 lines
arch/arm64/kvm/debug.c +8 -0
+		return;
+
+	if (has_vhe())
+		write_sysreg_s(trfcr_guest, SYS_TRFCR_EL12);
+	else
+		kvm_host_global_state[smp_processor_id()].guest_trfcr_el1 = trfcr_guest;
+}
+EXPORT_SYMBOL_GPL(kvm_etm_set_guest_trfcr);
#23 lore raw

PATCH v4 6/7 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 kvm_host_global_state. 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/kvm/hyp/nvhe/debug-sr.c | 55 ++++++++++++++++++------------
 1 file changed, 34 insertions(+), 21 deletions(-)
arch/arm64/kvm/hyp/nvhe/debug-sr.c +34 -21
index 4558c02eb352..7fd876d4f034 100644
@@ -51,32 +51,45 @@ 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;
+	u64 host_trfcr_el1 = read_sysreg_s(SYS_TRFCR_EL1);
+	u64 guest_trfcr_el1;
+
+	vcpu->arch.host_debug_state.trfcr_el1 = host_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 (vcpu_get_flag(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.
+		 */
+		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.
+		 */
+		guest_trfcr_el1 = kvm_host_global_state[vcpu->cpu].guest_trfcr_el1;
+		if (host_trfcr_el1 != guest_trfcr_el1)
+			write_sysreg_s(guest_trfcr_el1, SYS_TRFCR_EL1);
+	}
 }
 
 static void __debug_restore_trace(u64 trfcr_el1)
 {
-	if (!trfcr_el1)
-		return;
-
 	/* Restore trace filter controls */
-	write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1);
+	if (trfcr_el1 != read_sysreg_s(SYS_TRFCR_EL1))
+		write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1);
 }
 
 void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
@@ -85,8 +98,8 @@ 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))
-		__debug_save_trace(&vcpu->arch.host_debug_state.trfcr_el1);
+	if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
+		__debug_save_trace(vcpu);
 }
 
 void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
@@ -98,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_TRBE))
+	if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
 		__debug_restore_trace(vcpu->arch.host_debug_state.trfcr_el1);
 }
 
Signature · 1 line
2.34.1
maintainer Reviewed-by on v4 6/7
#24 lore raw

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

Quoted 73 lines · James Clark

The guest value for TRFCR requested by the Coresight driver is saved in kvm_host_global_state. 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/kvm/hyp/nvhe/debug-sr.c | 55 ++++++++++++++++++------------
  1 file changed, 34 insertions(+), 21 deletions(-)
arch/arm64/kvm/hyp/nvhe/debug-sr.c +29 -14
index 4558c02eb352..7fd876d4f034 100644
@@ -51,32 +51,45 @@ 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;
+	u64 host_trfcr_el1 = read_sysreg_s(SYS_TRFCR_EL1);
+	u64 guest_trfcr_el1;
+
+	vcpu->arch.host_debug_state.trfcr_el1 = host_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 (vcpu_get_flag(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.
+		 */
+		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.
+		 */
+		guest_trfcr_el1 = kvm_host_global_state[vcpu->cpu].guest_trfcr_el1;
+		if (host_trfcr_el1 != guest_trfcr_el1)
+			write_sysreg_s(guest_trfcr_el1, SYS_TRFCR_EL1);
+	}
  }

arch/arm64/kvm/hyp/nvhe/debug-sr.c static void __debug_restore_spe(u64 pmscr_el1) · line 92

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/debug-sr.c +2 -4
 
  static void __debug_restore_trace(u64 trfcr_el1)
  {
-	if (!trfcr_el1)
-		return;
-
  	/* Restore trace filter controls */
-	write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1);
+	if (trfcr_el1 != read_sysreg_s(SYS_TRFCR_EL1))
+		write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1);

Could we not write it unconditionally here ? In the saving step, we have to save the host setting. But while restoring, we could skip the check. A read and write is probably the same cost, as the value is implicitly synchronized by a later ISB.

Eitherways,

Quoted 23 lines
arch/arm64/kvm/hyp/nvhe/debug-sr.c +3 -3
  }
 
  void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
@@ -85,8 +98,8 @@ 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))
-		__debug_save_trace(&vcpu->arch.host_debug_state.trfcr_el1);
+	if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
+		__debug_save_trace(vcpu);
  }
 
  void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
@@ -98,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_TRBE))
+	if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
  		__debug_restore_trace(vcpu->arch.host_debug_state.trfcr_el1);
  }
 
#25 lore raw

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

Quoted 90 lines · Suzuki K Poulose
Quoted 75 lines · James Clark

The guest value for TRFCR requested by the Coresight driver is saved in kvm_host_global_state. 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/kvm/hyp/nvhe/debug-sr.c | 55 ++++++++++++++++++------------

  1 file changed, 34 insertions(+), 21 deletions(-)
arch/arm64/kvm/hyp/nvhe/debug-sr.c +2 -0
index 4558c02eb352..7fd876d4f034 100644
@@ -51,32 +51,45 @@ 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;
+    u64 host_trfcr_el1 = read_sysreg_s(SYS_TRFCR_EL1);
+    u64 guest_trfcr_el1;
+
+    vcpu->arch.host_debug_state.trfcr_el1 = host_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 (vcpu_get_flag(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.
+         */
+        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.
+         */
+        guest_trfcr_el1 = kvm_host_global_state[vcpu->cpu].guest_trfcr_el1;
+        if (host_trfcr_el1 != guest_trfcr_el1)
+            write_sysreg_s(guest_trfcr_el1, SYS_TRFCR_EL1); +    }
  }
    static void __debug_restore_trace(u64 trfcr_el1)

Quoted 8 lines

  {
-    if (!trfcr_el1)
-        return;
-
      /* Restore trace filter controls */
-    write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1); +    if (trfcr_el1 != read_sysreg_s(SYS_TRFCR_EL1)) +        write_sysreg_s(trfcr_el1, SYS_TRFCR_EL1);

Could we not write it unconditionally here ? In the saving step, we have to save the host setting. But while restoring, we could skip the check. A read and write is probably the same cost, as the value is implicitly synchronized by a later ISB.

Quoted 5 lines

Eitherways,

I did also wonder if it was better to just do it unconditionally. I'll update it in the next version.

Quoted 24 lines
Quoted 23 lines

  }
    void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)

arch/arm64/kvm/hyp/nvhe/debug-sr.c +3 -3
@@ -85,8 +98,8 @@ 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))
-        __debug_save_trace(&vcpu->arch.host_debug_state.trfcr_el1);
+    if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
+        __debug_save_trace(vcpu);
   }
     void __debug_switch_to_guest(struct kvm_vcpu *vcpu)
@@ -98,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_TRBE))
+    if (vcpu_get_flag(vcpu, DEBUG_STATE_SAVE_TRFCR))
           __debug_restore_trace(vcpu->arch.host_debug_state.trfcr_el1);
   }

#26 lore raw

PATCH v4 7/7 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 34aee59dd147..885d70fd6f40 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 Reviewed-by on v4 7/7
#27 lore raw

Re: [PATCH v4 7/7] coresight: Pass guest TRFCR value to KVM

Quoted 35 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
Quoted 7 lines

Host userspace only:

   $  perf record -e cs_etm//Hu -a -- true
     408 EL0
       1 EL2
Quoted 116 lines
---
  .../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 34aee59dd147..885d70fd6f40 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;