Skip to contentSkip to conversation

KVM: arm64: Trap guest MPAM accesses whenever MPAM is implemented

PATCH Superseded Fuad Tabba 5 participants 9 messages Sep 11 – Sep 24

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

PATCH v3 1/1 KVM: arm64: Trap guest MPAM accesses whenever MPAM is implemented

finalise_el2_state() clears the EL2 MPAM traps whenever the ID registers advertise MPAM, while KVM sets them only under ARM64_MPAM, which also requires MPAMEN. Without EL3 nothing sets that enable, so a guest reaches the MPAM registers while ID_AA64PFR0_EL1.MPAM reads 0 for it.

Gate on what finalise_el2_state() tests instead: this CPU's ID registers with the arm64.nompam override applied, and its MPAMIDR_EL1.HAS_HCR before writing MPAMHCR_EL2, which is UNDEFINED without it. MPAMEN isn't a term in any MPAM accessor, so the traps take effect without it.

Fixes 31ff96c38ea3 ("KVM: arm64: Fix missing traps of guest accesses to the MPAM registers")

---

Notes:
Changes since v2:
- A per-CPU flag in kvm_host_data, set at KVM's CPU init from the ID registers with the override applied, instead of a third cpucap (Will). The probe checks presence only, since MPAM3_EL3.TRAPLOWER can't be read at EL2.
- The MPAMHCR_EL2 branch reads this CPU's MPAMIDR_EL1 rather than the sanitised ARM64_MPAM_HCR, which would otherwise have been the one system-wide test left in a per-CPU function.
- Fixes: names the KVM commit that left this case open, rather than the head.S commit that cleared the traps.
- Yuan's Reviewed-by dropped, since the mechanism changed.

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 arch/arm64/include/asm/kvm_host.h       |  1 +
 arch/arm64/kvm/arm.c                    |  7 +++++++
 arch/arm64/kvm/hyp/include/hyp/switch.h | 13 ++++++++-----
 3 files changed, 16 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 27fe0cd5b2d7a..98ca2d9b9e18d 100644
@@ -755,6 +755,7 @@ struct kvm_host_data {
 #define KVM_HOST_DATA_FLAG_VCPU_IN_HYP_CONTEXT		4
 #define KVM_HOST_DATA_FLAG_L1_VNCR_MAPPED		5
 #define KVM_HOST_DATA_FLAG_HAS_BRBE			6
+#define KVM_HOST_DATA_FLAG_HAS_MPAM			7
 	unsigned long flags;
 
 	struct kvm_cpu_context host_ctxt;
arch/arm64/kvm/arm.c +7 -0
index 8b080804bc90b..fde75a63cf045 100644
@@ -2281,9 +2281,16 @@ static void cpu_set_hyp_vector(void)
 
 static void cpu_hyp_init_context(void)
 {
+	u64 pfr0 = __read_sysreg_by_encoding(SYS_ID_AA64PFR0_EL1);
+	u64 pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
+
 	kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
 	kvm_init_host_debug_data();
 
+	/* The traps take effect without MPAMEN, which ARM64_MPAM requires. */
+	if (id_aa64pfr0_mpam(pfr0) || id_aa64pfr1_mpamfrac(pfr1))
+		host_data_set_flag(HAS_MPAM);
+
 	if (!is_kernel_in_hyp_mode())
 		cpu_init_hyp_mode();
 }
arch/arm64/kvm/hyp/include/hyp/switch.h +8 -5
index 1ce7130e25490..2cb611e2bd69b 100644
@@ -298,14 +298,17 @@ static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)
 	u64 clr = MPAM2_EL2_EnMPAMSM;
 	u64 set = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1;
 
-	if (!system_supports_mpam())
+	if (!host_data_test_flag(HAS_MPAM))
 		return;
 
 	/* trap guest access to MPAMIDR_EL1 */
-	if (system_supports_mpam_hcr()) {
+	if (read_sysreg_s(SYS_MPAMIDR_EL1) & MPAMIDR_EL1_HAS_HCR) {
 		write_sysreg_s(MPAMHCR_EL2_TRAP_MPAMIDR_EL1, SYS_MPAMHCR_EL2);
 	} else {
-		/* From v1.1 TIDR can trap MPAMIDR, set it unconditionally */
+		/*
+		 * TIDR is RES0 without MPAMIDR_EL1.HAS_TIDR, which MPAM v1.0
+		 * prohibits: such a PE without HAS_HCR can't trap MPAMIDR_EL1.
+		 */
 		set |= MPAM2_EL2_TIDR;
 	}
 
@@ -317,12 +320,12 @@ static inline void __deactivate_traps_mpam(void)
 	u64 clr = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1 | MPAM2_EL2_TIDR;
 	u64 set = MPAM2_EL2_EnMPAMSM;
 
-	if (!system_supports_mpam())
+	if (!host_data_test_flag(HAS_MPAM))
 		return;
 
 	sysreg_clear_set_s(SYS_MPAM2_EL2, clr, set);
 
-	if (system_supports_mpam_hcr())
+	if (read_sysreg_s(SYS_MPAMIDR_EL1) & MPAMIDR_EL1_HAS_HCR)
 		write_sysreg_s(MPAMHCR_HOST_FLAGS, SYS_MPAMHCR_EL2);
 }
 
Signature · 1 line
2.39.5
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] KVM fails to trap guest MPAM accesses if the host boots with the arm64.nompam kernel parameter on MPAM-capable hardware, potentially allowing the guest to bypass virtualization isolation. --

commit b4685c49d4f8e9f9eb2e612a24ecf71fcf533869
Author: Fuad Tabba <[email protected]>

KVM: arm64: Trap guest MPAM accesses whenever MPAM is implemented

This commit changes how KVM determines whether to trap guest MPAM accesses. It relies on a per-CPU flag set based on the ID registers rather than using the system-wide system_supports_mpam() check.

arch/arm64/kvm/arm.c static void cpu_set_hyp_vector(void) · line 2286

Quoted 11 lines
arch/arm64/kvm/arm.c +3 -0
index 8b080804bc90b..fde75a63cf045 100644
@@ -2281,9 +2281,16 @@ static void cpu_set_hyp_vector(void)
 
 static void cpu_hyp_init_context(void)
 {
+	u64 pfr0 = __read_sysreg_by_encoding(SYS_ID_AA64PFR0_EL1);
+	u64 pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
+

[Severity: High]
This is a pre-existing issue, but does reading the software-overridden ID register value here in cpu_hyp_init_context() allow the arm64.nompam kernel parameter to falsely report no MPAM hardware to the KVM trap initialization logic?

By using __read_sysreg_by_encoding(), the software override for arm64.nompam forces the MPAM fields to 0. This causes the HAS_MPAM flag to evaluate to false even when physical MPAM hardware is present.

Quoted 9 lines
arch/arm64/kvm/arm.c +3 -0
 	kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
 	kvm_init_host_debug_data();
 
+	/* The traps take effect without MPAMEN, which ARM64_MPAM requires. */
+	if (id_aa64pfr0_mpam(pfr0) || id_aa64pfr1_mpamfrac(pfr1))
+		host_data_set_flag(HAS_MPAM);
 
 	if (!is_kernel_in_hyp_mode())
 		cpu_init_hyp_mode();

[ ... ]

arch/arm64/kvm/hyp/include/hyp/switch.h static inline void __activate_traps_mpam(struct kvm_vcpu *vcpu) · line 302

Quoted 11 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -1
index 1ce7130e25490..2cb611e2bd69b 100644
@@ -298,14 +298,17 @@ static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)
 	u64 clr = MPAM2_EL2_EnMPAMSM;
 	u64 set = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1;
 
-	if (!system_supports_mpam())
+	if (!host_data_test_flag(HAS_MPAM))
 		return;

[Severity: High]
This isn't a bug introduced by this patch, but does skipping the guest MPAM trap activation in __activate_traps_mpam() based on the overridden HAS_MPAM flag leave the hardware trap state at its UNKNOWN reset value?

When the host boots with the arm64.nompam parameter, HAS_MPAM is false. This causes KVM to skip setting the MPAM2_EL2 trap bits (TRAPMPAM0EL1 and TRAPMPAM1EL1) during guest entry. Additionally, the EL2 initialization in finalise_el2_state() also skips initializing MPAM2_EL2 when the ID registers are overridden.

Because MPAM2_EL2 is never initialized by the host, it retains its architecturally UNKNOWN reset value. If its trap bits happen to reset to 0, a guest can execute MPAM instructions without trapping to KVM. This would allow the guest to bypass virtualization isolation and directly manipulate memory bandwidth and cache partitioning across the system, potentially causing resource starvation or side-channel information leaks.

Does this patch codify and reinforce gating the trap initialization on the overridden software ID registers rather than the physical hardware capabilities?

Quoted 5 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -1
 	/* trap guest access to MPAMIDR_EL1 */
-	if (system_supports_mpam_hcr()) {
+	if (read_sysreg_s(SYS_MPAMIDR_EL1) & MPAMIDR_EL1_HAS_HCR) {
 		write_sysreg_s(MPAMHCR_EL2_TRAP_MPAMIDR_EL1, SYS_MPAMHCR_EL2);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#2 lore raw
Quoted 5 lines · On Fri, 11 Sept 2026 at 12:02, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] KVM fails to trap guest MPAM accesses if the host boots with the arm64.nompam kernel parameter on MPAM-capable hardware, potentially allowing the guest to bypass virtualization isolation.

Right, and it's documented rather than fixed: [1].

Cheers,
/fuad

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

Quoted 87 lines

--

commit b4685c49d4f8e9f9eb2e612a24ecf71fcf533869
Author: Fuad Tabba <[email protected]>

KVM: arm64: Trap guest MPAM accesses whenever MPAM is implemented

This commit changes how KVM determines whether to trap guest MPAM accesses. It relies on a per-CPU flag set based on the ID registers rather than using the system-wide system_supports_mpam() check.

arch/arm64/kvm/arm.c static void cpu_set_hyp_vector(void) · line 2286

Quoted 11 lines
arch/arm64/kvm/arm.c +3 -0
index 8b080804bc90b..fde75a63cf045 100644
@@ -2281,9 +2281,16 @@ static void cpu_set_hyp_vector(void)
 
 static void cpu_hyp_init_context(void)
 {
+     u64 pfr0 = __read_sysreg_by_encoding(SYS_ID_AA64PFR0_EL1);
+     u64 pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
+

[Severity: High]
This is a pre-existing issue, but does reading the software-overridden ID register value here in cpu_hyp_init_context() allow the arm64.nompam kernel parameter to falsely report no MPAM hardware to the KVM trap initialization logic?

By using __read_sysreg_by_encoding(), the software override for arm64.nompam forces the MPAM fields to 0. This causes the HAS_MPAM flag to evaluate to false even when physical MPAM hardware is present.

Quoted 9 lines
arch/arm64/kvm/arm.c +3 -0
      kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
      kvm_init_host_debug_data();
 
+     /* The traps take effect without MPAMEN, which ARM64_MPAM requires. */
+     if (id_aa64pfr0_mpam(pfr0) || id_aa64pfr1_mpamfrac(pfr1))
+             host_data_set_flag(HAS_MPAM);
 
      if (!is_kernel_in_hyp_mode())
              cpu_init_hyp_mode();

[ ... ]

arch/arm64/kvm/hyp/include/hyp/switch.h static inline void __activate_traps_mpam(struct kvm_vcpu *vcpu) · line 302

Quoted 11 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -1
index 1ce7130e25490..2cb611e2bd69b 100644
@@ -298,14 +298,17 @@ static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)
      u64 clr = MPAM2_EL2_EnMPAMSM;
      u64 set = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1;
 
-     if (!system_supports_mpam())
+     if (!host_data_test_flag(HAS_MPAM))
              return;

[Severity: High]
This isn't a bug introduced by this patch, but does skipping the guest MPAM trap activation in __activate_traps_mpam() based on the overridden HAS_MPAM flag leave the hardware trap state at its UNKNOWN reset value?

When the host boots with the arm64.nompam parameter, HAS_MPAM is false. This causes KVM to skip setting the MPAM2_EL2 trap bits (TRAPMPAM0EL1 and TRAPMPAM1EL1) during guest entry. Additionally, the EL2 initialization in finalise_el2_state() also skips initializing MPAM2_EL2 when the ID registers are overridden.

Because MPAM2_EL2 is never initialized by the host, it retains its architecturally UNKNOWN reset value. If its trap bits happen to reset to 0, a guest can execute MPAM instructions without trapping to KVM. This would allow the guest to bypass virtualization isolation and directly manipulate memory bandwidth and cache partitioning across the system, potentially causing resource starvation or side-channel information leaks.

Does this patch codify and reinforce gating the trap initialization on the overridden software ID registers rather than the physical hardware capabilities?

arch/arm64/kvm/hyp/include/hyp/switch.h static inline void __activate_traps_mpam(struct kvm_vcpu *vcpu) · line 306

Quoted 5 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -1
      /* trap guest access to MPAMIDR_EL1 */
-     if (system_supports_mpam_hcr()) {
+     if (read_sysreg_s(SYS_MPAMIDR_EL1) & MPAMIDR_EL1_HAS_HCR) {
              write_sysreg_s(MPAMHCR_EL2_TRAP_MPAMIDR_EL1, SYS_MPAMHCR_EL2);

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

Reviewed-by on v3 1/1
#3 lore raw
In reply to Fuad Tabba

Hi Fuad,

Quoted 3 lines · Fuad Tabba

finalise_el2_state() clears the EL2 MPAM traps whenever the ID registers advertise MPAM, while KVM sets them only under ARM64_MPAM, which also requires MPAMEN. Without EL3 nothing sets that enable, so a

This "Without EL3..." part reads oddly to me. I guess what you are getting at is that the MPAMEN is read only apart from at the highest implemented exception level.

Quoted 11 lines

guest reaches the MPAM registers while ID_AA64PFR0_EL1.MPAM reads 0 for it.

Gate on what finalise_el2_state() tests instead: this CPU's ID registers with the arm64.nompam override applied, and its MPAMIDR_EL1.HAS_HCR before writing MPAMHCR_EL2, which is UNDEFINED without it. MPAMEN isn't a term in any MPAM accessor, so the traps take effect without it.

Fixes 31ff96c38ea3 ("KVM: arm64: Fix missing traps of guest accesses to the MPAM registers")

This looks good to me. Now that you're determining if the MPAM registers are present on a per CPU basis and using that via a host flag, the traps will be set on all CPUs that have MPAM registers unless overridden on the command line. IIUC this covers the mismatched CPU case as well and so all cases that the register can be safely accessed (arm64.nompam not required) are covered.

Quoted 68 lines

---

Notes:
Changes since v2:
- A per-CPU flag in kvm_host_data, set at KVM's CPU init from the ID registers with the override applied, instead of a third cpucap (Will). The probe checks presence only, since MPAM3_EL3.TRAPLOWER can't be read at EL2.
- The MPAMHCR_EL2 branch reads this CPU's MPAMIDR_EL1 rather than the sanitised ARM64_MPAM_HCR, which would otherwise have been the one system-wide test left in a per-CPU function.
- Fixes: names the KVM commit that left this case open, rather than the head.S commit that cleared the traps.
- Yuan's Reviewed-by dropped, since the mechanism changed.

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 arch/arm64/include/asm/kvm_host.h       |  1 +
 arch/arm64/kvm/arm.c                    |  7 +++++++
 arch/arm64/kvm/hyp/include/hyp/switch.h | 13 ++++++++-----
 3 files changed, 16 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 27fe0cd5b2d7a..98ca2d9b9e18d 100644
@@ -755,6 +755,7 @@ struct kvm_host_data {
 #define KVM_HOST_DATA_FLAG_VCPU_IN_HYP_CONTEXT		4
 #define KVM_HOST_DATA_FLAG_L1_VNCR_MAPPED		5
 #define KVM_HOST_DATA_FLAG_HAS_BRBE			6
+#define KVM_HOST_DATA_FLAG_HAS_MPAM			7
 	unsigned long flags;
 
 	struct kvm_cpu_context host_ctxt;
arch/arm64/kvm/arm.c +7 -0
index 8b080804bc90b..fde75a63cf045 100644
@@ -2281,9 +2281,16 @@ static void cpu_set_hyp_vector(void)
 
 static void cpu_hyp_init_context(void)
 {
+	u64 pfr0 = __read_sysreg_by_encoding(SYS_ID_AA64PFR0_EL1);
+	u64 pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
+
 	kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
 	kvm_init_host_debug_data();
 
+	/* The traps take effect without MPAMEN, which ARM64_MPAM requires. */
+	if (id_aa64pfr0_mpam(pfr0) || id_aa64pfr1_mpamfrac(pfr1))
+		host_data_set_flag(HAS_MPAM);
+
 	if (!is_kernel_in_hyp_mode())
 		cpu_init_hyp_mode();
 }
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -1
index 1ce7130e25490..2cb611e2bd69b 100644
@@ -298,14 +298,17 @@ static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)
 	u64 clr = MPAM2_EL2_EnMPAMSM;
 	u64 set = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1;
 
-	if (!system_supports_mpam())
+	if (!host_data_test_flag(HAS_MPAM))
 		return;
 

arch/arm64/kvm/hyp/include/hyp/switch.h static inline void __activate_traps_mpam(struct kvm_vcpu *vcpu) · line 311

Quoted 10 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +5 -2
 	/* trap guest access to MPAMIDR_EL1 */
-	if (system_supports_mpam_hcr()) {
+	if (read_sysreg_s(SYS_MPAMIDR_EL1) & MPAMIDR_EL1_HAS_HCR) {
 		write_sysreg_s(MPAMHCR_EL2_TRAP_MPAMIDR_EL1, SYS_MPAMHCR_EL2);
 	} else {
-		/* From v1.1 TIDR can trap MPAMIDR, set it unconditionally */
+		/*
+		 * TIDR is RES0 without MPAMIDR_EL1.HAS_TIDR, which MPAM v1.0
+		 * prohibits: such a PE without HAS_HCR can't trap MPAMIDR_EL1.
+		 */

The lack of a trap too bad as all the fields of MPAMIDR_EL1 are read only.

Thanks,

Ben

Quoted 18 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -2
 		set |= MPAM2_EL2_TIDR;
 	}
 
@@ -317,12 +320,12 @@ static inline void __deactivate_traps_mpam(void)
 	u64 clr = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1 | MPAM2_EL2_TIDR;
 	u64 set = MPAM2_EL2_EnMPAMSM;
 
-	if (!system_supports_mpam())
+	if (!host_data_test_flag(HAS_MPAM))
 		return;
 
 	sysreg_clear_set_s(SYS_MPAM2_EL2, clr, set);
 
-	if (system_supports_mpam_hcr())
+	if (read_sysreg_s(SYS_MPAMIDR_EL1) & MPAMIDR_EL1_HAS_HCR)
 		write_sysreg_s(MPAMHCR_HOST_FLAGS, SYS_MPAMHCR_EL2);
 }
 
maintainer
#4 lore raw
In reply to Fuad Tabba
Quoted 55 lines · Fuad Tabba

finalise_el2_state() clears the EL2 MPAM traps whenever the ID registers advertise MPAM, while KVM sets them only under ARM64_MPAM, which also requires MPAMEN. Without EL3 nothing sets that enable, so a guest reaches the MPAM registers while ID_AA64PFR0_EL1.MPAM reads 0 for it.

Gate on what finalise_el2_state() tests instead: this CPU's ID registers with the arm64.nompam override applied, and its MPAMIDR_EL1.HAS_HCR before writing MPAMHCR_EL2, which is UNDEFINED without it. MPAMEN isn't a term in any MPAM accessor, so the traps take effect without it.

Fixes 31ff96c38ea3 ("KVM: arm64: Fix missing traps of guest accesses to the MPAM registers")

---

Notes:
Changes since v2:
- A per-CPU flag in kvm_host_data, set at KVM's CPU init from the ID registers with the override applied, instead of a third cpucap (Will). The probe checks presence only, since MPAM3_EL3.TRAPLOWER can't be read at EL2.
- The MPAMHCR_EL2 branch reads this CPU's MPAMIDR_EL1 rather than the sanitised ARM64_MPAM_HCR, which would otherwise have been the one system-wide test left in a per-CPU function.
- Fixes: names the KVM commit that left this case open, rather than the head.S commit that cleared the traps.
- Yuan's Reviewed-by dropped, since the mechanism changed.

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 arch/arm64/include/asm/kvm_host.h       |  1 +
 arch/arm64/kvm/arm.c                    |  7 +++++++
 arch/arm64/kvm/hyp/include/hyp/switch.h | 13 ++++++++-----
 3 files changed, 16 insertions(+), 5 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 27fe0cd5b2d7a..98ca2d9b9e18d 100644
@@ -755,6 +755,7 @@ struct kvm_host_data {
 #define KVM_HOST_DATA_FLAG_VCPU_IN_HYP_CONTEXT		4
 #define KVM_HOST_DATA_FLAG_L1_VNCR_MAPPED		5
 #define KVM_HOST_DATA_FLAG_HAS_BRBE			6
+#define KVM_HOST_DATA_FLAG_HAS_MPAM			7
 	unsigned long flags;
 
 	struct kvm_cpu_context host_ctxt;
arch/arm64/kvm/arm.c +0 -0
index 8b080804bc90b..fde75a63cf045 100644

arch/arm64/kvm/arm.c static void cpu_set_hyp_vector(void) · line 2286

Quoted 6 lines
arch/arm64/kvm/arm.c +3 -0
@@ -2281,9 +2281,16 @@ static void cpu_set_hyp_vector(void)
 
 static void cpu_hyp_init_context(void)
 {
+	u64 pfr0 = __read_sysreg_by_encoding(SYS_ID_AA64PFR0_EL1);
+	u64 pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
+

Why not directly read_sysreg(id_aa64pfr0_el1) and co? __read_sysreg_by_encoding() is useful when the encoding comes from a variable, but it looks odd in the case of a literal sysreg.

Quoted 15 lines
arch/arm64/kvm/arm.c +4 -0
 	kvm_init_host_cpu_context(host_data_ptr(host_ctxt));
 	kvm_init_host_debug_data();
 
+	/* The traps take effect without MPAMEN, which ARM64_MPAM requires. */
+	if (id_aa64pfr0_mpam(pfr0) || id_aa64pfr1_mpamfrac(pfr1))
+		host_data_set_flag(HAS_MPAM);
+
 	if (!is_kernel_in_hyp_mode())
 		cpu_init_hyp_mode();
 }
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -0
index 1ce7130e25490..2cb611e2bd69b 100644

arch/arm64/kvm/hyp/include/hyp/switch.h static inline void __activate_traps_mpam(struct kvm_vcpu *vcpu) · line 305

Quoted 10 lines
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -2
@@ -298,14 +298,17 @@ static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)
 	u64 clr = MPAM2_EL2_EnMPAMSM;
 	u64 set = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1;
 
-	if (!system_supports_mpam())
+	if (!host_data_test_flag(HAS_MPAM))
 		return;
 
 	/* trap guest access to MPAMIDR_EL1 */
-	if (system_supports_mpam_hcr()) {
+	if (read_sysreg_s(SYS_MPAMIDR_EL1) & MPAMIDR_EL1_HAS_HCR) {

This is going to suck under NV. The host hypervisor is of course going to set MPAMHCR_EL2.TRAP_MPAMIDR_EL1, and we're in for a recursive trap on the hottest possible path in KVM. Which is silly as the actual write to MPAMHCR_EL2 is free (it lands in NVMem[]).

This really should be replaced by a flag called HAS_MPAM_HCR, just like you have HAS_MPAM.

Thanks,

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

Hi Marc,

On Sun, 13 Sept 2026 at 11:00, Marc Zyngier <[email protected]> wrote:
[...]

Quoted 1 line
Quoted 1 line
arch/arm64/kvm/arm.c +0 -0

[...]

Quoted 9 lines
Quoted 5 lines

static void cpu_hyp_init_context(void)
{
+ u64 pfr0 = __read_sysreg_by_encoding(SYS_ID_AA64PFR0_EL1);
+ u64 pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1); +

Why not directly read_sysreg(id_aa64pfr0_el1) and co? __read_sysreg_by_encoding() is useful when the encoding comes from a variable, but it looks odd in the case of a literal sysreg.

It applies the arm64.nompam override, which is what finalise_el2_state() tests. With the override set, EL2 setup leaves the MPAM traps alone, and a raw read_sysreg() would still see MPAM in the ID registers, so KVM would write MPAM2_EL2 on exactly the firmware the option exists for. I'll add a comment.

[...]

Quoted 1 line
Quoted 1 line
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -0

[...]

Quoted 1 line
Quoted 1 line
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -0
@@ -298,14 +298,17 @@ static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)

[...]

Quoted 11 lines
Quoted 3 lines

/* trap guest access to MPAMIDR_EL1 */
- if (system_supports_mpam_hcr()) {
+ if (read_sysreg_s(SYS_MPAMIDR_EL1) & MPAMIDR_EL1_HAS_HCR) {

This is going to suck under NV. The host hypervisor is of course going to set MPAMHCR_EL2.TRAP_MPAMIDR_EL1, and we're in for a recursive trap on the hottest possible path in KVM. Which is silly as the actual write to MPAMHCR_EL2 is free (it lands in NVMem[]).

This really should be replaced by a flag called HAS_MPAM_HCR, just like you have HAS_MPAM.

I'll probe MPAMIDR_EL1.HAS_HCR once in cpu_hyp_init_context(), next to HAS_MPAM, and test the flag in both trap functions instead.

I'll hold off on v4 until Will has had a chance to comment as well.

Cheers,
/fuad

maintainer
#6 lore raw
Quoted 17 lines · Fuad Tabba

Hi Marc,

On Sun, 13 Sept 2026 at 11:00, Marc Zyngier <[email protected]> wrote:
[...]

Quoted 1 line
Quoted 1 line
arch/arm64/kvm/arm.c +0 -0

[...]

Quoted 9 lines
Quoted 5 lines

static void cpu_hyp_init_context(void)
{
+ u64 pfr0 = __read_sysreg_by_encoding(SYS_ID_AA64PFR0_EL1);
+ u64 pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1); +

Why not directly read_sysreg(id_aa64pfr0_el1) and co? __read_sysreg_by_encoding() is useful when the encoding comes from a variable, but it looks odd in the case of a literal sysreg.

Quoted 5 lines

It applies the arm64.nompam override, which is what finalise_el2_state() tests. With the override set, EL2 setup leaves the MPAM traps alone, and a raw read_sysreg() would still see MPAM in the ID registers, so KVM would write MPAM2_EL2 on exactly the firmware the option exists for. I'll add a comment.

Ah, good point. A comment would help indeed.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#7 lore raw
In reply to Fuad Tabba
Quoted 34 lines · Fuad Tabba

On Sun, 13 Sept 2026 at 11:00, Marc Zyngier <[email protected]> wrote:
[...]

Quoted 1 line
Quoted 1 line
arch/arm64/kvm/arm.c +0 -0

[...]

Quoted 9 lines
Quoted 5 lines

static void cpu_hyp_init_context(void)
{
+ u64 pfr0 = __read_sysreg_by_encoding(SYS_ID_AA64PFR0_EL1);
+ u64 pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1); +

Why not directly read_sysreg(id_aa64pfr0_el1) and co? __read_sysreg_by_encoding() is useful when the encoding comes from a variable, but it looks odd in the case of a literal sysreg.

It applies the arm64.nompam override, which is what finalise_el2_state() tests. With the override set, EL2 setup leaves the MPAM traps alone, and a raw read_sysreg() would still see MPAM in the ID registers, so KVM would write MPAM2_EL2 on exactly the firmware the option exists for. I'll add a comment.

[...]

Quoted 1 line
Quoted 1 line
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -0

[...]

Quoted 1 line
Quoted 1 line
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -0
@@ -298,14 +298,17 @@ static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)

[...]

Quoted 9 lines
Quoted 3 lines

/* trap guest access to MPAMIDR_EL1 */
- if (system_supports_mpam_hcr()) {
+ if (read_sysreg_s(SYS_MPAMIDR_EL1) & MPAMIDR_EL1_HAS_HCR) {

This is going to suck under NV. The host hypervisor is of course going to set MPAMHCR_EL2.TRAP_MPAMIDR_EL1, and we're in for a recursive trap on the hottest possible path in KVM. Which is silly as the actual write to MPAMHCR_EL2 is free (it lands in NVMem[]).

Quoted 7 lines
Quoted 2 lines

This really should be replaced by a flag called HAS_MPAM_HCR, just like you have HAS_MPAM.

I'll probe MPAMIDR_EL1.HAS_HCR once in cpu_hyp_init_context(), next to HAS_MPAM, and test the flag in both trap functions instead.

I'll hold off on v4 until Will has had a chance to comment as well.

I'm planning to generalise the logic you added recently in gmid_el1_accessible() so that __cpuinfo_store_cpu() stores the values in 'struct cpuinfo_arm64' with the overrides already applied. I don't think that directly impacts this patch, but it should make the general shape of things easier to reason about. I also need it for the parallel hotplug work.

If you fancy helping with that, please let me know.

Will

author reviewer
#8 lore raw

Hi Will,

On Thu, 24 Sept 2026 at 11:21, Will Deacon <[email protected]> wrote:
[...]

Quoted 11 lines
Quoted 4 lines

I'll probe MPAMIDR_EL1.HAS_HCR once in cpu_hyp_init_context(), next to HAS_MPAM, and test the flag in both trap functions instead.

I'll hold off on v4 until Will has had a chance to comment as well.

I'm planning to generalise the logic you added recently in gmid_el1_accessible() so that __cpuinfo_store_cpu() stores the values in 'struct cpuinfo_arm64' with the overrides already applied. I don't think that directly impacts this patch, but it should make the general shape of things easier to reason about. I also need it for the parallel hotplug work.

In that case, I'll respin this as Marc suggested.

Quoted 1 line

If you fancy helping with that, please let me know.

Sure! Just let me know how I could help.

Cheers,
/fuad

Quoted 1 line

Will