target/riscv: do not count ECALL in minstret
PATCH v2 1/1 target/riscv: do not count ECALL in minstret
With icount enabled, helper_raise_exception() leaves ECALL in icount_get_raw() because it exits without restoring the current TB state. This makes minstret count an instruction that does not retire.
Adjust only the fixed minstret baseline so that mcycle accounting remains unchanged.
Add an RV64 softmmu regression test for the issue.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4087
---
Changes in v2:
- Add the Fixes tag suggested by Daniel.
--- target/riscv/tcg/op_helper.c | 6 +++ target/riscv/tcg/pmu.c | 44 +++++++++++++----- target/riscv/tcg/pmu.h | 1 + tests/tcg/riscv64/Makefile.softmmu-target | 4 ++ tests/tcg/riscv64/test-minstret-ecall.S | 55 +++++++++++++++++++++++ 5 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 tests/tcg/riscv64/test-minstret-ecall.S
target/riscv/tcg/op_helper.c +6 -0
@@ -21,6 +21,9 @@#include "qemu/osdep.h" #include "cpu.h" #include "target/riscv/tcg/csr.h" +#ifndef CONFIG_USER_ONLY +#include "pmu.h" +#endif #include "internals.h" #include "exec/cputlb.h" #include "accel/tcg/cpu-ldst.h"
@@ -47,6 +50,9 @@ G_NORETURN void riscv_raise_exception(CPURISCVState *env,void helper_raise_exception (CPURISCVState *env,uint32_t exception) { +#ifndef CONFIG_USER_ONLY +riscv_pmu_decr_instret (env); +#endif riscv_raise_exception (env, exception,0 ); }
target/riscv/tcg/pmu.c +32 -12
67 lines not on this page · raw message
target/riscv/tcg/pmu.h +1 -0
@@ -36,6 +36,7 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx);void riscv_pmu_update_fixed_ctrs (CPURISCVState *env,privilege_mode_t newpriv,bool new_virt); +void riscv_pmu_decr_instret (CPURISCVState *env);RISCVException riscv_pmu_read_ctr (CPURISCVState *env,target_ulong *val,bool upper_half,uint32_t ctr_idx);
tests/tcg/riscv64/Makefile.softmmu-target +4 -0
@@ -24,6 +24,10 @@ EXTRA_RUNS += run-test-mepc-masking run-test-mepc-masking: test-mepc-masking $(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<) +EXTRA_RUNS += run-test-minstret-ecall +run-test-minstret-ecall: test-minstret-ecall + $(call run-test, $<, $(QEMU) -icount shift=1 $(QEMU_OPTS)$<) + EXTRA_RUNS += run-plugin-doubletrap run-plugin-doubletrap: doubletrap $(call run-test, $<, \
tests/tcg/riscv64/test-minstret-ecall.S new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 7 lines · Zephyr Li
With icount enabled,
helper_raise_exception()leaves ECALL inicount_get_raw()because it exits without restoring the current TB state. This makes minstret count an instruction that does not retire.Adjust only the fixed minstret baseline so that mcycle accounting remains unchanged.
Quoted 8 lines
Add an RV64 softmmu regression test for the issue.
Fixes 4fe8ae09062d ("target/riscv: Combine mhpmcounter andmhpmcounterh")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4087Signed-off-by Zephyr Li <[email protected]>Reviewed-by Daniel Henrique Barboza
Alistair
Quoted 218 lines
---
Changes in v2:
- Add the Fixes tag suggested by Daniel.---
target/riscv/tcg/op_helper.c | 6 +++ target/riscv/tcg/pmu.c | 44 +++++++++++++----- target/riscv/tcg/pmu.h | 1 + tests/tcg/riscv64/Makefile.softmmu-target | 4 ++ tests/tcg/riscv64/test-minstret-ecall.S | 55
+++++++++++++++++++++++5 files changed, 98 insertions(+), 12 deletions(-)create mode 100644
tests/tcg/riscv64/test-minstret-ecall.Starget/riscv/tcg/op_helper.c +3
-0@@ -21,6 +21,9 @@ #include "qemu/osdep.h" #include "cpu.h" #include "target/riscv/tcg/csr.h" +#ifndef CONFIG_USER_ONLY +#include "pmu.h" +#endif #include "internals.h" #include "exec/cputlb.h" #include "accel/tcg/cpu-ldst.h"@@ -47,6 +50,9 @@ G_NORETURN void riscv_raise_exception(CPURISCVState *env,
voidhelper_raise_exception(CPURISCVState *env, uint32_t exception){target/riscv/tcg/op_helper.c +3
-0+#ifndef CONFIG_USER_ONLY + riscv_pmu_decr_instret(env); +#endif
riscv_raise_exception(env, exception, 0);
}target/riscv/tcg/pmu.c +0
-0@@ -49,6 +49,21 @@ static bool riscv_pmu_counter_enabled(RISCVCPU *cpu, uint32_t ctr_idx) } }
+static boolriscv_pmu_counter_filtered(CPURISCVState *env, uint64_t cfg)target/riscv/tcg/pmu.c +14
-0+{ + bool virt_on = env->virt_enabled; + + return (env->priv == PRV_M && (cfg & MHPMEVENT_BIT_MINH)) || + (env->priv == PRV_S && virt_on && + (cfg & MHPMEVENT_BIT_VSINH)) || + (env->priv == PRV_U && virt_on && + (cfg & MHPMEVENT_BIT_VUINH)) || + (env->priv == PRV_S && !virt_on && + (cfg & MHPMEVENT_BIT_SINH)) || + (env->priv == PRV_U && !virt_on && + (cfg & MHPMEVENT_BIT_UINH)); +} +/*
* Information needed to update counters:
*new_priv,new_virt: To correctly save starting snapshot for the newlytarget/riscv/tcg/pmu.c +0
-0@@ -147,12 +162,27 @@ void riscv_pmu_update_fixed_ctrs(CPURISCVState *env, riscv_pmu_icount_update_priv(env, newpriv, new_virt); }target/riscv/tcg/pmu.c +11
-0+void riscv_pmu_decr_instret (CPURISCVState *env) +{ + if (!icount_enabled() || + (env->mcountinhibit & COUNTEREN_IR) || + riscv_pmu_counter_filtered(env, env->minstretcfg)) { + return; + } + + /* +* minstret is derived from icount, which includes the current +* instruction. Move the baseline forward to exclude an instruction
+ * that raises an exception and therefore does not retire. + */
+ env->pmu_ctrs[2].mhpmcounter_prev++;target/riscv/tcg/pmu.c +2
-0+} +int
riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx)
{
uint32_tctr_idx;
CPURISCVState *env =&cpu->env;
uint64_tmax_val=UINT64_MAX;
- boolvirt_on=env->virt_enabled;
PMUCTRState *counter;
gpointer value;target/riscv/tcg/pmu.c +0
-0@@ -170,17 +200,7 @@ int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx) return -1; }
- /* Privilege mode filtering */
- if ((env->priv==PRV_M&&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_MINH)) || - (env->priv==PRV_S&&virt_on&&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VSINH)) || - (env->priv==PRV_U&&virt_on&&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VUINH)) || - (env->priv==PRV_S&& !virt_on &&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_SINH)) || - (env->priv==PRV_U&& !virt_on &&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_UINH))) { + if (riscv_pmu_counter_filtered(env, env-Quoted 1 line
mhpmevent_val[ctr_idx])) {
return 0;
}target/riscv/tcg/pmu.h +1
-0@@ -36,6 +36,7 @@ int riscv_pmu_setup_timer(CPURISCVState *env,uint64_t value, uint32_t ctr_idx); void riscv_pmu_update_fixed_ctrs(CPURISCVState *env,privilege_mode_t newpriv, bool new_virt); +void riscv_pmu_decr_instret (CPURISCVState *env);RISCVException riscv_pmu_read_ctr(CPURISCVState *env,
target_ulong*val,
boolupper_half,uint32_tctr_idx);tests/tcg/riscv64/Makefile.softmmu-target +0
-0@@ -24,6 +24,10 @@ EXTRA_RUNS += run-test-mepc-maskingrun-test-mepc-masking: test-mepc-masking $(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
tests/tcg/riscv64/Makefile.softmmu-target +4
-0+EXTRA_RUNS += run-test-minstret-ecall +run-test-minstret-ecall: test-minstret-ecall + $(call run-test, $<, $(QEMU) -icount shift=1 $(QEMU_OPTS)$<) +EXTRA_RUNS += run-plugin-doubletrap
run-plugin-doubletrap: doubletrap
$(call run-test, $<, \tests/tcg/riscv64/test-minstret-ecall.S new file +12
-0@@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + + .option norvc + + .text + .global _start +_start: + lla t0, trap + csrw mtvec, t0 + + /* + * The first CSR read retires after obtaining s0. The ecalldoes not
tests/tcg/riscv64/test-minstret-ecall.S new file +1
-0+ * retire, so the trap handler must observe a difference ofone.
tests/tcg/riscv64/test-minstret-ecall.S new file +42
-0+ */ + csrr s0, minstret + ecall + sub t0, s1, s0 + li t1, 1 + bne t0, t1, fail + + li a0, 0 + j _exit + +trap: + csrr s1, minstret + csrr t0, mcause + li t1, 11 /* Environment call from M-mode */ + bne t0, t1, fail + + csrr t0, mepc + addi t0, t0, 4 + csrw mepc, t0 + mret + +fail: + li a0, 1 + +_exit: + lla a1, semiargs + li t0, 0x20026 /* ADP_Stopped_ApplicationExit */ + sd t0, 0(a1) + sd a0, 8(a1) + li a0, 0x20 /* TARGET_SYS_EXIT_EXTENDED */ + + /* Semihosting call sequence */ + .balign 16 + slli zero, zero, 0x1f + ebreak + srai zero, zero, 0x7 + j . + + .data + .balign 16 +semiargs: + .space 16
Alistair
AppliedQuoted 7 lines · Zephyr Li
With icount enabled,
helper_raise_exception()leaves ECALL inicount_get_raw()because it exits without restoring the current TB state. This makes minstret count an instruction that does not retire.Adjust only the fixed minstret baseline so that mcycle accounting remains unchanged.
Quoted 8 lines
Add an RV64 softmmu regression test for the issue.
Fixes 4fe8ae09062d ("target/riscv: Combine mhpmcounter andmhpmcounterh")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4087Signed-off-by Zephyr Li <[email protected]>Reviewed-by Daniel Henrique Barboza
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 218 lines
---
Changes in v2:
- Add the Fixes tag suggested by Daniel.---
target/riscv/tcg/op_helper.c | 6 +++ target/riscv/tcg/pmu.c | 44 +++++++++++++----- target/riscv/tcg/pmu.h | 1 + tests/tcg/riscv64/Makefile.softmmu-target | 4 ++ tests/tcg/riscv64/test-minstret-ecall.S | 55
+++++++++++++++++++++++5 files changed, 98 insertions(+), 12 deletions(-)create mode 100644
tests/tcg/riscv64/test-minstret-ecall.Starget/riscv/tcg/op_helper.c +3
-0@@ -21,6 +21,9 @@ #include "qemu/osdep.h" #include "cpu.h" #include "target/riscv/tcg/csr.h" +#ifndef CONFIG_USER_ONLY +#include "pmu.h" +#endif #include "internals.h" #include "exec/cputlb.h" #include "accel/tcg/cpu-ldst.h"@@ -47,6 +50,9 @@ G_NORETURN void riscv_raise_exception(CPURISCVState *env,
voidhelper_raise_exception(CPURISCVState *env, uint32_t exception){target/riscv/tcg/op_helper.c +3
-0+#ifndef CONFIG_USER_ONLY + riscv_pmu_decr_instret(env); +#endif
riscv_raise_exception(env, exception, 0);
}target/riscv/tcg/pmu.c +0
-0@@ -49,6 +49,21 @@ static bool riscv_pmu_counter_enabled(RISCVCPU *cpu, uint32_t ctr_idx) } }
+static boolriscv_pmu_counter_filtered(CPURISCVState *env, uint64_t cfg)target/riscv/tcg/pmu.c +14
-0+{ + bool virt_on = env->virt_enabled; + + return (env->priv == PRV_M && (cfg & MHPMEVENT_BIT_MINH)) || + (env->priv == PRV_S && virt_on && + (cfg & MHPMEVENT_BIT_VSINH)) || + (env->priv == PRV_U && virt_on && + (cfg & MHPMEVENT_BIT_VUINH)) || + (env->priv == PRV_S && !virt_on && + (cfg & MHPMEVENT_BIT_SINH)) || + (env->priv == PRV_U && !virt_on && + (cfg & MHPMEVENT_BIT_UINH)); +} +/*
* Information needed to update counters:
*new_priv,new_virt: To correctly save starting snapshot for the newlytarget/riscv/tcg/pmu.c +0
-0@@ -147,12 +162,27 @@ void riscv_pmu_update_fixed_ctrs(CPURISCVState *env, riscv_pmu_icount_update_priv(env, newpriv, new_virt); }target/riscv/tcg/pmu.c +11
-0+void riscv_pmu_decr_instret (CPURISCVState *env) +{ + if (!icount_enabled() || + (env->mcountinhibit & COUNTEREN_IR) || + riscv_pmu_counter_filtered(env, env->minstretcfg)) { + return; + } + + /* +* minstret is derived from icount, which includes the current +* instruction. Move the baseline forward to exclude an instruction
+ * that raises an exception and therefore does not retire. + */
+ env->pmu_ctrs[2].mhpmcounter_prev++;target/riscv/tcg/pmu.c +2
-0+} +int
riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx)
{
uint32_tctr_idx;
CPURISCVState *env =&cpu->env;
uint64_tmax_val=UINT64_MAX;
- boolvirt_on=env->virt_enabled;
PMUCTRState *counter;
gpointer value;target/riscv/tcg/pmu.c +0
-0@@ -170,17 +200,7 @@ int riscv_pmu_incr_ctr(RISCVCPU *cpu, enum riscv_pmu_event_idx event_idx) return -1; }
- /* Privilege mode filtering */
- if ((env->priv==PRV_M&&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_MINH)) || - (env->priv==PRV_S&&virt_on&&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VSINH)) || - (env->priv==PRV_U&&virt_on&&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VUINH)) || - (env->priv==PRV_S&& !virt_on &&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_SINH)) || - (env->priv==PRV_U&& !virt_on &&
- (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_UINH))) { + if (riscv_pmu_counter_filtered(env, env-Quoted 1 line
mhpmevent_val[ctr_idx])) {
return 0;
}target/riscv/tcg/pmu.h +1
-0@@ -36,6 +36,7 @@ int riscv_pmu_setup_timer(CPURISCVState *env,uint64_t value, uint32_t ctr_idx); void riscv_pmu_update_fixed_ctrs(CPURISCVState *env,privilege_mode_t newpriv, bool new_virt); +void riscv_pmu_decr_instret (CPURISCVState *env);RISCVException riscv_pmu_read_ctr(CPURISCVState *env,
target_ulong*val,
boolupper_half,uint32_tctr_idx);tests/tcg/riscv64/Makefile.softmmu-target +0
-0@@ -24,6 +24,10 @@ EXTRA_RUNS += run-test-mepc-maskingrun-test-mepc-masking: test-mepc-masking $(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
tests/tcg/riscv64/Makefile.softmmu-target +4
-0+EXTRA_RUNS += run-test-minstret-ecall +run-test-minstret-ecall: test-minstret-ecall + $(call run-test, $<, $(QEMU) -icount shift=1 $(QEMU_OPTS)$<) +EXTRA_RUNS += run-plugin-doubletrap
run-plugin-doubletrap: doubletrap
$(call run-test, $<, \tests/tcg/riscv64/test-minstret-ecall.S new file +12
-0@@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + + .option norvc + + .text + .global _start +_start: + lla t0, trap + csrw mtvec, t0 + + /* + * The first CSR read retires after obtaining s0. The ecalldoes not
tests/tcg/riscv64/test-minstret-ecall.S new file +1
-0+ * retire, so the trap handler must observe a difference ofone.
tests/tcg/riscv64/test-minstret-ecall.S new file +42
-0+ */ + csrr s0, minstret + ecall + sub t0, s1, s0 + li t1, 1 + bne t0, t1, fail + + li a0, 0 + j _exit + +trap: + csrr s1, minstret + csrr t0, mcause + li t1, 11 /* Environment call from M-mode */ + bne t0, t1, fail + + csrr t0, mepc + addi t0, t0, 4 + csrw mepc, t0 + mret + +fail: + li a0, 1 + +_exit: + lla a1, semiargs + li t0, 0x20026 /* ADP_Stopped_ApplicationExit */ + sd t0, 0(a1) + sd a0, 8(a1) + li a0, 0x20 /* TARGET_SYS_EXIT_EXTENDED */ + + /* Semihosting call sequence */ + .balign 16 + slli zero, zero, 0x1f + ebreak + srai zero, zero, 0x7 + j . + + .data + .balign 16 +semiargs: + .space 16