Skip to contentSkip to conversation

Hexagon: exception, interrupt, system reg fixes

PATCH In review Brian Cain 4 participants 20 messages Sep 1 – Sep 4

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v2 cover Hexagon: exception, interrupt, system reg fixes

Changes to address some test cases:
- user/system mode exception consolidation
- Direct-to-Guest interrupt delivery
- interrupt mask writes, tlb probe exception
- l2vic edge-trigger clear on deassert
- map architectural timer regs to clock

v2:
- for guard writes to unimplemented guest registers: updated the logic to early-return and clarified the commit message.
- implement direct-to-guest interrupt delivery: added a comment to explain "dtg".

v1: https://lore.kernel.org/qemu-devel/[email protected]/

Brian Cain (10):
target/hexagon: align exceptions for user/sysemu tests/tcg/hexagon: check priv instructions raise SIGILL target/hexagon: guard writes to unimplemented guest registers target/hexagon: take BQL when reading the system pcycle count target/hexagon: gate GPCYCLE guest register reads on SSR:CE target/hexagon: read UTIMERLO/UTIMERHI from the global timer target/hexagon: raise imprecise exception on multi-TLB match target/hexagon: implement direct-to-guest interrupt delivery target/hexagon: fix iassign{r,w} to cover all threads tests/functional/hexagon: update to v0.2.12,

    +test_{interrupts,sys_regs}

Sid Manning (1):
hw/intc: clear pending bit on l2vic de-assertion

 include/hw/hexagon/hexagon_tlb.h            |  3 +-
 target/hexagon/cpu.h                        |  1 +
 target/hexagon/helper.h                     |  4 +
 target/hexagon/translate.h                  |  6 +-
 target/hexagon/reg_fields_def.h.inc         |  7 ++
 hw/hexagon/hexagon_tlb.c                    |  5 +-
 hw/intc/hex-l2vic.c                         |  2 +
 linux-user/hexagon/cpu_loop.c               | 30 +++----
 target/hexagon/cpu.c                        | 15 +++-
 target/hexagon/cpu_helper.c                 | 12 ++-
 target/hexagon/genptr.c                     | 29 +++++++
 target/hexagon/hex_interrupts.c             | 91 +++++++++++++++++++--
 target/hexagon/hex_mmu.c                    |  5 +-
 target/hexagon/hexswi.c                     | 37 ++++++++-
 target/hexagon/machine.c                    |  5 +-
 target/hexagon/op_helper.c                  | 72 ++++++++--------
 target/hexagon/translate.c                  | 56 +++++++++----
 tests/tcg/hexagon/privileged-insn.c         | 79 ++++++++++++++++++
 tests/tcg/hexagon/reg_mut.c                 | 11 ++-
 target/hexagon/hex_common.py                | 18 +++-
 tests/functional/hexagon/test_arch_tests.py | 12 ++-
 tests/tcg/hexagon/Makefile.target           |  1 +
 22 files changed, 395 insertions(+), 106 deletions(-)
 create mode 100644 tests/tcg/hexagon/privileged-insn.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/11 target/hexagon: align exceptions for user/sysemu

System mode reports an exception as cs->exception_index = HEX_EVENT_* plus env->cause_code = HEX_CAUSE_*, but translated code in user mode put the cause code straight into exception_index, so cpu_loop() was decoding both forms. gen_exception_decode_fail() and the misaligned-PC check used the raw form unconditionally, so in system mode the cause code was misread as an event number.

Use the {event, cause} everywhere and drop the duplicated cases from cpu_loop(), which fixes HEX_CAUSE_PRIV_USER_NO_SINSN and HEX_CAUSE_PRIV_USER_NO_GINSN. The misaligned PC is no longer zeroed on its way out either, so it reaches the signal frame as si_addr instead of whatever r31 held.

---
 target/hexagon/translate.h    |  2 +-
 linux-user/hexagon/cpu_loop.c | 30 +++++++++++-------------------
 target/hexagon/cpu.c          |  3 ++-
 target/hexagon/translate.c    | 27 +++++++++------------------
 4 files changed, 23 insertions(+), 39 deletions(-)
target/hexagon/translate.h +1 -1
index 3c5773e2c73..00de2b0d2ec 100644
@@ -330,7 +330,7 @@ extern TCGv_i32 hex_t_sreg[NUM_SREGS];
 #endif
 
 
-void hex_gen_exception_end_tb(DisasContext *ctx, int excp);
+void hex_gen_exception_end_tb(DisasContext *ctx, int cause);
 
 void process_store(DisasContext *ctx, int slot_num);
 
linux-user/hexagon/cpu_loop.c +11 -19
index d7f73439dbc..e4ef97a1184 100644
@@ -66,21 +66,22 @@ void cpu_loop(CPUHexagonState *env)
             case HEX_CAUSE_FETCH_NO_UPAGE:
             case HEX_CAUSE_PRIV_NO_UREAD:
             case HEX_CAUSE_PRIV_NO_UWRITE:
-            force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR,
-                    env->gpr[HEX_REG_PC]);
-
-            break;
+                force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR,
+                                env->gpr[HEX_REG_PC]);
+                break;
             case HEX_CAUSE_PRIV_USER_NO_GINSN:
             case HEX_CAUSE_PRIV_USER_NO_SINSN:
             case HEX_CAUSE_INVALID_PACKET:
-            force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC,
-                    env->gpr[HEX_REG_PC]);
-            break;
+            case HEX_CAUSE_REG_WRITE_CONFLICT:
+                force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPC,
+                                env->gpr[HEX_REG_PC]);
+                break;
             case HEX_CAUSE_MISALIGNED_LOAD:
             case HEX_CAUSE_MISALIGNED_STORE:
-            force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN,
-                    env->gpr[HEX_REG_PC]);
-            break;
+            case HEX_CAUSE_PC_NOT_ALIGNED:
+                force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN,
+                                env->gpr[HEX_REG_PC]);
+                break;
             default:
                 EXCP_DUMP(env, "\nqemu: unhandled CPU precise exception "
                     "cause code 0x%x - aborting\n",
@@ -88,15 +89,6 @@ void cpu_loop(CPUHexagonState *env)
                 exit(EXIT_FAILURE);
             }
             break;
-        case HEX_CAUSE_PC_NOT_ALIGNED:
-            force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN,
-                            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;
         case EXCP_ATOMIC:
             cpu_exec_step_atomic(cs);
             break;
target/hexagon/cpu.c +2 -1
index 7067e5b70f7..0bbefc2fb87 100644
@@ -323,7 +323,8 @@ static TCGTBCPUState hexagon_get_tb_cpu_state(CPUState *cs)
         hex_flags = FIELD_DP32(hex_flags, TB_FLAGS, IS_TIGHT_LOOP, 1);
     }
     if (pc & PCALIGN_MASK) {
-        hexagon_raise_exception_err(env, HEX_CAUSE_PC_NOT_ALIGNED, 0);
+        env->cause_code = HEX_CAUSE_PC_NOT_ALIGNED;
+        hexagon_raise_exception_err(env, HEX_EVENT_PRECISE, pc);
     }
 
 #ifndef CONFIG_USER_ONLY
target/hexagon/translate.c +9 -18
index 06a8159d283..5cfa60ca302 100644
@@ -73,8 +73,8 @@ TCGv hex_vstore_pending[VSTORES_MAX];
 #ifndef CONFIG_USER_ONLY
 TCGv_i32 hex_greg[NUM_GREGS];
 TCGv_i32 hex_t_sreg[NUM_SREGS];
-TCGv_i32 hex_cause_code;
 #endif
+static TCGv_i32 hex_cause_code;
 
 static const char * const hexagon_prednames[] = {
   "p0", "p1", "p2", "p3"
@@ -128,19 +128,14 @@ intptr_t ctx_tmp_vreg_off(DisasContext *ctx, int regnum,
     return offset;
 }
 
-static void gen_exception(int excp, uint32_t PC)
+static void gen_precise_exception(int cause, uint32_t PC)
 {
-    gen_helper_raise_exception(tcg_env, tcg_constant_i32(excp),
+    tcg_gen_movi_i32(hex_cause_code, cause);
+    gen_helper_raise_exception(tcg_env, tcg_constant_i32(HEX_EVENT_PRECISE),
                                tcg_constant_i32(PC));
 }
 
 #ifndef CONFIG_USER_ONLY
-static inline void gen_precise_exception(int excp, uint32_t PC)
-{
-    tcg_gen_movi_i32(hex_cause_code, excp);
-    gen_exception(HEX_EVENT_PRECISE, PC);
-}
-
 static void gen_pcycle_counters(DisasContext *ctx)
 {
     if (ctx->pcycle_enabled) {
@@ -224,14 +219,10 @@ static void gen_end_tb(DisasContext *ctx)
     ctx->base.is_jmp = DISAS_NORETURN;
 }
 
-void hex_gen_exception_end_tb(DisasContext *ctx, int excp)
+void hex_gen_exception_end_tb(DisasContext *ctx, int cause)
 {
     gen_exec_counters(ctx);
-#ifdef CONFIG_USER_ONLY
-    gen_exception(excp, ctx->pkt.pc);
-#else
-    gen_precise_exception(excp, ctx->pkt.pc);
-#endif
+    gen_precise_exception(cause, ctx->pkt.pc);
     ctx->base.is_jmp = DISAS_NORETURN;
 }
 
@@ -239,13 +230,13 @@ void hex_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)
+static void gen_exception_decode_fail(DisasContext *ctx, int nwords, int cause)
 {
     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(excp, fail_pc);
+    gen_precise_exception(cause, fail_pc);
     ctx->base.is_jmp = DISAS_NORETURN;
     ctx->base.pc_next = fail_pc;
 }
@@ -1361,9 +1352,9 @@ void hexagon_translate_init(void)
         offsetof(CPUHexagonState, llsc_val), "llsc_val");
     hex_llsc_val_i64 = tcg_global_mem_new_i64(tcg_env,
         offsetof(CPUHexagonState, llsc_val_i64), "llsc_val_i64");
-#ifndef CONFIG_USER_ONLY
     hex_cause_code = tcg_global_mem_new_i32(tcg_env,
         offsetof(CPUHexagonState, cause_code), "cause_code");
+#ifndef CONFIG_USER_ONLY
     hex_cycle_count = tcg_global_mem_new_i64(tcg_env,
         offsetof(CPUHexagonState, t_cycle_count), "t_cycle_count");
 #endif
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/11 tests/tcg/hexagon: check priv instructions raise SIGILL

In reply to Brian Cain

A_PRIV and A_GUEST instructions executed from user mode raise HEX_CAUSE_PRIV_USER_NO_SINSN and HEX_CAUSE_PRIV_USER_NO_GINSN, which aborted qemu-hexagon until the preceding patch.

---
 tests/tcg/hexagon/privileged-insn.c | 79 +++++++++++++++++++++++++++++
 tests/tcg/hexagon/Makefile.target   |  1 +
 2 files changed, 80 insertions(+)
 create mode 100644 tests/tcg/hexagon/privileged-insn.c
tests/tcg/hexagon/privileged-insn.c new file +79 -0
new file mode 100644
index 00000000000..306d4542ea4
@@ -0,0 +1,79 @@
+/*
+ * Test that privileged and guest-mode instructions raise SIGILL in user mode.
+ *
+ * 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;
+}
+
+static int test_priv_insn(void)
+{
+    int sig;
+
+    asm volatile(
+        "r0 = #0\n"
+        "r1 = ##1f\n"
+        "memw(%[pc]) = r1\n"
+        "stop(r0)\n"
+        "1:\n"
+        "%[sig] = r0\n"
+        : [sig] "=r"(sig)
+        : [pc] "r"(&resume_pc)
+        : "r0", "r1", "memory");
+
+    return sig;
+}
+
+static int test_guest_insn(void)
+{
+    int sig;
+
+    asm volatile(
+        "r0 = #0\n"
+        "r1 = ##1f\n"
+        "memw(%[pc]) = r1\n"
+        "r0 = g0\n"
+        "1:\n"
+        "%[sig] = r0\n"
+        : [sig] "=r"(sig)
+        : [pc] "r"(&resume_pc)
+        : "r0", "r1", "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_priv_insn() == SIGILL);
+    assert(test_guest_insn() == SIGILL);
+
+    puts("PASS");
+    return EXIT_SUCCESS;
+}
tests/tcg/hexagon/Makefile.target +1 -0
index 61adf6356e4..641f6bc10e7 100644
@@ -58,6 +58,7 @@ HEX_TESTS += invalid-slots
 HEX_TESTS += valid-slots
 HEX_TESTS += invalid-encoding
 HEX_TESTS += multiple-writes
+HEX_TESTS += privileged-insn
 HEX_TESTS += unaligned_pc
 HEX_TESTS += unaligned_data
 
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 3/11 hw/intc: clear pending bit on l2vic de-assertion

In reply to Brian Cain

From: Sid Manning <[email protected]>

l2vic_set_irq() clears a level-triggered source's pending bit on de-assertion, so a source that drops before it is latched into int_status is not spuriously re-delivered on the next ciad. Edge-triggered sources (int_type set) keep the pulse semantics they already have via SOFT_INT/set_irq(level=1).

---
 hw/intc/hex-l2vic.c | 2 ++
 1 file changed, 2 insertions(+)
hw/intc/hex-l2vic.c +2 -0
index f07ec850d49..a986f0bdf35 100644
@@ -299,6 +299,8 @@ static void l2vic_set_irq(void *opaque, int irq, int level)
 
     if (level) {
         set_bit32(irq, s->int_pending);
+    } else if (!test_bit32(irq, s->int_type)) {
+        clear_bit32(irq, s->int_pending);
     }
     l2vic_update(s, irq);
 }
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v2 03/11] hw/intc: clear pending bit on l2vic de-assertion

Quoted 19 lines · Brian Cain

From: Sid Manning <[email protected]>

l2vic_set_irq() clears a level-triggered source's pending bit on de-assertion, so a source that drops before it is latched into int_status is not spuriously re-delivered on the next ciad. Edge-triggered sources (int_type set) keep the pulse semantics they already have via SOFT_INT/set_irq(level=1).

---
  hw/intc/hex-l2vic.c | 2 ++
  1 file changed, 2 insertions(+)
hw/intc/hex-l2vic.c +0 -0
index f07ec850d49..a986f0bdf35 100644

hw/intc/hex-l2vic.c static void l2vic_set_irq(void *opaque, int irq, int level) · line 305

Quoted 7 lines
hw/intc/hex-l2vic.c +2 -0
@@ -299,6 +299,8 @@ static void l2vic_set_irq(void *opaque, int irq, int level)
 
      if (level) {
          set_bit32(irq, s->int_pending);
+    } else if (!test_bit32(irq, s->int_type)) {
+        clear_bit32(irq, s->int_pending);
      }
      l2vic_update(s, irq);

I'm confused with l2vic_update() logic which seems to duplicate a bit.

hw/intc/hex-l2vic.c static void l2vic_set_irq(void *opaque, int irq, int level) · line 306

Quoted 1 line
hw/intc/hex-l2vic.c +0 -0
  }

hw/intc/hex-l2vic.c-95- /* Edge or Level interrupt */
hw/intc/hex-l2vic.c:96: DECLARE_BITMAP32(int_type, L2VIC_INTERRUPT_MAX);

Not obvious, so 0:edge and 1:level. Ah no...

hw/intc/hex-l2vic.c-268- * Only auto-disable for edge-triggered interrupts (type=1).

So 1:edge and 0:level?

Tiny helpers can help to make the code more readable:

static bool edge_triggered_irq(HexL2VICState *s, int irq)

{
    return test_bit32(irq, s->int_type) == 1;
}
author
#5 lore raw

Re: [PATCH v2 03/11] hw/intc: clear pending bit on l2vic de-assertion

Quoted 36 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Brian Cain

From: Sid Manning <[email protected]>

l2vic_set_irq() clears a level-triggered source's pending bit on de-assertion, so a source that drops before it is latched into int_status is not spuriously re-delivered on the next ciad. Edge-triggered sources (int_type set) keep the pulse semantics they already have via SOFT_INT/set_irq(level=1).

---
  hw/intc/hex-l2vic.c | 2 ++

  1 file changed, 2 insertions(+)
hw/intc/hex-l2vic.c +0 -0
index f07ec850d49..a986f0bdf35 100644

hw/intc/hex-l2vic.c static void l2vic_set_irq(void *opaque, int irq, · line 305

Quoted 7 lines
hw/intc/hex-l2vic.c +2 -0
@@ -299,6 +299,8 @@ static void l2vic_set_irq(void *opaque, int irq, 
 int level)
         if (level) {
           set_bit32(irq, s->int_pending);
+    } else if (!test_bit32(irq, s->int_type)) {
+        clear_bit32(irq, s->int_pending);
       }
       l2vic_update(s, irq);

I'm confused with l2vic_update() logic which seems to duplicate a bit.

hw/intc/hex-l2vic.c static void l2vic_set_irq(void *opaque, int irq, · line 306

Quoted 1 line
hw/intc/hex-l2vic.c +0 -0
   }

hw/intc/hex-l2vic.c-95-    /* Edge or Level interrupt */ hw/intc/hex-l2vic.c:96:    DECLARE_BITMAP32(int_type, L2VIC_INTERRUPT_MAX);

Quoted 7 lines

Not obvious, so 0:edge and 1:level. Ah no...

hw/intc/hex-l2vic.c-268-         * Only auto-disable for edge-triggered interrupts (type=1).

So 1:edge and 0:level?

Tiny helpers can help to make the code more readable:

Agreed! I like this change.  Sent a follow up.

Quoted 5 lines

static bool edge_triggered_irq(HexL2VICState *s, int irq) {
    return test_bit32(irq, s->int_type) == 1;
}

#6 lore raw

PATCH v2 10/11 target/hexagon: fix iassign{r, w} to cover all threads

In reply to Brian Cain

iassignw/iassignr were filtering CPU_FOREACH by MODECTL_E, the thread-enabled mask. That mask reflects whether a thread has been started, not whether it exists, so IMASK writes/reads never reached threads that hadn't been started yet, leaving their IMASK stale or unreadable. Drop the MODECTL_E filtering and operate on every thread.

---
 target/hexagon/op_helper.c | 42 +++++++++-----------------------------
 1 file changed, 10 insertions(+), 32 deletions(-)
target/hexagon/op_helper.c +10 -32
index df31ed2488a..e19c3c91085 100644
@@ -1637,61 +1637,39 @@ void HELPER(cswi)(CPUHexagonState *env, uint32_t mask)
 
 void HELPER(iassignw)(CPUHexagonState *env, uint32_t src)
 {
-    uint32_t modectl;
-    uint32_t thread_enabled_mask;
     CPUState *cpu;
-    HexagonCPU *hex_cpu;
 
     BQL_LOCK_GUARD();
-    hex_cpu = env_archcpu(env);
-    modectl = hex_cpu->globalregs ?
-        hexagon_globalreg_read(hex_cpu->globalregs, HEX_SREG_MODECTL,
-                               env->threadId) : 0;
-    thread_enabled_mask = GET_FIELD(MODECTL_E, modectl);
 
     CPU_FOREACH(cpu) {
         CPUHexagonState *thread_env = &(HEXAGON_CPU(cpu)->env);
-        uint32_t thread_id_mask = 0x1 << thread_env->threadId;
-        if (thread_enabled_mask & thread_id_mask) {
-            uint32_t imask = thread_env->t_sreg[HEX_SREG_IMASK];
-            uint32_t intbitpos = (src >> 16) & 0xF;
-            uint32_t val = (src >> thread_env->threadId) & 0x1;
-            imask = deposit32(imask, intbitpos, 1, val);
-            thread_env->t_sreg[HEX_SREG_IMASK] = imask;
+        uint32_t imask = thread_env->t_sreg[HEX_SREG_IMASK];
+        uint32_t intbitpos = (src >> 16) & 0xF;
+        uint32_t val = (src >> thread_env->threadId) & 0x1;
+        imask = deposit32(imask, intbitpos, 1, val);
+        thread_env->t_sreg[HEX_SREG_IMASK] = imask;
 
-            qemu_log_mask(CPU_LOG_INT, "%s: thread " TARGET_FMT_ld
-               ", new imask 0x%" PRIx32 "\n", __func__,
-               thread_env->threadId, imask);
-        }
+        qemu_log_mask(CPU_LOG_INT, "%s: thread " TARGET_FMT_ld
+           ", new imask 0x%" PRIx32 "\n", __func__,
+           thread_env->threadId, imask);
     }
     hex_interrupt_update(env);
 }
 
 uint32_t HELPER(iassignr)(CPUHexagonState *env, uint32_t src)
 {
-    uint32_t modectl;
-    uint32_t thread_enabled_mask;
     uint32_t intbitpos;
     uint32_t dest_reg;
     CPUState *cpu;
-    HexagonCPU *hex_cpu;
 
     BQL_LOCK_GUARD();
-    hex_cpu = env_archcpu(env);
-    modectl = hex_cpu->globalregs ?
-        hexagon_globalreg_read(hex_cpu->globalregs, HEX_SREG_MODECTL,
-                               env->threadId) : 0;
-    thread_enabled_mask = GET_FIELD(MODECTL_E, modectl);
     /* src fields are in same position as modectl, but mean different things */
     intbitpos = GET_FIELD(MODECTL_W, src);
     dest_reg = 0;
     CPU_FOREACH(cpu) {
         CPUHexagonState *thread_env = &(HEXAGON_CPU(cpu)->env);
-        uint32_t thread_id_mask = 0x1 << thread_env->threadId;
-        if (thread_enabled_mask & thread_id_mask) {
-            uint32_t imask = thread_env->t_sreg[HEX_SREG_IMASK];
-            dest_reg |= ((imask >> intbitpos) & 0x1) << thread_env->threadId;
-        }
+        uint32_t imask = thread_env->t_sreg[HEX_SREG_IMASK];
+        dest_reg |= ((imask >> intbitpos) & 0x1) << thread_env->threadId;
     }
 
     return dest_reg;
Signature · 1 line
2.34.1
#7 lore raw

PATCH v2 11/11 tests/functional/hexagon: update to v0.2.12, +test_{interrupts, sys_regs}

In reply to Brian Cain

test_interrupts also now passes in full following the iassign{r,w} fix, so enable it alongside the other arch tests.

---
 tests/functional/hexagon/test_arch_tests.py | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tests/functional/hexagon/test_arch_tests.py +10 -2
index 0834398c3b1..8e71386c186 100755
@@ -21,8 +21,8 @@ class ArchTestsUart(QemuSystemTest):
 
     ASSET_TARBALL = Asset(
         "https://github.com/qualcomm/qemu-hexagon-testing/releases/"
-        "download/v0.2.5/arch_tests_uart.tar.gz",
-        "edb4f37b877a3a72a0e10920477458a43b40045d34398fee8cb763fefd342f4f",
+        "download/v0.2.12/arch_tests_uart.tar.gz",
+        "871a339bf78cac0ebaf1b2509bfcd5b249ad8190be33e0cf848283b2f6915323",
     )
 
     def run_uart_test(self, test_name: str,
@@ -58,6 +58,10 @@ def test_int_steering(self) -> None:
         """
         self.run_uart_test("test_int_steering")
 
+    def test_interrupts(self) -> None:
+        """Tests interrupt delivery."""
+        self.run_uart_test("test_interrupts")
+
     def test_cache(self) -> None:
         """Tests cache operations: dckill/ickill, l2kill, dczeroa,
         dccleaninva, cache disable/enable, barriers, and dcinva/dccleana.
@@ -70,6 +74,10 @@ def test_l2vic(self) -> None:
         """
         self.run_uart_test("test_l2vic")
 
+    def test_sys_regs(self) -> None:
+        """Tests system registers."""
+        self.run_uart_test("test_sys_regs")
+
     def test_threads(self) -> None:
         """Tests hardware thread management: start/stop, MODECTL state,
         per-thread HTID, shared memory, wait/resume, STID priority, and
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v2 4/11 target/hexagon: guard writes to unimplemented guest registers

In reply to Brian Cain

Guest register G4 and above have PMU-ish features that are yet to be implemented. We'll skip these when we encounter this code rather than fault on an assertion.

Note that the generated call to greg_writable() has the side-effect of doing a LOG_UNIMP for these register accesses.

---
 target/hexagon/translate.h   |  4 +++-
 target/hexagon/hex_common.py | 18 +++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)
target/hexagon/translate.h +3 -1
index 00de2b0d2ec..d8628383f1d 100644
@@ -97,7 +97,9 @@ bool is_gather_store_insn(DisasContext *ctx);
 #ifndef CONFIG_USER_ONLY
 static inline void ctx_log_greg_write(DisasContext *ctx, int rnum)
 {
-    assert(rnum <= HEX_GREG_G3);
+    if (rnum > HEX_GREG_G3) {
+        return;
+    }
     ctx->greg_log[ctx->greg_log_idx] = rnum;
     ctx->greg_log_idx++;
 }
target/hexagon/hex_common.py +17 -1
index e33d43e3ce0..c180c19b092 100755
@@ -1097,11 +1097,24 @@ def analyze_write(self, f, tag, regno):
         """))
 
 class GuestRegister(Register):
-    pass
+    def gen_check_impl(self, f, regno):
+        if self.is_written():
+            f.write(code_fmt(f"""\
+                if (!greg_writable(insn->regno[{regno}],
+                    {str(self.is_pair()).lower()})) {{
+                    return;
+                }}
+            """))
+        else:
+            f.write(code_fmt(f"""\
+                check_greg_impl(insn->regno[{regno}],
+                                {str(self.is_pair()).lower()});
+            """))
 
 class GuestDest(GuestRegister, Single, Dest):
     def decl_tcg(self, f, tag, regno):
         self.decl_reg_num(f, regno)
+        self.gen_check_impl(f, regno)
         f.write(code_fmt(f"""\
             TCGv_i32 {self.reg_tcg()} = tcg_temp_new_i32();
         """))
@@ -1121,6 +1134,7 @@ def decl_reg_num(self, f, regno):
         """))
     def decl_tcg(self, f, tag, regno):
         self.decl_reg_num(f, regno)
+        self.gen_check_impl(f, regno)
         f.write(code_fmt(f"""\
             TCGv_i32 {self.reg_tcg()} = tcg_temp_new_i32();
             gen_read_greg({self.reg_tcg()}, {self.reg_num});
@@ -1131,6 +1145,7 @@ def analyze_read(self, f, regno):
 class GuestPairDest(GuestRegister, Pair, Dest):
     def decl_tcg(self, f, tag, regno):
         self.decl_reg_num(f, regno)
+        self.gen_check_impl(f, regno)
         f.write(code_fmt(f"""\
             TCGv_i64 {self.reg_tcg()} = tcg_temp_new_i64();
         """))
@@ -1150,6 +1165,7 @@ def decl_reg_num(self, f, regno):
         """))
     def decl_tcg(self, f, tag, regno):
         self.decl_reg_num(f, regno)
+        self.gen_check_impl(f, regno)
         f.write(code_fmt(f"""\
             TCGv_i64 {self.reg_tcg()} = tcg_temp_new_i64();
             gen_read_greg_pair({self.reg_tcg()}, {self.reg_num});
Signature · 1 line
2.34.1
Reviewed-by on v2 4/11
#9 lore raw

Re: [PATCH v2 04/11] target/hexagon: guard writes to unimplemented guest registers

Quoted 4 lines · Brian Cain

Guest register G4 and above have PMU-ish features that are yet to be implemented. We'll skip these when we encounter this code rather than fault on an assertion.

Quoted 9 lines

Note that the generated call to greg_writable() has the side-effect of doing a LOG_UNIMP for these register accesses.

---
 target/hexagon/translate.h   |  4 +++-
 target/hexagon/hex_common.py | 18 +++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)
author
#10 lore raw

PATCH v2 5/11 target/hexagon: take BQL when reading the system pcycle count

In reply to Brian Cain

hexagon_get_sys_pcycle_count() iterates all CPUs, so take the BQL with BQL_LOCK_GUARD() instead of asserting the caller already holds it. Convert the matching setters the same way, which keeps the locking contract symmetric and makes the read-modify-write in the _low/_high setters atomic. BQL_LOCK_GUARD() is a no-op when the lock is already held, so the nested guards on the guest-register read path cost nothing.

---
 target/hexagon/cpu_helper.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
target/hexagon/cpu_helper.c +8 -4
index 64c5746c6d9..649c583c0fa 100644
@@ -36,7 +36,7 @@ uint64_t hexagon_get_sys_pcycle_count(CPUHexagonState *env)
     uint64_t total = 0;
     CPUState *cs;
 
-    g_assert(bql_locked());
+    BQL_LOCK_GUARD();
     CPU_FOREACH(cs) {
         CPUHexagonState *thread_env = cpu_env(cs);
         total += thread_env->t_cycle_count;
@@ -54,11 +54,15 @@ uint32_t hexagon_get_sys_pcycle_count_low(CPUHexagonState *env)
     return (uint32_t)(hexagon_get_sys_pcycle_count(env));
 }
 
+/*
+ * Every function in this family takes the BQL itself, so the guard below
+ * holds it across the read-modify-write.  Nested guards are no-ops.
+ */
 void hexagon_set_sys_pcycle_count_high(CPUHexagonState *env, uint32_t val)
 {
     uint64_t old;
 
-    g_assert(bql_locked());
+    BQL_LOCK_GUARD();
     old = hexagon_get_sys_pcycle_count(env);
     old = deposit64(old, 32, 32, val);
     hexagon_set_sys_pcycle_count(env, old);
@@ -68,7 +72,7 @@ void hexagon_set_sys_pcycle_count_low(CPUHexagonState *env, uint32_t val)
 {
     uint64_t old;
 
-    g_assert(bql_locked());
+    BQL_LOCK_GUARD();
     old = hexagon_get_sys_pcycle_count(env);
     old = deposit64(old, 0, 32, val);
     hexagon_set_sys_pcycle_count(env, old);
@@ -81,7 +85,7 @@ void hexagon_set_sys_pcycle_count(CPUHexagonState *env, uint64_t val)
     int num_threads;
     int64_t delta, per_thread, remainder;
 
-    g_assert(bql_locked());
+    BQL_LOCK_GUARD();
     total = hexagon_get_sys_pcycle_count(env);
 
     /* Count active threads */
Signature · 1 line
2.34.1
author
#11 lore raw

PATCH v2 6/11 target/hexagon: gate GPCYCLE guest register reads on SSR:CE

In reply to Brian Cain

GPCYCLELO/GPCYCLEHI read as zero unless the cycle counter is enabled (SSR:CE), matching the hardware.

---
 target/hexagon/cpu.c       | 7 +++++--
 target/hexagon/op_helper.c | 9 ++-------
 2 files changed, 7 insertions(+), 9 deletions(-)
target/hexagon/cpu.c +5 -2
index 0bbefc2fb87..0a677840bcb 100644
@@ -809,14 +809,17 @@ static void hexagon_cpu_class_init(ObjectClass *c, const void *data)
 #ifndef CONFIG_USER_ONLY
 uint32_t hexagon_greg_read(CPUHexagonState *env, uint32_t reg)
 {
+    uint32_t ssr = env->t_sreg[HEX_SREG_SSR];
+    int ssr_ce = GET_SSR_FIELD(SSR_CE, ssr);
+
     if (reg <= HEX_GREG_G3) {
         return env->greg[reg];
     }
     switch (reg) {
     case HEX_GREG_GPCYCLELO:
-        return hexagon_get_sys_pcycle_count_low(env);
+        return ssr_ce ? hexagon_get_sys_pcycle_count_low(env) : 0;
     case HEX_GREG_GPCYCLEHI:
-        return hexagon_get_sys_pcycle_count_high(env);
+        return ssr_ce ? hexagon_get_sys_pcycle_count_high(env) : 0;
     default:
         qemu_log_mask(LOG_UNIMP, "reading greg %" PRId32
                 " not yet supported.\n", reg);
target/hexagon/op_helper.c +2 -7
index 23894ff3d28..2cea1927263 100644
@@ -1912,13 +1912,8 @@ uint64_t HELPER(greg_read_pair)(CPUHexagonState *env, uint32_t reg)
         return (uint64_t)(env->greg[reg]) |
                (((uint64_t)(env->greg[reg + 1])) << 32);
     }
-    switch (reg) {
-    case HEX_GREG_GPCYCLELO:
-        return hexagon_get_sys_pcycle_count(env);
-    default:
-        return (uint64_t)hexagon_greg_read(env, reg) |
-               ((uint64_t)(hexagon_greg_read(env, reg + 1)) << 32);
-    }
+    return (uint64_t)hexagon_greg_read(env, reg) |
+           ((uint64_t)(hexagon_greg_read(env, reg + 1)) << 32);
 }
 
 /*
Signature · 1 line
2.34.1
#12 lore raw

Re: [PATCH v2 06/11] target/hexagon: gate GPCYCLE guest register reads on SSR:CE

Hi Brian,

Quoted 19 lines · Brian Cain

GPCYCLELO/GPCYCLEHI read as zero unless the cycle counter is enabled (SSR:CE), matching the hardware.

---
  target/hexagon/cpu.c       | 7 +++++--
  target/hexagon/op_helper.c | 9 ++-------
  2 files changed, 7 insertions(+), 9 deletions(-)
target/hexagon/op_helper.c +0 -0
index 23894ff3d28..2cea1927263 100644
@@ -1912,13 +1912,8 @@ uint64_t HELPER(greg_read_pair)(CPUHexagonState *env, uint32_t reg)
          return (uint64_t)(env->greg[reg]) |
                 (((uint64_t)(env->greg[reg + 1])) << 32);
      }

target/hexagon/op_helper.c uint64_t HELPER(greg_read_pair)(CPUHexagonState *env, uint32_t reg) · line 1917

Quoted 10 lines
target/hexagon/op_helper.c +2 -7
-    switch (reg) {
-    case HEX_GREG_GPCYCLELO:
-        return hexagon_get_sys_pcycle_count(env);
-    default:
-        return (uint64_t)hexagon_greg_read(env, reg) |
-               ((uint64_t)(hexagon_greg_read(env, reg + 1)) << 32);
-    }
+    return (uint64_t)hexagon_greg_read(env, reg) |
+           ((uint64_t)(hexagon_greg_read(env, reg + 1)) << 32);
  }

Just curious, does greg_read_pair() work with impair %reg?

author
#13 lore raw

Re: [PATCH v2 06/11] target/hexagon: gate GPCYCLE guest register reads on SSR:CE

Quoted 27 lines · Philippe Mathieu-Daudé

Hi Brian,

Quoted 24 lines · Brian Cain

GPCYCLELO/GPCYCLEHI read as zero unless the cycle counter is enabled (SSR:CE), matching the hardware.

---
  target/hexagon/cpu.c       | 7 +++++--
  target/hexagon/op_helper.c | 9 ++-------

  2 files changed, 7 insertions(+), 9 deletions(-)
target/hexagon/op_helper.c +0 -0
index 23894ff3d28..2cea1927263 100644
@@ -1912,13 +1912,8 @@ uint64_t 
 HELPER(greg_read_pair)(CPUHexagonState *env, uint32_t reg)

          return (uint64_t)(env->greg[reg]) | (((uint64_t)(env->greg[reg + 1])) << 32); }
-    switch (reg) {
-    case HEX_GREG_GPCYCLELO:
-        return hexagon_get_sys_pcycle_count(env); -    default:

Quoted 8 lines
Quoted 6 lines

-        return (uint64_t)hexagon_greg_read(env, reg) |
-               ((uint64_t)(hexagon_greg_read(env, reg + 1)) << 32); -    }
+    return (uint64_t)hexagon_greg_read(env, reg) |
+           ((uint64_t)(hexagon_greg_read(env, reg + 1)) << 32); }

Just curious, does greg_read_pair() work with impair %reg?

Not sure if I understand "impair %reg"?

But - if this refers to the odd-value pairs, then no - they don't.  The guest reg encoding only has the "g{odd}:{even}" designated by the even reg's value.  Sent a follow up patch to assert this in greg_read_pair().

-Brian

#14 lore raw

Re: [PATCH v2 06/11] target/hexagon: gate GPCYCLE guest register reads on SSR:CE

Quoted 39 lines · Brian Cain
Quoted 27 lines · Philippe Mathieu-Daudé

Hi Brian,

Quoted 24 lines · Brian Cain

GPCYCLELO/GPCYCLEHI read as zero unless the cycle counter is enabled (SSR:CE), matching the hardware.

---
  target/hexagon/cpu.c       | 7 +++++--
  target/hexagon/op_helper.c | 9 ++-------

  2 files changed, 7 insertions(+), 9 deletions(-)
target/hexagon/op_helper.c +0 -0
index 23894ff3d28..2cea1927263 100644
@@ -1912,13 +1912,8 @@ uint64_t HELPER(greg_read_pair) 
 (CPUHexagonState *env, uint32_t reg)

          return (uint64_t)(env->greg[reg]) | (((uint64_t)(env->greg[reg + 1])) << 32); }
-    switch (reg) {
-    case HEX_GREG_GPCYCLELO:
-        return hexagon_get_sys_pcycle_count(env); -    default:

Quoted 8 lines
Quoted 6 lines

-        return (uint64_t)hexagon_greg_read(env, reg) |
-               ((uint64_t)(hexagon_greg_read(env, reg + 1)) << 32); -    }
+    return (uint64_t)hexagon_greg_read(env, reg) |
+           ((uint64_t)(hexagon_greg_read(env, reg + 1)) << 32); }

Just curious, does greg_read_pair() work with impair %reg?

Quoted 3 lines

Not sure if I understand "impair %reg"?

But - if this refers to the odd-value pairs,

Yes I meant 'odd' ;)

Quoted 5 lines

then no - they don't.  The guest reg encoding only has the "g{odd}:{even}" designated by the even reg's value.  Sent a follow up patch to assert this in greg_read_pair().

-Brian

author
#15 lore raw

PATCH v2 7/11 target/hexagon: read UTIMERLO/UTIMERHI from the global timer

In reply to Brian Cain

In system mode, route accesses to the qtimer-backed global TIMERLO/TIMERHI so guest reads see a live, monotonically increasing timer.

In user mode we derive it from QEMU_CLOCK_VIRTUAL.

---
 target/hexagon/helper.h     |  4 ++++
 target/hexagon/genptr.c     | 29 +++++++++++++++++++++++++++++
 target/hexagon/op_helper.c  | 21 +++++++++++++++++++++
 tests/tcg/hexagon/reg_mut.c | 11 ++++++++---
 4 files changed, 62 insertions(+), 3 deletions(-)
target/hexagon/helper.h +4 -0
index 78dc28ca9e5..e39afd623b5 100644
@@ -113,6 +113,10 @@ DEF_HELPER_FLAGS_4(gvec_sabsdiff_w, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32)
 DEF_HELPER_FLAGS_4(gvec_uabsdiff_b, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32)
 DEF_HELPER_FLAGS_4(gvec_uabsdiff_h, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32)
 
+#if defined(CONFIG_USER_ONLY)
+DEF_HELPER_FLAGS_0(utimer, TCG_CALL_NO_RWG, i64)
+#endif
+
 #if !defined(CONFIG_USER_ONLY)
 DEF_HELPER_3(raise_stack_overflow, void, env, i32, i32)
 DEF_HELPER_2(swi, void, env, i32)
target/hexagon/genptr.c +29 -0
index 2a98b13b714..282a697d190 100644
@@ -411,6 +411,23 @@ static inline void gen_read_ctrl_reg(DisasContext *ctx, const int reg_num,
     } else if (reg_num == HEX_REG_QEMU_HVX_CNT) {
         tcg_gen_addi_tl(dest, hex_gpr[HEX_REG_QEMU_HVX_CNT],
                         ctx->num_hvx_insns);
+#ifndef CONFIG_USER_ONLY
+    } else if (reg_num == HEX_REG_UTIMERLO) {
+        gen_helper_sreg_read(dest, tcg_env,
+                             tcg_constant_i32(HEX_SREG_TIMERLO));
+    } else if (reg_num == HEX_REG_UTIMERHI) {
+        gen_helper_sreg_read(dest, tcg_env,
+                             tcg_constant_i32(HEX_SREG_TIMERHI));
+#else
+    } else if (reg_num == HEX_REG_UTIMERLO) {
+        TCGv_i64 utimer = tcg_temp_new_i64();
+        gen_helper_utimer(utimer);
+        tcg_gen_extrl_i64_i32(dest, utimer);
+    } else if (reg_num == HEX_REG_UTIMERHI) {
+        TCGv_i64 utimer = tcg_temp_new_i64();
+        gen_helper_utimer(utimer);
+        tcg_gen_extrh_i64_i32(dest, utimer);
+#endif
     } else {
         tcg_gen_mov_tl(dest, hex_gpr[reg_num]);
     }
@@ -439,6 +456,18 @@ static inline void gen_read_ctrl_reg_pair(DisasContext *ctx, const int reg_num,
         tcg_gen_addi_tl(hvx_cnt, hex_gpr[HEX_REG_QEMU_HVX_CNT],
                         ctx->num_hvx_insns);
         tcg_gen_concat_i32_i64(dest, hvx_cnt, hex_gpr[reg_num + 1]);
+#ifndef CONFIG_USER_ONLY
+    } else if (reg_num == HEX_REG_UTIMERLO) {
+        TCGv lo = tcg_temp_new();
+        TCGv hi = tcg_temp_new();
+        gen_helper_sreg_read(lo, tcg_env, tcg_constant_i32(HEX_SREG_TIMERLO));
+        gen_helper_sreg_read(hi, tcg_env, tcg_constant_i32(HEX_SREG_TIMERHI));
+        tcg_gen_concat_i32_i64(dest, lo, hi);
+#else
+    } else if (reg_num == HEX_REG_UTIMERLO) {
+        /* One helper call, so the pair is a coherent 64-bit snapshot. */
+        gen_helper_utimer(dest);
+#endif
     } else {
         tcg_gen_concat_i32_i64(dest,
             hex_gpr[reg_num],
target/hexagon/op_helper.c +21 -0
index 2cea1927263..df31ed2488a 100644
@@ -37,6 +37,9 @@
 #include "cpu_helper.h"
 #include "tcg/tcg-gvec-desc.h"
 #include "translate.h"
+#ifdef CONFIG_USER_ONLY
+#include "qemu/timer.h"
+#endif
 #ifndef CONFIG_USER_ONLY
 #include "hw/hexagon/hexagon_globalreg.h"
 #include "hex_mmu.h"
@@ -46,6 +49,24 @@
 #include "hexswi.h"
 #endif
 
+#ifdef CONFIG_USER_ONLY
+/*
+ * User mode has no qtimer device backing TIMERLO/TIMERHI, so derive the
+ * user timer directly from the virtual clock -- the same clock the qtimer
+ * counts -- at the qtimer's default 19.2MHz tick rate, masked to the
+ * qtimer's counter width.
+ */
+#define HEX_UTIMER_FREQ_HZ  19200000ULL
+#define HEX_UTIMER_CNT_MASK 0x00ffffffffffffffULL
+
+uint64_t HELPER(utimer)(void)
+{
+    return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
+                    HEX_UTIMER_FREQ_HZ, NANOSECONDS_PER_SECOND) &
+           HEX_UTIMER_CNT_MASK;
+}
+#endif
+
 #define SF_BIAS        127
 #define SF_MANTBITS    23
 
tests/tcg/hexagon/reg_mut.c +8 -3
index c5a39e55100..9ce18f3ebe5 100644
@@ -77,10 +77,10 @@ static inline void write_control_registers(void)
     check32(result, 0x00000000);
 
     WRITE_REG_NOCLOBBER(result, "utimerlo", 0xffffffff);
-    check32(result, 0x00000000);
+    check32_ne(result, 0xffffffff);
 
     WRITE_REG_NOCLOBBER(result, "utimerhi", 0xffffffff);
-    check32(result, 0x00000000);
+    check32_ne(result, 0xffffffff);
 
     /*
      * PC is special.  Setting it to these values
@@ -106,8 +106,13 @@ static inline void write_control_register_pairs(void)
     WRITE_REG_NOCLOBBER(result, "c15:14", 0xffffffffffffffff);
     check64(result, 0x0000000000000000);
 
+    /*
+     * c31:30 is UTIMERHI:UTIMERLO, a read-only free-running counter.  The
+     * write must be discarded; the read-back is whatever the timer says,
+     * so only check that the written value did not stick.
+     */
     WRITE_REG_NOCLOBBER(result, "c31:30", 0xffffffffffffffff);
-    check64(result, 0x0000000000000000);
+    check64_ne(result, 0xffffffffffffffff);
 
     WRITE_REG_PAIR_ENCODED(result, "c9:8", (uint64_t) 0x0000000000000000,
                            C9_8_EQ_R1_0);
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH v2 07/11] target/hexagon: read UTIMERLO/UTIMERHI from the global timer

Quoted 41 lines · Brian Cain

In system mode, route accesses to the qtimer-backed global TIMERLO/TIMERHI so guest reads see a live, monotonically increasing timer.

In user mode we derive it from QEMU_CLOCK_VIRTUAL.

---
  target/hexagon/helper.h     |  4 ++++
  target/hexagon/genptr.c     | 29 +++++++++++++++++++++++++++++
  target/hexagon/op_helper.c  | 21 +++++++++++++++++++++
  tests/tcg/hexagon/reg_mut.c | 11 ++++++++---
  4 files changed, 62 insertions(+), 3 deletions(-)
target/hexagon/op_helper.c +10 -0
index 2cea1927263..df31ed2488a 100644
@@ -37,6 +37,9 @@
  #include "cpu_helper.h"
  #include "tcg/tcg-gvec-desc.h"
  #include "translate.h"
+#ifdef CONFIG_USER_ONLY
+#include "qemu/timer.h"
+#endif
  #ifndef CONFIG_USER_ONLY
  #include "hw/hexagon/hexagon_globalreg.h"
  #include "hex_mmu.h"
@@ -46,6 +49,24 @@
  #include "hexswi.h"
  #endif
 
+#ifdef CONFIG_USER_ONLY
+/*
+ * User mode has no qtimer device backing TIMERLO/TIMERHI, so derive the
+ * user timer directly from the virtual clock -- the same clock the qtimer
+ * counts -- at the qtimer's default 19.2MHz tick rate, masked to the
+ * qtimer's counter width.
+ */

target/hexagon/op_helper.c line 68

Quoted 10 lines
target/hexagon/op_helper.c +10 -0
+#define HEX_UTIMER_FREQ_HZ  19200000ULL
+#define HEX_UTIMER_CNT_MASK 0x00ffffffffffffffULL
+
+uint64_t HELPER(utimer)(void)
+{
+    return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
+                    HEX_UTIMER_FREQ_HZ, NANOSECONDS_PER_SECOND) &
+           HEX_UTIMER_CNT_MASK;
+}
+#endif

Long term it could be easier to maintain a new user-specific file, adding it to hexagon_user_ss[] then target_user_arch (less #ifdefs).

author
#17 lore raw

PATCH v2 8/11 target/hexagon: raise imprecise exception on multi-TLB match

In reply to Brian Cain

tlbp now records a pending imprecise exception (via env->imprecise_exception) when the lookup matches multiple entries, and the translator raises it after the tlbp packet. Implement the HEX_EVENT_IMPRECISE delivery path so the guest sees HEX_CAUSE_IMPRECISE_MULTI_TLB_MATCH instead of the exception being silently dropped.

---
 include/hw/hexagon/hexagon_tlb.h |  3 ++-
 target/hexagon/cpu.h             |  1 +
 hw/hexagon/hexagon_tlb.c         |  5 ++++-
 target/hexagon/hex_mmu.c         |  5 ++++-
 target/hexagon/hexswi.c          | 37 +++++++++++++++++++++++++++++---
 target/hexagon/machine.c         |  5 +++--
 target/hexagon/translate.c       | 29 +++++++++++++++++++++++++
 7 files changed, 77 insertions(+), 8 deletions(-)
include/hw/hexagon/hexagon_tlb.h +2 -1
index 760dc1ea811..67c0a56b79e 100644
@@ -32,7 +32,8 @@ bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid,
                             int32_t *excp, int *cause_code, int mmu_idx);
 
 uint32_t hexagon_tlb_lookup(HexagonTLBState *tlb, uint32_t asid,
-                            uint32_t VA, int *cause_code);
+                            uint32_t VA, uint32_t *imprecise_exception,
+                            int *cause_code);
 
 int hexagon_tlb_check_overlap(HexagonTLBState *tlb, uint64_t entry,
                               uint64_t index);
target/hexagon/cpu.h +1 -0
index c50fbb3f72a..ed5671abe5c 100644
@@ -147,6 +147,7 @@ typedef struct CPUArchState {
     uint64_t t_cycle_count;
 #endif
     uint32_t next_PC;
+    uint32_t imprecise_exception;
     target_ulong new_value_usr;
 
     MemLog mem_log_stores[STORES_MAX];
hw/hexagon/hexagon_tlb.c +4 -1
index b6d4aff389e..6539458f25f 100644
@@ -319,15 +319,18 @@ bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid,
 }
 
 uint32_t hexagon_tlb_lookup(HexagonTLBState *tlb, uint32_t asid,
-                            uint32_t VA, int *cause_code)
+                            uint32_t VA, uint32_t *imprecise_exception,
+                            int *cause_code)
 {
     uint32_t not_found = 0x80000000;
     uint32_t idx = not_found;
 
+    *imprecise_exception = 0;
     for (uint32_t i = 0; i < tlb->num_entries; i++) {
         uint64_t entry = tlb->entries[i];
         if (hex_tlb_entry_match_noperm(entry, asid, VA)) {
             if (idx != not_found) {
+                *imprecise_exception = HEX_EVENT_IMPRECISE;
                 *cause_code = HEX_CAUSE_IMPRECISE_MULTI_TLB_MATCH;
                 break;
             }
target/hexagon/hex_mmu.c +4 -1
index 81d64a6146c..de108f709c0 100644
@@ -86,9 +86,12 @@ uint32_t hex_tlb_lookup(CPUHexagonState *env, uint32_t ssr, uint32_t VA)
 {
     HexagonCPU *cpu = env_archcpu(env);
     uint8_t asid = GET_SSR_FIELD(SSR_ASID, ssr);
+    uint32_t imprecise_exception = 0;
     int cause_code = 0;
 
-    uint32_t result = hexagon_tlb_lookup(cpu->tlb, asid, VA, &cause_code);
+    uint32_t result = hexagon_tlb_lookup(cpu->tlb, asid, VA,
+                                         &imprecise_exception, &cause_code);
+    env->imprecise_exception = imprecise_exception;
     if (cause_code) {
         env->cause_code = cause_code;
     }
target/hexagon/hexswi.c +34 -3
index 43c373ea2ee..d29c12e6481 100644
@@ -69,6 +69,7 @@ void hexagon_cpu_do_interrupt(CPUState *cs)
 
 {
     CPUHexagonState *env = cpu_env(cs);
+    HexagonCPU *cpu = HEXAGON_CPU(cs);
     uint32_t ssr;
 
     BQL_LOCK_GUARD();
@@ -83,7 +84,6 @@ void hexagon_cpu_do_interrupt(CPUState *cs)
 
     ssr = env->t_sreg[HEX_SREG_SSR];
     if (GET_SSR_FIELD(SSR_EX, ssr) == 1) {
-        HexagonCPU *cpu = env_archcpu(env);
         if (cpu->globalregs) {
             hexagon_globalreg_write(cpu->globalregs, HEX_SREG_DIAG,
                                     env->cause_code, env->threadId);
@@ -243,8 +243,39 @@ void hexagon_cpu_do_interrupt(CPUState *cs)
         break;
 
     case HEX_EVENT_IMPRECISE:
-        qemu_log_mask(LOG_UNIMP,
-                "Imprecise exception: this case is not yet handled");
+        if (get_exe_mode(env) == HEX_EXE_MODE_WAIT) {
+            env->gpr[HEX_REG_PC] = env->wait_next_pc - 4;
+            clear_wait_mode(env);
+        }
+        switch (env->cause_code) {
+        case HEX_CAUSE_IMPRECISE_MULTI_TLB_MATCH:
+            hexagon_ssr_set_cause(env, env->cause_code);
+            set_addresses(env, 4, cs->exception_index);
+            if (cpu->globalregs) {
+                hexagon_globalreg_write(cpu->globalregs, HEX_SREG_DIAG,
+                    (0x4 << 4) | (env->t_sreg[HEX_SREG_HTID] & 0xF),
+                    env->threadId);
+            }
+            break;
+
+        case HEX_CAUSE_IMPRECISE_NMI:
+            hexagon_ssr_set_cause(env, env->cause_code);
+            set_addresses(env, 4, cs->exception_index);
+            if (cpu->globalregs) {
+                hexagon_globalreg_write(cpu->globalregs, HEX_SREG_DIAG,
+                    (0x3 << 4) | (env->t_sreg[HEX_SREG_HTID] & 0xF),
+                    env->threadId);
+            }
+            break;
+
+        default:
+            qemu_log_mask(LOG_GUEST_ERROR,
+                    "Imprecise exception with unhandled cause 0x%x\n",
+                    env->cause_code);
+            hexagon_ssr_set_cause(env, env->cause_code);
+            set_addresses(env, 4, cs->exception_index);
+            break;
+        }
         break;
 
     default:
target/hexagon/machine.c +3 -2
index 2dd95466e7d..bf4646f4a8b 100644
@@ -10,8 +10,8 @@
 
 const VMStateDescription vmstate_hexagon_cpu = {
     .name = "cpu",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .fields = (const VMStateField[]) {
         VMSTATE_UINT32_ARRAY(env.gpr, HexagonCPU, TOTAL_PER_THREAD_REGS),
         VMSTATE_UINT32_ARRAY(env.pred, HexagonCPU, NUM_PREGS),
@@ -26,6 +26,7 @@ const VMStateDescription vmstate_hexagon_cpu = {
         VMSTATE_UINT32(env.cause_code, HexagonCPU),
         VMSTATE_UINT32(env.wait_next_pc, HexagonCPU),
         VMSTATE_UINT64(env.t_cycle_count, HexagonCPU),
+        VMSTATE_UINT32(env.imprecise_exception, HexagonCPU),
 
         VMSTATE_END_OF_LIST()
     },
target/hexagon/translate.c +29 -0
index 5cfa60ca302..1d28f2db0fc 100644
@@ -65,6 +65,7 @@ TCGv hex_llsc_val;
 TCGv_i64 hex_llsc_val_i64;
 #ifndef CONFIG_USER_ONLY
 TCGv_i64 hex_cycle_count;
+TCGv hex_imprecise_exception;
 #endif
 TCGv hex_vstore_addr[VSTORES_MAX];
 TCGv hex_vstore_size[VSTORES_MAX];
@@ -1051,6 +1052,28 @@ static void update_exec_counters(DisasContext *ctx)
     ctx->num_cycles += PCYCLES_PER_PACKET;
 }
 
+#ifndef CONFIG_USER_ONLY
+/*
+ * A tlbp instruction may detect multiple TLB matches and set a pending
+ * imprecise exception.  Raise it after the packet that ran the tlbp.
+ */
+static void check_imprecise_exception(Packet *pkt)
+{
+    for (int i = 0; i < pkt->num_insns; i++) {
+        if (pkt->insn[i].opcode == Y2_tlbp) {
+            TCGv PC = tcg_constant_tl(pkt->pc);
+            TCGLabel *label = gen_new_label();
+            tcg_gen_brcondi_tl(TCG_COND_EQ, hex_imprecise_exception,
+                               0, label);
+            gen_helper_raise_exception(tcg_env,
+                                       hex_imprecise_exception, PC);
+            gen_set_label(label);
+            return;
+        }
+    }
+}
+#endif
+
 static void gen_commit_packet(DisasContext *ctx)
 {
     /*
@@ -1150,6 +1173,10 @@ static void gen_commit_packet(DisasContext *ctx)
         ctx->pkt.vhist_insn->generate(ctx);
     }
 
+#ifndef CONFIG_USER_ONLY
+    check_imprecise_exception(&ctx->pkt);
+#endif
+
     if (ctx->pkt_ends_tb || ctx->base.is_jmp == DISAS_NORETURN) {
         gen_end_tb(ctx);
     }
@@ -1357,6 +1384,8 @@ void hexagon_translate_init(void)
 #ifndef CONFIG_USER_ONLY
     hex_cycle_count = tcg_global_mem_new_i64(tcg_env,
         offsetof(CPUHexagonState, t_cycle_count), "t_cycle_count");
+    hex_imprecise_exception = tcg_global_mem_new(tcg_env,
+        offsetof(CPUHexagonState, imprecise_exception), "imprecise_exception");
 #endif
     for (i = 0; i < STORES_MAX; i++) {
         snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i);
Signature · 1 line
2.34.1
author
#18 lore raw

PATCH v2 9/11 target/hexagon: implement direct-to-guest interrupt delivery

In reply to Brian Cain

Interrupts 3 through 5 are routed to guest mode when CCR:GIE and the matching CCR:VV bit are set. Enter through GEVB rather than EVB, record the pre-entry state in GSR and the return address in GELR, and read the vector ID from the l2vic.

---
 target/hexagon/reg_fields_def.h.inc |  7 +++
 target/hexagon/cpu.c                |  5 ++
 target/hexagon/hex_interrupts.c     | 91 ++++++++++++++++++++++++++---
 3 files changed, 96 insertions(+), 7 deletions(-)
target/hexagon/reg_fields_def.h.inc +7 -0
index d2c706d56b5..29497fbcc4d 100644
@@ -136,6 +136,13 @@ DEF_REG_FIELD(CCR_VV1, 29, 1)
 DEF_REG_FIELD(CCR_VV2, 30, 1)
 DEF_REG_FIELD(CCR_VV3, 31, 1)
 
+/* GSR fields */
+DEF_REG_FIELD(GSR_CAUSE, 0, 16)
+DEF_REG_FIELD(GSR_CFI, 28, 1)
+DEF_REG_FIELD(GSR_SS, 29, 1)
+DEF_REG_FIELD(GSR_IE, 30, 1)
+DEF_REG_FIELD(GSR_UM, 31, 1)
+
 /* ISDB ST fields */
 DEF_REG_FIELD(ISDBST_WAITRUN, 24, 8)
 DEF_REG_FIELD(ISDBST_ONOFF, 16, 8)
target/hexagon/cpu.c +5 -0
index 0a677840bcb..efaf569c6f8 100644
@@ -475,6 +475,11 @@ static void hexagon_cpu_realize(DeviceState *dev, Error **errp)
         error_setg(errp, "hexagon cpu requires 'tlb' link property to be set");
         return;
     }
+    if (!HEXAGON_CPU(dev)->l2vic) {
+        error_setg(errp,
+                   "hexagon cpu requires 'l2vic' link property to be set");
+        return;
+    }
 #endif
 
     qemu_init_vcpu(cs);
target/hexagon/hex_interrupts.c +84 -7

124 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v2 9/11
#19 lore raw

Re: [PATCH v2 09/11] target/hexagon: implement direct-to-guest interrupt delivery

Quoted 5 lines · Brian Cain

Interrupts 3 through 5 are routed to guest mode when CCR:GIE and the matching CCR:VV bit are set. Enter through GEVB rather than EVB, record the pre-entry state in GSR and the return address in GELR, and read the vector ID from the l2vic.

Quoted 7 lines
---
 target/hexagon/reg_fields_def.h.inc |  7 +++
 target/hexagon/cpu.c                |  5 ++
 target/hexagon/hex_interrupts.c     | 91 ++++++++++++++++++++++++++---
 3 files changed, 96 insertions(+), 7 deletions(-)