Skip to content

Commit

Permalink
target/riscv: Fix mcycle/minstret increment behavior
Browse files Browse the repository at this point in the history
The mcycle/minstret counter's stop flag is mistakenly updated on a copy
on stack. Thus the counter increments even when the CY/IR bit in the
mcountinhibit register is set. This commit corrects its behavior.

Fixes: 3780e33 (target/riscv: Support mcycle/minstret write operation)
Signed-off-by: Xu Lu <luxu.kernel@bytedance.com>
Reviewed-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
(cherry picked from commit 5cb0e7a)
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
  • Loading branch information
Xu Lu authored and Michael Tokarev committed Jan 8, 2024
1 parent c6f6473 commit 0eab8d4
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions target/riscv/csr.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,24 +697,24 @@ static int write_mhpmcounterh(CPURISCVState *env, int csrno, target_ulong val)
static RISCVException riscv_pmu_read_ctr(CPURISCVState *env, target_ulong *val,
bool upper_half, uint32_t ctr_idx)
{
PMUCTRState counter = env->pmu_ctrs[ctr_idx];
target_ulong ctr_prev = upper_half ? counter.mhpmcounterh_prev :
counter.mhpmcounter_prev;
target_ulong ctr_val = upper_half ? counter.mhpmcounterh_val :
counter.mhpmcounter_val;
PMUCTRState *counter = &env->pmu_ctrs[ctr_idx];
target_ulong ctr_prev = upper_half ? counter->mhpmcounterh_prev :
counter->mhpmcounter_prev;
target_ulong ctr_val = upper_half ? counter->mhpmcounterh_val :
counter->mhpmcounter_val;

if (get_field(env->mcountinhibit, BIT(ctr_idx))) {
/**
* Counter should not increment if inhibit bit is set. We can't really
* stop the icount counting. Just return the counter value written by
* the supervisor to indicate that counter was not incremented.
*/
if (!counter.started) {
if (!counter->started) {
*val = ctr_val;
return RISCV_EXCP_NONE;
} else {
/* Mark that the counter has been stopped */
counter.started = false;
counter->started = false;
}
}

Expand Down

0 comments on commit 0eab8d4

Please sign in to comment.