Skip to contentSkip to conversation

arm: Ensure LSB of BLX is set

PATCH In review [email protected] 2 participants 4 messages Jul 6, 2015 – Sep 1, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm: Ensure LSB of BLX is set

From: Meador Inge <[email protected]>

This small patch adds a sanity check when disassembling the BLX instruction. The use case came to light when doing toolchain development and a similar check was upstreamed for Binutils:

* https://sourceware.org/ml/binutils/2011-01/msg00077.html

Patch by Nathan Sidwell.

---
 target-arm/translate.c | 6 ++++++
 1 file changed, 6 insertions(+)
target-arm/translate.c +6 -0
index 69ac18c..fedc8f3 100644
@@ -9912,6 +9912,12 @@ static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw
                     gen_jmp(s, offset);
                 } else {
                     /* blx */
+                    /* The instruction must have bit zero unset, even
+                       though it is part of the offset.  Real hardware
+                       will abort, so we do too.  */
+                    if (insn & 1) {
+                        goto illegal_op;
+                    }
                     offset &= ~(uint32_t)2;
                     /* thumb2 bx, no need to check */
                     gen_bx_im(s, offset);
Signature · 1 line
1.8.1.1
#1 lore raw
Quoted 21 lines · On 6 July 2015 at 19:09, <[email protected]> wrote:

From: Meador Inge <[email protected]>

This small patch adds a sanity check when disassembling the BLX instruction. The use case came to light when doing toolchain development and a similar check was upstreamed for Binutils:

* https://sourceware.org/ml/binutils/2011-01/msg00077.html

Patch by Nathan Sidwell.

---
 target-arm/translate.c | 6 ++++++
 1 file changed, 6 insertions(+)
target-arm/translate.c +0 -0
index 69ac18c..fedc8f3 100644

target-arm/translate.c static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw · line 9917

Quoted 6 lines
target-arm/translate.c +3 -0
@@ -9912,6 +9912,12 @@ static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw
                     gen_jmp(s, offset);
                 } else {
                     /* blx */
+                    /* The instruction must have bit zero unset, even
+                       though it is part of the offset.  Real hardware
+                       will abort, so we do too.  */

This comment isn't really correct -- bit zero in this encoding (BLX T2) isn't part of the immediate, it's described as a one bit field H which causes UNDEFINED if it's 1. This is architecturally mandated, not just somewhere we're following h/w on an IMPDEF or UNPREDICTABLE case.

target-arm/translate.c static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw · line 9920

Quoted 3 lines
target-arm/translate.c +3 -0
+                    if (insn & 1) {
+                        goto illegal_op;
+                    }

This check is happening too late -- we've already done the write to R14. We need to UNDEF before that.

target-arm/translate.c static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw · line 9921

Quoted 1 line
target-arm/translate.c +0 -0
                     offset &= ~(uint32_t)2;

The new check makes this masking unnecessary now, right?

target-arm/translate.c static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw

Quoted 3 lines
target-arm/translate.c +0 -1
                     /* thumb2 bx, no need to check */
                     gen_bx_im(s, offset);
--

thanks
-- PMM

#2 lore raw
In reply to [email protected]
Quoted 7 lines · On 6 July 2015 at 19:09, <[email protected]> wrote:

From: Meador Inge <[email protected]>

This small patch adds a sanity check when disassembling the BLX instruction. The use case came to light when doing toolchain development and a similar check was upstreamed for Binutils:

Quoted 5 lines

* https://sourceware.org/ml/binutils/2011-01/msg00077.html

Patch by Nathan Sidwell.

Hi; are you planning to send a v2 of this patch?

thanks
-- PMM

#3 lore raw
Quoted 8 lines · Peter Maydell
Quoted 7 lines · On 6 July 2015 at 19:09, <[email protected]> wrote:

From: Meador Inge <[email protected]>

This small patch adds a sanity check when disassembling the BLX instruction. The use case came to light when doing toolchain development and a similar check was upstreamed for Binutils:

Quoted 7 lines
Quoted 5 lines

* https://sourceware.org/ml/binutils/2011-01/msg00077.html

Patch by Nathan Sidwell.

Hi; are you planning to send a v2 of this patch?

I am. Thank you for the reminder. I will send a v2 some time tomorrow.

Thanks,

-- Meador