Skip to contentSkip to conversation

Handle some invalid encodings

PATCH Superseded Brian Cain 2 participants 6 messages Feb 10

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v3 cover Handle some invalid encodings

Fix the issues reported by Alex Rønne Petersen causing zig test suite failures. The 0x0 case is a conflicting "multiple register write" - the handler here will cover duplexes but not the more general case of multiple register writes. I'll include that in a subsequent patchset.

v2: use a signal handler for SIGILL, combine the tests. S-b Pierrick.

v3:
Dropped "Reject duplex encodings" and added "Detect register write conflicts..." -> more general algo S-b Taylor. This patch moved over the test_zero() case covering the same 0x00000000 inst encoding R-b Alex.

Brian Cain (4):
target/hexagon: Fix invalid duplex decoding target/hexagon: Return decode failure for invalid non-duplex encodings tests/tcg/hexagon: Handle SIGILL internally in invalid-slots test target/hexagon: Detect register write conflicts with bitmap algorithm

 target/hexagon/cpu_bits.h            |   1 +
 target/hexagon/insn.h                |   6 +
 linux-user/hexagon/cpu_loop.c        |   5 +
 target/hexagon/decode.c              |  59 +++++++++-
 target/hexagon/translate.c           |  39 ++++++-
 tests/tcg/hexagon/invalid-encoding.c | 106 +++++++++++++++++
 tests/tcg/hexagon/invalid-slots.c    |  76 +++++++++---
 tests/tcg/hexagon/multiple-writes.c  | 169 +++++++++++++++++++++++++++
 target/hexagon/gen_trans_funcs.py    |  10 ++
 tests/tcg/hexagon/Makefile.target    |   8 +-
 10 files changed, 449 insertions(+), 30 deletions(-)
 create mode 100644 tests/tcg/hexagon/invalid-encoding.c
 create mode 100644 tests/tcg/hexagon/multiple-writes.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/4 target/hexagon: Fix invalid duplex decoding

When decoding a duplex instruction, if the slot0 sub-instruction fails to decode after slot1 succeeds, QEMU was leaving the packet in a partially-decoded state. This allowed invalid duplex encodings (where one sub-instruction doesn't match any valid pattern) to be executed incorrectly.

Fix by resetting the decoder state when slot0 fails, returning an empty instruction that triggers an exception.

Add gen_exception_decode_fail() for raising exceptions when decode fails before ctx->next_PC is initialized. This keeps gen_exception_end_tb() semantics unchanged (it continues to use ctx->next_PC for the exception PC after successful decode).

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3291

---
 linux-user/hexagon/cpu_loop.c        |  4 ++
 target/hexagon/decode.c              | 13 ++++-
 target/hexagon/translate.c           | 18 ++++++-
 tests/tcg/hexagon/invalid-encoding.c | 81 ++++++++++++++++++++++++++++
 tests/tcg/hexagon/Makefile.target    |  1 +
 5 files changed, 113 insertions(+), 4 deletions(-)
 create mode 100644 tests/tcg/hexagon/invalid-encoding.c
linux-user/hexagon/cpu_loop.c +4 -0
index 1941f4c9c1..c0e1098e3f 100644
@@ -64,6 +64,10 @@ void cpu_loop(CPUHexagonState *env)
             force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN,
                             env->gpr[HEX_REG_R31]);
             break;
+        case HEX_CAUSE_INVALID_PACKET:
+            force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC,
+                            env->gpr[HEX_REG_PC]);
+            break;
         case EXCP_ATOMIC:
             cpu_exec_step_atomic(cs);
             break;
target/hexagon/decode.c +11 -2
index b5ece60450..69ba1ec96c 100644
@@ -509,8 +509,14 @@ decode_insns(DisasContext *ctx, Insn *insn, uint32_t encoding)
                 insn->iclass = iclass_bits(encoding);
                 return 2;
             }
+            /*
+             * Slot0 decode failed after slot1 succeeded. This is an invalid
+             * duplex encoding (both sub-instructions must be valid).
+             */
+            ctx->insn = --insn;
         }
-        g_assert_not_reached();
+        /* Invalid duplex encoding - return 0 to signal failure */
+        return 0;
     }
 }
 
@@ -674,7 +680,10 @@ int decode_packet(DisasContext *ctx, int max_words, const uint32_t *words,
         encoding32 = words[words_read];
         end_of_packet = is_packet_end(encoding32);
         new_insns = decode_insns(ctx, insn, encoding32);
-        g_assert(new_insns > 0);
+        if (new_insns == 0) {
+            /* Invalid instruction encoding */
+            return 0;
+        }
         /*
          * If we saw an extender, mark next word extended so immediate
          * decode works
target/hexagon/translate.c +16 -2
index e88e19cc1a..7fe8b35351 100644
@@ -195,7 +195,21 @@ static void gen_exception_end_tb(DisasContext *ctx, int excp)
     tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->next_PC);
     gen_exception_raw(excp);
     ctx->base.is_jmp = DISAS_NORETURN;
+}
 
+/*
+ * Generate exception for decode failures. Unlike gen_exception_end_tb,
+ * this is used when decode fails before ctx->next_PC is initialized.
+ */
+static void gen_exception_decode_fail(DisasContext *ctx, int nwords, int excp)
+{
+    target_ulong fail_pc = ctx->base.pc_next + nwords * sizeof(uint32_t);
+
+    gen_exec_counters(ctx);
+    tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], fail_pc);
+    gen_exception_raw(excp);
+    ctx->base.is_jmp = DISAS_NORETURN;
+    ctx->base.pc_next = fail_pc;
 }
 
 static int read_packet_words(CPUHexagonState *env, DisasContext *ctx,
@@ -935,7 +949,7 @@ static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
 
     nwords = read_packet_words(env, ctx, words);
     if (!nwords) {
-        gen_exception_end_tb(ctx, HEX_CAUSE_INVALID_PACKET);
+        gen_exception_decode_fail(ctx, 0, HEX_CAUSE_INVALID_PACKET);
         return;
     }
 
@@ -950,7 +964,7 @@ static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
         gen_commit_packet(ctx);
         ctx->base.pc_next += pkt.encod_pkt_size_in_bytes;
     } else {
-        gen_exception_end_tb(ctx, HEX_CAUSE_INVALID_PACKET);
+        gen_exception_decode_fail(ctx, nwords, HEX_CAUSE_INVALID_PACKET);
     }
 }
 
tests/tcg/hexagon/invalid-encoding.c new file +81 -0

81 lines not on this page · raw message

tests/tcg/hexagon/Makefile.target +1 -0
index e5182c01d8..16669e04a8 100644
@@ -51,6 +51,7 @@ HEX_TESTS += scatter_gather
 HEX_TESTS += hvx_misc
 HEX_TESTS += hvx_histogram
 HEX_TESTS += invalid-slots
+HEX_TESTS += invalid-encoding
 HEX_TESTS += unaligned_pc
 
 run-and-check-exception = $(call run-test,$2,$3 2>$2.stderr; \
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v3 2/4 target/hexagon: Return decode failure for invalid non-duplex encodings

In reply to Brian Cain

When a non-duplex encoding (parse_bits != 0) fails both decode_normal() and decode_hvx(), the decoder hit an unreachable. Instead, handle the decode failure and raise an exception.

---
 target/hexagon/decode.c              |  3 ++-
 tests/tcg/hexagon/invalid-encoding.c | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
target/hexagon/decode.c +2 -1
index 69ba1ec96c..33ad60c5b4 100644
@@ -489,7 +489,8 @@ decode_insns(DisasContext *ctx, Insn *insn, uint32_t encoding)
             insn->iclass = iclass_bits(encoding);
             return 1;
         }
-        g_assert_not_reached();
+        /* Invalid non-duplex encoding */
+        return 0;
     } else {
         uint32_t iclass = get_duplex_iclass(encoding);
         unsigned int slot0_subinsn = get_slot0_subinsn(encoding);
tests/tcg/hexagon/invalid-encoding.c +25 -0
index 010a5eb741..639d7f2495 100644
@@ -65,6 +65,30 @@ static int test_invalid_duplex(void)
     return sig;
 }
 
+/*
+ * Invalid non-duplex encoding:
+ * The encoding 0xffffc000 has parse bits [15:14] = 0b11, making it a
+ * non-duplex instruction and packet end.  The remaining bits do not match
+ * any valid normal or HVX instruction encoding, so this should raise SIGILL.
+ */
+static int test_invalid_nonduplex(void)
+{
+    int sig;
+
+    asm volatile(
+        "r0 = #0\n"
+        "r1 = ##1f\n"
+        "memw(%1) = r1\n"
+        ".word 0xffffc000\n"
+        "1:\n"
+        "%0 = r0\n"
+        : "=r"(sig)
+        : "r"(&resume_pc)
+        : "r0", "r1", "memory");
+
+    return sig;
+}
+
 int main()
 {
     struct sigaction act;
@@ -75,6 +99,7 @@ int main()
     assert(sigaction(SIGILL, &act, NULL) == 0);
 
     assert(test_invalid_duplex() == SIGILL);
+    assert(test_invalid_nonduplex() == SIGILL);
 
     puts("PASS");
     return EXIT_SUCCESS;
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v3 3/4 tests/tcg/hexagon: Handle SIGILL internally in invalid-slots test

In reply to Brian Cain

Rewrite invalid-slots.c to catch and verify SIGILL using a sigaction handler that modifies the ucontext, matching the pattern used by invalid-encoding.c.

---
 tests/tcg/hexagon/invalid-slots.c | 76 ++++++++++++++++++++++++-------
 tests/tcg/hexagon/Makefile.target |  6 ---
 2 files changed, 59 insertions(+), 23 deletions(-)
tests/tcg/hexagon/invalid-slots.c +59 -17
index 366ce4f42f..607027f314 100644
@@ -1,29 +1,71 @@
 /*
- *  Copyright(c) 2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
+ * Test that invalid slot assignments are properly rejected.
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
+#include <assert.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static void *resume_pc;
+
+static void handle_sigill(int sig, siginfo_t *info, void *puc)
+{
+    ucontext_t *uc = (ucontext_t *)puc;
+
+    if (sig != SIGILL) {
+        _exit(EXIT_FAILURE);
+    }
+
+    uc->uc_mcontext.r0 = SIGILL;
+    uc->uc_mcontext.pc = (unsigned long)resume_pc;
+}
+
 char mem[8] __attribute__((aligned(8)));
 
-int main()
+/*
+ * Invalid packet with 2 instructions at slot 0:
+ * - Word 0: 0xa1804100 = memw(r0) = r1
+ * - Word 1: 0x28032804 = { r3 = #0; r4 = #0 }
+ *
+ * This should raise SIGILL due to the invalid slot assignment.
+ */
+static int test_invalid_slots(void)
 {
+    int sig;
+
     asm volatile(
+        "r0 = #0\n"
+        "r1 = ##1f\n"
+        "memw(%1) = r1\n"
         "r0 = #mem\n"
-        /* Invalid packet (2 instructions at slot 0): */
         ".word 0xa1804100\n" /* { memw(r0) = r1;      */
         ".word 0x28032804\n" /*   r3 = #0; r4 = #0 }  */
-        : : : "r0", "r3", "r4", "memory");
-    return 0;
+        "1:\n"
+        "%0 = r0\n"
+        : "=r"(sig)
+        : "r"(&resume_pc)
+        : "r0", "r1", "r3", "r4", "memory");
+
+    return sig;
+}
+
+int main()
+{
+    struct sigaction act;
+
+    memset(&act, 0, sizeof(act));
+    act.sa_sigaction = handle_sigill;
+    act.sa_flags = SA_SIGINFO;
+    assert(sigaction(SIGILL, &act, NULL) == 0);
+
+    assert(test_invalid_slots() == SIGILL);
+
+    puts("PASS");
+    return EXIT_SUCCESS;
 }
tests/tcg/hexagon/Makefile.target +0 -6
index 16669e04a8..d64aeba090 100644
@@ -54,12 +54,6 @@ HEX_TESTS += invalid-slots
 HEX_TESTS += invalid-encoding
 HEX_TESTS += unaligned_pc
 
-run-and-check-exception = $(call run-test,$2,$3 2>$2.stderr; \
-	test $$? -eq 1 && grep -q "exception $(strip $1)" $2.stderr)
-
-run-invalid-slots: invalid-slots
-	$(call run-and-check-exception, 0x15, $@, $(QEMU) $(QEMU_OPTS) $<)
-
 HEX_TESTS += test_abs
 HEX_TESTS += test_bitcnt
 HEX_TESTS += test_bitsplit
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v3 4/4 target/hexagon: Detect register write conflicts with bitmap algorithm

In reply to Brian Cain

A conflict exists when any GPR is written by multiple instructions and at least one write is unconditional. This catches (1) two unconditional writes to the same GPR and (2) an unconditional write combined with a predicated write.

Add HEX_CAUSE_REG_WRITE_CONFLICT and map it to SIGILL.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2696

---
 target/hexagon/cpu_bits.h           |   1 +
 target/hexagon/insn.h               |   6 +
 linux-user/hexagon/cpu_loop.c       |   1 +
 target/hexagon/decode.c             |  43 +++++++
 target/hexagon/translate.c          |  21 +++-
 tests/tcg/hexagon/multiple-writes.c | 169 ++++++++++++++++++++++++++++
 target/hexagon/gen_trans_funcs.py   |  10 ++
 tests/tcg/hexagon/Makefile.target   |   1 +
 8 files changed, 250 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/hexagon/multiple-writes.c
target/hexagon/cpu_bits.h +1 -0
index ff596e2a94..19beca81c0 100644
@@ -34,6 +34,7 @@ enum hex_cause {
     HEX_CAUSE_FETCH_NO_UPAGE =  0x012,
     HEX_CAUSE_INVALID_PACKET =  0x015,
     HEX_CAUSE_INVALID_OPCODE =  0x015,
+    HEX_CAUSE_REG_WRITE_CONFLICT = 0x01d,
     HEX_CAUSE_PC_NOT_ALIGNED =  0x01e,
     HEX_CAUSE_PRIV_NO_UREAD  =  0x024,
     HEX_CAUSE_PRIV_NO_UWRITE =  0x025,
target/hexagon/insn.h +6 -0
index 5d59430da9..db4dbb728a 100644
@@ -41,6 +41,8 @@ struct Instruction {
     uint32_t new_value_producer_slot:4;
     int32_t new_read_idx;
     int32_t dest_idx;
+    bool dest_is_pair;
+    bool dest_is_gpr;
     bool has_pred_dest;
 
     bool part1;              /*
@@ -72,6 +74,10 @@ struct Packet {
     bool pkt_has_hvx;
     Insn *vhist_insn;
 
+    /* Bitmaps for detecting duplicate GPR destination writes */
+    DECLARE_BITMAP(wreg_mult_gprs, 32);   /* GPRs written by >1 insn */
+    DECLARE_BITMAP(uncond_wreg_gprs, 32); /* GPRs written unconditionally */
+
     Insn insn[INSTRUCTIONS_MAX];
 };
 
linux-user/hexagon/cpu_loop.c +1 -0
index c0e1098e3f..5711055aff 100644
@@ -65,6 +65,7 @@ void cpu_loop(CPUHexagonState *env)
                             env->gpr[HEX_REG_R31]);
             break;
         case HEX_CAUSE_INVALID_PACKET:
+        case HEX_CAUSE_REG_WRITE_CONFLICT:
             force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC,
                             env->gpr[HEX_REG_PC]);
             break;
target/hexagon/decode.c +43 -0

61 lines not on this page · raw message

target/hexagon/translate.c +19 -2
index 7fe8b35351..6e399bc2f2 100644
@@ -940,10 +940,21 @@ static void gen_commit_packet(DisasContext *ctx)
     }
 }
 
+/*
+ * Check for register write conflicts in the packet.
+ */
+static bool pkt_has_write_conflict(Packet *pkt)
+{
+    DECLARE_BITMAP(conflict, 32);
+
+    bitmap_and(conflict, pkt->wreg_mult_gprs, pkt->uncond_wreg_gprs, 32);
+    return !bitmap_empty(conflict, 32);
+}
+
 static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
 {
     uint32_t words[PACKET_WORDS_MAX];
-    int nwords;
+    int nwords, words_read;
     Packet pkt;
     int i;
 
@@ -954,8 +965,14 @@ static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
     }
 
     ctx->pkt = &pkt;
-    if (decode_packet(ctx, nwords, words, &pkt, false) > 0) {
+    words_read = decode_packet(ctx, nwords, words, &pkt, false);
+    if (words_read > 0) {
         pkt.pc = ctx->base.pc_next;
+        if (pkt_has_write_conflict(&pkt)) {
+            gen_exception_decode_fail(ctx, words_read,
+                                      HEX_CAUSE_REG_WRITE_CONFLICT);
+            return;
+        }
         gen_start_packet(ctx);
         for (i = 0; i < pkt.num_insns; i++) {
             ctx->insn = &pkt.insn[i];
tests/tcg/hexagon/multiple-writes.c new file +169 -0

169 lines not on this page · raw message

target/hexagon/gen_trans_funcs.py +10 -0
index 45da1b7b5d..19c1f9fdea 100755
@@ -91,6 +91,8 @@ def gen_trans_funcs(f):
         new_read_idx = -1
         dest_idx = -1
         dest_idx_reg_id = None
+        dest_is_pair = "false"
+        dest_is_gpr = "false"
         has_pred_dest = "false"
         for regno, (reg_type, reg_id, *_) in enumerate(regs):
             reg = hex_common.get_register(tag, reg_type, reg_id)
@@ -104,6 +106,12 @@ def gen_trans_funcs(f):
                 if dest_idx_reg_id is None or reg_id < dest_idx_reg_id:
                     dest_idx = regno
                     dest_idx_reg_id = reg_id
+                    dest_is_pair = ("true"
+                                    if isinstance(reg, hex_common.Pair)
+                                    else "false")
+                    dest_is_gpr = ("true"
+                                   if reg_type == "R"
+                                   else "false")
             if reg_type == "P" and reg.is_written() and not reg.is_read():
                 has_pred_dest = "true"
 
@@ -129,6 +137,8 @@ def gen_trans_funcs(f):
         f.write(code_fmt(f"""\
             insn->new_read_idx = {new_read_idx};
             insn->dest_idx = {dest_idx};
+            insn->dest_is_pair = {dest_is_pair};
+            insn->dest_is_gpr = {dest_is_gpr};
             insn->has_pred_dest = {has_pred_dest};
         """))
         f.write(textwrap.dedent(f"""\
tests/tcg/hexagon/Makefile.target +1 -0
index d64aeba090..f86f02bb31 100644
@@ -52,6 +52,7 @@ HEX_TESTS += hvx_misc
 HEX_TESTS += hvx_histogram
 HEX_TESTS += invalid-slots
 HEX_TESTS += invalid-encoding
+HEX_TESTS += multiple-writes
 HEX_TESTS += unaligned_pc
 
 HEX_TESTS += test_abs
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v3 4/4] target/hexagon: Detect register write conflicts with bitmap algorithm

Quoted 27 lines · Brian Cain

A conflict exists when any GPR is written by multiple instructions and at least one write is unconditional. This catches (1) two unconditional writes to the same GPR and (2) an unconditional write combined with a predicated write.

Add HEX_CAUSE_REG_WRITE_CONFLICT and map it to SIGILL.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2696

---
 target/hexagon/cpu_bits.h           |   1 +
 target/hexagon/insn.h               |   6 +
 linux-user/hexagon/cpu_loop.c       |   1 +
 target/hexagon/decode.c             |  43 +++++++
 target/hexagon/translate.c          |  21 +++-
 tests/tcg/hexagon/multiple-writes.c | 169 ++++++++++++++++++++++++++++
 target/hexagon/gen_trans_funcs.py   |  10 ++
 tests/tcg/hexagon/Makefile.target   |   1 +
 8 files changed, 250 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/hexagon/multiple-writes.c
target/hexagon/insn.h +0 -0
index 5d59430da9..db4dbb728a 100644

target/hexagon/insn.h struct Packet { · line 82

Quoted 9 lines
target/hexagon/insn.h +4 -0
@@ -72,6 +74,10 @@ struct Packet {
     bool pkt_has_hvx;
     Insn *vhist_insn;
 
+    /* Bitmaps for detecting duplicate GPR destination writes */
+    DECLARE_BITMAP(wreg_mult_gprs, 32);   /* GPRs written by >1 insn */
+    DECLARE_BITMAP(uncond_wreg_gprs, 32); /* GPRs written unconditionally
 */
+
 

Consider making these bitmaps local to decode_mark_dest_regs. Instead, only add a bool here that indicates a pkt_has_write_conflict.. Then move the pkt_has_write_conflict function from translate.c to decode.c.

Quoted 50 lines
target/hexagon/insn.h +0 -0
     Insn insn[INSTRUCTIONS_MAX];

};

linux-user/hexagon/cpu_loop.c +1 -0
index c0e1098e3f..5711055aff 100644
@@ -65,6 +65,7 @@ void cpu_loop(CPUHexagonState *env)
                             env->gpr[HEX_REG_R31]);
             break;
         case HEX_CAUSE_INVALID_PACKET:
+        case HEX_CAUSE_REG_WRITE_CONFLICT:
             force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC,
                             env->gpr[HEX_REG_PC]);
             break;
target/hexagon/decode.c +27 -0
index 33ad60c5b4..08b0fa2c8d 100644
@@ -655,6 +655,44 @@ decode_set_slot_number(Packet *pkt)
     return has_valid_slot_assignment(pkt);
 }
 
+/*
+ * Build bitmaps of destination GPR writes across the packet.
+ */
+static void decode_mark_dest_regs(Packet *pkt)
+{
+    DECLARE_BITMAP(all_dest_gprs, 32) = { 0 };
+
+    for (int i = 0; i < pkt->num_insns; i++) {
+        Insn *insn = &pkt->insn[i];
+        int dest = insn->dest_idx;
+
+        if (dest < 0 || !insn->dest_is_gpr) {
+            continue;
+        }
+
+        int rnum = insn->regno[dest];
+        bool is_uncond = !GET_ATTRIB(insn->opcode, A_CONDEXEC);
+
+        if (test_bit(rnum, all_dest_gprs)) {
+            set_bit(rnum, pkt->wreg_mult_gprs);
+        }
+        set_bit(rnum, all_dest_gprs);
+        if (is_uncond) {
+            set_bit(rnum, pkt->uncond_wreg_gprs);
+        }
+
+        if (insn->dest_is_pair) {

target/hexagon/decode.c decode_set_slot_number(Packet *pkt) · line 694

Quoted 10 lines
target/hexagon/decode.c +9 -0
+            if (test_bit(rnum + 1, all_dest_gprs)) {
+                set_bit(rnum + 1, pkt->wreg_mult_gprs);
+            }
+            set_bit(rnum + 1, all_dest_gprs);
+            if (is_uncond) {
+                set_bit(rnum + 1, pkt->uncond_wreg_gprs);
+            }
+        }
+    }
 

Move the body of pkt_has_write_conflict here and set a bool in the Packet struct.
That way, all the logic will be in a single place.

Quoted 35 lines
target/hexagon/decode.c +2 -0
+}
+
 /*
  * decode_packet

* Decodes packet with given words

target/hexagon/decode.c +4 -0
@@ -674,6 +712,10 @@ int decode_packet(DisasContext *ctx, int max_words,
 const uint32_t *words,
 
     /* Initialize */
     memset(pkt, 0, sizeof(*pkt));
+    for (i = 0; i < INSTRUCTIONS_MAX; i++) {
+        pkt->insn[i].dest_idx = -1;
+        pkt->insn[i].new_read_idx = -1;
+    }
     /* Try to build packet */
     while (!end_of_packet && (words_read < max_words)) {
         Insn *insn = &pkt->insn[num_insns];
target/hexagon/decode.c +1 -0
@@ -737,6 +779,7 @@ int decode_packet(DisasContext *ctx, int max_words,
 const uint32_t *words,
             /* Invalid packet */
             return 0;
         }
+        decode_mark_dest_regs(pkt);
     }
     decode_fill_newvalue_regno(pkt);
target/hexagon/translate.c +1 -0
index 7fe8b35351..6e399bc2f2 100644
@@ -940,10 +940,21 @@ static void gen_commit_packet(DisasContext *ctx)
     }
 }
 
+/*

target/hexagon/translate.c static void gen_commit_packet(DisasContext *ctx) · line 953

Quoted 10 lines
target/hexagon/translate.c +10 -0
+ * Check for register write conflicts in the packet.
+ */
+static bool pkt_has_write_conflict(Packet *pkt)
+{
+    DECLARE_BITMAP(conflict, 32);
+
+    bitmap_and(conflict, pkt->wreg_mult_gprs, pkt->uncond_wreg_gprs, 32);
+    return !bitmap_empty(conflict, 32);
+}
+

This doesn't address instructions that write more than one register. Try
this test case

    .word 0x9b004021    /* {    r1 = memb(r0++#1)      */
    .word 0x9b00c022    /*      r2 = memb(r0++#1) }    */

Also, this doesn't check for multiple writes to other types of registers (predicate, control, HVX).

Thanks,
Taylor