diff mbox series

[8/9] target/arm: Support some Thumb insns being always unconditional

Message ID 1507556919-24992-9-git-send-email-peter.maydell@linaro.org
State Superseded
Headers show
Series v8M: BLXNS, SG, secure function return | expand

Commit Message

Peter Maydell Oct. 9, 2017, 1:48 p.m. UTC
A few Thumb instructions are always unconditional even inside an
IT block (as opposed to being UNPREDICTABLE if used inside an
IT block): BKPT, the v8M SG instruction, and the A profile
HLT (debug halt) instruction.

This means we need to suppress the jump-over-instruction-on-condfail
code generation (though the IT state still advances as usual and
subsequent insns in the IT block may be conditional).

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

---
 target/arm/translate.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

-- 
2.7.4

Comments

Richard Henderson Oct. 11, 2017, 2:52 a.m. UTC | #1
On 10/09/2017 06:48 AM, Peter Maydell wrote:
> -    if (dc->condexec_mask) {

> +    if (dc->condexec_mask && !thumb_insn_is_unconditional(dc, insn)) {

>          uint32_t cond = dc->condexec_cond;

>  

>          if (cond != 0x0e) {     /* Skip conditional when condition is AL. */


Don't you still need to advance the condexec_mask?

For HLT it doesn't matter, clearly.
I'm not sure what happens to an IT block for debug breakpoints.

But SG behaves as NOP when in non-secure memory.
That would seem to require that

	itft	eq
	add	r1, r0, r0
	sg
	add	r2, r0, r0
	add	r3, r0, r0

should modify both r1 and r2 for T and r3 should be unconditional.  If you
don't advance condexec_mask, it would seem that r2 gets F and r3 gets T.


r~
Peter Maydell Oct. 11, 2017, 9:57 a.m. UTC | #2
On 11 October 2017 at 03:52, Richard Henderson
<richard.henderson@linaro.org> wrote:
> On 10/09/2017 06:48 AM, Peter Maydell wrote:

>> -    if (dc->condexec_mask) {

>> +    if (dc->condexec_mask && !thumb_insn_is_unconditional(dc, insn)) {

>>          uint32_t cond = dc->condexec_cond;

>>

>>          if (cond != 0x0e) {     /* Skip conditional when condition is AL. */

>

> Don't you still need to advance the condexec_mask?


Yes -- that happens after we've called disas_thumb{,2}_insn()
in thumb_tr_translate_insn().

This patch just makes always-unconditional insns behave
as if the condition is AL.

thanks
-- PMM
Richard Henderson Oct. 11, 2017, 2:14 p.m. UTC | #3
On 10/11/2017 02:57 AM, Peter Maydell wrote:
> On 11 October 2017 at 03:52, Richard Henderson

> <richard.henderson@linaro.org> wrote:

>> On 10/09/2017 06:48 AM, Peter Maydell wrote:

>>> -    if (dc->condexec_mask) {

>>> +    if (dc->condexec_mask && !thumb_insn_is_unconditional(dc, insn)) {

>>>          uint32_t cond = dc->condexec_cond;

>>>

>>>          if (cond != 0x0e) {     /* Skip conditional when condition is AL. */

>>

>> Don't you still need to advance the condexec_mask?

> 

> Yes -- that happens after we've called disas_thumb{,2}_insn()

> in thumb_tr_translate_insn().


Ah, mis-read the context of this hunk.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>



r~
diff mbox series

Patch

diff --git a/target/arm/translate.c b/target/arm/translate.c
index 5838e67..9d16760 100644
--- a/target/arm/translate.c
+++ b/target/arm/translate.c
@@ -12115,6 +12115,52 @@  static void arm_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
        in init_disas_context by adjusting max_insns.  */
 }
 
+static bool thumb_insn_is_unconditional(DisasContext *s, uint32_t insn)
+{
+    /* Return true if this Thumb insn is always unconditional,
+     * even inside an IT block. This is true of only a very few
+     * instructions: BKPT, HLT, and SG.
+     *
+     * A larger class of instructions are UNPREDICTABLE if used
+     * inside an IT block; we do not need to detect those here, because
+     * what we do by default (perform the cc check and update the IT
+     * bits state machine) is a permitted CONSTRAINED UNPREDICTABLE
+     * choice for those situations.
+     *
+     * insn is either a 16-bit or a 32-bit instruction; the two are
+     * distinguishable because for the 16-bit case the top 16 bits
+     * are zeroes, and that isn't a valid 32-bit encoding.
+     */
+    if ((insn & 0xffffff00) == 0xbe00) {
+        /* BKPT */
+        return true;
+    }
+
+    if ((insn & 0xffffffc0) == 0xba80 && arm_dc_feature(s, ARM_FEATURE_V8) &&
+        !arm_dc_feature(s, ARM_FEATURE_M)) {
+        /* HLT: v8A only. This is unconditional even when it is going to
+         * UNDEF; see the v8A ARM ARM DDI0487B.a H3.3.
+         * For v7 cores this was a plain old undefined encoding and so
+         * honours its cc check. (We might be using the encoding as
+         * a semihosting trap, but we don't change the cc check behaviour
+         * on that account, because a debugger connected to a real v7A
+         * core and emulating semihosting traps by catching the UNDEF
+         * exception would also only see cases where the cc check passed.
+         * No guest code should be trying to do a HLT semihosting trap
+         * in an IT block anyway.
+         */
+        return true;
+    }
+
+    if (insn == 0xe97fe97f && arm_dc_feature(s, ARM_FEATURE_V8) &&
+        arm_dc_feature(s, ARM_FEATURE_M)) {
+        /* SG: v8M only */
+        return true;
+    }
+
+    return false;
+}
+
 static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
 {
     DisasContext *dc = container_of(dcbase, DisasContext, base);
@@ -12136,7 +12182,7 @@  static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
         dc->pc += 2;
     }
 
-    if (dc->condexec_mask) {
+    if (dc->condexec_mask && !thumb_insn_is_unconditional(dc, insn)) {
         uint32_t cond = dc->condexec_cond;
 
         if (cond != 0x0e) {     /* Skip conditional when condition is AL. */