[PATCH v2] MIPS: fix optimised memset returning garbage

Denis Orlov denorl2009 at gmail.com
Thu Mar 10 12:42:06 PST 2022


There is a subtle bug in arch/mips/lib/memset.S, used if option
MIPS_OPTIMIZED_STRING_FUNCTIONS is enabled, which results in not writing
the correct return value into v0 register if the value to fill memory
with is 0.

The bug stems from a missing '.set noreorder' directive before the first
beqz instruction. This results in an assembler stuffing a 'nop'
instruction in the beqz delay slot instead of an instruction writing the
return value into v0 register that was supposed to go there. That leads
to an error that appears when reading memory from SPI Flash and that was
mentioned here:
http://lists.infradead.org/pipermail/barebox/2021-June/036557.html

There are a few ways of fixing this bug. The directive '.set noreorder'
that shows up a bit later in code could be moved up and placed right
before the memset label (as it was originally in Linux codebase - see
a583158c9ce822c96a718fbf877cec1e5f9ad75d). Or, rather, the move and the
branch instruction could be swapped, thus allowing the assembler to
properly place move in delay slot (as it is done in Linux now - see
68dec269ee29c3abfd09596fbee7e40d875a6ab3) - that is what is done in this
patch.

Signed-off-by: Denis Orlov <denorl2009 at gmail.com>
---
 arch/mips/lib/memset.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/mips/lib/memset.S b/arch/mips/lib/memset.S
index 0b81bd7d8d..cd4e4ccfe8 100644
--- a/arch/mips/lib/memset.S
+++ b/arch/mips/lib/memset.S
@@ -34,8 +34,8 @@
  */
 
 LEAF(memset)
-	beqz		a1, 1f
 	move		v0, a0			/* result */
+	beqz		a1, 1f
 
 	andi		a1, 0xff		/* spread fillword */
 	LONG_SLL		t1, a1, 8
-- 
2.20.1




More information about the barebox mailing list