target/riscv: Cache TB flags in CPURISCVState
cover target/riscv: Cache TB flags in CPURISCVState
riscv_get_tb_cpu_state() recomputes TB flags on every lookup. Most of the inputs change infrequently, at CSR writes, traps, privilege changes or vsetvl. Cache the derived TB flags in CPURISCVState and rebuild them when their inputs may change. The design follows the Arm hflags cache, commit e979972a6a ("target/arm: Rely on hflags correct in cpu_get_tb_cpu_state") and its series [1].
Patch 1 extracts the flag computation. Patch 2 adds the cache and its rebuild points. Patch 3 switches TB lookup to the cache and adds a CONFIG_DEBUG_TCG check that reports the cached and recomputed values and aborts on a mismatch.
In an RV64 virt Linux boot and power-off profile, the self overhead of riscv_get_tb_cpu_state() in the vCPU thread dropped from 16.54% to 6.78%. Median boot time dropped from 5.69 s to 4.81 s (-15.5%) compared with the base revision. See patch 3 for measurement details.
Runtime tests with --enable-debug-tcg
- RV64 virt Linux boot and power-off, covering M/S/U modes.
- KVM selftests inside the RV64 virt guest (ebreak_test and kvm_page_table_test with Sv39, Sv48 and Sv57 guests). These exercise HS/VS transitions, two-stage translation and writes to hstatus, vsstatus and hgatp.
- A vle8ff.v regression test that checks tail elements after a fault-only-first load reduces vl below VLMAX without a trap.
- Guest reboot and migration save/restore, exercising the reset and post-load hooks.
- The vle8ff.v test under riscv64-linux-user.
Build-tested riscv32-softmmu. Boot timings were measured with release builds.
VU mode, M-mode accesses with MPRV+MPV, pointer masking and CFI were reviewed but not exercised.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
SeungJu Cheon (3):
target/riscv: Split out riscv_compute_tb_flags target/riscv: Rebuild cached TB flags at state changes target/riscv: Rely on cached TB flags in riscv_get_tb_cpu_state
target/riscv/cpu.c | 1 + target/riscv/cpu.h | 19 +++++ target/riscv/machine.c | 1 + target/riscv/tcg/cpu_helper.c | 1 + target/riscv/tcg/csr.c | 8 +- target/riscv/tcg/debug.c | 2 + target/riscv/tcg/tcg-cpu.c | 123 +++++++++++++++++++++++-------- target/riscv/tcg/vector_helper.c | 2 + 8 files changed, 124 insertions(+), 33 deletions(-)
base-commit: 08c9bbb825148dec9669f5184ecf18823bb6d631
Signature · 1 line
2.52.0
PATCH 1/3 target/riscv: Split out riscv_compute_tb_flags
Move the computation of TB flags derived from state that changes on CSR writes, traps, privilege changes and vsetvl into riscv_compute_tb_flags(). Continue to read FS/VS, elp, vstart and vl in riscv_get_tb_cpu_state(), since they can change between lookups.
The lookup calls the new helper directly, preserving the TB flag values. The helper returns UINT32_MAX when maxsz is below the 8-byte gvec minimum, so the vl comparison leaves VL_EQ_VLMAX clear in the same cases as the previous maxsz >= 8 check.
--- target/riscv/tcg/tcg-cpu.c | 101 +++++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 32 deletions(-)
target/riscv/tcg/tcg-cpu.c +69 -32
@@ -99,13 +99,20 @@ static int riscv_cpu_mmu_index(CPUState *cs, bool ifetch)return riscv_env_mmu_index (cpu_env (cs), ifetch); } -static TCGTBCPUState riscv_get_tb_cpu_state (CPUState *cs) +/* +* Compute the TB flags that depend only on state changed through CSR +* writes, traps, privilege changes and vsetvl. For now the result is +* fed straight back to riscv_get_tb_cpu_state(), which adds the bits +* that translated code changes between two lookups -- FS/VS dirtying, +* elp, vstart, and vl, which fault-only-first loads shrink. +*/ +static void riscv_compute_tb_flags (CPURISCVState *env,uint32_t *pflags, +uint64_t *pext_flags,uint32_t *pvlmax) { -CPURISCVState *env =cpu_env (cs);RISCVCPU *cpu =env_archcpu (env); -RISCVExtStatus fs, vs;uint32_t flags =0 ;uint64_t ext_flags =0 ; +uint32_t vlmax_cached =UINT32_MAX ;bool pm_signext =riscv_cpu_virt_mem_enabled (env, false);if (cpu->cfg.ext_zve32x) {
@@ -124,49 +131,93 @@ static TCGTBCPUState riscv_get_tb_cpu_state(CPUState *cs)uint32_t vsew =FIELD_EX64 (env->vtype,VTYPE ,VSEW );uint32_t vlmax =vext_get_vlmax (cpu->cfg.vlenb, vsew, lmul);uint32_t maxsz = vlmax << vsew; -bool vl_eq_vlmax = (env->vstart ==0 ) && (vlmax == env->vl) && - (maxsz >=8 ); + +/* vl is compared per lookup: fault-only-first loads shrink it. */ + vlmax_cached = maxsz >=8 ? vlmax :UINT32_MAX ; flags =FIELD_DP32 (flags,TB_FLAGS ,VILL , env->vill); flags =FIELD_DP32 (flags,TB_FLAGS ,SEW , vsew); flags =FIELD_DP32 (flags,TB_FLAGS ,LMUL ,FIELD_EX64 (env->vtype,VTYPE ,VLMUL )); - flags =FIELD_DP32 (flags,TB_FLAGS ,VL_EQ_VLMAX , vl_eq_vlmax); flags =FIELD_DP32 (flags,TB_FLAGS ,VTA ,FIELD_EX64 (env->vtype,VTYPE ,VTA )); flags =FIELD_DP32 (flags,TB_FLAGS ,VMA ,FIELD_EX64 (env->vtype,VTYPE ,VMA )); - flags =FIELD_DP32 (flags,TB_FLAGS ,VSTART_EQ_ZERO , env->vstart ==0 ); ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,ALTFMT , altfmt); }else { flags =FIELD_DP32 (flags,TB_FLAGS ,VILL ,1 ); }if (cpu_get_fcfien (env)) { + flags =FIELD_DP32 (flags,TB_FLAGS ,FCFI_ENABLED ,1 ); + } + +if (cpu_get_bcfien (env)) { + flags =FIELD_DP32 (flags,TB_FLAGS ,BCFI_ENABLED ,1 ); + } + +#ifndef CONFIG_USER_ONLY + flags =FIELD_DP32 (flags,TB_FLAGS ,PRIV , env->priv); + flags |=riscv_env_mmu_index (env,0 ); + +if (env->virt_enabled) { + flags =FIELD_DP32 (flags,TB_FLAGS ,VIRT_ENABLED ,1 ); + } + +if ((cpu->cfg.debug || cpu->cfg.ext_sdtrig) + && !icount_enabled ()) { + flags =FIELD_DP32 (flags,TB_FLAGS ,ITRIGGER , env->itrigger_enabled); + } +#endif + + flags =FIELD_DP32 (flags,TB_FLAGS ,XL , env->xl); + flags =FIELD_DP32 (flags,TB_FLAGS ,AXL ,cpu_address_xl (env)); + flags =FIELD_DP32 (flags,TB_FLAGS ,PM_PMM ,riscv_pm_get_pmm (env)); + flags =FIELD_DP32 (flags,TB_FLAGS ,PM_SIGNEXTEND , pm_signext); + + ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,MISA_EXT , env->misa_ext); + ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,BIG_ENDIAN , +mo_endian_env (env) ==MO_BE ); + + *pflags = flags; + *pext_flags = ext_flags; + *pvlmax = vlmax_cached; +} + +static TCGTBCPUState riscv_get_tb_cpu_state (CPUState *cs) +{ +CPURISCVState *env =cpu_env (cs); +RISCVCPU *cpu =env_archcpu (env); +RISCVExtStatus fs, vs; +uint32_t flags, vlmax; +uint64_t ext_flags; + +riscv_compute_tb_flags (env, &flags, &ext_flags, &vlmax); + +if (cpu->cfg.ext_zve32x) { +bool vstart_eq_zero = env->vstart ==0 ; + + flags =FIELD_DP32 (flags,TB_FLAGS ,VL_EQ_VLMAX , + vstart_eq_zero && env->vl == vlmax); + flags =FIELD_DP32 (flags,TB_FLAGS ,VSTART_EQ_ZERO , vstart_eq_zero); + } + +if (FIELD_EX32 (flags,TB_FLAGS ,FCFI_ENABLED )) {/* * For Forward CFI, only the expectation of a lpad at * the start of the block is tracked via env->elp. env->elp * is turned on during jalr translation. */ flags =FIELD_DP32 (flags,TB_FLAGS ,FCFI_LP_EXPECTED , env->elp); - flags =FIELD_DP32 (flags,TB_FLAGS ,FCFI_ENABLED ,1 ); - } - -if (cpu_get_bcfien (env)) { - flags =FIELD_DP32 (flags,TB_FLAGS ,BCFI_ENABLED ,1 ); }#ifdef CONFIG_USER_ONLY fs =EXT_STATUS_DIRTY ; vs =EXT_STATUS_DIRTY ;#else - flags =FIELD_DP32 (flags,TB_FLAGS ,PRIV , env->priv); - - flags |=riscv_env_mmu_index (env,0 ); fs =get_field (env->mstatus,MSTATUS_FS ); vs =get_field (env->mstatus,MSTATUS_VS );if (env->virt_enabled) { - flags =FIELD_DP32 (flags,TB_FLAGS ,VIRT_ENABLED ,1 );/* * Merge DISABLED and !DIRTY states using MIN. * We will set both fields when dirtying.
@@ -181,27 +232,13 @@ static TCGTBCPUState riscv_get_tb_cpu_state(CPUState *cs) ? EXT_STATUS_DIRTY : EXT_STATUS_DISABLED; } -if ((cpu->cfg.debug || cpu->cfg.ext_sdtrig) - && !icount_enabled ()) { - flags =FIELD_DP32 (flags,TB_FLAGS ,ITRIGGER , env->itrigger_enabled); +if (cpu->cfg.ext_sdext && !env->debug_mode && (env->dcsr &DCSR_STEP )) { + ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,SDEXT_STEP ,1 ); }#endif flags =FIELD_DP32 (flags,TB_FLAGS ,FS , fs); flags =FIELD_DP32 (flags,TB_FLAGS ,VS , vs); - flags =FIELD_DP32 (flags,TB_FLAGS ,XL , env->xl); - flags =FIELD_DP32 (flags,TB_FLAGS ,AXL ,cpu_address_xl (env)); - flags =FIELD_DP32 (flags,TB_FLAGS ,PM_PMM ,riscv_pm_get_pmm (env)); - flags =FIELD_DP32 (flags,TB_FLAGS ,PM_SIGNEXTEND , pm_signext); - - ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,MISA_EXT , env->misa_ext); - ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,BIG_ENDIAN , -mo_endian_env (env) ==MO_BE ); -#ifndef CONFIG_USER_ONLY -if (cpu->cfg.ext_sdext && !env->debug_mode && (env->dcsr &DCSR_STEP )) { - ext_flags =FIELD_DP64 (ext_flags,EXT_TB_FLAGS ,SDEXT_STEP ,1 ); - } -#endif return (TCGTBCPUState ){ .pc = env->xl ==MXL_RV32 ? env->pc &UINT32_MAX : env->pc,
Signature · 1 line
2.52.0
PATCH 2/3 target/riscv: Rebuild cached TB flags at state changes
In reply to SeungJu CheonAdd tb_flags_cached, ext_tb_flags_cached and vlmax_cached to CPURISCVState and riscv_rebuild_tb_flags() to fill them. Call it where the inputs of riscv_compute_tb_flags() change: CSR writes, riscv_cpu_set_mode() (trap entry, xRET and debug mode exit), vsetvl and vtype invalidation, itrigger updates, reset and migration post-load.
riscv_get_tb_cpu_state() does not use the cached values yet.
--- target/riscv/cpu.c | 1 + target/riscv/cpu.h | 19 +++++++++++++++++++ target/riscv/machine.c | 1 + target/riscv/tcg/cpu_helper.c | 1 + target/riscv/tcg/csr.c | 8 +++++++- target/riscv/tcg/debug.c | 2 ++ target/riscv/tcg/tcg-cpu.c | 7 +++++++ target/riscv/tcg/vector_helper.c | 2 ++ 8 files changed, 40 insertions(+), 1 deletion(-)
target/riscv/cpu.c +1 -0
@@ -1122,6 +1122,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) kvm_riscv_reset_vcpu(cpu); }#endif +riscv_rebuild_tb_flags (env); }static void riscv_cpu_disas_set_info (const CPUState *s,disassemble_info *info)
target/riscv/cpu.h +19 -0
@@ -262,6 +262,20 @@ struct CPUArchState {uint8_t vxsat;bool vill; +/* +* TB flags that depend only on state changed by CSR writes, traps, +* privilege changes and vsetvl; rebuilt by riscv_rebuild_tb_flags() at +* those points. Bits that translated code changes directly between +* two lookups (FS/VS dirtying, elp, vstart, vl) are added on each +* lookup. vlmax_cached is VLMAX for the current vtype when the +* VL_EQ_VLMAX fast path is allowed, else UINT32_MAX; vl is compared +* against it per lookup because fault-only-first loads shrink vl +* without a trap. +*/ +uint64_t ext_tb_flags_cached; +uint32_t tb_flags_cached; +uint32_t vlmax_cached; +uint64_t pc;uint64_t load_res;uint64_t load_val;
@@ -967,6 +981,11 @@ static inline uint32_t vext_get_vlmax(uint32_t vlenb, uint32_t vsew,bool riscv_cpu_is_32bit (RISCVCPU *cpu);bool riscv_cpu_virt_mem_enabled (CPURISCVState *env,bool is_vm_ldst); +#ifdef CONFIG_TCG +void riscv_rebuild_tb_flags (CPURISCVState *env); +#else +static inline void riscv_rebuild_tb_flags (CPURISCVState *env) { } +#endif RISCVPmPmm riscv_pm_get_pmm (CPURISCVState *env);RISCVPmPmm riscv_pm_get_vm_ldst_pmm (CPURISCVState *env);uint32_t riscv_pm_get_pmlen (RISCVPmPmm pmm);
target/riscv/machine.c +1 -0
@@ -388,6 +388,7 @@ static int riscv_cpu_post_load(void *opaque, int version_id)CPURISCVState *env = &cpu->env; env->xl =cpu_recompute_xl (env); +riscv_rebuild_tb_flags (env);return 0 ; }
target/riscv/tcg/cpu_helper.c +1 -0
@@ -982,6 +982,7 @@ void riscv_cpu_set_mode(CPURISCVState *env, privilege_mode_t newpriv,riscv_cpu_interrupt (env); } } +riscv_rebuild_tb_flags (env); }/*
target/riscv/tcg/csr.c +7 -1
@@ -5909,7 +5909,11 @@ static RISCVException riscv_csrrw_do64(CPURISCVState *env, int csrno,/* execute combined read/write operation if it exists */ if (csr_ops[csrno].op) { -return csr_ops[csrno].op (env, csrno, ret_value, new_value, write_mask); + ret = csr_ops[csrno].op (env, csrno, ret_value, new_value, write_mask); +if (ret ==RISCV_EXCP_NONE && write_mask) { +riscv_rebuild_tb_flags (env); + } +return ret; }/*
@@ -5937,6 +5941,7 @@ static RISCVException riscv_csrrw_do64(CPURISCVState *env, int csrno,return ret; } } +riscv_rebuild_tb_flags (env); }/* return old value */
@@ -6015,6 +6020,7 @@ static RISCVException riscv_csrrw_do128(CPURISCVState *env, int csrno,return ret; } } +riscv_rebuild_tb_flags (env); }/* return old value */
target/riscv/tcg/debug.c +2 -0
@@ -822,6 +822,7 @@ void HELPER(itrigger_match)(CPURISCVState *env)itrigger_set_pending (env, i,1 ); env->itrigger_enabled =riscv_itrigger_enabled (env); +riscv_rebuild_tb_flags (env);do_trigger_action (env, i);itrigger_set_pending (env, i,0 ); }
@@ -922,6 +923,7 @@ static void itrigger_reg_write(CPURISCVState *env, target_ulong index, env->last_icount + itrigger_get_count(env, index)); }else { env->itrigger_enabled =riscv_itrigger_enabled (env); +riscv_rebuild_tb_flags (env); } }break ;
target/riscv/tcg/tcg-cpu.c +7 -0
@@ -183,6 +183,13 @@ static void riscv_compute_tb_flags(CPURISCVState *env, uint32_t *pflags, *pvlmax = vlmax_cached; } +void riscv_rebuild_tb_flags (CPURISCVState *env) +{ +riscv_compute_tb_flags (env, &env->tb_flags_cached, + &env->ext_tb_flags_cached, + &env->vlmax_cached); +} +static TCGTBCPUState riscv_get_tb_cpu_state (CPUState *cs) {CPURISCVState *env =cpu_env (cs);
target/riscv/tcg/vector_helper.c +2 -0
@@ -56,6 +56,7 @@ static inline void reset_ill_vtype(CPURISCVState *env) env->vtype =0 ; env->vl =0 ; env->vstart =0 ; +riscv_rebuild_tb_flags (env); } target_ulong HELPER(vsetvl)(CPURISCVState *env, target_ulong s1,
@@ -125,6 +126,7 @@ target_ulong HELPER(vsetvl)(CPURISCVState *env, target_ulong s1, env->vtype = s2; env->vstart =0 ; env->vill =0 ; +riscv_rebuild_tb_flags (env);return vl; }
Signature · 1 line
2.52.0
PATCH 3/3 target/riscv: Rely on cached TB flags in riscv_get_tb_cpu_state
In reply to SeungJu CheonUse the cached TB flags and VLMAX on each lookup. Compare the current vl with vlmax_cached when setting VL_EQ_VLMAX, since fault-only-first loads can reduce vl without triggering a cache rebuild.
With CONFIG_DEBUG_TCG, recompute the cached values on each lookup. If they differ, report the cached and recomputed values and abort.
The profile below is from a Linux boot and power-off on an RV64 virt machine with one vCPU pinned to a host core. The table shows perf self overhead in the vCPU thread. The profiles contain 57,013 samples before and 47,942 after; percentages are relative to each profile.
Symbol Before After riscv_get_tb_cpu_state 16.54% 6.78% helper_lookup_tb_ptr 11.30% 12.59% riscv_pm_get_pmm 1.89% 0.09% riscv_env_mmu_index 1.62% 0.18% riscv_cpu_virt_mem_enabled 1.45% 0.09% riscv_rebuild_tb_flags - 0.67%
riscv_rebuild_tb_flags does not exist before this series. The share of helper_lookup_tb_ptr rises because the total shrinks; its sample count falls from about 6,440 to about 6,040.
Median boot time fell from 5.69 s to 4.81 s (-15.5%) over five runs per binary, alternating between the two binaries.
--- target/riscv/tcg/tcg-cpu.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-)
target/riscv/tcg/tcg-cpu.c +25 -10
@@ -100,11 +100,13 @@ static int riscv_cpu_mmu_index(CPUState *cs, bool ifetch) }/* -* Compute the TB flags that depend only on state changed through CSR -* writes, traps, privilege changes and vsetvl. For now the result is -* fed straight back to riscv_get_tb_cpu_state(), which adds the bits -* that translated code changes between two lookups -- FS/VS dirtying, -* elp, vstart, and vl, which fault-only-first loads shrink. +* Most TB flags depend only on state that changes through CSR writes, +* traps, privilege changes and vsetvl, so compute them once at those +* points and keep them in env (see riscv_rebuild_tb_flags() callers). +* This mirrors arm_rebuild_hflags(). The bits that translated code +* changes directly between two lookups -- FS/VS dirtying, elp, vstart, +* and vl, which fault-only-first loads shrink -- are added by +* riscv_get_tb_cpu_state() on each lookup. */ static void riscv_compute_tb_flags (CPURISCVState *env,uint32_t *pflags,uint64_t *pext_flags,uint32_t *pvlmax)
@@ -195,16 +197,29 @@ static TCGTBCPUState riscv_get_tb_cpu_state(CPUState *cs)CPURISCVState *env =cpu_env (cs);RISCVCPU *cpu =env_archcpu (env);RISCVExtStatus fs, vs; -uint32_t flags, vlmax; -uint64_t ext_flags; - -riscv_compute_tb_flags (env, &flags, &ext_flags, &vlmax); +uint32_t flags = env->tb_flags_cached; +uint64_t ext_flags = env->ext_tb_flags_cached; + +#ifdef CONFIG_DEBUG_TCG + { +uint32_t f, v; +uint64_t e; + +/* Catch a state change that did not rebuild the cached flags. */ +riscv_compute_tb_flags (env, &f, &e, &v); +if (f != flags || e != ext_flags || v != env->vlmax_cached) { +cpu_abort (cs,"stale TB flags: cached %08x/%016" PRIx64"/%u, " +"recomputed %08x/%016" PRIx64"/%u\n" , + flags, ext_flags, env->vlmax_cached, f, e, v); + } + } +#endif if (cpu->cfg.ext_zve32x) {bool vstart_eq_zero = env->vstart ==0 ; flags =FIELD_DP32 (flags,TB_FLAGS ,VL_EQ_VLMAX , - vstart_eq_zero && env->vl == vlmax); + vstart_eq_zero && env->vl == env->vlmax_cached); flags =FIELD_DP32 (flags,TB_FLAGS ,VSTART_EQ_ZERO , vstart_eq_zero); }
Signature · 1 line
2.52.0