Skip to contentSkip to conversation

ARM_PMU: Trap PMCR when MDCR_EL2.TPMCR is set

PATCH Superseded Smail AIDER via 4 participants 6 messages Aug 1, 2025 – Aug 11, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover ARM_PMU: Trap PMCR when MDCR_EL2.TPMCR is set

Trap PMCR_EL0 or PMCR accesses to EL2 when MDCR_EL2.TPMCR is set. Similar to MDCR_EL2.TPM, MDCR_EL2.TPMCR allows trapping EL0 and EL1 accesses to the PMCR register to EL2.

Smail AIDER (1):
target/arm: Code refactoring of pmreg_access/pmcr

Smail AIDER via (1):
target/arm: Trap PMCR when MDCR_EL2.TPMCR is set

 target/arm/cpregs-pmu.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 target/arm: Trap PMCR when MDCR_EL2.TPMCR is set

From: Smail AIDER via <[email protected]>

Trap PMCR_EL0 or PMCR accesses to EL2 when MDCR_EL2.TPMCR is set. Similar to MDCR_EL2.TPM, MDCR_EL2.TPMCR allows trapping EL0 and EL1 accesses to the PMCR register to EL2.

Message-Id: <[email protected]>

---
 target/arm/cpregs-pmu.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
target/arm/cpregs-pmu.c +22 -2
index 0f295b1376..3aacd4f652 100644
@@ -252,6 +252,26 @@ static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
     return CP_ACCESS_OK;
 }
 
+/*
+ * Check for traps to PMCR register, which are controlled by PMUSERENR.EN
+ * for EL1, MDCR_EL2.TPM/TPMCR for EL2 and MDCR_EL3.TPM for EL3.
+ */
+static CPAccessResult pmreg_access_pmcr(CPUARMState *env, const ARMCPRegInfo *ri,
+                                   bool isread)
+{
+    int el = arm_current_el(env);
+    uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
+
+    CPAccessResult ret = pmreg_access(env, ri, isread);
+
+    /* Check MDCR_TPMCR if not already trapped to EL1 */
+    if (ret != CP_ACCESS_TRAP_EL1 && el < 2 && (mdcr_el2 & MDCR_TPMCR)) {
+        ret = CP_ACCESS_TRAP_EL2;
+    }
+
+    return ret;
+}
+
 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
                                            const ARMCPRegInfo *ri,
                                            bool isread)
@@ -1192,14 +1212,14 @@ void define_pm_cpregs(ARMCPU *cpu)
             .fgt = FGT_PMCR_EL0,
             .type = ARM_CP_IO | ARM_CP_ALIAS,
             .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
-            .accessfn = pmreg_access,
+            .accessfn = pmreg_access_pmcr,
             .readfn = pmcr_read, .raw_readfn = raw_read,
             .writefn = pmcr_write, .raw_writefn = raw_write,
         };
         const ARMCPRegInfo pmcr64 = {
             .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
             .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
-            .access = PL0_RW, .accessfn = pmreg_access,
+            .access = PL0_RW, .accessfn = pmreg_access_pmcr,
             .fgt = FGT_PMCR_EL0,
             .type = ARM_CP_IO,
             .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/2] target/arm: Trap PMCR when MDCR_EL2.TPMCR is set

Quoted 8 lines · Smail AIDER via

From: Smail AIDER via <[email protected]>

Trap PMCR_EL0 or PMCR accesses to EL2 when MDCR_EL2.TPMCR is set. Similar to MDCR_EL2.TPM, MDCR_EL2.TPMCR allows trapping EL0 and EL1 accesses to the PMCR register to EL2.

Message-Id: <[email protected]>

I'm assuming this is for qemu-stable too (Cc'ed), just like the v1 of this patch was. Please let me know it it isn't.

Thanks,

/mjt

#3 lore raw

RE: [PATCH v2 1/2] target/arm: Trap PMCR when MDCR_EL2.TPMCR is set

Yes, this patch is also intended to qemu-stable.

Thank you.

--
Best Regards,
Smail AIDER
E-Mail: [email protected]
Operating System Researcher/Developer
Dresden Research Center, OS Kernel Lab
Huawei Technologies Co., Ltd

-----Original Message-----
From: Michael Tokarev <[email protected]>
Sent: Sunday, August 10, 2025 4:41 PM
To: Smail AIDER <[email protected]>; [email protected]
Cc: Alexander Spyridakis <[email protected]>; [email protected]; Peter Maydell <[email protected]>; zhangyue (BA) <[email protected]>; [email protected]; Liuyutao(DRC) <[email protected]>; qemu-stable <[email protected]>
Subject: Re: [PATCH v2 1/2] target/arm: Trap PMCR when MDCR_EL2.TPMCR is set

Quoted 8 lines · Smail AIDER via

From: Smail AIDER via <[email protected]>

Trap PMCR_EL0 or PMCR accesses to EL2 when MDCR_EL2.TPMCR is set. Similar to MDCR_EL2.TPM, MDCR_EL2.TPMCR allows trapping EL0 and EL1 accesses to the PMCR register to EL2.

Message-Id: <[email protected]>

I'm assuming this is for qemu-stable too (Cc'ed), just like the v1 of this patch was. Please let me know it it isn't.

Thanks,

/mjt

#4 lore raw

PATCH v2 2/2 target/arm: Code refactoring of pmreg_access/pmcr

In reply to Smail AIDER via

Create a common helper for pmreg_access and pmreg_access_pmcr to improve readability.

---
 target/arm/cpregs-pmu.c | 40 +++++++++++++++++++---------------------
 1 file changed, 19 insertions(+), 21 deletions(-)
target/arm/cpregs-pmu.c +19 -21
index 3aacd4f652..adaaaf2517 100644
@@ -228,22 +228,28 @@ static bool event_supported(uint16_t number)
     return supported_event_map[number] != UNSUPPORTED_EVENT;
 }
 
-static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
-                                   bool isread)
+static CPAccessResult do_pmreg_access(CPUARMState *env, bool is_pmcr)
 {
     /*
      * Performance monitor registers user accessibility is controlled
-     * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
+     * by PMUSERENR. MDCR_EL2.TPM/TPMCR and MDCR_EL3.TPM allow configurable
      * trapping to EL2 or EL3 for other accesses.
      */
     int el = arm_current_el(env);
-    uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
+    uint64_t mdcr_el2;
 
     if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
         return CP_ACCESS_TRAP_EL1;
     }
-    if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
-        return CP_ACCESS_TRAP_EL2;
+    if (el < 2) {
+        mdcr_el2 = arm_mdcr_el2_eff(env);
+
+        if (mdcr_el2 & MDCR_TPM) {
+            return CP_ACCESS_TRAP_EL2;
+        }
+        if (is_pmcr && (mdcr_el2 & MDCR_TPMCR)) {
+            return CP_ACCESS_TRAP_EL2;
+        }
     }
     if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
         return CP_ACCESS_TRAP_EL3;
@@ -252,24 +258,16 @@ static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
     return CP_ACCESS_OK;
 }
 
-/*
- * Check for traps to PMCR register, which are controlled by PMUSERENR.EN
- * for EL1, MDCR_EL2.TPM/TPMCR for EL2 and MDCR_EL3.TPM for EL3.
- */
-static CPAccessResult pmreg_access_pmcr(CPUARMState *env, const ARMCPRegInfo *ri,
+static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
                                    bool isread)
 {
-    int el = arm_current_el(env);
-    uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
-
-    CPAccessResult ret = pmreg_access(env, ri, isread);
-
-    /* Check MDCR_TPMCR if not already trapped to EL1 */
-    if (ret != CP_ACCESS_TRAP_EL1 && el < 2 && (mdcr_el2 & MDCR_TPMCR)) {
-        ret = CP_ACCESS_TRAP_EL2;
-    }
+    return do_pmreg_access(env, false);
+}
 
-    return ret;
+static CPAccessResult pmreg_access_pmcr(CPUARMState *env, const ARMCPRegInfo *ri,
+                                   bool isread)
+{
+    return do_pmreg_access(env, true);
 }
 
 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env,
Signature · 1 line
2.34.1
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] target/arm: Code refactoring of pmreg_access/pmcr

Quoted 7 lines · Smail AIDER

Create a common helper for pmreg_access and pmreg_access_pmcr to improve readability.

---
  target/arm/cpregs-pmu.c | 40 +++++++++++++++++++---------------------
  1 file changed, 19 insertions(+), 21 deletions(-)

The squash of these two patch is what I asked for.

Quoted 20 lines
target/arm/cpregs-pmu.c +2 -3
index 3aacd4f652..adaaaf2517 100644
@@ -228,22 +228,28 @@ static bool event_supported(uint16_t number)
      return supported_event_map[number] != UNSUPPORTED_EVENT;
  }
 
-static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
-                                   bool isread)
+static CPAccessResult do_pmreg_access(CPUARMState *env, bool is_pmcr)
  {
      /*
       * Performance monitor registers user accessibility is controlled
-     * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
+     * by PMUSERENR. MDCR_EL2.TPM/TPMCR and MDCR_EL3.TPM allow configurable
       * trapping to EL2 or EL3 for other accesses.
       */
      int el = arm_current_el(env);

target/arm/cpregs-pmu.c static bool event_supported(uint16_t number) · line 245

Quoted 10 lines
target/arm/cpregs-pmu.c +3 -3
-    uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);
+    uint64_t mdcr_el2;
 
      if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) {
          return CP_ACCESS_TRAP_EL1;
      }
-    if (el < 2 && (mdcr_el2 & MDCR_TPM)) {
-        return CP_ACCESS_TRAP_EL2;
+    if (el < 2) {
+        mdcr_el2 = arm_mdcr_el2_eff(env);

Move the declaration here too:

	uint64_t mdcr_el2 = arm_mdcr_el2_eff(env);

With that,

r~