Add BHRB Facility Support
cover Add BHRB Facility Support
This is a series of patches for adding support for the Branch History Rolling Buffer (BHRB) facility. This was added to the Power ISA starting with version 2.07. Changes were subsequently made in version 3.1 to limit BHRB recording to instructions run in problem state only and to add a control bit to disable recording (MMCRA[BHRBRD]).
Glenn Miles (4):
target/ppc: Add new hflags to support BHRB target/ppc: Add recording of taken branches to BHRB target/ppc: Add clrbhrb and mfbhrbe instructions target/ppc: Add migration support for BHRB
target/ppc/cpu.h | 24 +++++ target/ppc/cpu_init.c | 45 ++++++++- target/ppc/helper.h | 5 + target/ppc/helper_regs.c | 44 ++++++++ target/ppc/helper_regs.h | 1 + target/ppc/machine.c | 25 ++++- target/ppc/misc_helper.c | 43 ++++++++ target/ppc/power8-pmu-regs.c.inc | 5 + target/ppc/power8-pmu.c | 17 +++- target/ppc/power8-pmu.h | 11 +- target/ppc/spr_common.h | 1 + target/ppc/translate.c | 133 +++++++++++++++++++++++-- target/ppc/translate/branch-impl.c.inc | 2 +- 13 files changed, 337 insertions(+), 19 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/4 target/ppc: Add new hflags to support BHRB
This commit is preparatory to the addition of Branch History Rolling Buffer (BHRB) functionality, which is being provided today starting with the P8 processor.
BHRB uses several SPR register fields to control whether or not a branch instruction's address (and sometimes target address) should be recorded. Checking each of these fields with each branch instruction using jitted code would lead to a significant decrease in performance.
Therefore, it was decided that BHRB configuration bits that are not expected to change frequently should have their state stored in hflags so that the amount of checking done by jitted code can be reduced.
This commit contains the changes for storing the state of the following register fields as hflags:
MMCR0[FCP] - Determines if BHRB recording is frozen in the
problem state
MMCR0[FCPC] - A modifier for MMCR0[FCP]
MMCRA[BHRBRD] - Disables all BHRB recording for a thread
--- target/ppc/cpu.h | 9 +++++++++ target/ppc/cpu_init.c | 4 ++-- target/ppc/helper.h | 1 + target/ppc/helper_regs.c | 12 ++++++++++++ target/ppc/machine.c | 2 +- target/ppc/power8-pmu-regs.c.inc | 5 +++++ target/ppc/power8-pmu.c | 15 +++++++++++---- target/ppc/power8-pmu.h | 4 ++-- target/ppc/spr_common.h | 1 + target/ppc/translate.c | 6 ++++++ 10 files changed, 50 insertions(+), 9 deletions(-)
target/ppc/cpu.h +9 -0
@@ -439,6 +439,9 @@ FIELD(MSR, LE, MSR_LE, 1)#define MMCR0_FC56 PPC_BIT(59)/* PMC Freeze Counters 5-6 bit */ #define MMCR0_PMC1CE PPC_BIT(48)/* MMCR0 PMC1 Condition Enabled */ #define MMCR0_PMCjCE PPC_BIT(49)/* MMCR0 PMCj Condition Enabled */ +#define MMCR0_BHRBA PPC_BIT_NR(42)/* BHRB Available */ +#define MMCR0_FCP PPC_BIT(34)/* Freeze Counters/BHRB if PR=1 */ +#define MMCR0_FCPC PPC_BIT(51)/* Condition for FCP bit */ /* MMCR0 userspace r/w mask */ #define MMCR0_UREG_MASK (MMCR0_FC | MMCR0_PMAO | MMCR0_PMAE)/* MMCR2 userspace r/w mask */
@@ -451,6 +454,9 @@ FIELD(MSR, LE, MSR_LE, 1)#define MMCR2_UREG_MASK (MMCR2_FC1P0 | MMCR2_FC2P0 | MMCR2_FC3P0 | \ MMCR2_FC4P0 | MMCR2_FC5P0 | MMCR2_FC6P0) +#define MMCRA_BHRBRD PPC_BIT(26)/* BHRB Recording Disable */ + +#define MMCR1_EVT_SIZE 8/* extract64() does a right shift before extracting */ #define MMCR1_PMC1SEL_START 32
@@ -703,6 +709,9 @@ enum {HFLAGS_PMCJCE =17 ,/* MMCR0 PMCjCE bit */ HFLAGS_PMC_OTHER =18 ,/* PMC other than PMC5-6 is enabled */ HFLAGS_INSN_CNT =19 ,/* PMU instruction count enabled */ +HFLAGS_FCPC =20 ,/* MMCR0 FCPC bit */ +HFLAGS_FCP =21 ,/* MMCR0 FCP bit */ +HFLAGS_BHRBRD =22 ,/* MMCRA BHRBRD bit */ HFLAGS_VSX =23 ,/* MSR_VSX if cpu has VSX */ HFLAGS_VR = 25, /* MSR_VR if cpu has VRE */
target/ppc/cpu_init.c +2 -2
@@ -5152,7 +5152,7 @@ static void register_book3s_pmu_sup_sprs(CPUPPCState *env) KVM_REG_PPC_MMCR1, 0x00000000);spr_register_kvm (env,SPR_POWER_MMCRA ,"MMCRA" ,SPR_NOACCESS ,SPR_NOACCESS , - &spr_read_generic, &spr_write_generic, + &spr_read_generic, &spr_write_MMCRA,KVM_REG_PPC_MMCRA ,0x00000000 ); spr_register_kvm(env, SPR_POWER_PMC1, "PMC1", SPR_NOACCESS, SPR_NOACCESS,
@@ -7152,7 +7152,7 @@ static void ppc_cpu_reset_hold(Object *obj)if (env->mmu_model !=POWERPC_MMU_REAL ) {ppc_tlb_invalidate_all (env); } -pmu_mmcr01_updated (env); +pmu_mmcr01a_updated (env); }/* clean any pending stop state */
target/ppc/helper.h +1 -0
@@ -27,6 +27,7 @@ DEF_HELPER_2(store_lpcr, void, env, tl) DEF_HELPER_2(store_pcr, void, env, tl) DEF_HELPER_2(store_mmcr0, void, env, tl) DEF_HELPER_2(store_mmcr1, void, env, tl) +DEF_HELPER_2(store_mmcrA, void, env, tl) DEF_HELPER_3(store_pmc, void, env, i32, i64) DEF_HELPER_2(read_pmc, tl, env, i32)DEF_HELPER_2 (insns_inc, void, env, i32)
target/ppc/helper_regs.c +12 -0
@@ -61,6 +61,15 @@ static uint32_t hreg_compute_pmu_hflags_value(CPUPPCState *env)if (env->spr[SPR_POWER_MMCR0 ] & MMCR0_PMCjCE) { hflags |=1 <<HFLAGS_PMCJCE ; } +if (env->spr[SPR_POWER_MMCR0 ] &MMCR0_FCP ) { + hflags |=1 <<HFLAGS_FCP ; + } +if (env->spr[SPR_POWER_MMCR0 ] &MMCR0_FCPC ) { + hflags |=1 <<HFLAGS_FCPC ; + } +if (env->spr[SPR_POWER_MMCRA ] &MMCRA_BHRBRD ) { + hflags |=1 <<HFLAGS_BHRBRD ; + }#ifndef CONFIG_USER_ONLY if (env->pmc_ins_cnt) {
@@ -85,6 +94,9 @@ static uint32_t hreg_compute_pmu_hflags_mask(CPUPPCState *env) hflags_mask |=1 <<HFLAGS_PMCJCE ; hflags_mask |=1 <<HFLAGS_INSN_CNT ; hflags_mask |=1 <<HFLAGS_PMC_OTHER ; + hflags_mask |=1 <<HFLAGS_FCP ; + hflags_mask |=1 <<HFLAGS_FCPC ; + hflags_mask |=1 <<HFLAGS_BHRBRD ;#endif return hflags_mask; }
target/ppc/machine.c +1 -1
@@ -313,7 +313,7 @@ static int cpu_post_load(void *opaque, int version_id)post_load_update_msr (env);if (tcg_enabled ()) { -pmu_mmcr01_updated (env); +pmu_mmcr01a_updated (env); }return 0 ;
target/ppc/power8-pmu-regs.c.inc +5 -0
@@ -175,6 +175,11 @@ void spr_write_MMCR2_ureg(DisasContext *ctx, int sprn, int gprn) gen_store_spr(SPR_POWER_MMCR2, masked_gprn); } +void spr_write_MMCRA(DisasContext *ctx, int sprn, int gprn) +{ + gen_helper_store_mmcrA(cpu_env, cpu_gpr[gprn]); +} + void spr_read_PMC(DisasContext *ctx, int gprn, int sprn) { TCGv_i32 t_sprn = tcg_constant_i32(sprn);
target/ppc/power8-pmu.c +11 -4
@@ -82,7 +82,7 @@ static void pmu_update_summaries(CPUPPCState *env) env->pmc_cyc_cnt = cyc_cnt; } -void pmu_mmcr01_updated (CPUPPCState *env) +void pmu_mmcr01a_updated (CPUPPCState *env) {PowerPCCPU *cpu =env_archcpu (env);
@@ -260,7 +260,7 @@ void helper_store_mmcr0(CPUPPCState *env, target_ulong value) env->spr[SPR_POWER_MMCR0 ] = value; -pmu_mmcr01_updated (env); +pmu_mmcr01a_updated (env);/* Update cycle overflow timers with the current MMCR0 state */ pmu_update_overflow_timers (env);
@@ -272,7 +272,14 @@ void helper_store_mmcr1(CPUPPCState *env, uint64_t value) env->spr[SPR_POWER_MMCR1 ] = value; -pmu_mmcr01_updated (env); +pmu_mmcr01a_updated (env); +} + +void helper_store_mmcrA (CPUPPCState *env,uint64_t value) +{ + env->spr[SPR_POWER_MMCRA ] = value; + +pmu_mmcr01a_updated (env); }target_ulong helper_read_pmc (CPUPPCState *env,uint32_t sprn)
@@ -301,7 +308,7 @@ static void perfm_alert(PowerPCCPU *cpu) env->spr[SPR_POWER_MMCR0 ] |=MMCR0_FC ;/* Changing MMCR0_FC requires summaries and hflags update */ -pmu_mmcr01_updated (env); +pmu_mmcr01a_updated (env);/* * Delete all pending timers if we need to freeze
target/ppc/power8-pmu.h +2 -2
@@ -18,10 +18,10 @@#define PMC_COUNTER_NEGATIVE_VAL 0x80000000ULvoid cpu_ppc_pmu_init (CPUPPCState *env); -void pmu_mmcr01_updated (CPUPPCState *env); +void pmu_mmcr01a_updated (CPUPPCState *env);#else static inline void cpu_ppc_pmu_init (CPUPPCState *env) { } -static inline void pmu_mmcr01_updated (CPUPPCState *env) { } +static inline void pmu_mmcr01a_updated (CPUPPCState *env) { }#endif #endif
target/ppc/spr_common.h +1 -0
@@ -85,6 +85,7 @@ void spr_write_generic32(DisasContext *ctx, int sprn, int gprn);void spr_core_write_generic (DisasContext *ctx,int sprn,int gprn);void spr_write_MMCR0 (DisasContext *ctx,int sprn,int gprn);void spr_write_MMCR1 (DisasContext *ctx,int sprn,int gprn); +void spr_write_MMCRA (DisasContext *ctx,int sprn,int gprn);void spr_write_PMC (DisasContext *ctx,int sprn,int gprn);void spr_write_CTRL (DisasContext *ctx,int sprn,int gprn);void spr_read_xer (DisasContext *ctx,int gprn,int sprn);
target/ppc/translate.c +6 -0
@@ -190,6 +190,9 @@ struct DisasContext {bool mmcr0_pmcjce;bool pmc_other;bool pmu_insn_cnt; +bool mmcr0_fcpc; +bool mmcr0_fcp; +bool mmcra_bhrbrd;ppc_spr_t *spr_cb;/* Needed to check rights for mfspr/mtspr */ int singlestep_enabled;uint32_t flags;
@@ -7326,6 +7329,9 @@ static void ppc_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->mmcr0_pmcjce = (hflags >>HFLAGS_PMCJCE ) &1 ; ctx->pmc_other = (hflags >>HFLAGS_PMC_OTHER ) &1 ; ctx->pmu_insn_cnt = (hflags >>HFLAGS_INSN_CNT ) &1 ; + ctx->mmcr0_fcpc = (hflags >>HFLAGS_FCPC ) &1 ; + ctx->mmcr0_fcp = (hflags >>HFLAGS_FCP ) &1 ; + ctx->mmcra_bhrbrd = (hflags >>HFLAGS_BHRBRD ) &1 ; ctx->singlestep_enabled =0 ;if ((hflags >>HFLAGS_SE ) &1 ) {
Signature · 1 line
2.31.1
Re: [PATCH 1/4] target/ppc: Add new hflags to support BHRB
Sorry, this is my first attempt at sending out a patch and it looks like only part of the patch made it. Until I can figure out what I did wrong, please ignore this patch.
Thanks,
Glenn Miles
Re: [PATCH 1/4] target/ppc: Add new hflags to support BHRB
Quoted 1 line · Glenn Miles
Sorry, this is my first attempt at sending out a patch and it looks like only part of the patch made it. Until I can figure out what I did wrong, please ignore this patch.
I didn't get patches 2-4. Patch 1 looked good though. Please resend.
Thanks,
C.
PATCH 2/4 target/ppc: Add recording of taken branches to BHRB
In reply to Glenn MilesThis commit continues adding support for the Branch History Rolling Buffer (BHRB) as is provided starting with the P8 processor and continuing with its successors. This commit is limited to the recording and filtering of taken branches.
The following changes were made:
- Added a BHRB buffer for storing branch instruction and target addresses for taken branches
- Renamed gen_update_cfar to gen_update_branch_history and added a 'target' parameter to hold the branch target address and 'inst_type' parameter to use for filtering
- Added a combination of jit-time and run-time checks to gen_update_branch_history for determining if a branch should be recorded
- Added TCG code to gen_update_branch_history that stores data to the BHRB and updates the BHRB offset.
- Added BHRB resource initialization and reset functions
- Enabled functionality for P8, P9 and P10 processors.
--- target/ppc/cpu.h | 18 +++- target/ppc/cpu_init.c | 41 ++++++++- target/ppc/helper_regs.c | 32 +++++++ target/ppc/helper_regs.h | 1 + target/ppc/power8-pmu.c | 2 + target/ppc/power8-pmu.h | 7 ++ target/ppc/translate.c | 114 +++++++++++++++++++++++-- target/ppc/translate/branch-impl.c.inc | 2 +- 8 files changed, 205 insertions(+), 12 deletions(-)
target/ppc/cpu.h +16 -2
@@ -454,8 +454,9 @@ FIELD(MSR, LE, MSR_LE, 1)#define MMCR2_UREG_MASK (MMCR2_FC1P0 | MMCR2_FC2P0 | MMCR2_FC3P0 | \ MMCR2_FC4P0 | MMCR2_FC5P0 | MMCR2_FC6P0) -#define MMCRA_BHRBRD PPC_BIT(26)/* BHRB Recording Disable */ - +#define MMCRA_BHRBRD PPC_BIT(26)/* BHRB Recording Disable */ +#define MMCRA_IFM_MASK PPC_BITMASK(32, 33)/* BHRB Instruction Filtering */ +#define MMCRA_IFM_SHIFT PPC_BIT_NR(33)#define MMCR1_EVT_SIZE 8/* extract64() does a right shift before extracting */
@@ -682,6 +683,8 @@ enum {POWERPC_FLAG_SMT =0x00400000 ,/* Using "LPAR per core" mode (as opposed to per-thread) */ POWERPC_FLAG_SMT_1LPAR =0x00800000 , +/* Has BHRB */ + POWERPC_FLAG_BHRB = 0x01000000, };/*
@@ -1110,6 +1113,9 @@ DEXCR_ASPECT(PHIE, 6)#define PPC_CPU_OPCODES_LEN 0x40#define PPC_CPU_INDIRECT_OPCODES_LEN 0x20 +#define BHRB_MAX_NUM_ENTRIES_LOG2 (5) +#define BHRB_MAX_NUM_ENTRIES (1 << BHRB_MAX_NUM_ENTRIES_LOG2) +struct CPUArchState {/* Most commonly used resources during translated code execution first */ target_ulong gpr[32 ];/* general purpose registers */
@@ -1196,6 +1202,14 @@ struct CPUArchState {int dcache_line_size;int icache_line_size; +/* Branch History Rolling Buffer (BHRB) resources */ +target_ulong bhrb_num_entries; +target_ulong bhrb_base; +target_ulong bhrb_filter; +target_ulong bhrb_offset; +target_ulong bhrb_offset_mask; +uint64_t bhrb[BHRB_MAX_NUM_ENTRIES ]; +/* These resources are used during exception processing */ /* CPU model definition */ target_ulong msr_mask;
target/ppc/cpu_init.c +38 -3
101 lines not on this page · raw message
target/ppc/helper_regs.c +32 -0
@@ -752,3 +752,35 @@ void register_usprgh_sprs(CPUPPCState *env) &spr_read_ureg,SPR_NOACCESS , 0x00000000); } + +void hreg_bhrb_filter_update (CPUPPCState *env) +{ +target_long ifm; + +if (!(env->spr[SPR_POWER_MMCR0 ] &MMCR0_PMAE )) { +/* disable recording to BHRB */ + env->bhrb_filter =BHRB_TYPE_NORECORD ; +return ; + } + + ifm = (env->spr[SPR_POWER_MMCRA ] &MMCRA_IFM_MASK ) >>MMCRA_IFM_SHIFT ; +switch (ifm) { +case 0 : +/* record all branches */ + env->bhrb_filter =-1 ; +break ; +case 1 : +/* only record calls (LK = 1) */ + env->bhrb_filter =BHRB_TYPE_CALL ; +break ; +case 2 : +/* only record indirect branches */ + env->bhrb_filter =BHRB_TYPE_INDIRECT ; +break ; +case 3 : +/* only record conditional branches */ + env->bhrb_filter =BHRB_TYPE_COND ; +break ; + } +} +
target/ppc/helper_regs.h +1 -0
@@ -25,6 +25,7 @@ void hreg_compute_hflags(CPUPPCState *env);void hreg_update_pmu_hflags (CPUPPCState *env);void cpu_interrupt_exittb (CPUState *cs);int hreg_store_msr (CPUPPCState *env,target_ulong value,int alter_hv); +void hreg_bhrb_filter_update (CPUPPCState *env);#ifdef CONFIG_USER_ONLY static inline void check_tlb_flush (CPUPPCState *env,bool global) { }
target/ppc/power8-pmu.c +2 -0
@@ -261,6 +261,7 @@ void helper_store_mmcr0(CPUPPCState *env, target_ulong value) env->spr[SPR_POWER_MMCR0 ] = value;pmu_mmcr01a_updated (env); +hreg_bhrb_filter_update (env);/* Update cycle overflow timers with the current MMCR0 state */ pmu_update_overflow_timers (env);
@@ -280,6 +281,7 @@ void helper_store_mmcrA(CPUPPCState *env, uint64_t value) env->spr[SPR_POWER_MMCRA ] = value;pmu_mmcr01a_updated (env); +hreg_bhrb_filter_update (env); }target_ulong helper_read_pmc (CPUPPCState *env,uint32_t sprn)
target/ppc/power8-pmu.h +7 -0
@@ -17,6 +17,13 @@#define PMC_COUNTER_NEGATIVE_VAL 0x80000000UL +#define BHRB_TYPE_NORECORD 0x00 +#define BHRB_TYPE_CALL 0x01 +#define BHRB_TYPE_INDIRECT 0x02 +#define BHRB_TYPE_COND 0x04 +#define BHRB_TYPE_OTHER 0x08 +#define BHRB_TYPE_XL_FORM 0x10 +void cpu_ppc_pmu_init (CPUPPCState *env);void pmu_mmcr01a_updated (CPUPPCState *env);#else
target/ppc/translate.c +108 -6
192 lines not on this page · raw message
target/ppc/translate/branch-impl.c.inc +1 -1
@@ -17,7 +17,7 @@ static bool trans_RFEBB(DisasContext *ctx, arg_XL_s *arg) REQUIRE_INSNS_FLAGS2(ctx, ISA207S); translator_io_start(&ctx->base); - gen_update_cfar(ctx, ctx->cia); + gen_update_branch_history(ctx, ctx->cia, NULL, BHRB_TYPE_NORECORD); gen_helper_rfebb(cpu_env, cpu_gpr[arg->s]); ctx->base.is_jmp = DISAS_CHAIN;
Signature · 1 line
2.31.1
PATCH 3/4 target/ppc: Add clrbhrb and mfbhrbe instructions
In reply to Glenn MilesAdd support for the clrbhrb and mfbhrbe instructions.
Since neither instruction is believed to be critical to performance, both instructions were implemented using helper functions.
Access to both instructions is controlled by bits in the HFSCR (for privileged state) and MMCR0 (for problem state). A new function, helper_mmcr0_facility_check, was added for checking MMCR0[BHRBA] and raising a facility_unavailable exception if required.
--- target/ppc/cpu.h | 1 + target/ppc/helper.h | 4 ++++ target/ppc/misc_helper.c | 43 ++++++++++++++++++++++++++++++++++++++++ target/ppc/translate.c | 13 ++++++++++++ 4 files changed, 61 insertions(+)
target/ppc/cpu.h +1 -0
@@ -541,6 +541,7 @@ FIELD(MSR, LE, MSR_LE, 1)/* HFSCR bits */ #define HFSCR_MSGP PPC_BIT(53)/* Privileged Message Send Facilities */ +#define HFSCR_BHRB PPC_BIT(59)/* BHRB Instructions */ #define HFSCR_IC_MSGP 0xA#define DBCR0_ICMP (1 << 27)
target/ppc/helper.h +4 -0
@@ -816,3 +816,7 @@ DEF_HELPER_4(DSCLIQ, void, env, fprp, fprp, i32) DEF_HELPER_1(tbegin, void, env) DEF_HELPER_FLAGS_1(fixup_thrm, TCG_CALL_NO_RWG, void, env) + +DEF_HELPER_1(clrbhrb, void, env) +DEF_HELPER_FLAGS_2 (mfbhrbe,TCG_CALL_NO_WG , i64, env, i32) +
target/ppc/misc_helper.c +43 -0
52 lines not on this page · raw message
target/ppc/translate.c +13 -0
@@ -6549,12 +6549,25 @@ static void gen_brh(DisasContext *ctx) }#endif +static void gen_clrbhrb (DisasContext *ctx) +{ +gen_helper_clrbhrb (cpu_env); +} + +static void gen_mfbhrbe (DisasContext *ctx) +{ +TCGv_i32 bhrbe =tcg_constant_i32 (_SPR (ctx->opcode)); +gen_helper_mfbhrbe (cpu_gpr[rD (ctx->opcode)], cpu_env, bhrbe); +} + static opcode_t opcodes[] = {#if defined(TARGET_PPC64 ) GEN_HANDLER_E(brd, 0x1F, 0x1B, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA310), GEN_HANDLER_E(brw, 0x1F, 0x1B, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA310), GEN_HANDLER_E(brh, 0x1F, 0x1B, 0x06, 0x0000F801, PPC_NONE, PPC2_ISA310), #endif +GEN_HANDLER_E(clrbhrb, 0x1F, 0x0E, 0x0D, 0x3FFF801, PPC_NONE, PPC2_ISA207S), +GEN_HANDLER_E(mfbhrbe, 0x1F, 0x0E, 0x09, 0x0000001, PPC_NONE, PPC2_ISA207S), GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE), #if defined(TARGET_PPC64) GEN_HANDLER_E(cmpeqb, 0x1F, 0x00, 0x07, 0x00600000, PPC_NONE, PPC2_ISA300),
Signature · 1 line
2.31.1