Skip to contentSkip to conversation

Hexagon: fix icinva

PATCH In review Brian Cain 3 participants 8 messages Aug 19 – Aug 20

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

cover Hexagon: fix icinva

icinva (icache invalidate addr) instruction was a nop, but this behavior is not correct for userspace.

Also note that this checkpatch issue was bypassed because I believe it's appropriate/acceptable to check the "guest" page size in a TCG test case.

Patch 1/2 has no obvious style problems and is ready for submission. 2/2 Checking commit ea94abbfc875 (tests/tcg/hexagon: add icinva test)
ERROR: use qemu_real_host_page_size() instead of sysconf(_SC_PAGESIZE) #74: FILE: tests/tcg/hexagon/icinva.c:56:
+ long pagesize = sysconf(_SC_PAGESIZE);

Brian Cain (2):
target/hexagon: invalidate translated code for user icinva tests/tcg/hexagon: add icinva test

 target/hexagon/gen_tcg.h          |  9 ++++
 target/hexagon/helper.h           |  4 ++
 target/hexagon/op_helper.c        | 15 +++++++
 target/hexagon/translate.c        |  3 ++
 tests/tcg/hexagon/icinva.c        | 71 +++++++++++++++++++++++++++++++
 tests/tcg/hexagon/Makefile.target |  2 +
 6 files changed, 104 insertions(+)
 create mode 100644 tests/tcg/hexagon/icinva.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 target/hexagon: invalidate translated code for user icinva

Route linux-user icinva through a helper that invalidates the addressed icache line under mmap_lock

For system mode icinva has nothing left to do, so it stays a nop.

---
 target/hexagon/gen_tcg.h   |  9 +++++++++
 target/hexagon/helper.h    |  4 ++++
 target/hexagon/op_helper.c | 15 +++++++++++++++
 target/hexagon/translate.c |  3 +++
 4 files changed, 31 insertions(+)
target/hexagon/gen_tcg.h +9 -0
index 40e03781d36..1d25391282a 100644
@@ -497,8 +497,17 @@
     do { RsV = RsV; } while (0)
 #define fGEN_TCG_Y2_dccleana(SHORTCODE) \
     do { RsV = RsV; } while (0)
+
+#ifdef CONFIG_USER_ONLY
+#define fGEN_TCG_Y2_icinva(SHORTCODE) \
+    gen_helper_insn_cache_op(tcg_env, RsV, \
+                             tcg_constant_tl(insn->slot), \
+                             tcg_constant_tl(ctx->mem_idx), \
+                             tcg_constant_tl(ctx->pkt.pc))
+#else
 #define fGEN_TCG_Y2_icinva(SHORTCODE) \
     do { RsV = RsV; } while (0)
+#endif
 
 /*
  * allocframe(#uiV)
target/hexagon/helper.h +4 -0
index 78dc28ca9e5..a5a5c123533 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_5(insn_cache_op, void, env, i32, int, int, i32)
+#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/op_helper.c +15 -0
index 23894ff3d28..84f5564b1e6 100644
@@ -23,6 +23,9 @@
 #include "qemu/main-loop.h"
 #include "cpu.h"
 #include "exec/helper-proto.h"
+#include "exec/mmap-lock.h"
+#include "exec/target_page.h"
+#include "exec/translation-block.h"
 #include "fpu/softfloat.h"
 #include "exec/cpu-interrupt.h"
 #include "internal.h"
@@ -311,6 +314,18 @@ int32_t HELPER(vacsh_pred)(CPUHexagonState *env,
     return PeV;
 }
 
+#ifdef CONFIG_USER_ONLY
+void HELPER(insn_cache_op)(CPUHexagonState *env, target_ulong RsV,
+                           int slot, int mmu_idx, target_ulong PC)
+{
+    target_ulong start = RsV & ~31;
+
+    mmap_lock();
+    tb_invalidate_phys_range(env_cpu(env), start, start + 31);
+    mmap_unlock();
+}
+#endif
+
 int64_t HELPER(cabacdecbin_val)(int64_t RssV, int64_t RttV)
 {
     int64_t RddV = 0;
target/hexagon/translate.c +3 -0
index 06a8159d283..1560c412732 100644
@@ -354,6 +354,9 @@ static bool pkt_ends_tb(Packet *pkt)
     if (pkt->pkt_has_cof) {
         return true;
     }
+    if (check_for_attrib(pkt, A_ICFLUSHOP)) {
+        return true;
+    }
 #ifndef CONFIG_USER_ONLY
     /* System mode instructions that end TLB */
     if (check_for_opcode(pkt, Y2_swi) ||
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] target/hexagon: invalidate translated code for user icinva

Quoted 5 lines · Brian Cain

Route linux-user icinva through a helper that invalidates the addressed icache line under mmap_lock

For system mode icinva has nothing left to do, so it stays a nop.

Why does system mode icinva is a nop?
Shouldn't we invalidate translated code also?

Quoted 92 lines
---
 target/hexagon/gen_tcg.h   |  9 +++++++++
 target/hexagon/helper.h    |  4 ++++
 target/hexagon/op_helper.c | 15 +++++++++++++++
 target/hexagon/translate.c |  3 +++
 4 files changed, 31 insertions(+)
target/hexagon/gen_tcg.h +9 -0
index 40e03781d36..1d25391282a 100644
@@ -497,8 +497,17 @@
     do { RsV = RsV; } while (0)
 #define fGEN_TCG_Y2_dccleana(SHORTCODE) \
     do { RsV = RsV; } while (0)
+
+#ifdef CONFIG_USER_ONLY
+#define fGEN_TCG_Y2_icinva(SHORTCODE) \
+    gen_helper_insn_cache_op(tcg_env, RsV, \
+                             tcg_constant_tl(insn->slot), \
+                             tcg_constant_tl(ctx->mem_idx), \
+                             tcg_constant_tl(ctx->pkt.pc))
+#else
 #define fGEN_TCG_Y2_icinva(SHORTCODE) \
     do { RsV = RsV; } while (0)
+#endif
 
 /*
  * allocframe(#uiV)
target/hexagon/helper.h +4 -0
index 78dc28ca9e5..a5a5c123533 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_5(insn_cache_op, void, env, i32, int, int, i32)
+#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/op_helper.c +15 -0
index 23894ff3d28..84f5564b1e6 100644
@@ -23,6 +23,9 @@
 #include "qemu/main-loop.h"
 #include "cpu.h"
 #include "exec/helper-proto.h"
+#include "exec/mmap-lock.h"
+#include "exec/target_page.h"
+#include "exec/translation-block.h"
 #include "fpu/softfloat.h"
 #include "exec/cpu-interrupt.h"
 #include "internal.h"
@@ -311,6 +314,18 @@ int32_t HELPER(vacsh_pred)(CPUHexagonState *env,
     return PeV;
 }
 
+#ifdef CONFIG_USER_ONLY
+void HELPER(insn_cache_op)(CPUHexagonState *env, target_ulong RsV,
+                           int slot, int mmu_idx, target_ulong PC)
+{
+    target_ulong start = RsV & ~31;
+
+    mmap_lock();
+    tb_invalidate_phys_range(env_cpu(env), start, start + 31);
+    mmap_unlock();
+}
+#endif
+
 int64_t HELPER(cabacdecbin_val)(int64_t RssV, int64_t RttV)
 {
     int64_t RddV = 0;
target/hexagon/translate.c +3 -0
index 06a8159d283..1560c412732 100644
@@ -354,6 +354,9 @@ static bool pkt_ends_tb(Packet *pkt)
     if (pkt->pkt_has_cof) {
         return true;
     }
+    if (check_for_attrib(pkt, A_ICFLUSHOP)) {
+        return true;
+    }
 #ifndef CONFIG_USER_ONLY
     /* System mode instructions that end TLB */
     if (check_for_opcode(pkt, Y2_swi) ||
author
#3 lore raw

Re: [PATCH 1/2] target/hexagon: invalidate translated code for user icinva

Quoted 8 lines · Pierrick Bouvier
Quoted 5 lines · Brian Cain

Route linux-user icinva through a helper that invalidates the addressed icache line under mmap_lock

For system mode icinva has nothing left to do, so it stays a nop.

Why does system mode icinva is a nop?
Shouldn't we invalidate translated code also?

The normal behavior of instruction translation in QEMU is conservative relative to the ISA constraint.  QEMU will invalidate translated blocks that become dirty/stale from a store instruction, so icinva can't flush anything: it's already flushed.

Quoted 92 lines
Quoted 92 lines
---
  target/hexagon/gen_tcg.h   |  9 +++++++++
  target/hexagon/helper.h    |  4 ++++
  target/hexagon/op_helper.c | 15 +++++++++++++++
  target/hexagon/translate.c |  3 +++
  4 files changed, 31 insertions(+)
target/hexagon/gen_tcg.h +9 -0
index 40e03781d36..1d25391282a 100644
@@ -497,8 +497,17 @@
      do { RsV = RsV; } while (0)
  #define fGEN_TCG_Y2_dccleana(SHORTCODE) \
      do { RsV = RsV; } while (0)
+
+#ifdef CONFIG_USER_ONLY
+#define fGEN_TCG_Y2_icinva(SHORTCODE) \
+    gen_helper_insn_cache_op(tcg_env, RsV, \
+                             tcg_constant_tl(insn->slot), \
+                             tcg_constant_tl(ctx->mem_idx), \
+                             tcg_constant_tl(ctx->pkt.pc))
+#else
  #define fGEN_TCG_Y2_icinva(SHORTCODE) \
      do { RsV = RsV; } while (0)
+#endif
 
  /*
   * allocframe(#uiV)
target/hexagon/helper.h +4 -0
index 78dc28ca9e5..a5a5c123533 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_5(insn_cache_op, void, env, i32, int, int, i32)
+#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/op_helper.c +15 -0
index 23894ff3d28..84f5564b1e6 100644
@@ -23,6 +23,9 @@
  #include "qemu/main-loop.h"
  #include "cpu.h"
  #include "exec/helper-proto.h"
+#include "exec/mmap-lock.h"
+#include "exec/target_page.h"
+#include "exec/translation-block.h"
  #include "fpu/softfloat.h"
  #include "exec/cpu-interrupt.h"
  #include "internal.h"
@@ -311,6 +314,18 @@ int32_t HELPER(vacsh_pred)(CPUHexagonState *env,
      return PeV;
  }
 
+#ifdef CONFIG_USER_ONLY
+void HELPER(insn_cache_op)(CPUHexagonState *env, target_ulong RsV,
+                           int slot, int mmu_idx, target_ulong PC)
+{
+    target_ulong start = RsV & ~31;
+
+    mmap_lock();
+    tb_invalidate_phys_range(env_cpu(env), start, start + 31);
+    mmap_unlock();
+}
+#endif
+
  int64_t HELPER(cabacdecbin_val)(int64_t RssV, int64_t RttV)
  {
      int64_t RddV = 0;
target/hexagon/translate.c +3 -0
index 06a8159d283..1560c412732 100644
@@ -354,6 +354,9 @@ static bool pkt_ends_tb(Packet *pkt)
      if (pkt->pkt_has_cof) {
          return true;
      }
+    if (check_for_attrib(pkt, A_ICFLUSHOP)) {
+        return true;
+    }
  #ifndef CONFIG_USER_ONLY
      /* System mode instructions that end TLB */
      if (check_for_opcode(pkt, Y2_swi) ||
Reviewed-by
#4 lore raw

Re: [PATCH 1/2] target/hexagon: invalidate translated code for user icinva

Quoted 8 lines · Brian Cain
Quoted 6 lines · Pierrick Bouvier
Quoted 5 lines · Brian Cain

Route linux-user icinva through a helper that invalidates the addressed icache line under mmap_lock

For system mode icinva has nothing left to do, so it stays a nop.

Quoted 8 lines
Quoted 2 lines

Why does system mode icinva is a nop?
Shouldn't we invalidate translated code also?

The normal behavior of instruction translation in QEMU is conservative relative to the ISA constraint.  QEMU will invalidate translated blocks that become dirty/stale from a store instruction, so icinva can't flush
anything: it's already flushed.

I see, it's related to the fact we emulate mmu in system mode.

Quoted 84 lines
Quoted 84 lines
Quoted 84 lines

---
  target/hexagon/gen_tcg.h   |  9 +++++++++
  target/hexagon/helper.h    |  4 ++++
  target/hexagon/op_helper.c | 15 +++++++++++++++
  target/hexagon/translate.c |  3 +++

  4 files changed, 31 insertions(+)
target/hexagon/gen_tcg.h +9 -0
index 40e03781d36..1d25391282a 100644
@@ -497,8 +497,17 @@
       do { RsV = RsV; } while (0)
   #define fGEN_TCG_Y2_dccleana(SHORTCODE) \
       do { RsV = RsV; } while (0)
+
+#ifdef CONFIG_USER_ONLY
+#define fGEN_TCG_Y2_icinva(SHORTCODE) \
+    gen_helper_insn_cache_op(tcg_env, RsV, \
+                             tcg_constant_tl(insn->slot), \
+                             tcg_constant_tl(ctx->mem_idx), \
+                             tcg_constant_tl(ctx->pkt.pc))
+#else
   #define fGEN_TCG_Y2_icinva(SHORTCODE) \
       do { RsV = RsV; } while (0)
+#endif
     /*
    * allocframe(#uiV)
target/hexagon/helper.h +3 -0
index 78dc28ca9e5..a5a5c123533 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_5(insn_cache_op, void, env, i32, int, int, i32)
+#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/op_helper.c +3 -0
index 23894ff3d28..84f5564b1e6 100644
@@ -23,6 +23,9 @@
   #include "qemu/main-loop.h"
   #include "cpu.h"
   #include "exec/helper-proto.h"
+#include "exec/mmap-lock.h"
+#include "exec/target_page.h"
+#include "exec/translation-block.h"
   #include "fpu/softfloat.h"
   #include "exec/cpu-interrupt.h"
   #include "internal.h"
@@ -311,6 +314,18 @@ int32_t HELPER(vacsh_pred)(CPUHexagonState *env,
       return PeV;
   }

  +#ifdef CONFIG_USER_ONLY

target/hexagon/op_helper.c +11 -0
+void HELPER(insn_cache_op)(CPUHexagonState *env, target_ulong RsV,
+                           int slot, int mmu_idx, target_ulong PC)
+{
+    target_ulong start = RsV & ~31;
+
+    mmap_lock();
+    tb_invalidate_phys_range(env_cpu(env), start, start + 31);
+    mmap_unlock();
+}
+#endif
+

  int64_t HELPER(cabacdecbin_val)(int64_t RssV, int64_t RttV) {
      int64_t RddV = 0;

target/hexagon/translate.c +0 -0
index 06a8159d283..1560c412732 100644
@@ -354,6 +354,9 @@ static bool pkt_ends_tb(Packet *pkt)
       if (pkt->pkt_has_cof) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/hexagon/translate.c +3 -0
           return true;
       }
+    if (check_for_attrib(pkt, A_ICFLUSHOP)) {
+        return true;
+    }
   #ifndef CONFIG_USER_ONLY
       /* System mode instructions that end TLB */

      if (check_for_opcode(pkt, Y2_swi) ||

Regards,
Pierrick

#5 lore raw

Re: [PATCH 1/2] target/hexagon: invalidate translated code for user icinva

In reply to Brian Cain
Quoted 6 lines · Brian Cain
Quoted 4 lines · Pierrick Bouvier
Quoted 3 lines · Brian Cain

Route linux-user icinva through a helper that invalidates the addressed icache line under mmap_lock

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

For system mode icinva has nothing left to do, so it stays a nop.

Why does system mode icinva is a nop?
Shouldn't we invalidate translated code also?

The normal behavior of instruction translation in QEMU is conservative relative to the ISA constraint.  QEMU will invalidate translated blocks that become dirty/stale from a store instruction, so icinva can't flush anything: it's already flushed.

We do the same in user mode, trapping the stores via read-only pages. See handle_sigsegv_accerr_write and page_unprotect.

So why do you believe that you need to invalidate pages manually here?

r~

author
#6 lore raw

PATCH 2/2 tests/tcg/hexagon: add icinva test

In reply to Brian Cain
---
 tests/tcg/hexagon/icinva.c        | 71 +++++++++++++++++++++++++++++++
 tests/tcg/hexagon/Makefile.target |  2 +
 2 files changed, 73 insertions(+)
 create mode 100644 tests/tcg/hexagon/icinva.c
tests/tcg/hexagon/icinva.c new file +71 -0
new file mode 100644
index 00000000000..0252e0f899d
@@ -0,0 +1,71 @@
+/*
+ * Test that icinva ends the current translation block.
+ *
+ * icinva only invalidates the emulator's cached translation for a
+ * code range; it doesn't retroactively fix up code that has already
+ * been decoded as part of the still-executing translation block. If
+ * icinva doesn't force a new TB to start right after it, a packet
+ * patched via a store immediately before icinva (with no
+ * change-of-flow in between) still runs the stale decode baked into
+ * the current TB instead of the freshly-patched instruction.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+int err;
+
+#include "hex_test.h"
+
+/* Encoding of "r0 = #99" */
+#define ICINVA_NEW_INSN 0x7800cc60
+
+static uint32_t __attribute__((noinline)) test_icinva_smc(void)
+{
+    uint32_t result;
+
+    /*
+     * r1 = address of the "patch_slot" packet below (1:)
+     * Overwrite it with the "r0 = #99" encoding, invalidate the
+     * icache for that address, then fall straight through into it
+     * with no intervening jump/call.
+     */
+    asm volatile(
+        "r1 = ##1f\n"
+        "r2 = ##%[newinsn]\n"
+        "memw(r1) = r2\n"
+        "icinva(r1)\n"
+        "1:\n"
+        "   r0 = #11\n"
+        "%[out] = r0\n"
+        : [out] "=r"(result)
+        : [newinsn] "i"(ICINVA_NEW_INSN)
+        : "r0", "r1", "r2", "memory"
+    );
+
+    return result;
+}
+
+int main(void)
+{
+    long pagesize = sysconf(_SC_PAGESIZE);
+    uintptr_t page = (uintptr_t)test_icinva_smc & ~(pagesize - 1);
+    uint32_t result;
+
+    if (mprotect((void *)page, 2 * pagesize,
+                 PROT_READ | PROT_WRITE | PROT_EXEC) != 0) {
+        perror("mprotect");
+        return 1;
+    }
+
+    result = test_icinva_smc();
+    check32(result, 99);
+
+    puts(err ? "FAIL" : "PASS");
+    return err;
+}
tests/tcg/hexagon/Makefile.target +2 -0
index 61adf6356e4..27268e66b7a 100644
@@ -60,6 +60,7 @@ HEX_TESTS += invalid-encoding
 HEX_TESTS += multiple-writes
 HEX_TESTS += unaligned_pc
 HEX_TESTS += unaligned_data
+HEX_TESTS += icinva
 
 HEX_TESTS += test_abs
 HEX_TESTS += test_bitcnt
@@ -103,6 +104,7 @@ circ: circ.c hex_test.h
 dual_stores: dual_stores.c hex_test.h
 fpstuff: fpstuff.c hex_test.h
 hex_sigsegv: hex_sigsegv.c hex_test.h
+icinva: icinva.c hex_test.h
 load_align: load_align.c hex_test.h
 load_unpack: load_unpack.c hex_test.h
 mem_noshuf_exception: mem_noshuf_exception.c hex_test.h
Signature · 1 line
2.34.1