Skip to contentSkip to conversation

ARM_PMU: Trap PMCR when MDCR_EL2.TPMCR is set

PATCH Applied Smail AIDER via 5 participants 11 messages Aug 11, 2025 – Sep 1, 2025

lore
Share & tools
lore
#0 lore raw

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

Changes from v2:
- Squashed patches, no other changes.

Changes from v1:
- Some refactoring to simplify code.

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

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

PATCH v3 1/1 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 | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
target/arm/cpregs-pmu.c +25 -8
index 9c4431c18b..13392ddc4c 100644
@@ -228,22 +228,27 @@ 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);
 
     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) {
+        uint64_t 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,6 +257,18 @@ static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
     return CP_ACCESS_OK;
 }
 
+static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
+                                   bool isread)
+{
+    return do_pmreg_access(env, false);
+}
+
+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,
                                            const ARMCPRegInfo *ri,
                                            bool isread)
@@ -1187,14 +1204,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 v3 1/1] target/arm: Trap PMCR when MDCR_EL2.TPMCR is set

Hi Smail,

(no need to Cc qemu-stable with this patch, it is a simple refactor)

Quoted 18 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]>

---
  target/arm/cpregs-pmu.c | 33 +++++++++++++++++++++++++--------
  1 file changed, 25 insertions(+), 8 deletions(-)
target/arm/cpregs-pmu.c +0 -0
index 9c4431c18b..13392ddc4c 100644

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

Quoted 6 lines
target/arm/cpregs-pmu.c +1 -2
@@ -228,22 +228,27 @@ 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)

"bool is_pmcr" vs ...

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

Quoted 2 lines
target/arm/cpregs-pmu.c +2 -0
+static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
+                                   bool isread)

... "bool isread".

I suppose we want to use "is_pmcr" here instead of "isread".

Quoted 30 lines
target/arm/cpregs-pmu.c +12 -2
+{
+    return do_pmreg_access(env, false);
+}
+
+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,
                                             const ARMCPRegInfo *ri,
                                             bool isread)
@@ -1187,14 +1204,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),
#3 lore raw

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

Hi Philippe,

It is not just some refactoring. The last patch v3 is a squash of two previous patches v1 and v2. Maybe I need to change the history description if not clear (I was talking from v3 point of view). The purpose of the series is the main description itself. Please check the v1 below:

https://patchew.org/QEMU/[email protected]/

Other than that, the argument (is_pmcr) is correct. "isread" is not used in this case.

--
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: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, August 11, 2025 2:36 PM
To: Smail AIDER <[email protected]>; [email protected]
Cc: Alexander Spyridakis <[email protected]>; zhangyue (BA) <[email protected]>; Liuyutao(DRC) <[email protected]>; [email protected]; Peter Maydell <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v3 1/1] target/arm: Trap PMCR when MDCR_EL2.TPMCR is set

Hi Smail,

(no need to Cc qemu-stable with this patch, it is a simple refactor)

Quoted 18 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]>

---
  target/arm/cpregs-pmu.c | 33 +++++++++++++++++++++++++--------
  1 file changed, 25 insertions(+), 8 deletions(-)
target/arm/cpregs-pmu.c +0 -0
index 9c4431c18b..13392ddc4c 100644

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

Quoted 6 lines
target/arm/cpregs-pmu.c +1 -2
@@ -228,22 +228,27 @@ 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)

"bool is_pmcr" vs ...

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

Quoted 2 lines
target/arm/cpregs-pmu.c +2 -0
+static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
+                                   bool isread)

... "bool isread".

I suppose we want to use "is_pmcr" here instead of "isread".

Quoted 30 lines
target/arm/cpregs-pmu.c +12 -2
+{
+    return do_pmreg_access(env, false);
+}
+
+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,
                                             const ARMCPRegInfo *ri,
                                             bool isread)
@@ -1187,14 +1204,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),
#4 lore raw

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

Quoted 7 lines · Smail AIDER

Hi Philippe,

It is not just some refactoring. The last patch v3 is a squash of two previous patches v1 and v2. Maybe I need to change the history description if not clear (I was talking from v3 point of view). The purpose of the series is the main description itself. Please check the v1 below:

https://patchew.org/QEMU/[email protected]/

Then please add a Cc tag (maintainer can do it if this v3 is OK, no need for v4):

Quoted 1 line

Other than that, the argument (is_pmcr) is correct. "isread" is not used in this case.

Right, I missed it during review. Maybe we want to forward the arguments for clarity?

-static CPAccessResult pmreg_access(CPUARMState *env, const
ARMCPRegInfo *ri,
- bool isread)
+static CPAccessResult do_pmreg_access(CPUARMState *env, const
ARMCPRegInfo *ri,
+ bool isread, bool is_pmcr)

Anyhow I'll let Richard review. No objection.

Quoted 86 lines

--
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: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, August 11, 2025 2:36 PM
To: Smail AIDER <[email protected]>; [email protected]
Cc: Alexander Spyridakis <[email protected]>; zhangyue (BA) <[email protected]>; Liuyutao(DRC) <[email protected]>; [email protected]; Peter Maydell <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v3 1/1] target/arm: Trap PMCR when MDCR_EL2.TPMCR is set

Hi Smail,

(no need to Cc qemu-stable with this patch, it is a simple refactor)

Quoted 18 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]>

---
   target/arm/cpregs-pmu.c | 33 +++++++++++++++++++++++++--------
   1 file changed, 25 insertions(+), 8 deletions(-)
target/arm/cpregs-pmu.c +0 -0
index 9c4431c18b..13392ddc4c 100644

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

Quoted 6 lines
target/arm/cpregs-pmu.c +1 -2
@@ -228,22 +228,27 @@ 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)

"bool is_pmcr" vs ...

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

Quoted 2 lines
target/arm/cpregs-pmu.c +2 -0
+static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
+                                   bool isread)

... "bool isread".

I suppose we want to use "is_pmcr" here instead of "isread".

Quoted 30 lines
target/arm/cpregs-pmu.c +12 -2
+{
+    return do_pmreg_access(env, false);
+}
+
+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,
                                              const ARMCPRegInfo *ri,
                                              bool isread)
@@ -1187,14 +1204,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),
#5 lore raw

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

Quoted 5 lines · Philippe Mathieu-Daudé

On 11/8/25 15:33, Smail AIDER wrote:

Then please add a Cc tag (maintainer can do it if this v3 is OK, no need for v4):

I'm picking it up already - it's on my watch list since the v1 :)

/mjt

#6 lore raw

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

Hi Michael,

Could you please remove "via" from the author name, it's not part of my name : ) I guess it was just a typo.

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: Monday, August 11, 2025 3:48 PM
To: Philippe Mathieu-Daudé <[email protected]>; Smail AIDER <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v3 1/1] target/arm: Trap PMCR when MDCR_EL2.TPMCR is set

Quoted 6 lines · Philippe Mathieu-Daudé

On 11/8/25 15:33, Smail AIDER wrote:

Then please add a Cc tag (maintainer can do it if this v3 is OK, no need for v4):

I'm picking it up already - it's on my watch list since the v1 :)

/mjt

#7 lore raw

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

Quoted 3 lines · Smail AIDER

Hi Michael,

Could you please remove "via" from the author name, it's not part of my name : ) I guess it was just a typo.

I'm not the one to do this and there's nothing to remove really - it is the mailing list who does it, replacing From with the mailing list itself. It will be done by whomever applies the patch to the master branch. Don't worry about it.

Thanks,

/mjt

#8 lore raw

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

In reply to Smail AIDER via
Quoted 9 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]>

Hi! Please take a look at
https://gitlab.com/mjt0k/qemu/-/commit/842918ee3c1cb9ac687bb27920c8d9020c2c109b
for my back-port of this commit to v10.0.x series, -- does it look sane? I assume this change is targetting 10.0.x too, not just 10.1.x.

Thanks,

/mjt

#9 lore raw

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

Hi Michael,

Your back-port commit is sane. Yes, I suppose this change needs to be ported to other releases. It has already been applied to the next 10.2 by @Peter Maydell.

Thanks,
Smail AIDER

-----Original Message-----

Hi! Please take a look at
https://gitlab.com/mjt0k/qemu/-/commit/842918ee3c1cb9ac687bb27920c8d9020c2c109b
for my back-port of this commit to v10.0.x series, -- does it look sane? I assume this change is targetting 10.0.x too, not just 10.1.x.

Thanks,

/mjt

#10 lore raw
In reply to Smail AIDER via
Quoted 5 lines · Smail AIDER

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.

Quoted 8 lines

Changes from v2:
- Squashed patches, no other changes.

Changes from v1:
- Some refactoring to simplify code.

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

Applied to target-arm.next for 10.2, thanks.

PS: a cover letter is not necessary for a single patch, only for multiple patch series.

-- PMM