target/arm: Various minor SME bugfixes
cover target/arm: Various minor SME bugfixes
This patchset fixes a handful of minor bugs in our SME implementation:
one spotted by Coverity, one raised as a Gitlab issue, and a couple that I just noticed in passing while I was working with the code.
thanks
-- PMM
Peter Maydell (4):
target/arm: Don't assert for 128-bit tile accesses when SVL is 128 target/arm: Fix UMOPA/UMOPS of 16-bit values
target/arm: Avoid shifts by -1 in tszimm_shr() and tszimm_shl() target/arm: Ignore SMCR_EL2.LEN and SVCR_EL2.LEN if EL2 is not enabled
target/arm/helper.c | 2 +- target/arm/tcg/sme_helper.c | 8 ++++---- target/arm/tcg/translate-sme.c | 10 +++++++++- target/arm/tcg/translate-sve.c | 18 ++++++++++++++++-- 4 files changed, 30 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/4 target/arm: Don't assert for 128-bit tile accesses when SVL is 128
For an instruction which accesses a 128-bit element tile when the SVL is also 128 (for example MOV z0.Q, p0/M, ZA0H.Q[w0,0]), we will assert in get_tile_rowcol():
qemu-system-aarch64: ../../tcg/tcg-op.c:926: tcg_gen_deposit_z_i32: Assertion `len > 0' failed.
This happens because we calculate
len = ctz32(streaming_vec_reg_size(s)) - esz;$
but if the SVL and the element size are the same len is 0, and the deposit operation asserts.
In this case the ZA storage contains exactly one 128 bit element ZA tile, and the horizontal or vertical slice is just that tile. This means that regardless of the index value in the Ws register, we always access that tile. (In pseudocode terms, we calculate (index + offset) MOD 1, which is 0.)
Special case the len == 0 case to avoid hitting the assertion in tcg_gen_deposit_z_i32().
--- target/arm/tcg/translate-sme.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
target/arm/tcg/translate-sme.c +9 -1
@@ -49,7 +49,15 @@ static TCGv_ptr get_tile_rowcol(DisasContext *s, int esz, int rs,/* Prepare a power-of-two modulo via extraction of @len bits. */ len =ctz32 (streaming_vec_reg_size (s)) - esz; -if (vertical) { +if (!len) { +/* +* SVL is 128 and the element size is 128. There is exactly +* one 128x128 tile in the ZA storage, and so we calculate +* (Rs + imm) MOD 1, which is always 0. We need to special case +* this because TCG doesn't allow deposit ops with len 0. +*/ +tcg_gen_movi_i32 (tmp,0 ); + }else if (vertical) {/* * Compute the byte offset of the index within the tile: * (index % (svl / size)) * size
Signature · 1 line
2.34.1
PATCH 2/4 target/arm: Fix UMOPA/UMOPS of 16-bit values
In reply to Peter MaydellThe UMOPA/UMOPS instructions are supposed to multiply unsigned 8 or 16 bit elements and accumulate the products into a 64-bit element. In the Arm ARM pseudocode, this is done with the usual infinite-precision signed arithmetic. However our implementation doesn't quite get it right, because in the DEF_IMOP_64() macro we do:
sum += (NTYPE)(n >> 0) * (MTYPE)(m >> 0);
where NTYPE and MTYPE are uint16_t or int16_t. In the uint16_t case, the C usual arithmetic conversions mean the values are converted to "int" type and the multiply is done as a 32-bit multiply. This means that if the inputs are, for example, 0xffff and 0xffff then the result is 0xFFFE0001 as an int, which is then promoted to uint64_t for the accumulation into sum; this promotion incorrectly sign extends the multiply.
Avoid the incorrect sign extension by casting to int64_t before the multiply, so we do the multiply as 64-bit signed arithmetic, which is a type large enough that the multiply can never overflow into the sign bit.
(The equivalent 8-bit operations in DEF_IMOP_32() are fine, because the 8-bit multiplies can never overflow into the sign bit of a 32-bit integer.)
Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2372
--- target/arm/tcg/sme_helper.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
target/arm/tcg/sme_helper.c +4 -4
@@ -1146,10 +1146,10 @@ static uint64_t NAME(uint64_t n, uint64_t m, uint64_t a, uint8_t p, bool neg) \uint64_t sum =0 ; \/* Apply P to N as a mask, making the inactive elements 0. */ \ n &=expand_pred_h (p); \ - sum += (NTYPE )(n >>0 ) * (MTYPE )(m >>0 ); \ - sum += (NTYPE )(n >>16 ) * (MTYPE )(m >>16 ); \ - sum += (NTYPE )(n >>32 ) * (MTYPE )(m >>32 ); \ - sum += (NTYPE )(n >>48 ) * (MTYPE )(m >>48 ); \ + sum += (int64_t )(NTYPE )(n >>0 ) * (MTYPE )(m >>0 ); \ + sum += (int64_t )(NTYPE )(n >>16 ) * (MTYPE )(m >>16 ); \ + sum += (int64_t )(NTYPE )(n >>32 ) * (MTYPE )(m >>32 ); \ + sum += (int64_t )(NTYPE )(n >>48 ) * (MTYPE )(m >>48 ); \return neg ? a - sum : a + sum; \ }
Signature · 1 line
2.34.1
PATCH 3/4 target/arm: Avoid shifts by -1 in tszimm_shr() and tszimm_shl()
In reply to Peter MaydellThe function tszimm_esz() returns a shift amount, or possibly -1 in certain cases that correspond to unallocated encodings in the instruction set. We catch these later in the trans_ functions (generally with an "a-esz < 0" check), but before we do the decodetree-generated code will also call tszimm_shr() or tszimm_sl(), which will use the tszimm_esz() return value as a shift count without checking that it is not negative, which is undefined behaviour.
Avoid the UB by checking the return value in tszimm_shr() and tszimm_shl().
Cc: [email protected]
Resolves: Coverity CID 1547617, 1547694
--- target/arm/tcg/translate-sve.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
target/arm/tcg/translate-sve.c +16 -2
@@ -50,13 +50,27 @@ static int tszimm_esz(DisasContext *s, int x)static int tszimm_shr (DisasContext *s,int x) { -return (16 <<tszimm_esz (s, x)) - x; +/* +* We won't use the tszimm_shr() value if tszimm_esz() returns -1 (the +* trans function will check for esz < 0), so we can return any +* value we like from here in that case as long as we avoid UB. +*/ +int esz =tszimm_esz (s, x); +if (esz <0 ) { +return esz; + } +return (16 << esz) - x; }/* See e.g. LSL (immediate, predicated). */ static int tszimm_shl (DisasContext *s,int x) { -return x - (8 <<tszimm_esz (s, x)); +/* As with tszimm_shr(), value will be unused if esz < 0 */ +int esz =tszimm_esz (s, x); +if (esz <0 ) { +return esz; + } +return x - (8 << esz); }/* The SH bit is in bit 8. Extract the low 8 and shift. */
Signature · 1 line
2.34.1
PATCH 4/4 target/arm: Ignore SMCR_EL2.LEN and SVCR_EL2.LEN if EL2 is not enabled
In reply to Peter MaydellWhen determining the current vector length, the SMCR_EL2.LEN and SVCR_EL2.LEN settings should only be considered if EL2 is enabled (compare the pseudocode CurrentSVL and CurrentNSVL which call EL2Enabled()).
We were checking against ARM_FEATURE_EL2 rather than calling arm_is_el2_enabled(), which meant that we would look at SMCR_EL2/SVCR_EL2 when in Secure EL1 or Secure EL0 even if Secure EL2 was not enabled.
Use the correct check in sve_vqm1_for_el_sm().
--- target/arm/helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/arm/helper.c +1 -1
@@ -7232,7 +7232,7 @@ uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)if (el <=1 && !el_is_in_host (env, el)) { len =MIN (len,0xf & (uint32_t )cr[1 ]); } -if (el <=2 &&arm_feature (env,ARM_FEATURE_EL2 )) { +if (el <=2 &&arm_is_el2_enabled (env)) { len =MIN (len,0xf & (uint32_t )cr[2 ]); }if (arm_feature (env,ARM_FEATURE_EL3 )) {
Signature · 1 line
2.34.1
Re: [PATCH 4/4] target/arm: Ignore SMCR_EL2.LEN and SVCR_EL2.LEN if EL2 is not enabled
Quoted 9 lines · Peter Maydell
When determining the current vector length, the SMCR_EL2.LEN and SVCR_EL2.LEN settings should only be considered if EL2 is enabled (compare the pseudocode CurrentSVL and CurrentNSVL which call
EL2Enabled()).We were checking against
ARM_FEATURE_EL2rather than callingarm_is_el2_enabled(), which meant that we would look at SMCR_EL2/SVCR_EL2 when in Secure EL1 or Secure EL0 even if Secure EL2 was not enabled.
Just curious, how did you notice?
Quoted 21 lines
Use the correct check in
sve_vqm1_for_el_sm().Signed-off-by Peter Maydell <[email protected]>--- target/arm/helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/arm/helper.c +1
-1@@ -7232,7 +7232,7 @@ uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm)if (el <=1 && !el_is_in_host (env, el)) { len =MIN (len,0xf & (uint32_t )cr[1 ]); } -if (el <=2 &&arm_feature (env,ARM_FEATURE_EL2 )) { +if (el <=2 &&arm_is_el2_enabled (env)) { len =MIN (len,0xf & (uint32_t )cr[2 ]); }if (arm_feature (env,ARM_FEATURE_EL3 )) {
Quoted 6 lines · Peter Maydell
Peter Maydell (4):
target/arm: Don't assert for 128-bit tile accesses when SVL is 128 target/arm: Fix UMOPA/UMOPS of 16-bit values
target/arm: Avoid shifts by -1 intszimm_shr()andtszimm_shl()target/arm: Ignore SMCR_EL2.LEN and SVCR_EL2.LEN if EL2 is not enabled
r~