Add vPMU vPMU support under TCG mode
v3 cover Add vPMU vPMU support under TCG mode
QEMU has implemented cycle count support for guest VM under TCG mode. But this feature is not complete. In fact using perf inside a 64-bit Linux guest VM (under TCG) can cause the following kernel panic because some PMU registers are not implemented.
[ 329.445970] [<fffffe000009e600>] armv8pmu_enable_event+0x58/0x8c [ 329.446713] [<fffffe0000621e74>] armpmu_start+0x4c/0x74
This patchset solves the problem by adding support for missing vPMU registers. Basic perf test can work (both ACPI and DT) now under TCG by applying this patchset.
address@hidden ~]# perf stat ls
Performance counter stats for 'ls':
226.740256 task-clock (msec) # 0.312 CPUs utilized
76 context-switches # 0.335 K/sec
0 cpu-migrations # 0.000 K/sec
64 page-faults # 0.282 K/sec
186,031,410 cycles # 0.820 GHz (36.40%)
<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
<not counted> instructions (0.00%)
<not supported> branches
<not counted> branch-misses (0.00%)V2->V3:
* Remove PMXEVCNTR_EL0 support
* Add read access support for PMXEVTYPER and change the CONSTRAINED UNPREDICTABLE behavior of PMXEVTYPER to RAZ/WI.
V1->V2:
* Change most PMU registers to 64bit and the behavior of PMXEVTYPER
* Add support for PMXEVCNTR_EL0
* Misc fixes (DT, ID_AA64DFR0_EL1, ...) under TCG mode
Thanks,
-Wei
Wei Huang (4):
target-arm: Add support for PMU register PMSELR_EL0
target-arm: Add support for AArch64 PMU register PMXEVTYPER_EL0
target-arm: Add support for PMU register PMINTENSET_EL1
target-arm: Enable vPMU support under TCG mode
hw/arm/virt.c | 2 +- target/arm/cpu.c | 2 +- target/arm/cpu.h | 4 +-- target/arm/helper.c | 72 ++++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 59 insertions(+), 21 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v3 1/4 target-arm: Add support for PMU register PMSELR_EL0
This patch adds support for AArch64 register PMSELR_EL0. The existing PMSELR definition is revised accordingly.
--- target/arm/cpu.h | 1 + target/arm/helper.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)
target/arm/cpu.h +1 -0
@@ -309,6 +309,7 @@ typedef struct CPUARMState {uint32_t c9_pmovsr;/* perf monitor overflow status */ uint32_t c9_pmxevtyper;/* perf monitor event type */ uint32_t c9_pmuserenr;/* perf monitor user enable */ +uint64_t c9_pmselr;/* perf monitor counter selection register */ uint32_t c9_pminten;/* perf monitor interrupt enables */ union { /* Memory attribute redirection */ struct {
target/arm/helper.c +20 -5
@@ -975,6 +975,17 @@ static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)return total_ticks - env->cp15.c15_ccnt; } +static void pmselr_write (CPUARMState *env,const ARMCPRegInfo *ri, +uint64_t value) +{ +/* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and +* PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the +* meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are +* accessed. +*/ + env->cp15.c9_pmselr = value &0x1f ; +} +static void pmccntr_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) {
@@ -1194,12 +1205,16 @@ static const ARMCPRegInfo v7_cp_reginfo[] = {/* Unimplemented so WI. */ { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP }, - /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE. - * We choose to RAZ/WI. - */ { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, - .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0, - .accessfn = pmreg_access }, + .access = PL0_RW, .type = ARM_CP_ALIAS, + .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr), + .accessfn = pmreg_access, .writefn = pmselr_write, + .raw_writefn = raw_write}, + { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64, + .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5, + .access = PL0_RW, .accessfn = pmreg_access, + .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr), + .writefn = pmselr_write, .raw_writefn = raw_write, }, #ifndef CONFIG_USER_ONLY { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
Signature · 1 line
1.8.3.1
PATCH v3 2/4 target-arm: Add support for AArch64 PMU register PMXEVTYPER_EL0
In reply to Wei HuangIn order to support Linux perf, which uses PMXEVTYPER register, this patch adds read/write access support for PMXEVTYPER. The access is CONSTRAINED UNPREDICTABLE when PMSELR is not 0x1f. Additionally this patch adds support for PMXEVTYPER_EL0.
--- target/arm/cpu.h | 1 - target/arm/helper.c | 30 +++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-)
target/arm/cpu.h +0 -1
@@ -307,7 +307,6 @@ typedef struct CPUARMState {uint64_t c9_pmcr;/* performance monitor control register */ uint64_t c9_pmcnten;/* perf monitor counter enables */ uint32_t c9_pmovsr;/* perf monitor overflow status */ -uint32_t c9_pmxevtyper;/* perf monitor event type */ uint32_t c9_pmuserenr;/* perf monitor user enable */ uint64_t c9_pmselr;/* perf monitor counter selection register */ uint32_t c9_pminten;/* perf monitor interrupt enables */
target/arm/helper.c +25 -5
@@ -1054,7 +1054,25 @@ static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,static void pmxevtyper_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { - env->cp15.c9_pmxevtyper = value &0xff ; +/* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when +* PMSELR value is equal to or greater than the number of implemented +* counters, but not equal to 0x1f. We opt to behave as a RAZ/WI. +*/ +if (env->cp15.c9_pmselr ==0x1f ) { +pmccfiltr_write (env, ri, value); + } +} + +static uint64_t pmxevtyper_read (CPUARMState *env,const ARMCPRegInfo *ri) +{ +/* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER +* are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write(). +*/ +if (env->cp15.c9_pmselr ==0x1f ) { +return env->cp15.pmccfiltr_el0; + }else { +return 0 ; + } } static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
@@ -1234,10 +1252,12 @@ static const ARMCPRegInfo v7_cp_reginfo[] = { .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), .resetvalue = 0, }, { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, - .access = PL0_RW, - .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper), - .accessfn = pmreg_access, .writefn = pmxevtyper_write, - .raw_writefn = raw_write }, + .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access, + .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, + { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64, + .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1, + .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access, + .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, /* Unimplemented, RAZ/WI. */ { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
Signature · 1 line
1.8.3.1
PATCH v3 3/4 target-arm: Add support for PMU register PMINTENSET_EL1
In reply to Wei HuangThis patch adds access support for PMINTENSET_EL1.
--- target/arm/cpu.h | 2 +- target/arm/helper.c | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-)
target/arm/cpu.h +1 -1
@@ -309,7 +309,7 @@ typedef struct CPUARMState {uint32_t c9_pmovsr;/* perf monitor overflow status */ uint32_t c9_pmuserenr;/* perf monitor user enable */ uint64_t c9_pmselr;/* perf monitor counter selection register */ -uint32_t c9_pminten;/* perf monitor interrupt enables */ +uint64_t c9_pminten;/* perf monitor interrupt enables */ union { /* Memory attribute redirection */ struct { #ifdef HOST_WORDS_BIGENDIAN
target/arm/helper.c +9 -1
@@ -1275,9 +1275,17 @@ static const ARMCPRegInfo v7_cp_reginfo[] = { .writefn = pmuserenr_write, .raw_writefn = raw_write }, { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, .access = PL1_RW, .accessfn = access_tpm, - .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), + .type = ARM_CP_ALIAS, + .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten), .resetvalue = 0, .writefn = pmintenset_write, .raw_writefn = raw_write }, + { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64, + .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1, + .access = PL1_RW, .accessfn = access_tpm, + .type = ARM_CP_IO, + .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), + .writefn = pmintenset_write, .raw_writefn = raw_write, + .resetvalue = 0x0 }, { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
Signature · 1 line
1.8.3.1
PATCH v3 4/4 target-arm: Enable vPMU support under TCG mode
In reply to Wei HuangThis patch contains several fixes to enable vPMU under TCG mode. It first removes the checking of kvm_enabled() while unsetting ARM_FEATURE_PMU. With it, the .pmu option can be used to turn on/off vPMU under TCG mode. Secondly the PMU node of DT table is now created under TCG. The last fix is to disable the masking of PMUver field of ID_AA64DFR0_EL1.
--- hw/arm/virt.c | 2 +- target/arm/cpu.c | 2 +- target/arm/helper.c | 7 +------ 3 files changed, 3 insertions(+), 8 deletions(-)
hw/arm/virt.c +1 -1
@@ -471,7 +471,7 @@ static void fdt_add_pmu_nodes(const VirtMachineState *vms)CPU_FOREACH (cpu) { armcpu =ARM_CPU (cpu);if (!arm_feature (&armcpu->env,ARM_FEATURE_PMU ) || - !kvm_arm_pmu_create (cpu,PPI (VIRTUAL_PMU_IRQ ))) { + (kvm_enabled () && !kvm_arm_pmu_create (cpu,PPI (VIRTUAL_PMU_IRQ )))) {return ; } }
target/arm/cpu.c +1 -1
@@ -745,7 +745,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)unset_feature (env,ARM_FEATURE_EL2 ); } -if (!cpu->has_pmu || !kvm_enabled ()) { +if (!cpu->has_pmu) { cpu->has_pmu = false;unset_feature (env,ARM_FEATURE_PMU ); }
target/arm/helper.c +1 -6
@@ -4633,12 +4633,7 @@ void register_cp_regs_for_features(ARMCPU *cpu) { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST, -/* We mask out the PMUVer field, because we don't currently -* implement the PMU. Not advertising it prevents the guest -* from trying to use it and getting UNDEFs on registers we -* don't implement. -*/ - .resetvalue = cpu->id_aa64dfr0 & ~0xf00 }, + .resetvalue = cpu->id_aa64dfr0 }, { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST,
Signature · 1 line
1.8.3.1
Peter Maydell
AppliedQuoted 31 lines · Wei Huang
QEMU has implemented cycle count support for guest VM under TCG mode. But this feature is not complete. In fact using perf inside a 64-bit Linux guest VM (under TCG) can cause the following kernel panic because some PMU registers are not implemented.
[ 329.445970] [<fffffe000009e600>] armv8pmu_enable_event+0x58/0x8c [ 329.446713] [<fffffe0000621e74>] armpmu_start+0x4c/0x74This patchset solves the problem by adding support for missing vPMU registers. Basic perf test can work (both ACPI and DT) now under TCG by applying this patchset.
address@hidden ~]# perf stat ls
Performance counter stats for 'ls':226.740256 task-clock (msec) # 0.312 CPUs utilized 76 context-switches # 0.335 K/sec 0 cpu-migrations # 0.000 K/sec 64 page-faults # 0.282 K/sec 186,031,410 cycles # 0.820 GHz (36.40%) <not supported> stalled-cycles-frontend <not supported> stalled-cycles-backend <not counted> instructions (0.00%) <not supported> branches <not counted> branch-misses (0.00%)
V2->V3:
* RemovePMXEVCNTR_EL0support
* Add read access support for PMXEVTYPER and change the CONSTRAINED UNPREDICTABLE behavior of PMXEVTYPER to RAZ/WI.
Quoted 4 lines
V1->V2:
* Change most PMU registers to 64bit and the behavior of PMXEVTYPER
* Add support forPMXEVCNTR_EL0
* Misc fixes (DT,ID_AA64DFR0_EL1, ...) under TCG mod
Applied to target-arm.next, thanks.
-- PMM
Quoted 32 lines · Peter Maydell
Quoted 31 lines · Wei Huang
QEMU has implemented cycle count support for guest VM under TCG mode. But this feature is not complete. In fact using perf inside a 64-bit Linux guest VM (under TCG) can cause the following kernel panic because some PMU registers are not implemented.
[ 329.445970] [<fffffe000009e600>] armv8pmu_enable_event+0x58/0x8c [ 329.446713] [<fffffe0000621e74>] armpmu_start+0x4c/0x74This patchset solves the problem by adding support for missing vPMU registers. Basic perf test can work (both ACPI and DT) now under TCG by applying this patchset.
address@hidden ~]# perf stat ls
Performance counter stats for 'ls':226.740256 task-clock (msec) # 0.312 CPUs utilized 76 context-switches # 0.335 K/sec 0 cpu-migrations # 0.000 K/sec 64 page-faults # 0.282 K/sec 186,031,410 cycles # 0.820 GHz (36.40%) <not supported> stalled-cycles-frontend <not supported> stalled-cycles-backend <not counted> instructions (0.00%) <not supported> branches <not counted> branch-misses (0.00%)
V2->V3:
* RemovePMXEVCNTR_EL0support
* Add read access support for PMXEVTYPER and change the CONSTRAINED UNPREDICTABLE behavior of PMXEVTYPER to RAZ/WI.
Quoted 6 lines
Quoted 4 lines
V1->V2:
* Change most PMU registers to 64bit and the behavior of PMXEVTYPER
* Add support forPMXEVCNTR_EL0
* Misc fixes (DT,ID_AA64DFR0_EL1, ...) under TCG modApplied to target-arm.next, thanks.
...though patch 1 breaks compilation of linux-user targets. I've fixed it up by moving the #ifndef CONFIG_USER_ONLY to the right place to cover the new regdefs.
thanks
-- PMM
Quoted 38 lines · Peter Maydell
Quoted 37 lines · Peter Maydell
Quoted 35 lines · Wei Huang
QEMU has implemented cycle count support for guest VM under TCG mode. But this feature is not complete. In fact using perf inside a 64-bit Linux guest VM (under TCG) can cause the following kernel panic because some PMU registers are not implemented.
[ 329.445970] [<fffffe000009e600>] armv8pmu_enable_event+0x58/0x8c [ 329.446713] [<fffffe0000621e74>] armpmu_start+0x4c/0x74This patchset solves the problem by adding support for missing vPMU registers. Basic perf test can work (both ACPI and DT) now under TCG by applying this patchset.
address@hidden ~]# perf stat ls
Performance counter stats for 'ls':226.740256 task-clock (msec) # 0.312 CPUs utilized 76 context-switches # 0.335 K/sec 0 cpu-migrations # 0.000 K/sec 64 page-faults # 0.282 K/sec 186,031,410 cycles # 0.820 GHz (36.40%) <not supported> stalled-cycles-frontend <not supported> stalled-cycles-backend <not counted> instructions (0.00%) <not supported> branches <not counted> branch-misses (0.00%)
V2->V3:
* RemovePMXEVCNTR_EL0support
* Add read access support for PMXEVTYPER and change the CONSTRAINED UNPREDICTABLE behavior of PMXEVTYPER to RAZ/WI.
V1->V2:
* Change most PMU registers to 64bit and the behavior of PMXEVTYPER
* Add support forPMXEVCNTR_EL0
* Misc fixes (DT,ID_AA64DFR0_EL1, ...) under TCG mod
Quoted 5 lines
Quoted 1 line
Applied to target-arm.next, thanks.
...though patch 1 breaks compilation of linux-user targets. I've fixed it up by moving the #ifndef
CONFIG_USER_ONLYto the right place to cover the new regdefs.
My bad. I normally did compilation for all targets + "make check" before sending patches out. But on that particular machine, qemu was configured for aarch64-softmmu only, thus missing this compilation error ...
Quoted 4 lines
thanks
-- PMM