target/riscv: Fix CPU state access on big-endian hosts
PATCH 1/1 target/riscv: Fix CPU state access on big-endian hosts
mstatus and mstatus_hs are uint64_t but accessed through
tcg_gen_[ld|st]_tl(..., offsetof(mstatus))
which on a big-endian host running riscv32 incorrectly accesses the top 32 bits rather than the lower bits.
Widen the operations to 64 bit so the lower bits are modified.
--- target/riscv/tcg/translate.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
target/riscv/tcg/translate.c +16 -16
@@ -693,7 +693,7 @@ static TCGv get_address_indexed(DisasContext *ctx, int rs1, TCGv offs)*/ static void mark_fs_dirty (DisasContext *ctx) { -TCGv tmp; +TCGv_i64 tmp;if (!has_ext (ctx,RVF )) {return ;
@@ -703,15 +703,15 @@ static void mark_fs_dirty(DisasContext *ctx)/* Remember the state change for the rest of the TB. */ ctx->mstatus_fs =EXT_STATUS_DIRTY ; - tmp =tcg_temp_new (); -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_FS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); + tmp =tcg_temp_new_i64 (); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_FS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus));if (ctx->virt_enabled) { -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_FS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_FS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); } } }
@@ -726,21 +726,21 @@ static inline void mark_fs_dirty(DisasContext *ctx) { }*/ static void mark_vs_dirty (DisasContext *ctx) { -TCGv tmp; +TCGv_i64 tmp;if (ctx->mstatus_vs !=EXT_STATUS_DIRTY ) {/* Remember the state change for the rest of the TB. */ ctx->mstatus_vs =EXT_STATUS_DIRTY ; - tmp =tcg_temp_new (); -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_VS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); + tmp =tcg_temp_new_i64 (); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_VS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus));if (ctx->virt_enabled) { -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_VS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_VS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); } } }
Signature · 1 line
2.52.0
Quoted 9 lines · Anton Johansson via qemu development
mstatus and
mstatus_hsareuint64_tbut accessed throughtcg_gen_[ld|st]_tl(..., offsetof(mstatus))
which on a big-endian host running riscv32 incorrectly accesses the top 32 bits rather than the lower bits.
Widen the operations to 64 bit so the lower bits are modified.
Maybe a Fixes Tag and cc to QEMU stable?
Quoted 61 lines
Signed-off-by Anton Johansson <[email protected]>--- target/riscv/tcg/translate.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)target/riscv/tcg/translate.c +13
-13@@ -693,7 +693,7 @@ static TCGv get_address_indexed(DisasContext *ctx, int rs1, TCGv offs)*/ static void mark_fs_dirty (DisasContext *ctx) { -TCGv tmp; +TCGv_i64 tmp;if (!has_ext (ctx,RVF )) {return ;@@ -703,15 +703,15 @@ static void mark_fs_dirty(DisasContext *ctx)/* Remember the state change for the rest of the TB. */ ctx->mstatus_fs =EXT_STATUS_DIRTY ; - tmp =tcg_temp_new (); -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_FS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); + tmp =tcg_temp_new_i64 (); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_FS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus));if (ctx->virt_enabled) { -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_FS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_FS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); } } }@@ -726,21 +726,21 @@ static inline void mark_fs_dirty(DisasContext *ctx) { }*/ static void mark_vs_dirty (DisasContext *ctx) { -TCGv tmp; +TCGv_i64 tmp;if (ctx->mstatus_vs !=EXT_STATUS_DIRTY ) {/* Remember the state change for the rest of the TB. */ ctx->mstatus_vs =EXT_STATUS_DIRTY ; - tmp =tcg_temp_new (); - tcg_ge\n_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_VS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); + tmp =tcg_temp_new_i64 (); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_VS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus));
target/riscv/tcg/translate.c static inline void mark_fs_dirty(DisasContext *ctx) { } · line 746
Quoted 10 lines
target/riscv/tcg/translate.c +3
-3if (ctx->virt_enabled) { -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_VS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_VS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); } } }
Regarding the code I am not an expert, but this fix LGTM.
Regards,
Tommaso
Quoted 9 lines · Anton Johansson
mstatus and
mstatus_hsareuint64_tbut accessed throughtcg_gen_[ld|st]_tl(..., offsetof(mstatus))
which on a big-endian host running riscv32 incorrectly accesses the top 32 bits rather than the lower bits.
Widen the operations to 64 bit so the lower bits are modified.
Please also add a Fixes tag pointing to the commit that introduced this bug.
The format is,
Thanks,
Chao
Quoted 74 lines
Signed-off-by Anton Johansson <[email protected]>--- target/riscv/tcg/translate.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)target/riscv/tcg/translate.c +16
-16@@ -693,7 +693,7 @@ static TCGv get_address_indexed(DisasContext *ctx, int rs1, TCGv offs)*/ static void mark_fs_dirty (DisasContext *ctx) { -TCGv tmp; +TCGv_i64 tmp;if (!has_ext (ctx,RVF )) {return ;@@ -703,15 +703,15 @@ static void mark_fs_dirty(DisasContext *ctx)/* Remember the state change for the rest of the TB. */ ctx->mstatus_fs =EXT_STATUS_DIRTY ; - tmp =tcg_temp_new (); -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_FS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); + tmp =tcg_temp_new_i64 (); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_FS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus));if (ctx->virt_enabled) { -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_FS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_FS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); } } }@@ -726,21 +726,21 @@ static inline void mark_fs_dirty(DisasContext *ctx) { }*/ static void mark_vs_dirty (DisasContext *ctx) { -TCGv tmp; +TCGv_i64 tmp;if (ctx->mstatus_vs !=EXT_STATUS_DIRTY ) {/* Remember the state change for the rest of the TB. */ ctx->mstatus_vs =EXT_STATUS_DIRTY ; - tmp =tcg_temp_new (); -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_VS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); + tmp =tcg_temp_new_i64 (); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_VS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus));if (ctx->virt_enabled) { -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_VS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_VS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); } } }Signature · 1 line
2.52.0
Quoted 6 lines · Anton Johansson via qemu development
mstatus and
mstatus_hsareuint64_tbut accessed throughtcg_gen_[ld|st]_tl(..., offsetof(mstatus))which on a big-endian host running riscv32 incorrectly accesses the top 32 bits rather than the lower bits.
mstatus and mstatus_hs are uint64_t, but mark_fs_dirty() and mark_vs_dirty() update them with tcg_gen_[ld|st]_tl(). On big-endian hosts with 32-bit target_ulong, this modifies the upper half instead of the lower one, so the FS/VS dirty bits are never set. Use 64-bit accesses instead.
Quoted 33 lines
Widen the operations to 64 bit so the lower bits are modified.
Signed-off-by Anton Johansson <[email protected]>--- target/riscv/tcg/translate.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)target/riscv/tcg/translate.c +5
-5@@ -693,7 +693,7 @@ static TCGv get_address_indexed(DisasContext *ctx, int rs1, TCGv offs)*/ static void mark_fs_dirty (DisasContext *ctx) { -TCGv tmp; +TCGv_i64 tmp;if (!has_ext (ctx,RVF )) {return ;@@ -703,15 +703,15 @@ static void mark_fs_dirty(DisasContext *ctx)/* Remember the state change for the rest of the TB. */ ctx->mstatus_fs =EXT_STATUS_DIRTY ; - tmp =tcg_temp_new (); -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_FS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); + tmp =tcg_temp_new_i64 (); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_FS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus));
target/riscv/tcg/translate.c static void mark_fs_dirty(DisasContext *ctx) · line 717
Quoted 10 lines
target/riscv/tcg/translate.c +3
-3if (ctx->virt_enabled) { -tcg_gen_ld_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); -tcg_gen_ori_tl (tmp, tmp,MSTATUS_FS ); -tcg_gen_st_tl (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ld_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); +tcg_gen_ori_i64 (tmp, tmp,MSTATUS_FS ); +tcg_gen_st_i64 (tmp, tcg_env, offsetof(CPURISCVState , mstatus_hs)); } } }
Wondering about being safe (or catching other bad uses)
#define TCG_ENV_OFS_(f) offsetof(CPUArchState, f)
#define TCG_ENV_SIZE_(f) sizeof_field(CPUArchState, f)
#define tcg_gen_ld_env_tl(ret, f) \
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != \
sizeof(target_ulong)); \
tcg_gen_ld_tl(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)#define tcg_gen_ld_env_i64(ret, f) \
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != 8); \
tcg_gen_ld_i64(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)or even using _Generic:
#define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f)
#define TCG_ENV_OFS_(f) offsetof(CPUArchState, f)
#define tcg_gen_st_env_i32(arg, f) \
_Generic(TCG_ENV_FIELD_(f), \
bool: tcg_gen_st8_i32, \
uint8_t: tcg_gen_st8_i32, \
int8_t: tcg_gen_st8_i32, \
uint16_t: tcg_gen_st16_i32, \
int16_t: tcg_gen_st16_i32, \
uint32_t: tcg_gen_st_i32, \
int32_t: tcg_gen_st_i32) \
(arg, tcg_env, TCG_ENV_OFS_(f))#define tcg_gen_st_env_i64(arg, f) \
_Generic(TCG_ENV_FIELD_(f), \
uint64_t: tcg_gen_st_i64, \
int64_t: tcg_gen_st_i64) \
(arg, tcg_env, TCG_ENV_OFS_(f))#if TARGET_LONG_BITS == 32
# define tcg_gen_ld_env_tl tcg_gen_ld_env_i32 # define tcg_gen_st_env_tl tcg_gen_st_env_i32 #else # define tcg_gen_ld_env_tl tcg_gen_ld_env_i64 # define tcg_gen_st_env_tl tcg_gen_st_env_i64 #endif
Then replacing (a cheap replacement that can be done with coccinelle spatch):
@@ -704,14 +757,14 @@ static void mark_fs_dirty(DisasContext *ctx)
ctx->mstatus_fs = EXT_STATUS_DIRTY;
tmp = tcg_temp_new();
- tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus));
+ tcg_gen_ld_env_tl(tmp, mstatus);
tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);- tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus));
+ tcg_gen_st_env_tl(tmp, mstatus);
if (ctx->virt_enabled) {
- tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
+ tcg_gen_ld_env_tl(tmp, mstatus_hs);
tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS);
- tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
+ tcg_gen_st_env_tl(tmp, mstatus_hs);
}
}triggers:
FAILED: [code=1] libqemu-riscv32-softmmu.a.p/target_riscv_tcg_translate.c.o../target/riscv/tcg/translate.c:760:9: error: controlling expression type 'uint64_t' (aka 'unsigned long long') not compatible with any generic association type
760 | tcg_gen_ld_env_tl(tmp, mstatus);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:86:29: note: expanded from macro 'tcg_gen_ld_env_tl'
86 | # define tcg_gen_ld_env_tl tcg_gen_ld_env_i32
| ^
../target/riscv/tcg/translate.c:45:14: note: expanded from macro 'tcg_gen_ld_env_i32'
45 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:41:29: note: expanded from macro 'TCG_ENV_FIELD_'
41 | #define TCG_ENV_FIELD_(f) (((CPURISCVState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:762:9: error: controlling expression type 'uint64_t' (aka 'unsigned long long') not compatible with any generic association type
762 | tcg_gen_st_env_tl(tmp, mstatus);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:87:29: note: expanded from macro 'tcg_gen_st_env_tl'
87 | # define tcg_gen_st_env_tl tcg_gen_st_env_i32
| ^
../target/riscv/tcg/translate.c:69:14: note: expanded from macro 'tcg_gen_st_env_i32'
69 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:41:29: note: expanded from macro 'TCG_ENV_FIELD_'
41 | #define TCG_ENV_FIELD_(f) (((CPURISCVState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:765:13: error: controlling expression type 'uint64_t' (aka 'unsigned long long') not compatible with any generic association type
765 | tcg_gen_ld_env_tl(tmp, mstatus_hs);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:86:29: note: expanded from macro 'tcg_gen_ld_env_tl'
86 | # define tcg_gen_ld_env_tl tcg_gen_ld_env_i32
| ^
../target/riscv/tcg/translate.c:45:14: note: expanded from macro 'tcg_gen_ld_env_i32'
45 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:41:29: note: expanded from macro 'TCG_ENV_FIELD_'
41 | #define TCG_ENV_FIELD_(f) (((CPURISCVState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:767:13: error: controlling expression type 'uint64_t' (aka 'unsigned long long') not compatible with any generic association type
767 | tcg_gen_st_env_tl(tmp, mstatus_hs);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:87:29: note: expanded from macro 'tcg_gen_st_env_tl'
87 | # define tcg_gen_st_env_tl tcg_gen_st_env_i32
| ^
../target/riscv/tcg/translate.c:69:14: note: expanded from macro 'tcg_gen_st_env_i32'
69 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
../target/riscv/tcg/translate.c:41:29: note: expanded from macro 'TCG_ENV_FIELD_'
41 | #define TCG_ENV_FIELD_(f) (((CPURISCVState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~~4 errors generated.
Quoted 105 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Anton Johansson via qemu development
mstatus and
mstatus_hsareuint64_tbut accessed throughtcg_gen_[ld|st]_tl(..., offsetof(mstatus))
which on a big-endian host running riscv32 incorrectly accesses the top 32 bits rather than the lower bits.
mstatus and
mstatus_hsareuint64_t, butmark_fs_dirty()andmark_vs_dirty()update them with tcg_gen_[ld|st]_tl(). On big-endian hosts with 32-bittarget_ulong, this modifies the upper half instead of the lower one, so the FS/VS dirty bits are never set. Use 64-bit accesses instead.Fixes 284d697c74e ("target/riscv: Merge m/vsstatus and m/vsstatush into one uint64_t unit")Fixes 8e1ee1fb571 ("target/riscv: rvv-1.0: add translation-time vector context status")Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 43 lines
Widen the operations to 64 bit so the lower bits are modified.
Signed-off-by Anton Johansson <[email protected]>---
target/riscv/tcg/translate.c| 32 ++++++++++++++++----------------1 file changed, 16 insertions(+), 16 deletions(-)target/riscv/tcg/translate.c +8
-8@@ -693,7 +693,7 @@ static TCGv get_address_indexed(DisasContext *ctx,int rs1, TCGv offs) */ static void mark_fs_dirty(DisasContext *ctx) { - TCGv tmp; + TCGv_i64 tmp; if (!has_ext(ctx, RVF)) { return;@@ -703,15 +703,15 @@ static void mark_fs_dirty(DisasContext *ctx) /* Remember the state change for the rest of the TB. */ ctx->mstatus_fs = EXT_STATUS_DIRTY; - tmp = tcg_temp_new(); - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); + tmp = tcg_temp_new_i64(); + tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); + tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS); + tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); if (ctx->virt_enabled) { - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); + tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); + tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS); + tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); }Quoted 2 lines
}
}Wondering about being safe (or catching other bad uses)
#define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)#define TCG_ENV_SIZE_(f)sizeof_field(CPUArchState, f)#define
tcg_gen_ld_env_tl(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != \
sizeof(target_ulong)); \
tcg_gen_ld_tl(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)#define
tcg_gen_ld_env_i64(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != 8); \
tcg_gen_ld_i64(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)or even using
_Generic:#define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f) #define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)#define
tcg_gen_st_env_i32(arg, f)\
_Generic(TCG_ENV_FIELD_(f), \
bool: tcg_gen_st8_i32, \
uint8_t: tcg_gen_st8_i32, \
int8_t: tcg_gen_st8_i32, \
uint16_t: tcg_gen_st16_i32, \
int16_t: tcg_gen_st16_i32, \
uint32_t: tcg_gen_st_i32, \
int32_t: tcg_gen_st_i32) \
(arg,tcg_env,TCG_ENV_OFS_(f))#define
tcg_gen_st_env_i64(arg, f)\
_Generic(TCG_ENV_FIELD_(f), \
uint64_t: tcg_gen_st_i64, \
int64_t: tcg_gen_st_i64) \
(arg,tcg_env,TCG_ENV_OFS_(f))
Quoted 7 lines
#if
TARGET_LONG_BITS== 32# define tcg_gen_ld_env_tl tcg_gen_ld_env_i32 # define tcg_gen_st_env_tl tcg_gen_st_env_i32 #else # define tcg_gen_ld_env_tl tcg_gen_ld_env_i64 # define tcg_gen_st_env_tl tcg_gen_st_env_i64 #endif
I added that to "tcg/tcg-op-env.h".
Quoted 3 lines
Then replacing (a cheap replacement that can be done with coccinelle spatch):
-- >8 --
@@
expression R;
identifier F;
symbol tcg_env;
type T;
@@
(
- tcg_gen_ld_tl(R, tcg_env, offsetof(T, F))
+ tcg_gen_ld_env_tl(R, F)
|
- tcg_gen_st_tl(R, tcg_env, offsetof(T, F))
+ tcg_gen_st_env_tl(R, F)
)
@@
expression R;
identifier F;
symbol tcg_env;
identifier T;
@@
(
- tcg_gen_ld_tl(R, tcg_env, offsetof(T, F))
+ tcg_gen_ld_env_tl(R, F)
|
- tcg_gen_st_tl(R, tcg_env, offsetof(T, F))
+ tcg_gen_st_env_tl(R, F)
)
---
Converting i386:
-- >8 --
target/i386/tcg/translate.c +9 -12
@@ -25,2 +25,3 @@#include "tcg/tcg-op.h" +#include "tcg/tcg-op-env.h" #include "tcg/tcg-op-gvec.h"
@@ -706,5 +707,5 @@ static void gen_set_eflags(DisasContext *s, target_ulong mask) -tcg_gen_ld_tl (t, tcg_env, offsetof(CPUX86State , eflags)); +tcg_gen_ld_env_tl (t, eflags);tcg_gen_ori_tl (t, t, mask); -tcg_gen_st_tl (t, tcg_env, offsetof(CPUX86State , eflags)); +tcg_gen_st_env_tl (t, eflags); }
@@ -715,5 +716,5 @@ static void gen_reset_eflags(DisasContext *s, target_ulong mask) -tcg_gen_ld_tl (t, tcg_env, offsetof(CPUX86State , eflags)); +tcg_gen_ld_env_tl (t, eflags);tcg_gen_andi_tl (t, t, ~mask); -tcg_gen_st_tl (t, tcg_env, offsetof(CPUX86State , eflags)); +tcg_gen_st_env_tl (t, eflags); }
@@ -2447,4 +2448,3 @@ static void gen_x87(DisasContext *s, X86DecodedInsn *decode) offsetof(CPUX86State, fpds)); -tcg_gen_st_tl (last_addr, tcg_env, - offsetof(CPUX86State , fpdp)); +tcg_gen_st_env_tl (last_addr, fpdp); }
@@ -2756,4 +2756,3 @@ static void gen_x87(DisasContext *s, X86DecodedInsn *decode) offsetof(CPUX86State, fpcs)); -tcg_gen_st_tl (eip_cur_tl (s), - tcg_env, offsetof(CPUX86State , fpip)); +tcg_gen_st_env_tl (eip_cur_tl (s), fpip); }
@@ -3200,6 +3199,4 @@ static void gen_multi0F(DisasContext *s, X86DecodedInsn *decode)tcg_gen_mov_tl (s->T0, cpu_seg_base[R_GS ]); -tcg_gen_ld_tl (cpu_seg_base[R_GS ], tcg_env, - offsetof(CPUX86State , kernelgsbase)); -tcg_gen_st_tl (s->T0, tcg_env, - offsetof(CPUX86State , kernelgsbase)); +tcg_gen_ld_env_tl (cpu_seg_base[R_GS ], kernelgsbase); +tcg_gen_st_env_tl (s->T0, kernelgsbase); }
---
triggers:
../target/i386/tcg/translate.c:2449:13: error: controlling expression type 'uint64_t' (aka 'unsigned long long') not compatible with any generic association type
2449 | tcg_gen_st_env_tl(last_addr, fpdp);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:65:29: note: expanded from macro 'tcg_gen_st_env_tl'
65 | # define tcg_gen_st_env_tl tcg_gen_st_env_i32
| ^
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:47:14: note: expanded from macro 'tcg_gen_st_env_i32'
47 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:16:29: note: expanded from macro 'TCG_ENV_FIELD_'
16 | #define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~
../target/i386/tcg/translate.c:2757:9: error: controlling expression type 'uint64_t' (aka 'unsigned long long') not compatible with any generic association type
2757 | tcg_gen_st_env_tl(eip_cur_tl(s), fpip);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:65:29: note: expanded from macro 'tcg_gen_st_env_tl'
65 | # define tcg_gen_st_env_tl tcg_gen_st_env_i32
| ^
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:47:14: note: expanded from macro 'tcg_gen_st_env_i32'
47 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:16:29: note: expanded from macro 'TCG_ENV_FIELD_'
16 | #define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~2 errors generated.
Both CPUX86State::fpip/fpdp fields are uint64_t, so the same class of bug on big-endian hosts.
FAILED: [code=1] libqemu-i386-softmmu.a.p/target_i386_tcg_translate.c.o
Quoted 89 lines · Philippe Mathieu-Daudé
Quoted 88 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Anton Johansson via qemu development
mstatus and
mstatus_hsareuint64_tbut accessed throughtcg_gen_[ld|st]_tl(..., offsetof(mstatus))
which on a big-endian host running riscv32 incorrectly accesses the top 32 bits rather than the lower bits.
mstatus and
mstatus_hsareuint64_t, butmark_fs_dirty()andmark_vs_dirty()update them with tcg_gen_[ld|st]_tl(). On big-endian hosts with 32-bittarget_ulong, this modifies the upper half instead of the lower one, so the FS/VS dirty bits are never set. Use 64-bit accesses instead.Fixes 284d697c74e ("target/riscv: Merge m/vsstatus and m/vsstatush into one uint64_t unit")Fixes 8e1ee1fb571 ("target/riscv: rvv-1.0: add translation-time vector context status")Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 43 lines
Widen the operations to 64 bit so the lower bits are modified.
Signed-off-by Anton Johansson <[email protected]>---
target/riscv/tcg/translate.c| 32 ++++++++++++++++----------------1 file changed, 16 insertions(+), 16 deletions(-)target/riscv/tcg/translate.c +8
-8@@ -693,7 +693,7 @@ static TCGv get_address_indexed(DisasContext*ctx, int rs1, TCGv offs) */ static void mark_fs_dirty(DisasContext *ctx) { - TCGv tmp; + TCGv_i64 tmp; if (!has_ext(ctx, RVF)) { return;@@ -703,15 +703,15 @@ static void mark_fs_dirty(DisasContext *ctx) /* Remember the state change for the rest of the TB. */ ctx->mstatus_fs = EXT_STATUS_DIRTY; - tmp = tcg_temp_new(); - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); + tmp = tcg_temp_new_i64(); + tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); + tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS); + tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); if (ctx->virt_enabled) { - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); + tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); + tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS); + tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); }Quoted 2 lines
}
}Wondering about being safe (or catching other bad uses)
#define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)#define TCG_ENV_SIZE_(f)sizeof_field(CPUArchState, f)#define
tcg_gen_ld_env_tl(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != \
sizeof(target_ulong)); \
tcg_gen_ld_tl(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)#define
tcg_gen_ld_env_i64(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != 8); \
tcg_gen_ld_i64(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)or even using
_Generic:#define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f) #define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)
Quoted 7 lines
Quoted 7 lines
#define
tcg_gen_st_env_i32(arg, f)\
_Generic(TCG_ENV_FIELD_(f), \
bool: tcg_gen_st8_i32, \
uint8_t: tcg_gen_st8_i32, \
int8_t: tcg_gen_st8_i32, \
uint16_t: tcg_gen_st16_i32, \
int16_t: tcg_gen_st16_i32, \
Hmm tricky (dangerous?), this also change our current uses of target-long accesses to smaller fields. This is how I'd expect the uses but not always what we have now. For example, commit ebc9401a406 ("target/hppa: Split PSW X and B into their own field") reduced CPUHPPAState::psw from uint64_t to uint32_t, but both accessors -- trans_rsm() and trans_ssm() -- keep accessing with tcg_gen_ld_i64() instead of tcg_gen_ld32u_i64(). Likely buggy wrt endianness.
Quoted 143 lines
Quoted 10 lines
uint32_t: tcg_gen_st_i32, \
int32_t: tcg_gen_st_i32) \
(arg,tcg_env,TCG_ENV_OFS_(f))#define
tcg_gen_st_env_i64(arg, f)\
_Generic(TCG_ENV_FIELD_(f), \
uint64_t: tcg_gen_st_i64, \
int64_t: tcg_gen_st_i64) \
(arg,tcg_env,TCG_ENV_OFS_(f))Quoted 7 lines
#if
TARGET_LONG_BITS== 32# define tcg_gen_ld_env_tl tcg_gen_ld_env_i32 # define tcg_gen_st_env_tl tcg_gen_st_env_i32 #else # define tcg_gen_ld_env_tl tcg_gen_ld_env_i64 # define tcg_gen_st_env_tl tcg_gen_st_env_i64 #endifI added that to "
tcg/tcg-op-env.h".Quoted 3 lines
Then replacing (a cheap replacement that can be done with coccinelle spatch):
-- >8 --
@@
expression R;
identifier F;
symboltcg_env;
type T;
@@
(
-tcg_gen_ld_tl(R, tcg_env, offsetof(T, F))
+tcg_gen_ld_env_tl(R, F)
|
-tcg_gen_st_tl(R, tcg_env, offsetof(T, F))
+tcg_gen_st_env_tl(R, F)
)@@
expression R;
identifier F;
symboltcg_env;
identifier T;
@@
(
-tcg_gen_ld_tl(R, tcg_env, offsetof(T, F))
+tcg_gen_ld_env_tl(R, F)
|
-tcg_gen_st_tl(R, tcg_env, offsetof(T, F))
+tcg_gen_st_env_tl(R, F)
)
---Converting i386:
-- >8 --
target/i386/tcg/translate.c +3
-2@@ -25,2 +25,3 @@ #include "tcg/tcg-op.h" +#include "tcg/tcg-op-env.h" #include "tcg/tcg-op-gvec.h"@@ -706,5 +707,5 @@ static void gen_set_eflags(DisasContext *s, target_ulong mask) - tcg_gen_ld_tl(t, tcg_env, offsetof(CPUX86State, eflags)); + tcg_gen_ld_env_tl(t, eflags); tcg_gen_ori_tl(t, t, mask); - tcg_gen_st_tl(t, tcg_env, offsetof(CPUX86State, eflags)); + tcg_gen_st_env_tl(t, eflags);}
target/i386/tcg/translate.c +2
-2@@ -715,5 +716,5 @@ static void gen_reset_eflags(DisasContext *s, target_ulong mask) - tcg_gen_ld_tl(t, tcg_env, offsetof(CPUX86State, eflags)); + tcg_gen_ld_env_tl(t, eflags); tcg_gen_andi_tl(t, t, ~mask); - tcg_gen_st_tl(t, tcg_env, offsetof(CPUX86State, eflags)); + tcg_gen_st_env_tl(t, eflags);}
target/i386/tcg/translate.c +1
-2@@ -2447,4 +2448,3 @@ static void gen_x87(DisasContext *s, X86DecodedInsn *decode) offsetof(CPUX86State, fpds)); - tcg_gen_st_tl(last_addr, tcg_env, - offsetof(CPUX86State, fpdp)); + tcg_gen_st_env_tl(last_addr, fpdp);}
target/i386/tcg/translate.c +1
-2@@ -2756,4 +2756,3 @@ static void gen_x87(DisasContext *s, X86DecodedInsn *decode) offsetof(CPUX86State, fpcs)); - tcg_gen_st_tl(eip_cur_tl(s), - tcg_env, offsetof(CPUX86State, fpip)); + tcg_gen_st_env_tl(eip_cur_tl(s), fpip);}
target/i386/tcg/translate.c +2
-4@@ -3200,6 +3199,4 @@ static void gen_multi0F(DisasContext *s, X86DecodedInsn *decode) tcg_gen_mov_tl(s->T0, cpu_seg_base[R_GS]); - tcg_gen_ld_tl(cpu_seg_base[R_GS], tcg_env, - offsetof(CPUX86State, kernelgsbase)); - tcg_gen_st_tl(s->T0, tcg_env, - offsetof(CPUX86State, kernelgsbase)); + tcg_gen_ld_env_tl(cpu_seg_base[R_GS], kernelgsbase); + tcg_gen_st_env_tl(s->T0, kernelgsbase);}
---triggers:
../target/i386/tcg/translate.c:2449:13: error: controlling expression type 'uint64_t' (aka 'unsigned long long') not compatible with any generic association type
2449 |tcg_gen_st_env_tl(last_addr, fpdp);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:65:29: note: expanded from macro 'tcg_gen_st_env_tl'
65 | # define tcg_gen_st_env_tltcg_gen_st_env_i32
| ^
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:47:14: note: expanded from macro 'tcg_gen_st_env_i32'
47 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:16:29: note: expanded from macro 'TCG_ENV_FIELD_'
16 | #define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~../target/i386/tcg/translate.c:2757:9: error: controlling expression type 'uint64_t' (aka 'unsigned long long') not compatible with any generic association type
2757 |tcg_gen_st_env_tl(eip_cur_tl(s), fpip);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:65:29: note: expanded from macro 'tcg_gen_st_env_tl'
65 | # define tcg_gen_st_env_tltcg_gen_st_env_i32
| ^
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:47:14: note: expanded from macro 'tcg_gen_st_env_i32'
47 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:16:29: note: expanded from macro 'TCG_ENV_FIELD_'
16 | #define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.Both CPUX86State::fpip/fpdp fields are
uint64_t, so the same class of bug on big-endian hosts.FAILED: [code=1]
libqemu-i386-softmmu.a.p/target_i386_tcg_translate.c.o
Quoted 100 lines · Philippe Mathieu-Daudé
Quoted 91 lines · Philippe Mathieu-Daudé
Quoted 90 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Anton Johansson via qemu development
mstatus and
mstatus_hsareuint64_tbut accessed throughtcg_gen_[ld|st]_tl(..., offsetof(mstatus))
which on a big-endian host running riscv32 incorrectly accesses the top 32 bits rather than the lower bits.
mstatus and
mstatus_hsareuint64_t, butmark_fs_dirty()andmark_vs_dirty()update them with tcg_gen_[ld|st]_tl(). On big-endian hosts with 32-bittarget_ulong, this modifies the upper half instead of the lower one, so the FS/VS dirty bits are never set. Use 64-bit accesses instead.Fixes 284d697c74e ("target/riscv: Merge m/vsstatus and m/vsstatush into one uint64_t unit")Fixes 8e1ee1fb571 ("target/riscv: rvv-1.0: add translation-time vector context status")Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 43 lines
Widen the operations to 64 bit so the lower bits are modified.
Signed-off-by Anton Johansson <[email protected]>---
target/riscv/tcg/translate.c| 32 ++++++++++++++++----------------1 file changed, 16 insertions(+), 16 deletions(-)target/riscv/tcg/translate.c +8
-8@@ -693,7 +693,7 @@ static TCGv get_address_indexed(DisasContext*ctx, int rs1, TCGv offs) */ static void mark_fs_dirty(DisasContext *ctx) { - TCGv tmp; + TCGv_i64 tmp; if (!has_ext(ctx, RVF)) { return;@@ -703,15 +703,15 @@ static void mark_fs_dirty(DisasContext *ctx) /* Remember the state change for the rest of the TB. */ ctx->mstatus_fs = EXT_STATUS_DIRTY; - tmp = tcg_temp_new(); - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); + tmp = tcg_temp_new_i64(); + tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); + tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS); + tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); if (ctx->virt_enabled) { - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); + tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); + tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS); + tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState,Quoted 4 lines
mstatus_hs));
}
}
}Wondering about being safe (or catching other bad uses)
#define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)#define TCG_ENV_SIZE_(f)sizeof_field(CPUArchState, f)#define
tcg_gen_ld_env_tl(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != \
sizeof(target_ulong)); \
tcg_gen_ld_tl(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)#define
tcg_gen_ld_env_i64(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != 8); \
tcg_gen_ld_i64(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)or even using
_Generic:#define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f) #define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)Quoted 7 lines
Quoted 7 lines
#define
tcg_gen_st_env_i32(arg, f)\
_Generic(TCG_ENV_FIELD_(f), \
bool: tcg_gen_st8_i32, \
uint8_t: tcg_gen_st8_i32, \
int8_t: tcg_gen_st8_i32, \
uint16_t: tcg_gen_st16_i32, \
int16_t: tcg_gen_st16_i32, \
Quoted 8 lines
Hmm tricky (dangerous?), this also change our current uses of target-long accesses to smaller fields. This is how I'd expect the uses but not always what we have now. For example, commit
ebc9401a406("target/hppa: Split PSW X and B into their own field") reducedCPUHPPAState::pswfromuint64_ttouint32_t, but both accessors --trans_rsm()andtrans_ssm()-- keep accessing withtcg_gen_ld_i64()instead oftcg_gen_ld32u_i64(). Likely buggy wrt endianness.
Noticed because the coccinelle conversion now triggers:
../target/hppa/translate.c:2367:5: error: controlling expression type 'uint32_t' (aka 'unsigned int') not compatible with any generic association type
2367 | tcg_gen_ld_env_i64(tmp, psw);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:30:14: note: expanded from macro 'tcg_gen_ld_env_i64'
30 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:15:29: note: expanded from macro 'TCG_ENV_FIELD_'
15 | #define TCG_ENV_FIELD_(f) (((struct CPUArchState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../target/hppa/translate.c:2387:5: error: controlling expression type 'uint32_t' (aka 'unsigned int') not compatible with any generic association type
2387 | tcg_gen_ld_env_i64(tmp, psw);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:30:14: note: expanded from macro 'tcg_gen_ld_env_i64'
30 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:15:29: note: expanded from macro 'TCG_ENV_FIELD_'
15 | #define TCG_ENV_FIELD_(f) (((struct CPUArchState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2 errors generated.
Quoted 125 lines · Philippe Mathieu-Daudé
Quoted 102 lines · Philippe Mathieu-Daudé
Quoted 93 lines · Philippe Mathieu-Daudé
Quoted 92 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Anton Johansson via qemu development
mstatus and
mstatus_hsareuint64_tbut accessed throughtcg_gen_[ld|st]_tl(..., offsetof(mstatus))
which on a big-endian host running riscv32 incorrectly accesses the top 32 bits rather than the lower bits.
mstatus and
mstatus_hsareuint64_t, butmark_fs_dirty()andmark_vs_dirty()update them with tcg_gen_[ld|st]_tl(). On big-endian hosts with 32-bittarget_ulong, this modifies the upper half instead of the lower one, so the FS/VS dirty bits are never set. Use 64-bit accesses instead.Fixes 284d697c74e ("target/riscv: Merge m/vsstatus and m/vsstatush into one uint64_t unit")Fixes 8e1ee1fb571 ("target/riscv: rvv-1.0: add translation-time vector context status")Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 41 lines
Widen the operations to 64 bit so the lower bits are modified.
Signed-off-by Anton Johansson <[email protected]>---
target/riscv/tcg/translate.c| 32 ++++++++++++++++----------------1 file changed, 16 insertions(+), 16 deletions(-)target/riscv/tcg/translate.c +5
-8@@ -693,7 +693,7 @@ static TCGv get_address_indexed(DisasContext*ctx, int rs1, TCGv offs) */ static void mark_fs_dirty(DisasContext *ctx) { - TCGv tmp; + TCGv_i64 tmp; if (!has_ext(ctx, RVF)) { return;@@ -703,15 +703,15 @@ static void mark_fs_dirty(DisasContext *ctx) /* Remember the state change for the rest of the TB. */ ctx->mstatus_fs = EXT_STATUS_DIRTY; - tmp = tcg_temp_new(); - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); + tmp = tcg_temp_new_i64(); + tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); + tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS); + tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); if (ctx->virt_enabled) { - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState,
mstatus_hs));Quoted 8 lines
+
tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
+tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS);
+tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
}
}
}Wondering about being safe (or catching other bad uses)
#define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)#define TCG_ENV_SIZE_(f)sizeof_field(CPUArchState, f)#define
tcg_gen_ld_env_tl(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != \
sizeof(target_ulong)); \
tcg_gen_ld_tl(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)#define
tcg_gen_ld_env_i64(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != 8); \
tcg_gen_ld_i64(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)or even using
_Generic:#define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f) #define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)Quoted 7 lines
Quoted 7 lines
#define
tcg_gen_st_env_i32(arg, f)\
_Generic(TCG_ENV_FIELD_(f), \
bool: tcg_gen_st8_i32, \
uint8_t: tcg_gen_st8_i32, \
int8_t: tcg_gen_st8_i32, \
uint16_t: tcg_gen_st16_i32, \
int16_t: tcg_gen_st16_i32, \Quoted 8 lines
Hmm tricky (dangerous?), this also change our current uses of target-long accesses to smaller fields. This is how I'd expect the uses but not always what we have now. For example, commit
ebc9401a406("target/hppa: Split PSW X and B into their own field") reducedCPUHPPAState::pswfromuint64_ttouint32_t, but both accessors --trans_rsm()andtrans_ssm()-- keep accessing withtcg_gen_ld_i64()instead oftcg_gen_ld32u_i64(). Likely buggy wrt endianness.Noticed because the coccinelle conversion now triggers:
../target/hppa/translate.c:2367:5: error: controlling expression type 'uint32_t' (aka 'unsigned int') not compatible with any generic association type
2367 |tcg_gen_ld_env_i64(tmp, psw);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:30:14: note: expanded from macro 'tcg_gen_ld_env_i64'
30 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:15:29: note: expanded from macro 'TCG_ENV_FIELD_'
15 | #define TCG_ENV_FIELD_(f) (((struct CPUArchState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~../target/hppa/translate.c:2387:5: error: controlling expression type 'uint32_t' (aka 'unsigned int') not compatible with any generic association type
2387 |tcg_gen_ld_env_i64(tmp, psw);
Quoted 10 lines
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:30:14: note: expanded from macro 'tcg_gen_ld_env_i64'
30 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:15:29: note: expanded from macro 'TCG_ENV_FIELD_'
15 | #define TCG_ENV_FIELD_(f) (((struct CPUArchState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.
And using these macros on tcg_global_mem_*, another issue on Hexagon (slot_cancelled is u8 but registered to be accessed as u32):
FAILED: [code=1] libqemu-hexagon-softmmu.a.p/target_hexagon_translate.c.o../target/hexagon/translate.c:1472:26: error: controlling expression type 'uint8_t' (aka 'unsigned char') not compatible with any generic association type
1472 | hex_slot_cancelled = tcg_global_mem_new_env_tl(slot_cancelled,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1473 |
"slot_cancelled");
|
~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:160:37: note: expanded from macro 'tcg_global_mem_new_env_tl'
160 | # define tcg_global_mem_new_env_tl tcg_global_mem_new_env_i32
| ^
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:52:14: note: expanded from macro 'tcg_global_mem_new_env_i32'
52 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:15:29: note: expanded from macro 'TCG_ENV_FIELD_'
15 | #define TCG_ENV_FIELD_(f) (((struct CPUArchState *)0)->f)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1 error generated.
Quoted 172 lines · Philippe Mathieu-Daudé
Quoted 137 lines · Philippe Mathieu-Daudé
Quoted 106 lines · Philippe Mathieu-Daudé
Quoted 97 lines · Philippe Mathieu-Daudé
Quoted 96 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Anton Johansson via qemu development
mstatus and
mstatus_hsareuint64_tbut accessed throughtcg_gen_[ld|st]_tl(..., offsetof(mstatus))
which on a big-endian host running riscv32 incorrectly accesses the top 32 bits rather than the lower bits.
mstatus and
mstatus_hsareuint64_t, butmark_fs_dirty()andmark_vs_dirty()update them with tcg_gen_[ld|st]_tl(). On big-endian hosts with 32-bittarget_ulong, this modifies the upper half instead of the lower one, so the FS/VS dirty bits are never set. Use 64-bit accesses instead.Fixes 284d697c74e ("target/riscv: Merge m/vsstatus andm/vsstatush into one
uint64_tunit")Fixes 8e1ee1fb571 ("target/riscv: rvv-1.0: addtranslation-time vector context status")
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 43 lines
Widen the operations to 64 bit so the lower bits are modified.
Signed-off-by Anton Johansson <[email protected]>---
target/riscv/tcg/translate.c| 32 ++++++++++++++++----------------1 file changed, 16 insertions(+), 16 deletions(-)(quoted diff) +0
-0b/target/riscv/tcg/
translate.c
indexede9c2563f..79b0f444ef100644target/riscv/tcg/translate.c +0
-0@@ -693,7 +693,7 @@ static TCGv
get_address_indexed(DisasContext *ctx, int rs1, TCGv offs)
*/
static voidmark_fs_dirty(DisasContext *ctx){
- TCGv tmp;
+TCGv_i64tmp;
if (!has_ext(ctx, RVF)) {
return;target/riscv/tcg/translate.c +4
-7@@ -703,15 +703,15 @@ static void mark_fs_dirty(DisasContext *ctx) /* Remember the state change for the rest of the TB. */ ctx->mstatus_fs = EXT_STATUS_DIRTY; - tmp = tcg_temp_new(); - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus)); + tmp = tcg_temp_new_i64(); + tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState , mstatus)); + tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS); + tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState , mstatus)); if (ctx->virt_enabled) { - tcg_gen_ld_tl(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs)); - tcg_gen_ori_tl(tmp, tmp, MSTATUS_FS); - tcg_gen_st_tl(tmp, tcg_env,
offsetof(CPURISCVState, mstatus_hs));Quoted 8 lines
+
tcg_gen_ld_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
+tcg_gen_ori_i64(tmp, tmp, MSTATUS_FS); +tcg_gen_st_i64(tmp, tcg_env, offsetof(CPURISCVState, mstatus_hs));
}
}
}Wondering about being safe (or catching other bad uses)
#define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)#define TCG_ENV_SIZE_(f)sizeof_field(CPUArchState, f)#define
tcg_gen_ld_env_tl(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != \
sizeof(target_ulong)); \
tcg_gen_ld_tl(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)#define
tcg_gen_ld_env_i64(ret, f)\
do { \
QEMU_BUILD_BUG_ON(TCG_ENV_SIZE_(f) != 8); \
tcg_gen_ld_i64(ret, tcg_env, TCG_ENV_OFS_(f)); \
} while (0)or even using
_Generic:#define TCG_ENV_FIELD_(f) (((CPUArchState *)0)->f) #define TCG_ENV_OFS_(f)
offsetof(CPUArchState, f)Quoted 7 lines
Quoted 7 lines
#define
tcg_gen_st_env_i32(arg, f)\
_Generic(TCG_ENV_FIELD_(f), \
bool: tcg_gen_st8_i32, \
uint8_t: tcg_gen_st8_i32, \
int8_t: tcg_gen_st8_i32, \
uint16_t: tcg_gen_st16_i32, \
int16_t: tcg_gen_st16_i32, \Quoted 8 lines
Hmm tricky (dangerous?), this also change our current uses of target-long accesses to smaller fields. This is how I'd expect the uses but not always what we have now. For example, commit
ebc9401a406("target/hppa: Split PSW X and B into their own field") reducedCPUHPPAState::pswfromuint64_ttouint32_t, but both accessors --trans_rsm()andtrans_ssm()-- keep accessing withtcg_gen_ld_i64()instead oftcg_gen_ld32u_i64(). Likely buggy wrt endianness.Noticed because the coccinelle conversion now triggers:
../target/hppa/translate.c:2367:5: error: controlling expression type 'uint32_t' (aka 'unsigned int') not compatible with any generic association type
2367 |tcg_gen_ld_env_i64(tmp, psw);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:30:14: note:
expanded from macro 'tcg_gen_ld_env_i64'
30 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:15:29: note:
expanded from macro 'TCG_ENV_FIELD_'
15 | #define TCG_ENV_FIELD_(f) (((struct CPUArchState *)0)->f) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~../target/hppa/translate.c:2387:5: error: controlling expression type 'uint32_t' (aka 'unsigned int') not compatible with any generic association type
2387 |tcg_gen_ld_env_i64(tmp, psw);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:30:14: note:Quoted 10 lines
expanded from macro '
tcg_gen_ld_env_i64'
30 | _Generic(TCG_ENV_FIELD_(f), \
| ^~~~~~~~~~~~~~~~~
/Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:15:29: note:
expanded from macro 'TCG_ENV_FIELD_'
15 | #define TCG_ENV_FIELD_(f) (((struct CPUArchState *)0)->f) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.And using these macros on tcg_global_mem_*, another issue on Hexagon (
slot_cancelledis u8 but registered to be accessed as u32):FAILED: [code=1]
libqemu-hexagon-softmmu.a.p/target_hexagon_translate.c.o../target/hexagon/translate.c:1472:26: error: controlling expression type 'uint8_t' (aka 'unsigned char') not compatible with any generic association type1472 | hex_slot_cancelled = tcg_global_mem_new_env_tl(slot_cancelled, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1473 | "slot_cancelled"); | ~~~~~~~~~~~~~~~~~ /Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:160:37: note: expanded from macro 'tcg_global_mem_new_env_tl' 160 | # define tcg_global_mem_new_env_tl tcg_global_mem_new_env_i32 | ^ /Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:52:14: note: expanded from macro 'tcg_global_mem_new_env_i32' 52 | _Generic(TCG_ENV_FIELD_(f), \ | ^~~~~~~~~~~~~~~~~ /Users/philmd/worktree/qemu/excp/include/tcg/tcg-op-env.h:15:29: note: expanded from macro 'TCG_ENV_FIELD_' 15 | #define TCG_ENV_FIELD_(f) (((struct CPUArchState *)0)->f) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 3 lines
1 error generated.
Ah this is nice, let's drop my patch and go for this instead IMO! :)