Skip to contentSkip to conversation

Hexagon (target/hexagon) Check opcodes versions

PATCH Superseded Taylor Simpson 2 participants 11 messages Feb 12 – Feb 13

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Hexagon (target/hexagon) Check opcodes versions

Currently, all versions of Hexagon CPU are treated the same, and all opcodes are able to run on any version. This series changes the behavior such that only the opcodes available on the currently executing Hexagon CPU version can execute.

Changes include:
Mark which Hexagon CPU version is currently executing Check that each opcode is supported in the Hexagon CPU version If not, raise SIGILL
Properly handle disassembly (-d in_asm)
Test case added

Note that this series borrows elements from Brian Cain's "Handle some invalid encodings" series. Therefore, it will need slight rework after those patches are merged.

Brian Cain (2):
Hexagon (linux-user/hexagon) Convert INVALID_PACKET into SIGILL tests/tcg/hexagon: Add test for revision-gated instruction decoding

Matheus Tavares Bernardino (1):
Hexagon (target/hexagon) Introduce tag_rev_info.c.inc

Taylor Simpson (5):
Hexagon (target/hexagon) Properly handle Hexagon CPU version Hexagon (linux-user/hexagon) Identify Hexagon version in ELF file Hexagon (target/hexagon) Add Hexagon version field to DisasContext Hexagon (target/hexagon) Check each opcode against current CPU version Hexagon (target/hexagon) Disassembly of invalid packets

 target/hexagon/cpu-qom.h             |  21 ++++
 target/hexagon/cpu.h                 |   2 +
 target/hexagon/cpu_bits.h            |   4 +-
 target/hexagon/translate.h           |   1 +
 disas/hexagon.c                      |   3 +-
 linux-user/hexagon/cpu_loop.c        |   4 +
 linux-user/hexagon/elfload.c         |  43 ++++----
 target/hexagon/cpu.c                 |  31 ++++--
 target/hexagon/decode.c              |  43 +++++++-
 target/hexagon/translate.c           |  20 +++-
 tests/tcg/hexagon/check_rev_gating.c | 141 +++++++++++++++++++++++++++
 tests/tcg/hexagon/invalid-slots.c    |  76 +++++++++++----
 target/hexagon/tag_rev_info.c.inc    |  61 ++++++++++++
 tests/tcg/hexagon/Makefile.target    |  12 +--
 14 files changed, 409 insertions(+), 53 deletions(-)
 create mode 100644 tests/tcg/hexagon/check_rev_gating.c
 create mode 100644 target/hexagon/tag_rev_info.c.inc
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/8 Hexagon (target/hexagon) Properly handle Hexagon CPU version

Add the following CPU versions that were previously missing

    v5
    v55
    v60
    v61
    v62
    v65

Create an enum with the known Hexagon CPU versions Add a field to HexagonCPUClass to note the Hexagon CPU version

---
 target/hexagon/cpu-qom.h | 21 +++++++++++++++++++++
 target/hexagon/cpu.h     |  2 ++
 target/hexagon/cpu.c     | 28 ++++++++++++++++++++++------
 3 files changed, 45 insertions(+), 6 deletions(-)
target/hexagon/cpu-qom.h +21 -0
index 0b149bd5fe..830a2f8cf8 100644
@@ -11,11 +11,32 @@
 
 #include "hw/core/cpu.h"
 
+typedef enum {
+    HEX_VER_V5 = 0x04,
+    HEX_VER_V55 = 0x05,
+    HEX_VER_V60 = 0x60,
+    HEX_VER_V61 = 0x61,
+    HEX_VER_V62 = 0x62,
+    HEX_VER_V65 = 0x65,
+    HEX_VER_V66 = 0x66,
+    HEX_VER_V67 = 0x67,
+    HEX_VER_V68 = 0x68,
+    HEX_VER_V69 = 0x69,
+    HEX_VER_V71 = 0x71,
+    HEX_VER_V73 = 0x73,
+} HexagonVersion;
+
 #define TYPE_HEXAGON_CPU "hexagon-cpu"
 
 #define HEXAGON_CPU_TYPE_SUFFIX "-" TYPE_HEXAGON_CPU
 #define HEXAGON_CPU_TYPE_NAME(name) (name HEXAGON_CPU_TYPE_SUFFIX)
 
+#define TYPE_HEXAGON_CPU_V5 HEXAGON_CPU_TYPE_NAME("v5")
+#define TYPE_HEXAGON_CPU_V55 HEXAGON_CPU_TYPE_NAME("v55")
+#define TYPE_HEXAGON_CPU_V60 HEXAGON_CPU_TYPE_NAME("v60")
+#define TYPE_HEXAGON_CPU_V61 HEXAGON_CPU_TYPE_NAME("v61")
+#define TYPE_HEXAGON_CPU_V62 HEXAGON_CPU_TYPE_NAME("v62")
+#define TYPE_HEXAGON_CPU_V65 HEXAGON_CPU_TYPE_NAME("v65")
 #define TYPE_HEXAGON_CPU_V66 HEXAGON_CPU_TYPE_NAME("v66")
 #define TYPE_HEXAGON_CPU_V67 HEXAGON_CPU_TYPE_NAME("v67")
 #define TYPE_HEXAGON_CPU_V68 HEXAGON_CPU_TYPE_NAME("v68")
target/hexagon/cpu.h +2 -0
index 656b7dc044..071fc2b6b7 100644
@@ -117,6 +117,8 @@ typedef struct HexagonCPUClass {
 
     DeviceRealize parent_realize;
     ResettablePhases parent_phases;
+
+    HexagonVersion hex_version;
 } HexagonCPUClass;
 
 struct ArchCPU {
target/hexagon/cpu.c +22 -6
index 58a22ee41f..09a0de3c2f 100644
@@ -27,12 +27,22 @@
 #include "exec/gdbstub.h"
 #include "accel/tcg/cpu-ops.h"
 
-static void hexagon_v66_cpu_init(Object *obj) { }
-static void hexagon_v67_cpu_init(Object *obj) { }
-static void hexagon_v68_cpu_init(Object *obj) { }
-static void hexagon_v69_cpu_init(Object *obj) { }
-static void hexagon_v71_cpu_init(Object *obj) { }
-static void hexagon_v73_cpu_init(Object *obj) { }
+#define HEX_CPU_INIT(NAME, VER) \
+static void hexagon_##NAME##_cpu_init(Object *obj) \
+{ HEXAGON_CPU_GET_CLASS(obj)->hex_version = VER; }
+
+HEX_CPU_INIT(v5, HEX_VER_V5)
+HEX_CPU_INIT(v55, HEX_VER_V55)
+HEX_CPU_INIT(v60, HEX_VER_V60)
+HEX_CPU_INIT(v61, HEX_VER_V61)
+HEX_CPU_INIT(v62, HEX_VER_V62)
+HEX_CPU_INIT(v65, HEX_VER_V65)
+HEX_CPU_INIT(v66, HEX_VER_V66)
+HEX_CPU_INIT(v67, HEX_VER_V67)
+HEX_CPU_INIT(v68, HEX_VER_V68)
+HEX_CPU_INIT(v69, HEX_VER_V69)
+HEX_CPU_INIT(v71, HEX_VER_V71)
+HEX_CPU_INIT(v73, HEX_VER_V73)
 
 static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
 {
@@ -395,6 +405,12 @@ static const TypeInfo hexagon_cpu_type_infos[] = {
         .class_size = sizeof(HexagonCPUClass),
         .class_init = hexagon_cpu_class_init,
     },
+    DEFINE_CPU(TYPE_HEXAGON_CPU_V5,               hexagon_v5_cpu_init),
+    DEFINE_CPU(TYPE_HEXAGON_CPU_V55,              hexagon_v55_cpu_init),
+    DEFINE_CPU(TYPE_HEXAGON_CPU_V60,              hexagon_v60_cpu_init),
+    DEFINE_CPU(TYPE_HEXAGON_CPU_V61,              hexagon_v61_cpu_init),
+    DEFINE_CPU(TYPE_HEXAGON_CPU_V62,              hexagon_v62_cpu_init),
+    DEFINE_CPU(TYPE_HEXAGON_CPU_V65,              hexagon_v65_cpu_init),
     DEFINE_CPU(TYPE_HEXAGON_CPU_V66,              hexagon_v66_cpu_init),
     DEFINE_CPU(TYPE_HEXAGON_CPU_V67,              hexagon_v67_cpu_init),
     DEFINE_CPU(TYPE_HEXAGON_CPU_V68,              hexagon_v68_cpu_init),
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/8 Hexagon (linux-user/hexagon) Identify Hexagon version in ELF file

In reply to Taylor Simpson

Return proper Hexagon CPU version from get_elf_cpu_model

---
 linux-user/hexagon/elfload.c | 43 ++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 17 deletions(-)
linux-user/hexagon/elfload.c +26 -17
index d8b545032a..39b0201814 100644
@@ -10,23 +10,32 @@ const char *get_elf_cpu_model(uint32_t eflags)
     static char buf[32];
     int err;
 
-    /* For now, treat anything newer than v5 as a v73 */
-    /* FIXME - Disable instructions that are newer than the specified arch */
-    if (eflags == 0x04 ||    /* v5  */
-        eflags == 0x05 ||    /* v55 */
-        eflags == 0x60 ||    /* v60 */
-        eflags == 0x61 ||    /* v61 */
-        eflags == 0x62 ||    /* v62 */
-        eflags == 0x65 ||    /* v65 */
-        eflags == 0x66 ||    /* v66 */
-        eflags == 0x67 ||    /* v67 */
-        eflags == 0x8067 ||  /* v67t */
-        eflags == 0x68 ||    /* v68 */
-        eflags == 0x69 ||    /* v69 */
-        eflags == 0x71 ||    /* v71 */
-        eflags == 0x8071 ||  /* v71t */
-        eflags == 0x73       /* v73 */
-       ) {
+    switch (eflags) {
+    case 0x04:
+        return "v5";
+    case 0x05:
+        return "v55";
+    case 0x60:
+        return "v60";
+    case 0x61:
+        return "v61";
+    case 0x62:
+        return "v62";
+    case 0x65:
+        return "v65";
+    case 0x66:
+        return "v66";
+    case 0x67:
+    case 0x8067:        /* v67t */
+        return "v67";
+    case 0x68:
+        return "v68";
+    case 0x69:
+        return "v69";
+    case 0x71:
+    case 0x8071:        /* v71t */
+        return "v71";
+    case 0x73:
         return "v73";
     }
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/8 Hexagon (target/hexagon) Add Hexagon version field to DisasContext

In reply to Taylor Simpson

Initialize the field in hexagon_tr_init_disas_context

---
 target/hexagon/translate.h | 1 +
 target/hexagon/translate.c | 1 +
 2 files changed, 2 insertions(+)
target/hexagon/translate.h +1 -0
index a0102b6cbd..7d4236b8b7 100644
@@ -30,6 +30,7 @@ typedef struct DisasContext {
     DisasContextBase base;
     Packet *pkt;
     Insn *insn;
+    HexagonVersion hex_version;
     uint32_t next_PC;
     uint32_t mem_idx;
     uint32_t num_packets;
target/hexagon/translate.c +1 -0
index e88e19cc1a..9498cd4502 100644
@@ -968,6 +968,7 @@ static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
     ctx->branch_cond = TCG_COND_NEVER;
     ctx->is_tight_loop = FIELD_EX32(hex_flags, TB_FLAGS, IS_TIGHT_LOOP);
     ctx->short_circuit = hex_cpu->short_circuit;
+    ctx->hex_version = HEXAGON_CPU_GET_CLASS(hex_cpu)->hex_version;
 }
 
 static void hexagon_tr_tb_start(DisasContextBase *db, CPUState *cpu)
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/8 Hexagon (target/hexagon) Introduce tag_rev_info.c.inc

In reply to Taylor Simpson

From: Matheus Tavares Bernardino <[email protected]>

Table that records which CPU revision introduced or removed each opcode

---
 target/hexagon/tag_rev_info.c.inc | 61 +++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 target/hexagon/tag_rev_info.c.inc
target/hexagon/tag_rev_info.c.inc new file +61 -0
new file mode 100644
index 0000000000..ed7fb68aaf
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HEXAGON_TAG_ARCH_TABLE_H
+#define HEXAGON_TAG_ARCH_TABLE_H
+
+struct tag_rev_info { int introduced, removed; };
+
+static const struct tag_rev_info tag_rev_info[XX_LAST_OPCODE] = {
+    [A7_clip] = { .introduced = 0x67, .removed = 0x0 },
+    [A7_croundd_ri] = { .introduced = 0x67, .removed = 0x0 },
+    [A7_croundd_rr] = { .introduced = 0x67, .removed = 0x0 },
+    [A7_vclip] = { .introduced = 0x67, .removed = 0x0 },
+    [F2_dfmax] = { .introduced = 0x67, .removed = 0x0 },
+    [F2_dfmin] = { .introduced = 0x67, .removed = 0x0 },
+    [F2_dfmpyfix] = { .introduced = 0x67, .removed = 0x0 },
+    [F2_dfmpyhh] = { .introduced = 0x67, .removed = 0x0 },
+    [F2_dfmpylh] = { .introduced = 0x67, .removed = 0x0 },
+    [F2_dfmpyll] = { .introduced = 0x67, .removed = 0x0 },
+    [J2_callrh] = { .introduced = 0x73, .removed = 0x0 },
+    [J2_jumprh] = { .introduced = 0x73, .removed = 0x0 },
+    [L2_loadw_aq] = { .introduced = 0x68, .removed = 0x0 },
+    [L4_loadd_aq] = { .introduced = 0x68, .removed = 0x0 },
+    [M7_dcmpyiw] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_dcmpyiw_acc] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_dcmpyiwc] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_dcmpyiwc_acc] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_dcmpyrw] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_dcmpyrw_acc] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_dcmpyrwc] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_dcmpyrwc_acc] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_wcmpyiw] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_wcmpyiw_rnd] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_wcmpyiwc] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_wcmpyiwc_rnd] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_wcmpyrw] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_wcmpyrw_rnd] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_wcmpyrwc] = { .introduced = 0x67, .removed = 0x0 },
+    [M7_wcmpyrwc_rnd] = { .introduced = 0x67, .removed = 0x0 },
+    [R6_release_at_vi] = { .introduced = 0x68, .removed = 0x0 },
+    [R6_release_st_vi] = { .introduced = 0x68, .removed = 0x0 },
+    [S2_storew_rl_at_vi] = { .introduced = 0x68, .removed = 0x0 },
+    [S2_storew_rl_st_vi] = { .introduced = 0x68, .removed = 0x0 },
+    [S4_stored_rl_at_vi] = { .introduced = 0x68, .removed = 0x0 },
+    [S4_stored_rl_st_vi] = { .introduced = 0x68, .removed = 0x0 },
+    [V6_v6mpyhubs10] = { .introduced = 0x68, .removed = 0x0 },
+    [V6_v6mpyhubs10_vxx] = { .introduced = 0x68, .removed = 0x0 },
+    [V6_v6mpyvubs10] = { .introduced = 0x68, .removed = 0x0 },
+    [V6_v6mpyvubs10_vxx] = { .introduced = 0x68, .removed = 0x0 },
+    [V6_vasrvuhubrndsat] = { .introduced = 0x69, .removed = 0x0 },
+    [V6_vasrvuhubsat] = { .introduced = 0x69, .removed = 0x0 },
+    [V6_vasrvwuhrndsat] = { .introduced = 0x69, .removed = 0x0 },
+    [V6_vasrvwuhsat] = { .introduced = 0x69, .removed = 0x0 },
+    [V6_vassign_tmp] = { .introduced = 0x69, .removed = 0x0 },
+    [V6_vcombine_tmp] = { .introduced = 0x69, .removed = 0x0 },
+    [V6_vmpyuhvs] = { .introduced = 0x69, .removed = 0x0 },
+};
+
+#endif /* HEXAGON_TAG_ARCH_TABLE_H */
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 4/8] Hexagon (target/hexagon) Introduce tag_rev_info.c.inc

Quoted 23 lines · Taylor Simpson

From: Matheus Tavares Bernardino<[email protected]>

Table that records which CPU revision introduced or removed each opcode

---
  target/hexagon/tag_rev_info.c.inc | 61 +++++++++++++++++++++++++++++++
  1 file changed, 61 insertions(+)

create mode 100644 target/hexagon/tag_rev_info.c.inc

target/hexagon/tag_rev_info.c.inc new file +4 -0
new file mode 100644
index 0000000000..ed7fb68aaf
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */

target/hexagon/tag_rev_info.c.inc line 14

Quoted 10 lines
target/hexagon/tag_rev_info.c.inc new file +10 -0
+
+#ifndef HEXAGON_TAG_ARCH_TABLE_H
+#define HEXAGON_TAG_ARCH_TABLE_H
+
+struct tag_rev_info { int introduced, removed; };
+
+static const struct tag_rev_info tag_rev_info[XX_LAST_OPCODE] = {
+    [A7_clip] = { .introduced = 0x67, .removed = 0x0 },
+    [A7_croundd_ri] = { .introduced = 0x67, .removed = 0x0 },
+    [A7_croundd_rr] = { .introduced = 0x67, .removed = 0x0 },

Any reason not to use enum HexagonVersion?

r~

#6 lore raw

Re: [PATCH 4/8] Hexagon (target/hexagon) Introduce tag_rev_info.c.inc

Quoted 38 lines · Richard Henderson
Quoted 8 lines · Taylor Simpson

From: Matheus Tavares Bernardino<[email protected]>

Table that records which CPU revision introduced or removed each opcode

Review

Quoted 6 lines
---
  target/hexagon/tag_rev_info.c.inc | 61 +++++++++++++++++++++++++++++++
  1 file changed, 61 insertions(+)

create mode 100644 target/hexagon/tag_rev_info.c.inc

(quoted diff) +0 -0

b/target/hexagon/tag_rev_info.c.inc

Quoted 9 lines

new file mode 100644
index 0000000000..ed7fb68aaf

target/hexagon/tag_rev_info.c.inc new file +4 -0
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */

target/hexagon/tag_rev_info.c.inc line 14

Quoted 10 lines
target/hexagon/tag_rev_info.c.inc new file +10 -0
+
+#ifndef HEXAGON_TAG_ARCH_TABLE_H
+#define HEXAGON_TAG_ARCH_TABLE_H
+
+struct tag_rev_info { int introduced, removed; };
+
+static const struct tag_rev_info tag_rev_info[XX_LAST_OPCODE] = {
+    [A7_clip] = { .introduced = 0x67, .removed = 0x0 },
+    [A7_croundd_ri] = { .introduced = 0x67, .removed = 0x0 },
+    [A7_croundd_rr] = { .introduced = 0x67, .removed = 0x0 },

Any reason not to use enum HexagonVersion?

Quoted 2 lines

r~

Good call. I will change this.

Thanks,
Taylor

#7 lore raw

PATCH 5/8 Hexagon (target/hexagon) Check each opcode against current CPU version

In reply to Taylor Simpson

During decoding, check that the opcode is supported in the current Hexagon CPU version

---
 target/hexagon/decode.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
target/hexagon/decode.c +23 -0
index b5ece60450..0cff15e890 100644
@@ -648,6 +648,18 @@ decode_set_slot_number(Packet *pkt)
     return has_valid_slot_assignment(pkt);
 }
 
+static bool opcode_supported(uint16_t opcode, HexagonVersion hex_version)
+{
+#include "tag_rev_info.c.inc"
+
+    struct tag_rev_info info = tag_rev_info[opcode];
+    if ((info.introduced && hex_version < info.introduced) ||
+        (info.removed && hex_version >= info.removed)) {
+        return false;
+    }
+    return true;
+}
+
 /*
  * decode_packet
  * Decodes packet with given words
@@ -691,6 +703,17 @@ int decode_packet(DisasContext *ctx, int max_words, const uint32_t *words,
         /* Ran out of words! */
         return 0;
     }
+
+    /*
+     * Check that all the opcodes are supported in this Hexagon version
+     * If not, return decode error
+     */
+    for (i = 0; i < num_insns; i++) {
+        if (!opcode_supported(pkt->insn[i].opcode, ctx->hex_version)) {
+            return 0;
+        }
+    }
+
     pkt->encod_pkt_size_in_bytes = words_read * 4;
     pkt->pkt_has_hvx = false;
     for (i = 0; i < num_insns; i++) {
Signature · 1 line
2.43.0
#8 lore raw

PATCH 6/8 Hexagon (linux-user/hexagon) Convert INVALID_PACKET into SIGILL

In reply to Taylor Simpson

From: Brian Cain <[email protected]>

tests/tcg/hexagon: Handle SIGILL internally in invalid-slots test

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.

---
 linux-user/hexagon/cpu_loop.c     |  4 ++
 target/hexagon/translate.c        | 19 +++++++-
 tests/tcg/hexagon/invalid-slots.c | 76 ++++++++++++++++++++++++-------
 tests/tcg/hexagon/Makefile.target |  6 ---
 4 files changed, 80 insertions(+), 25 deletions(-)
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/translate.c +17 -2
index 9498cd4502..78e0d6c219 100644
@@ -198,6 +198,21 @@ static void gen_exception_end_tb(DisasContext *ctx, int excp)
 
 }
 
+/*
+ * 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,
                              uint32_t words[])
 {
@@ -935,7 +950,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 +965,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-slots.c +59 -17

88 lines not on this page · raw message

tests/tcg/hexagon/Makefile.target +0 -6
index e5182c01d8..8c9d48fc4d 100644
@@ -53,12 +53,6 @@ HEX_TESTS += hvx_histogram
 HEX_TESTS += invalid-slots
 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.43.0
#9 lore raw

PATCH 7/8 Hexagon (target/hexagon) Disassembly of invalid packets

In reply to Taylor Simpson

We pass the Hexagon CPU version to disassemble_hexagon. This allows decode_packet to know if the opcodes are supported.

Fix the printing of invalid packets.

---
 target/hexagon/cpu_bits.h |  4 +++-
 disas/hexagon.c           |  3 ++-
 target/hexagon/cpu.c      |  3 +++
 target/hexagon/decode.c   | 20 +++++++++++++++++---
 4 files changed, 25 insertions(+), 5 deletions(-)
target/hexagon/cpu_bits.h +3 -1
index ff596e2a94..ba580df3c1 100644
@@ -19,6 +19,7 @@
 #define HEXAGON_CPU_BITS_H
 
 #include "qemu/bitops.h"
+#include "cpu-qom.h"
 
 #define PCALIGN 4
 #define PCALIGN_MASK (PCALIGN - 1)
@@ -64,6 +65,7 @@ static inline bool is_packet_end(uint32_t endocing)
     return ((bits == 0x3) || (bits == 0x0));
 }
 
-int disassemble_hexagon(uint32_t *words, int nwords, bfd_vma pc, GString *buf);
+int disassemble_hexagon(uint32_t *words, int nwords, bfd_vma pc, GString *buf,
+                        HexagonVersion hex_version);
 
 #endif
disas/hexagon.c +2 -1
index c1a4ffc5f6..9a18f12854 100644
@@ -31,6 +31,7 @@
 
 int print_insn_hexagon(bfd_vma memaddr, struct disassemble_info *info)
 {
+    HexagonVersion hex_version = (HexagonVersion)info->target_info;
     uint32_t words[PACKET_WORDS_MAX];
     bool found_end = false;
     GString *buf;
@@ -58,7 +59,7 @@ int print_insn_hexagon(bfd_vma memaddr, struct disassemble_info *info)
     }
 
     buf = g_string_sized_new(PACKET_BUFFER_LEN);
-    len = disassemble_hexagon(words, i, memaddr, buf);
+    len = disassemble_hexagon(words, i, memaddr, buf, hex_version);
     (*info->fprintf_func)(info->stream, "%s", buf->str);
     g_string_free(buf, true);
 
target/hexagon/cpu.c +3 -0
index 09a0de3c2f..4b16214ace 100644
@@ -314,8 +314,11 @@ static void hexagon_cpu_reset_hold(Object *obj, ResetType type)
 static void hexagon_cpu_disas_set_info(const CPUState *cs,
                                        disassemble_info *info)
 {
+    CPUState *cs_nonconst = (CPUState *)cs;  /* so we can get hex_cpu */
+    HexagonCPU *hex_cpu = env_archcpu(cpu_env(cs_nonconst));
     info->print_insn = print_insn_hexagon;
     info->endian = BFD_ENDIAN_LITTLE;
+    info->target_info = (void *)HEXAGON_CPU_GET_CLASS(hex_cpu)->hex_version;
 }
 
 static void hexagon_cpu_realize(DeviceState *dev, Error **errp)
target/hexagon/decode.c +17 -3
index 0cff15e890..bfc769be62 100644
@@ -768,19 +768,33 @@ int decode_packet(DisasContext *ctx, int max_words, const uint32_t *words,
 
 /* Used for "-d in_asm" logging */
 int disassemble_hexagon(uint32_t *words, int nwords, bfd_vma pc,
-                        GString *buf)
+                        GString *buf, HexagonVersion hex_version)
 {
     DisasContext ctx;
     Packet pkt;
 
     memset(&ctx, 0, sizeof(DisasContext));
+    ctx.hex_version = hex_version;
     ctx.pkt = &pkt;
 
     if (decode_packet(&ctx, nwords, words, &pkt, true) > 0) {
         snprint_a_pkt_disas(buf, &pkt, words, pc);
         return pkt.encod_pkt_size_in_bytes;
     } else {
-        g_string_assign(buf, "<invalid>");
-        return 0;
+        for (int i = 0; i < nwords; i++) {
+            g_string_append_printf(buf, "0x" TARGET_FMT_lx "\t", words[i]);
+            if (i == 0) {
+                g_string_append(buf, "{");
+            }
+            g_string_append(buf, "\t");
+            g_string_append(buf, "<invalid>");
+            if (i < nwords - 1) {
+                pc += 4;
+                g_string_append_printf(buf, "\n0x" TARGET_FMT_lx ": ",
+                                       (target_ulong)pc);
+            }
+        }
+        g_string_append(buf, " }");
+        return nwords * sizeof(uint32_t);
     }
 }
Signature · 1 line
2.43.0
#10 lore raw

PATCH 8/8 tests/tcg/hexagon: Add test for revision-gated instruction decoding

In reply to Taylor Simpson

From: Brian Cain <[email protected]>

Add check_rev_gating, a linux-user test that verifies the decoder rejects instructions from a newer CPU revision than the one selected by the ELF binary's e_flags.

---
 tests/tcg/hexagon/check_rev_gating.c | 141 +++++++++++++++++++++++++++
 tests/tcg/hexagon/Makefile.target    |   6 ++
 2 files changed, 147 insertions(+)
 create mode 100644 tests/tcg/hexagon/check_rev_gating.c
tests/tcg/hexagon/check_rev_gating.c new file +141 -0
new file mode 100644
index 0000000000..26b66f5455
@@ -0,0 +1,141 @@
+/*
+ * Test that instructions from a newer revision than the running CPU
+ * are rejected with SIGILL.
+ *
+ * Compiled with -mv66 so that e_flags selects CPU v66. The test embeds
+ * a v68 instruction (L2_loadw_aq: "r0 = memw_aq(r0)") via .word
+ * encoding. The revision-gated decoder must reject it, and linux-user
+ * must deliver SIGILL.
+ *
+ * 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 int signals_handled;
+static int expected_signals;
+
+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;
+    signals_handled++;
+}
+
+/*
+ * Try to execute an instruction introduced after v66
+ * On a v66 CPU this must raise SIGILL.
+ *
+ * Since we are building for v66, the assembler will reject
+ * the instructions, so introduce them with .word.
+ */
+#define TRY_FUNC(NAME, WORD) \
+static int try_##NAME(void) \
+{ \
+    int sig; \
+    expected_signals++; \
+    asm volatile( \
+        "r0 = #0\n" \
+        "r1 = ##1f\n" \
+        "memw(%1) = r1\n" \
+        WORD \
+        "1:\n" \
+        "%0 = r0\n" \
+        : "=r"(sig) \
+        : "r"(&resume_pc) \
+        : "r0", "r1", "memory"); \
+    return sig; \
+}
+
+TRY_FUNC(v68_loadw_aq,
+         ".word 0x9200c800    /* { r0 = memw_aq(r0) } */\n")
+TRY_FUNC(v68_loadd_aq,
+         ".word 0x9201d800    /* r1:0 = memd_aq(r1) */\n")
+TRY_FUNC(v68_release_at,
+         ".word 0xa0e0c00c    /* release(r0):at */\n")
+TRY_FUNC(v68_release_st,
+         ".word 0xa0e0c02c    /* release(r0):st */\n")
+TRY_FUNC(v68_storew_rl_at,
+         ".word 0xa0a0c108    /* memw_rl(r0):at = r1 */\n")
+TRY_FUNC(v68_stored_rl_at,
+         ".word 0xa0e2c008    /* memd_rl(r2):at = r1:0 */\n")
+TRY_FUNC(v68_storew_rl_st,
+         ".word 0xa0a0c128    /* memw_rl(r0):st = r1 */\n")
+TRY_FUNC(v68_stored_rl_st,
+         ".word 0xa0e2c028    /* memd_rl(r2):st = r1:0 */\n")
+
+TRY_FUNC(v68hvx_v6mpy,
+         ".word 0x1f42e424    /* v5:4.w = v6mpy(v5:4.ub, v3:2.b, #1):v */\n")
+
+TRY_FUNC(v69hvx_vasrvuhubrndsat,
+         ".word 0x1d06c465    /* v5.ub = vasr(v5:4.uh, v6.ub):rnd:sat */\n")
+TRY_FUNC(v69hvx_vasrvuhubsat,
+         ".word 0x1d06c445    /* v5.ub = vasr(v5:4.uh, v6.ub):sat */\n")
+TRY_FUNC(v69hvx_vasrvwuhrndsat,
+         ".word 0x1d06c425    /* v5.uh = vasr(v5:4.w, v6.uh):rnd:sat */\n")
+TRY_FUNC(v69hvx_vasrvwuhsat,
+         ".word 0x1d06c405    /* v5.uh = vasr(v5:4.w, v6.uh):sat */\n")
+TRY_FUNC(v69hvx_vassign_tmp,
+         ".word 0x1e014dcc    /* { v12.tmp = v13 */\n"
+         ".word 0x1c43cc04    /*    v4.w = vadd(v12.w, v3.w) } */\n")
+TRY_FUNC(v69hvx_vcombine_tmp,
+         ".word 0x1eae4fec    /* { v13:12.tmp = vcombine(v15, v14) */\n"
+         ".word 0x1c434c04    /*   v4.w = vadd(v12.w, v3.w) */\n"
+         ".word 0x1e03edf0    /*   v16 = v13 } */\n")
+TRY_FUNC(v69hvx_vmpyuhvs,
+         ".word 0x1fc5e4e4    /* v4.uh = vmpy(V4.uh, v5.uh):>>16 */\n")
+
+TRY_FUNC(v73_callrh,
+         ".word 0x50c5c000    /* callrh r5 */\n")
+TRY_FUNC(v73_jumprh,
+         ".word 0x52c0c000    /* jumprh r0 */\n")
+
+int main(void)
+{
+    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(try_v68_loadw_aq() == SIGILL);
+    assert(try_v68_loadd_aq() == SIGILL);
+    assert(try_v68_release_at() == SIGILL);
+    assert(try_v68_release_st() == SIGILL);
+    assert(try_v68_storew_rl_at() == SIGILL);
+    assert(try_v68_stored_rl_at() == SIGILL);
+    assert(try_v68_storew_rl_st() == SIGILL);
+    assert(try_v68_stored_rl_st() == SIGILL);
+
+    assert(try_v68hvx_v6mpy() == SIGILL);
+
+    assert(try_v69hvx_vasrvuhubrndsat() == SIGILL);
+    assert(try_v69hvx_vasrvuhubsat() == SIGILL);
+    assert(try_v69hvx_vasrvwuhrndsat() == SIGILL);
+    assert(try_v69hvx_vasrvwuhsat() == SIGILL);
+    assert(try_v69hvx_vassign_tmp() == SIGILL);
+    assert(try_v69hvx_vcombine_tmp() == SIGILL);
+    assert(try_v69hvx_vmpyuhvs() == SIGILL);
+
+    assert(try_v73_callrh() == SIGILL);
+    assert(try_v73_jumprh() == SIGILL);
+
+    assert(signals_handled == expected_signals);
+
+    puts("PASS");
+    return EXIT_SUCCESS;
+}
tests/tcg/hexagon/Makefile.target +6 -0
index 8c9d48fc4d..c768ba1a4f 100644
@@ -79,6 +79,7 @@ HEX_TESTS += test_vminh
 HEX_TESTS += test_vpmpyh
 HEX_TESTS += test_vspliceb
 
+HEX_TESTS += check_rev_gating
 HEX_TESTS += v68_scalar
 HEX_TESTS += v68_hvx
 HEX_TESTS += v69_hvx
@@ -104,6 +105,11 @@ read_write_overlap: read_write_overlap.c hex_test.h
 reg_mut: reg_mut.c hex_test.h
 unaligned_pc: unaligned_pc.c
 
+# Compile for v66 so that the ELF selects a v66 CPU; the test then
+# exercises revision gating by executing a v68 .word instruction.
+check_rev_gating: check_rev_gating.c
+	$(CC) $(CFLAGS) -mv66 -O2 $< -o $@ $(LDFLAGS)
+
 # This test has to be compiled for the -mv67t target
 usr: usr.c hex_test.h
 	$(CC) $(CFLAGS) -mv67t -O2 -Wno-inline-asm -Wno-expansion-to-defined $< -o $@ $(LDFLAGS)
Signature · 1 line
2.43.0