Skip to contentSkip to conversation

target/arm: pmu fixes

PATCH Applied Andrew Jones 4 participants 11 messages Mar 22, 2019 – Mar 26, 2019

lore
Share & tools
lore
#0 lore raw

cover target/arm: pmu fixes

The first two patches fix a regression I found when running the arm (as opposed to arm64) pmu kvm-unit-tests test on tcg,

  $ git clone git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git
  $ cd kvm-unit-tests
  $ ./configure --arch=arm --cross-prefix=arm-linux-gnu-
  $ make -j
  $ export QEMU=/path/to/qemu-system-arm or qemu-system-aarch64
  $ arm/run arm/pmu.flat

After checking the QEMU code I found it's also reproducible with the arm64 test if the PMU is removed,

  $ ./configure --arch=arm64 --cross-prefix=aarch64-linux-gnu-
  $ make -j
  $ export QEMU=/path/to/qemu-system-aarch64
  $ arm/run arm/pmu.flat -cpu cortex-a57,pmu=off

Those tests used to pass, but now QEMU was crashing. I've broken the fix into two patches because the second patch is a bit of an RFC since I don't know if it's safe to enable all ARM_FEATURE_PMU code for PMUv2. Maybe that feature is only for PMUv3? This patch also enables the pmu cpu property to work with those cpu types, i.e. we can now do '-cpu cortex-a15,pmu=off' to remove the pmu. Although it wasn't clear to me if the PMU is optional (permitted to be removed) on those cpu types from the manuals.

The last patch is just a trivial cleanup.

Andrew Jones (3):
target/arm: fix crash on pmu register access
target/arm: cortex-a7 and cortex-a15 have pmus
target/arm: make pmccntr_op_start/finish static

 target/arm/cpu.c    |  3 +++
 target/arm/cpu.h    | 11 -----------
 target/arm/helper.c |  8 ++++++--
 3 files changed, 9 insertions(+), 13 deletions(-)
Signature · 1 line
2.17.2
#1 lore raw

PATCH 1/3 target/arm: fix crash on pmu register access

Fix a QEMU NULL derefence that occurs when the guest attempts to enable PMU counters with a non-v8 cpu model or a v8 cpu model which has not configured a PMU.

Fixes 4e7beb0cc0f3 ("target/arm: Add a timer to predict PMU counter overflow")
---
 target/arm/helper.c | 4 ++++
 1 file changed, 4 insertions(+)
target/arm/helper.c +4 -0
index c8d3c213b6b7..fc73488f6cc0 100644
@@ -1259,6 +1259,10 @@ static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
     int el = arm_current_el(env);
     uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
 
+    if (!arm_feature(env, ARM_FEATURE_PMU)) {
+        return false;
+    }
+
     if (!arm_feature(env, ARM_FEATURE_EL2) ||
             (counter < hpmn || counter == 31)) {
         e = env->cp15.c9_pmcr & PMCRE;
Signature · 1 line
2.17.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-arm] [PATCH 1/3] target/arm: fix crash on pmu register access

Quoted 6 lines · Andrew Jones

Fix a QEMU NULL derefence that occurs when the guest attempts to enable PMU counters with a non-v8 cpu model or a v8 cpu model which has not configured a PMU.

Fixes 4e7beb0cc0f3 ("target/arm: Add a timer to predict PMU counter overflow")
Quoted 22 lines
---
 target/arm/helper.c | 4 ++++
 1 file changed, 4 insertions(+)
target/arm/helper.c +4 -0
index c8d3c213b6b7..fc73488f6cc0 100644
@@ -1259,6 +1259,10 @@ static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter)
     int el = arm_current_el(env);
     uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN;
 
+    if (!arm_feature(env, ARM_FEATURE_PMU)) {
+        return false;
+    }
+
     if (!arm_feature(env, ARM_FEATURE_EL2) ||
             (counter < hpmn || counter == 31)) {
         e = env->cp15.c9_pmcr & PMCRE;
Signature · 1 line
2.17.2
#3 lore raw

PATCH 2/3 target/arm: cortex-a7 and cortex-a15 have pmus

In reply to Andrew Jones

cortex-a7 and cortex-a15 have pmus (PMUv2) and they advertise them in ID_DFR0. Let's allow them to function. This also enables the pmu cpu property to work with these cpu types, i.e. we can now do '-cpu cortex-a15,pmu=off' to remove the pmu.

---
 target/arm/cpu.c | 3 +++
 1 file changed, 3 insertions(+)
target/arm/cpu.c +3 -0
index 96f0ff0ec727..504a4771fbd3 100644
@@ -1109,6 +1109,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
 #endif
     } else {
         cpu->id_aa64dfr0 &= ~0xf00;
+        cpu->id_dfr0 &= ~(0xf << 24);
         cpu->pmceid0 = 0;
         cpu->pmceid1 = 0;
     }
@@ -1744,6 +1745,7 @@ static void cortex_a7_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_CBAR_RO);
     set_feature(&cpu->env, ARM_FEATURE_EL2);
     set_feature(&cpu->env, ARM_FEATURE_EL3);
+    set_feature(&cpu->env, ARM_FEATURE_PMU);
     cpu->kvm_target = QEMU_KVM_ARM_TARGET_CORTEX_A7;
     cpu->midr = 0x410fc075;
     cpu->reset_fpsid = 0x41023075;
@@ -1789,6 +1791,7 @@ static void cortex_a15_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_CBAR_RO);
     set_feature(&cpu->env, ARM_FEATURE_EL2);
     set_feature(&cpu->env, ARM_FEATURE_EL3);
+    set_feature(&cpu->env, ARM_FEATURE_PMU);
     cpu->kvm_target = QEMU_KVM_ARM_TARGET_CORTEX_A15;
     cpu->midr = 0x412fc0f1;
     cpu->reset_fpsid = 0x410430f0;
Signature · 1 line
2.17.2
#4 lore raw

Re: [Qemu-arm] [PATCH 2/3] target/arm: cortex-a7 and cortex-a15 have pmus

Quoted 4 lines · Andrew Jones

cortex-a7 and cortex-a15 have pmus (PMUv2) and they advertise them in ID_DFR0. Let's allow them to function. This also enables the pmu cpu property to work with these cpu types, i.e. we can now do '-cpu cortex-a15,pmu=off' to remove the pmu.

I'm a little nervous about this one, but PMUv2 isn't fresh enough in my mind to have a good reason other than that I didn't consider it when writing my recent PMU patches. Do you know if this boots a kernel that can detect and use the PMU successfully while thinking it's PMUv2?

-Aaron

Quoted 36 lines
---
 target/arm/cpu.c | 3 +++
 1 file changed, 3 insertions(+)
target/arm/cpu.c +3 -0
index 96f0ff0ec727..504a4771fbd3 100644
@@ -1109,6 +1109,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
 #endif
     } else {
         cpu->id_aa64dfr0 &= ~0xf00;
+        cpu->id_dfr0 &= ~(0xf << 24);
         cpu->pmceid0 = 0;
         cpu->pmceid1 = 0;
     }
@@ -1744,6 +1745,7 @@ static void cortex_a7_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_CBAR_RO);
     set_feature(&cpu->env, ARM_FEATURE_EL2);
     set_feature(&cpu->env, ARM_FEATURE_EL3);
+    set_feature(&cpu->env, ARM_FEATURE_PMU);
     cpu->kvm_target = QEMU_KVM_ARM_TARGET_CORTEX_A7;
     cpu->midr = 0x410fc075;
     cpu->reset_fpsid = 0x41023075;
@@ -1789,6 +1791,7 @@ static void cortex_a15_initfn(Object *obj)
     set_feature(&cpu->env, ARM_FEATURE_CBAR_RO);
     set_feature(&cpu->env, ARM_FEATURE_EL2);
     set_feature(&cpu->env, ARM_FEATURE_EL3);
+    set_feature(&cpu->env, ARM_FEATURE_PMU);
     cpu->kvm_target = QEMU_KVM_ARM_TARGET_CORTEX_A15;
     cpu->midr = 0x412fc0f1;
     cpu->reset_fpsid = 0x410430f0;
Signature · 1 line
2.17.2
#5 lore raw

Re: [Qemu-arm] [PATCH 2/3] target/arm: cortex-a7 and cortex-a15 have pmus

Quoted 11 lines · Aaron Lindsay OS
Quoted 4 lines · Andrew Jones

cortex-a7 and cortex-a15 have pmus (PMUv2) and they advertise them in ID_DFR0. Let's allow them to function. This also enables the pmu cpu property to work with these cpu types, i.e. we can now do '-cpu cortex-a15,pmu=off' to remove the pmu.

I'm a little nervous about this one, but PMUv2 isn't fresh enough in my mind to have a good reason other than that I didn't consider it when writing my recent PMU patches. Do you know if this boots a kernel that can detect and use the PMU successfully while thinking it's PMUv2?

I didn't try booting a kernel and running perf or anything like that. It does now run the pmu kvm-unit-tests test though. As this patch is now in master the test you propose can be done with a latest build :)

Thanks,
drew

#6 lore raw

PATCH 3/3 target/arm: make pmccntr_op_start/finish static

In reply to Andrew Jones

These functions are not used outside helper.c

---
 target/arm/cpu.h    | 11 -----------
 target/arm/helper.c |  4 ++--
 2 files changed, 2 insertions(+), 13 deletions(-)
target/arm/cpu.h +0 -11
index 5f23c621325c..d4d2836923df 100644
@@ -992,17 +992,6 @@ static inline bool is_a64(CPUARMState *env)
 int cpu_arm_signal_handler(int host_signum, void *pinfo,
                            void *puc);
 
-/**
- * pmccntr_op_start/finish
- * @env: CPUARMState
- *
- * Convert the counter in the PMCCNTR between its delta form (the typical mode
- * when it's enabled) and the guest-visible value. These two calls must always
- * surround any action which might affect the counter.
- */
-void pmccntr_op_start(CPUARMState *env);
-void pmccntr_op_finish(CPUARMState *env);
-
 /**
  * pmu_op_start/finish
  * @env: CPUARMState
target/arm/helper.c +2 -2
index fc73488f6cc0..a36f4b3d6997 100644
@@ -1337,7 +1337,7 @@ static void pmu_update_irq(CPUARMState *env)
  * etc. can be done logically. This is essentially a no-op if the counter is
  * not enabled at the time of the call.
  */
-void pmccntr_op_start(CPUARMState *env)
+static void pmccntr_op_start(CPUARMState *env)
 {
     uint64_t cycles = cycles_get_count(env);
 
@@ -1367,7 +1367,7 @@ void pmccntr_op_start(CPUARMState *env)
  * guest-visible count. A call to pmccntr_op_finish should follow every call to
  * pmccntr_op_start.
  */
-void pmccntr_op_finish(CPUARMState *env)
+static void pmccntr_op_finish(CPUARMState *env)
 {
     if (pmu_counter_enabled(env, 31)) {
 #ifndef CONFIG_USER_ONLY
Signature · 1 line
2.17.2
Reviewed-by on v1 3/3
#7 lore raw

Re: [Qemu-arm] [PATCH 3/3] target/arm: make pmccntr_op_start/finish static

Quoted 3 lines · Andrew Jones

These functions are not used outside helper.c

I'm late to the party, but:

Quoted 52 lines
---
 target/arm/cpu.h    | 11 -----------
 target/arm/helper.c |  4 ++--
 2 files changed, 2 insertions(+), 13 deletions(-)
target/arm/cpu.h +0 -11
index 5f23c621325c..d4d2836923df 100644
@@ -992,17 +992,6 @@ static inline bool is_a64(CPUARMState *env)
 int cpu_arm_signal_handler(int host_signum, void *pinfo,
                            void *puc);
 
-/**
- * pmccntr_op_start/finish
- * @env: CPUARMState
- *
- * Convert the counter in the PMCCNTR between its delta form (the typical mode
- * when it's enabled) and the guest-visible value. These two calls must always
- * surround any action which might affect the counter.
- */
-void pmccntr_op_start(CPUARMState *env);
-void pmccntr_op_finish(CPUARMState *env);
-
 /**
  * pmu_op_start/finish
  * @env: CPUARMState
target/arm/helper.c +2 -2
index fc73488f6cc0..a36f4b3d6997 100644
@@ -1337,7 +1337,7 @@ static void pmu_update_irq(CPUARMState *env)
  * etc. can be done logically. This is essentially a no-op if the counter is
  * not enabled at the time of the call.
  */
-void pmccntr_op_start(CPUARMState *env)
+static void pmccntr_op_start(CPUARMState *env)
 {
     uint64_t cycles = cycles_get_count(env);
 
@@ -1367,7 +1367,7 @@ void pmccntr_op_start(CPUARMState *env)
  * guest-visible count. A call to pmccntr_op_finish should follow every call to
  * pmccntr_op_start.
  */
-void pmccntr_op_finish(CPUARMState *env)
+static void pmccntr_op_finish(CPUARMState *env)
 {
     if (pmu_counter_enabled(env, 31)) {
 #ifndef CONFIG_USER_ONLY
Signature · 1 line
2.17.2
#8 lore raw
In reply to Andrew Jones
Quoted 10 lines · Andrew Jones

The first two patches fix a regression I found when running the arm (as opposed to arm64) pmu kvm-unit-tests test on tcg,

  $ git clone git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git
  $ cd kvm-unit-tests
  $ ./configure --arch=arm --cross-prefix=arm-linux-gnu-
  $ make -j
  $ export QEMU=/path/to/qemu-system-arm or qemu-system-aarch64
  $ arm/run arm/pmu.flat
Quoted 7 lines

After checking the QEMU code I found it's also reproducible with the arm64 test if the PMU is removed,

  $ ./configure --arch=arm64 --cross-prefix=aarch64-linux-gnu-
  $ make -j
  $ export QEMU=/path/to/qemu-system-aarch64
  $ arm/run arm/pmu.flat -cpu cortex-a57,pmu=off

Small correction here: I had to also hack the unit test to not bail out when it didn't see a PMU advertised in the ID register.

arm/pmu.c +0 -0
index 1de7d77d184f..185ff77244ed 100644
@@ -291,8 +291,9 @@ int main(int argc, char *argv[])
		cpi = atol(argv[1]);
 
	if (!pmu_probe()) {
-	       printf("No PMU found, test skipped...\n");
-	       return report_summary();
+//	     printf("No PMU found, test skipped...\n");
+	       printf("No PMU found, let's crash!\n");
+//	     return report_summary();
	}
 
	report_prefix_push("pmu");
Quoted 26 lines

Those tests used to pass, but now QEMU was crashing. I've broken the fix into two patches because the second patch is a bit of an RFC since I don't know if it's safe to enable all ARM_FEATURE_PMU code for PMUv2. Maybe that feature is only for PMUv3? This patch also enables the pmu cpu property to work with those cpu types, i.e. we can now do '-cpu cortex-a15,pmu=off' to remove the pmu. Although it wasn't clear to me if the PMU is optional (permitted to be removed) on those cpu types from the manuals.

The last patch is just a trivial cleanup.

Andrew Jones (3):
target/arm: fix crash on pmu register access
target/arm: cortex-a7 and cortex-a15 have pmus
target/arm: make pmccntr_op_start/finish static

 target/arm/cpu.c    |  3 +++
 target/arm/cpu.h    | 11 -----------
 target/arm/helper.c |  8 ++++++--
 3 files changed, 9 insertions(+), 13 deletions(-)
Signature · 1 line
2.17.2
#10 lore raw
In reply to Andrew Jones
Quoted 29 lines · Andrew Jones

The first two patches fix a regression I found when running the arm (as opposed to arm64) pmu kvm-unit-tests test on tcg,

  $ git clone git://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git
  $ cd kvm-unit-tests
  $ ./configure --arch=arm --cross-prefix=arm-linux-gnu-
  $ make -j
  $ export QEMU=/path/to/qemu-system-arm or qemu-system-aarch64
  $ arm/run arm/pmu.flat

After checking the QEMU code I found it's also reproducible with the arm64 test if the PMU is removed,

  $ ./configure --arch=arm64 --cross-prefix=aarch64-linux-gnu-
  $ make -j
  $ export QEMU=/path/to/qemu-system-aarch64
  $ arm/run arm/pmu.flat -cpu cortex-a57,pmu=off

Those tests used to pass, but now QEMU was crashing. I've broken the fix into two patches because the second patch is a bit of an RFC since I don't know if it's safe to enable all ARM_FEATURE_PMU code for PMUv2. Maybe that feature is only for PMUv3? This patch also enables the pmu cpu property to work with those cpu types, i.e. we can now do '-cpu cortex-a15,pmu=off' to remove the pmu. Although it wasn't clear to me if the PMU is optional (permitted to be removed) on those cpu types from the manuals.

Quoted 7 lines

The last patch is just a trivial cleanup.

Andrew Jones (3):
target/arm: fix crash on pmu register access
target/arm: cortex-a7 and cortex-a15 have pmus
target/arm: make pmccntr_op_start/finish static

Applied to target-arm.next, thanks.

-- PMM