Skip to content

Commit 507957e

Browse files
clementlegeralistair23
authored andcommitted
target/riscv: Fix henvcfg potentially containing stale bits
With the current implementation, if we had the following scenario: - Set bit x in menvcfg - Set bit x in henvcfg - Clear bit x in menvcfg then, the internal variable env->henvcfg would still contain bit x due to both a wrong menvcfg mask used in write_henvcfg() as well as a missing update of henvcfg upon menvcfg update. This can lead to some wrong interpretation of the context. In order to update henvcfg upon menvcfg writing, call write_henvcfg() after writing menvcfg. Clearing henvcfg upon writing the new value is also needed in write_henvcfg() as well as clearing henvcfg upper part when writing it with write_henvcfgh(). Signed-off-by: Clément Léger <[email protected]> Reviewed-by: Daniel Henrique Barboza <[email protected]> Reviewed-by: Alistair Francis <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]>
1 parent fdb7bce commit 507957e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

target/riscv/csr.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2946,6 +2946,8 @@ static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
29462946
return RISCV_EXCP_NONE;
29472947
}
29482948

2949+
static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
2950+
target_ulong val);
29492951
static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
29502952
target_ulong val)
29512953
{
@@ -2974,6 +2976,7 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
29742976
}
29752977
}
29762978
env->menvcfg = (env->menvcfg & ~mask) | (val & mask);
2979+
write_henvcfg(env, CSR_HENVCFG, env->henvcfg);
29772980

29782981
return RISCV_EXCP_NONE;
29792982
}
@@ -2985,6 +2988,8 @@ static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
29852988
return RISCV_EXCP_NONE;
29862989
}
29872990

2991+
static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
2992+
target_ulong val);
29882993
static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
29892994
target_ulong val)
29902995
{
@@ -2996,6 +3001,7 @@ static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
29963001
uint64_t valh = (uint64_t)val << 32;
29973002

29983003
env->menvcfg = (env->menvcfg & ~mask) | (valh & mask);
3004+
write_henvcfgh(env, CSR_HENVCFGH, env->henvcfg >> 32);
29993005

30003006
return RISCV_EXCP_NONE;
30013007
}
@@ -3101,7 +3107,7 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
31013107
}
31023108
}
31033109

3104-
env->henvcfg = (env->henvcfg & ~mask) | (val & mask);
3110+
env->henvcfg = val & mask;
31053111

31063112
return RISCV_EXCP_NONE;
31073113
}
@@ -3134,7 +3140,7 @@ static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
31343140
return ret;
31353141
}
31363142

3137-
env->henvcfg = (env->henvcfg & ~mask) | (valh & mask);
3143+
env->henvcfg = (env->henvcfg & 0xFFFFFFFF) | (valh & mask);
31383144
return RISCV_EXCP_NONE;
31393145
}
31403146

0 commit comments

Comments
 (0)