Skip to contentSkip to conversation

target/mips: Use probe_access_full() in Atomic Load/Store helpers

PATCH Superseded Philippe Mathieu-Daudé 2 participants 5 messages Apr 16

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover target/mips: Use probe_access_full() in Atomic Load/Store helpers

Following Richard suggestion in [*], use probe_access_full() and replace legacy cpu_ld*_mmuidx_ra() by cpu_ld*_mmu() in Atomic Load/Store helpers.

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

Philippe Mathieu-Daudé (2):
target/mips: Pass MemOpIdx argument to Linked Load/Store helpers target/mips: Use probe_access_full() in Atomic Load/Store helpers

 target/mips/helper.h          |  4 ++--
 target/mips/tcg/ldst_helper.c | 22 +++++++++++++++++-----
 target/mips/tcg/translate.c   |  7 ++++---
 3 files changed, 23 insertions(+), 10 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v4 1/2 target/mips: Pass MemOpIdx argument to Linked Load/Store helpers

In preparation of using the MemOp content in the next commit (thus stopping ignoring it), pass it as MemOpIdx.

The helper prototype declaration always took a TCGv_i32 as last argument, correct that.

Rename the ignored 'mem_idx' argument on user emulation.

---
 target/mips/helper.h          | 4 ++--
 target/mips/tcg/ldst_helper.c | 5 ++++-
 target/mips/tcg/translate.c   | 7 ++++---
 3 files changed, 10 insertions(+), 6 deletions(-)
target/mips/helper.h +2 -2
index e70a3942fac..10c6470514d 100644
@@ -10,9 +10,9 @@ DEF_HELPER_4(swl, void, env, tl, tl, int)
 DEF_HELPER_4(swr, void, env, tl, tl, int)
 
 #ifndef CONFIG_USER_ONLY
-DEF_HELPER_3(ll, tl, env, tl, int)
+DEF_HELPER_3(ll, tl, env, tl, i32)
 #ifdef TARGET_MIPS64
-DEF_HELPER_3(lld, tl, env, tl, int)
+DEF_HELPER_3(lld, tl, env, tl, i32)
 #endif
 #endif
 
target/mips/tcg/ldst_helper.c +4 -1
index 10319bf03a6..b725c6d0333 100644
@@ -30,8 +30,11 @@
 #ifndef CONFIG_USER_ONLY
 
 #define HELPER_LD_ATOMIC(name, insn, almask, do_cast)                         \
-target_ulong helper_##name(CPUMIPSState *env, target_ulong arg, int mem_idx)  \
+target_ulong helper_##name(CPUMIPSState *env, target_ulong arg,               \
+                           uint32_t memop_idx)                                \
 {                                                                             \
+    MemOpIdx oi = memop_idx; \
+    unsigned mem_idx = get_mmuidx(oi); \
     if (arg & almask) {                                                       \
         if (!(env->hflags & MIPS_HFLAG_DM)) {                                 \
             env->CP0_BadVAddr = arg;                                          \
target/mips/tcg/translate.c +4 -3
index 6d4b6412d70..4889bd1e518 100644
@@ -1922,7 +1922,7 @@ FOP_CONDNS(s, FMT_S, 32, gen_store_fpr32(ctx, fp0, fd))
 /* load/store instructions. */
 #ifdef CONFIG_USER_ONLY
 #define OP_LD_ATOMIC(insn, memop)                                          \
-static inline void op_ld_##insn(TCGv ret, TCGv arg1, int mem_idx,          \
+static inline void op_ld_##insn(TCGv ret, TCGv arg1, int mem_idx_ignored,  \
                                 DisasContext *ctx)                         \
 {                                                                          \
     TCGv t0 = tcg_temp_new();                                              \
@@ -1932,11 +1932,12 @@ static inline void op_ld_##insn(TCGv ret, TCGv arg1, int mem_idx,          \
     tcg_gen_st_tl(ret, tcg_env, offsetof(CPUMIPSState, llval));            \
 }
 #else
-#define OP_LD_ATOMIC(insn, ignored_memop)                                  \
+#define OP_LD_ATOMIC(insn, memop)                                          \
 static inline void op_ld_##insn(TCGv ret, TCGv arg1, int mem_idx,          \
                                 DisasContext *ctx)                         \
 {                                                                          \
-    gen_helper_##insn(ret, tcg_env, arg1, tcg_constant_i32(mem_idx));      \
+    MemOpIdx oi = make_memop_idx(memop | MO_ALIGN, mem_idx);               \
+    gen_helper_##insn(ret, tcg_env, arg1, tcg_constant_i32(oi));           \
 }
 #endif
 OP_LD_ATOMIC(ll, mo_endian(ctx) | MO_SL);
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v4 1/2] target/mips: Pass MemOpIdx argument to Linked Load/Store helpers

Quoted 16 lines · Philippe Mathieu-Daudé

In preparation of using the MemOp content in the next commit (thus stopping ignoring it), pass it as MemOpIdx.

The helper prototype declaration always took a TCGv_i32 as last argument, correct that.

Rename the ignored 'mem_idx' argument on user emulation.

---
  target/mips/helper.h          | 4 ++--
  target/mips/tcg/ldst_helper.c | 5 ++++-
  target/mips/tcg/translate.c   | 7 ++++---
  3 files changed, 10 insertions(+), 6 deletions(-)

target/mips/tcg/translate.c static inline void op_ld_##insn(TCGv ret, TCGv arg1, int mem_idx, \ · line 1939

Quoted 7 lines
target/mips/tcg/translate.c +2 -2
-#define OP_LD_ATOMIC(insn, ignored_memop)                                  \
+#define OP_LD_ATOMIC(insn, memop)                                          \
  static inline void op_ld_##insn(TCGv ret, TCGv arg1, int mem_idx,          \
                                  DisasContext *ctx)                         \
  {                                                                          \
-    gen_helper_##insn(ret, tcg_env, arg1, tcg_constant_i32(mem_idx));      \
+    MemOpIdx oi = make_memop_idx(memop | MO_ALIGN, mem_idx);               \

Oops, MO_ALIGN is part of the following patch.

Quoted 4 lines

+ gen_helper_##insn(ret, tcg_env, arg1, tcg_constant_i32(oi)); \
}
#endif
OP_LD_ATOMIC(ll, mo_endian(ctx) | MO_SL);

#3 lore raw

PATCH v4 2/2 target/mips: Use probe_access_full() in Atomic Load/Store helpers

In reply to Philippe Mathieu-Daudé

Let the probe_access() API check the address alignment.

Move env->CP0_LLAddr and env->lladdr assignments so we don't update them when an alignment fault occurs.

Since we have a handy MemOpIdx, replace the legacy cpu_ld*_mmuidx_ra() calls by cpu_ld*_mmu() equivalent.

---
 target/mips/tcg/ldst_helper.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
target/mips/tcg/ldst_helper.c +15 -6
index b725c6d0333..4ebf72d610e 100644
@@ -28,23 +28,32 @@
 #include "internal.h"
 
 #ifndef CONFIG_USER_ONLY
+#include "accel/tcg/probe.h"
+#include "exec/tlb-flags.h"
 
 #define HELPER_LD_ATOMIC(name, insn, almask, do_cast)                         \
 target_ulong helper_##name(CPUMIPSState *env, target_ulong arg,               \
                            uint32_t memop_idx)                                \
 {                                                                             \
-    MemOpIdx oi = memop_idx; \
-    unsigned mem_idx = get_mmuidx(oi); \
-    if (arg & almask) {                                                       \
+    MemOpIdx oi = memop_idx;                                                  \
+    unsigned mem_idx = get_mmuidx(oi);                                        \
+    unsigned size = memop_size(get_memop(oi));                                \
+    uintptr_t ra = GETPC();                                                   \
+    CPUTLBEntryFull *full;                                                    \
+    void *host_unused;                                                        \
+    int flags;                                                                \
+                                                                              \
+    env->llval = do_cast cpu_##insn##_mmu(env, arg, oi, ra);                  \
+    flags = probe_access_full(env, arg, size, MMU_DATA_LOAD, mem_idx,         \
+                              true, &host_unused, &full, ra);                 \
+    if (unlikely(flags & TLB_INVALID_MASK)) {                                 \
         if (!(env->hflags & MIPS_HFLAG_DM)) {                                 \
             env->CP0_BadVAddr = arg;                                          \
         }                                                                     \
         do_raise_exception(env, EXCP_AdEL, GETPC());                          \
     }                                                                         \
-    env->CP0_LLAddr = cpu_mips_translate_address(env, arg, MMU_DATA_LOAD,     \
-                                                 GETPC());                    \
+    env->CP0_LLAddr = full->phys_addr;                                        \
     env->lladdr = arg;                                                        \
-    env->llval = do_cast cpu_##insn##_mmuidx_ra(env, arg, mem_idx, GETPC());  \
     return env->llval;                                                        \
 }
 HELPER_LD_ATOMIC(ll, ldl, 0x3, (target_long)(int32_t))
Signature · 1 line
2.53.0
#4 lore raw
Quoted 1 line · Philippe Mathieu-Daudé

Let the probe_access() API check the address alignment.

The probe_access will not check the alignment.
The use of the memory access before the probe checks the alignment.

Quoted 30 lines

Move env->CP0_LLAddr and env->lladdr assignments so we don't update them when an alignment fault occurs.

Since we have a handy MemOpIdx, replace the legacy cpu_ld*_mmuidx_ra() calls by cpu_ld*_mmu() equivalent.

---
  target/mips/tcg/ldst_helper.c | 21 +++++++++++++++------
  1 file changed, 15 insertions(+), 6 deletions(-)
target/mips/tcg/ldst_helper.c +2 -2
index b725c6d0333..4ebf72d610e 100644
@@ -28,23 +28,32 @@
  #include "internal.h"
 
  #ifndef CONFIG_USER_ONLY
+#include "accel/tcg/probe.h"
+#include "exec/tlb-flags.h"
 
  #define HELPER_LD_ATOMIC(name, insn, almask, do_cast)                         \
  target_ulong helper_##name(CPUMIPSState *env, target_ulong arg,               \
                             uint32_t memop_idx)                                \
  {                                                                             \
-    MemOpIdx oi = memop_idx; \
-    unsigned mem_idx = get_mmuidx(oi); \

target/mips/tcg/ldst_helper.c line 46

Quoted 10 lines
target/mips/tcg/ldst_helper.c +9 -1
-    if (arg & almask) {                                                       \
+    MemOpIdx oi = memop_idx;                                                  \
+    unsigned mem_idx = get_mmuidx(oi);                                        \
+    unsigned size = memop_size(get_memop(oi));                                \
+    uintptr_t ra = GETPC();                                                   \
+    CPUTLBEntryFull *full;                                                    \
+    void *host_unused;                                                        \
+    int flags;                                                                \
+                                                                              \
+    env->llval = do_cast cpu_##insn##_mmu(env, arg, oi, ra);                  \

This performs the access, filling the tlb in the process.
If the tlb cannot be filled, access is not permitted an an exception is raised.

target/mips/tcg/ldst_helper.c line 48

Quoted 2 lines
target/mips/tcg/ldst_helper.c +2 -0
+    flags = probe_access_full(env, arg, size, MMU_DATA_LOAD, mem_idx,         \
+                              true, &host_unused, &full, ra);                 \

Which means this probe for the tlb entry *must* succeed, because we can't reach here unless it was confirmed present by the access above.

target/mips/tcg/ldst_helper.c line 49

Quoted 1 line
target/mips/tcg/ldst_helper.c +1 -0
+    if (unlikely(flags & TLB_INVALID_MASK)) {    

Which means this must be

    assert(!(flags & TLB_INVALID_MASK));
                             \

target/mips/tcg/ldst_helper.c line 58

Quoted 5 lines
target/mips/tcg/ldst_helper.c +0 -0
          if (!(env->hflags & MIPS_HFLAG_DM)) {                                 \
              env->CP0_BadVAddr = arg;                                          \
          }                                                                     \
          do_raise_exception(env, EXCP_AdEL, GETPC());                          \
      }                                                    

... and the rest of the IF is dead code.

r~