Skip to contentSkip to conversation

Hexagon: implement nmi, pause

PATCH In review Brian Cain 2 participants 8 messages Oct 5

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

cover Hexagon: implement nmi, pause

nmi and pause instructions were nops. Now nmi raises interrupts on all of the enabled CPUs and pause raises an EXCP_YIELD.

Brian Cain (2):
target/hexagon: yield on system-mode pause
target/hexagon: implement nmi

 target/hexagon/cpu.h        |  1 +
 target/hexagon/cpu_helper.h |  1 +
 target/hexagon/gen_tcg.h    | 10 ++++++++++
 target/hexagon/sys_macros.h |  3 +--
 target/hexagon/cpu.c        | 11 ++++++++++-
 target/hexagon/cpu_helper.c | 17 +++++++++++++++++
 6 files changed, 40 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 target/hexagon: yield on system-mode pause

System-mode pause currently advances the PC and continues execution instead of yielding the vCPU. Raise EXCP_YIELD with the next packet PC so execution returns to the CPU loop and another hardware thread can run. Keep the linux-user behavior unchanged.

---
 target/hexagon/gen_tcg.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
target/hexagon/gen_tcg.h +10 -0
index 5f7df66ae67..80d8a7b6bac 100644
@@ -1125,11 +1125,21 @@
 #define fGEN_TCG_Y2_break(SHORTCODE)
 #define fGEN_TCG_J2_unpause(SHORTCODE)
 
+#ifdef CONFIG_USER_ONLY
 #define fGEN_TCG_J2_pause(SHORTCODE) \
     do { \
         uiV = uiV; \
         tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->next_PC); \
     } while (0)
+#else
+#define fGEN_TCG_J2_pause(SHORTCODE) \
+    do { \
+        uiV = uiV; \
+        gen_helper_raise_exception(tcg_env, \
+            tcg_constant_i32(EXCP_YIELD), \
+            tcg_constant_i32(ctx->next_PC)); \
+    } while (0)
+#endif
 
 /* r0 = asr(r1, r2):sat */
 #define fGEN_TCG_S2_asr_r_r_sat(SHORTCODE) \
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] target/hexagon: yield on system-mode pause

Quoted 26 lines · Brian Cain

System-mode pause currently advances the PC and continues execution instead of yielding the vCPU. Raise EXCP_YIELD with the next packet PC so execution returns to the CPU loop and another hardware thread can run. Keep the linux-user behavior unchanged.

---
 target/hexagon/gen_tcg.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
target/hexagon/gen_tcg.h +3 -0
index 5f7df66ae67..80d8a7b6bac 100644
@@ -1125,11 +1125,21 @@
 #define fGEN_TCG_Y2_break(SHORTCODE)
 #define fGEN_TCG_J2_unpause(SHORTCODE)
 
+#ifdef CONFIG_USER_ONLY
 #define fGEN_TCG_J2_pause(SHORTCODE) \
     do { \
         uiV = uiV; \
         tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->next_PC); \
     } while (0)
+#else
+#define fGEN_TCG_J2_pause(SHORTCODE) \

target/hexagon/gen_tcg.h line 1145

Quoted 10 lines
target/hexagon/gen_tcg.h +7 -0
+    do { \
+        uiV = uiV; \
+        gen_helper_raise_exception(tcg_env, \
+            tcg_constant_i32(EXCP_YIELD), \
+            tcg_constant_i32(ctx->next_PC)); \
+    } while (0)
+#endif
 
 /* r0 = asr(r1, r2):sat */
 #define fGEN_TCG_S2_asr_r_r_sat(SHORTCODE) \

How is this working at the moment?
In system mode, if a given cpu never really yield, a given "thread" could keep it spinning with a busy loop forever.

author
#3 lore raw

Re: [PATCH 1/2] target/hexagon: yield on system-mode pause

Quoted 35 lines · Pierrick Bouvier
Quoted 34 lines · Brian Cain

System-mode pause currently advances the PC and continues execution instead of yielding the vCPU. Raise EXCP_YIELD with the next packet PC so execution returns to the CPU loop and another hardware thread can run. Keep the linux-user behavior unchanged.

---
  target/hexagon/gen_tcg.h | 10 ++++++++++
  1 file changed, 10 insertions(+)
target/hexagon/gen_tcg.h +10 -0
index 5f7df66ae67..80d8a7b6bac 100644
@@ -1125,11 +1125,21 @@
  #define fGEN_TCG_Y2_break(SHORTCODE)
  #define fGEN_TCG_J2_unpause(SHORTCODE)
 
+#ifdef CONFIG_USER_ONLY
  #define fGEN_TCG_J2_pause(SHORTCODE) \
      do { \
          uiV = uiV; \
          tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->next_PC); \
      } while (0)
+#else
+#define fGEN_TCG_J2_pause(SHORTCODE) \
+    do { \
+        uiV = uiV; \
+        gen_helper_raise_exception(tcg_env, \
+            tcg_constant_i32(EXCP_YIELD), \
+            tcg_constant_i32(ctx->next_PC)); \
+    } while (0)
+#endif
 
Quoted 5 lines

target/hexagon/gen_tcg.h line 1145

Quoted 2 lines
target/hexagon/gen_tcg.h +0 -0
  /* r0 = asr(r1, r2):sat */
  #define fGEN_TCG_S2_asr_r_r_sat(SHORTCODE) \

How is this working at the moment?
In system mode, if a given cpu never really yield, a given "thread" could keep it spinning with a busy loop forever.

The wait() instruction is the instruction that halts-until-an-interrupt.  That instruction has a working implementation already.

This instruction, pause(), resembles the x86_64 pause / arm yield instructions.  It delays a number of cycles (based on the reg operand) before continuing execution.  The baseline's nop behavior is arguably fine/correct.  But this behavior is better - especially for round-robin.

In practice, few if any of the tests we're running so far make use of the pause() instruction or spin loops.  But some of the soon-to-be-enabled tests do use it, so that's some of the incentive to implement it here.

Reviewed-by
#4 lore raw

Re: [PATCH 1/2] target/hexagon: yield on system-mode pause

Quoted 51 lines · Brian Cain
Quoted 32 lines · Pierrick Bouvier
Quoted 31 lines · Brian Cain

System-mode pause currently advances the PC and continues execution instead
of yielding the vCPU. Raise EXCP_YIELD with the next packet PC so execution
returns to the CPU loop and another hardware thread can run. Keep the linux-user behavior unchanged.

---
  target/hexagon/gen_tcg.h | 10 ++++++++++

  1 file changed, 10 insertions(+)
target/hexagon/gen_tcg.h +0 -0
index 5f7df66ae67..80d8a7b6bac 100644
@@ -1125,11 +1125,21 @@
   #define fGEN_TCG_Y2_break(SHORTCODE)
   #define fGEN_TCG_J2_unpause(SHORTCODE)

  +#ifdef CONFIG_USER_ONLY
  #define fGEN_TCG_J2_pause(SHORTCODE) \
      do { \
          uiV = uiV; \
          tcg_gen_movi_tl(hex_gpr[HEX_REG_PC], ctx->next_PC); \
      } while (0)

target/hexagon/gen_tcg.h +6 -0
+#else
+#define fGEN_TCG_J2_pause(SHORTCODE) \
+    do { \
+        uiV = uiV; \
+        gen_helper_raise_exception(tcg_env, \
+            tcg_constant_i32(EXCP_YIELD), \
Quoted 8 lines
Quoted 5 lines

+            tcg_constant_i32(ctx->next_PC)); \
+    } while (0)
+#endif
    /* r0 = asr(r1, r2):sat */
  #define fGEN_TCG_S2_asr_r_r_sat(SHORTCODE) \

How is this working at the moment?
In system mode, if a given cpu never really yield, a given "thread" could keep it spinning with a busy loop forever.

The wait() instruction is the instruction that halts-until-an- interrupt.  That instruction has a working implementation already.

This instruction, pause(), resembles the x86_64 pause / arm yield instructions.  It delays a number of cycles (based on the reg operand) before continuing execution.  The baseline's nop behavior is arguably fine/correct.  But this behavior is better - especially for round-robin.

Quoted 5 lines

In practice, few if any of the tests we're running so far make use of the pause() instruction or spin loops.  But some of the soon-to-be- enabled tests do use it, so that's some of the incentive to implement it here.

Sounds good, thanks.

author
#5 lore raw

PATCH 2/2 target/hexagon: implement nmi

In reply to Brian Cain

Y4_nmi was a no-op, let's fill in the missing implementation.

We'll raise CPU_INTERRUPT_NMI on each running thread in the mask.

---
 target/hexagon/cpu.h        |  1 +
 target/hexagon/cpu_helper.h |  1 +
 target/hexagon/sys_macros.h |  3 +--
 target/hexagon/cpu.c        | 11 ++++++++++-
 target/hexagon/cpu_helper.c | 17 +++++++++++++++++
 5 files changed, 30 insertions(+), 3 deletions(-)
target/hexagon/cpu.h +1 -0
index df4c0182247..773fa45c52a 100644
@@ -59,6 +59,7 @@ typedef struct HexagonHVXContextState HexagonHVXContextState;
 #define CPU_INTERRUPT_SWI      CPU_INTERRUPT_TGT_INT_0
 #define CPU_INTERRUPT_K0_UNLOCK CPU_INTERRUPT_TGT_INT_1
 #define CPU_INTERRUPT_TLB_UNLOCK CPU_INTERRUPT_TGT_INT_2
+#define CPU_INTERRUPT_NMI CPU_INTERRUPT_TGT_EXT_0
 
 #define HEX_CPU_MODE_USER    1
 #define HEX_CPU_MODE_GUEST   2
target/hexagon/cpu_helper.h +1 -0
index afb4df59f80..95a8f6587b6 100644
@@ -22,6 +22,7 @@ void clear_wait_mode(CPUHexagonState *env);
 void hexagon_ssr_set_cause(CPUHexagonState *env, uint32_t cause);
 void hexagon_start_threads(CPUHexagonState *env, uint32_t mask);
 void hexagon_stop_thread(CPUHexagonState *env);
+void hexagon_nmi(CPUHexagonState *env, uint32_t thread_mask);
 void hexagon_resume_threads(CPUHexagonState *env, uint32_t mask);
 uint64_t hexagon_get_sys_pcycle_count(CPUHexagonState *env);
 uint32_t hexagon_get_sys_pcycle_count_high(CPUHexagonState *env);
target/hexagon/sys_macros.h +1 -2
index a5b9b7f2592..1a4513d66f8 100644
@@ -253,7 +253,6 @@
 
 #define NUM_TLB_REGS(x) (hexagon_tlb_get_num_entries(env_archcpu(env)->tlb))
 
-/* NMI routing not yet implemented; Y4_nmi is a no-op for now */
-#define fDO_NMI(THREAD_MASK) do { } while (0)
+#define fDO_NMI(THREAD_MASK) hexagon_nmi(env, THREAD_MASK)
 
 #endif
target/hexagon/cpu.c +10 -1
index 1f7d8a8df70..ba05cba2be7 100644
@@ -393,7 +393,8 @@ static bool hexagon_cpu_has_work(CPUState *cs)
 
     return hexagon_thread_is_enabled(env) &&
         (cs->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_SWI
-            | CPU_INTERRUPT_K0_UNLOCK | CPU_INTERRUPT_TLB_UNLOCK));
+            | CPU_INTERRUPT_K0_UNLOCK | CPU_INTERRUPT_TLB_UNLOCK
+            | CPU_INTERRUPT_NMI));
 }
 #endif
 
@@ -754,6 +755,14 @@ static const struct SysemuCPUOps hexagon_sysemu_ops = {
 static bool hexagon_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
 {
     CPUHexagonState *env = cpu_env(cs);
+    if (interrupt_request & CPU_INTERRUPT_NMI) {
+        cpu_reset_interrupt(cs, CPU_INTERRUPT_NMI);
+        cs->halted = false;
+        cs->exception_index = HEX_EVENT_IMPRECISE;
+        env->cause_code = HEX_CAUSE_IMPRECISE_NMI;
+        hexagon_cpu_do_interrupt(cs);
+        return true;
+    }
     if (interrupt_request & CPU_INTERRUPT_TLB_UNLOCK) {
         cs->halted = false;
         cpu_reset_interrupt(cs, CPU_INTERRUPT_TLB_UNLOCK);
target/hexagon/cpu_helper.c +17 -0
index cf0762ff05f..af5167e4eeb 100644
@@ -547,6 +547,23 @@ void hexagon_stop_thread(CPUHexagonState *env)
     }
 }
 
+void hexagon_nmi(CPUHexagonState *env, uint32_t thread_mask)
+{
+    CPUState *cs;
+
+    BQL_LOCK_GUARD();
+
+    CPU_FOREACH(cs) {
+        CPUHexagonState *thread = cpu_env(cs);
+
+        if (!(thread_mask & (1u << thread->threadId)) ||
+            get_exe_mode(thread) == HEX_EXE_MODE_OFF) {
+            continue;
+        }
+        cpu_interrupt(cs, CPU_INTERRUPT_NMI);
+    }
+}
+
 static int sys_in_monitor_mode_ssr(uint32_t ssr)
 {
     if ((GET_SSR_FIELD(SSR_EX, ssr) != 0) ||
Signature · 1 line
2.34.1
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] target/hexagon: implement nmi

Quoted 4 lines · Brian Cain

Y4_nmi was a no-op, let's fill in the missing implementation.

We'll raise CPU_INTERRUPT_NMI on each running thread in the mask.

Quoted 9 lines
---
 target/hexagon/cpu.h        |  1 +
 target/hexagon/cpu_helper.h |  1 +
 target/hexagon/sys_macros.h |  3 +--
 target/hexagon/cpu.c        | 11 ++++++++++-
 target/hexagon/cpu_helper.c | 17 +++++++++++++++++
 5 files changed, 30 insertions(+), 3 deletions(-)
author
#7 lore raw
In reply to Brian Cain
Quoted 2 lines · Brian Cain

nmi and pause instructions were nops. Now nmi raises interrupts on all of the enabled CPUs and pause raises an EXCP_YIELD.

Whoops - didn't take enough time to write this message clearly:

    all of the enabled CPUs ... in the nmi mask operand.

Quoted 13 lines

Brian Cain (2):
target/hexagon: yield on system-mode pause
target/hexagon: implement nmi

  target/hexagon/cpu.h        |  1 +
  target/hexagon/cpu_helper.h |  1 +
  target/hexagon/gen_tcg.h    | 10 ++++++++++
  target/hexagon/sys_macros.h |  3 +--
  target/hexagon/cpu.c        | 11 ++++++++++-
  target/hexagon/cpu_helper.c | 17 +++++++++++++++++
  6 files changed, 40 insertions(+), 3 deletions(-)