Skip to contentSkip to conversation

target-mips: Add support for misaligned accesses

PATCH Superseded Yongbok Kim 2 participants 11 messages May 20, 2015 – May 21, 2015

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover target-mips: Add support for misaligned accesses

This patch set adds support for misaligned memory accesses in MIPS architecture Release 6 and MIPS SIMD Architecture.

The behaviour, semantics, and architecture specifications of misaligned memory accesses are described in:
MIPS Architecture For Programmers Volume I-A: Introduction to the MIPS64 Architecture, Appendix B Misaligned Memory Accesses.
Available at http://www.imgtec.com/mips/architectures/mips64.asp

Regards,
Yongbok

v4:
* Removed the work-around per the recent TCG change for misaligned accesses
* Added probe_write() (Richard)
* Used helper_ret_*_mmu directly (Richard)
* Removed TLB checking for MSA LD (Richard)
* Removed unnecessary save_cpu_state() calls

v3:
* Rewrote MSA patch
* Work-around is using byte-to-byte accesses and endianness corrections for R5+MSA. (This replaces the misaligned flag from v2.) (Leon)
* Bug fixes (Leon)
* Separate helper functions for each data formats

v2:
* Removed re-translation in the mips_cpu_do_unaligned_access() (Peter)
* Checks validity only if an access is spanning into two pages in MSA (Leon)
* Introduced misaligned flag to indicate MSA ld/st is ongoing, is used to allow misaligned accesses in the mips_cpu_do_unaligned_access() callback. This is crucial to support MSA misaligned accesses in Release 5 cores.

Yongbok Kim (3):
target-mips: Misaligned memory accesses for R6
softmmu: Add probe_write()
target-mips: Misaligned memory accesses for MSA

 include/exec/exec-all.h      |    2 +
 softmmu_template.h           |   21 ++++++
 target-mips/helper.h         |   10 +++-
 target-mips/op_helper.c      |  144 +++++++++++++++++++++++------------------
 target-mips/translate.c      |   21 ++++--
 target-mips/translate_init.c |    2 +-
 6 files changed, 127 insertions(+), 73 deletions(-)
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH v4 1/3 target-mips: Misaligned memory accesses for R6

Release 6 requires misaligned memory access support for all ordinary memory access instructions (for example, LW/SW, LWC1/SWC1).
However misaligned support is not provided for certain special memory accesses such as atomics (for example, LL/SC).

Allows misaligned accesses from mips_cpu_do_unaligned_access() callback, if it is a R6 core. As the helper functions of LL/SC is checking misalignment, just allowing all for R6 is good enough.

---
 target-mips/op_helper.c      |    7 +++++++
 target-mips/translate_init.c |    2 +-
 2 files changed, 8 insertions(+), 1 deletions(-)
target-mips/op_helper.c +7 -0
index 73a8e45..58f02cf 100644
@@ -2215,6 +2215,13 @@ void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
     int error_code = 0;
     int excp;
 
+    if (env->insn_flags & ISA_MIPS32R6) {
+        /* Release 6 provides support for misaligned memory access for
+         * all ordinary memory reference instructions
+         * */
+        return;
+    }
+
     env->CP0_BadVAddr = addr;
 
     if (access_type == MMU_DATA_STORE) {
target-mips/translate_init.c +1 -1
index 85a65e7..ec54fef 100644
@@ -607,7 +607,7 @@ static const mips_def_t mips_defs[] =
     },
     {
         /* A generic CPU supporting MIPS64 Release 6 ISA.
-           FIXME: Support IEEE 754-2008 FP and misaligned memory accesses.
+           FIXME: Support IEEE 754-2008 FP.
                   Eventually this should be replaced by a real CPU model. */
         .name = "MIPS64R6-generic",
         .CP0_PRid = 0x00010000,
Signature · 1 line
1.7.5.4
#2 lore raw

Re: [Qemu-devel] [PATCH v4 1/3] target-mips: Misaligned memory accesses for R6

Quoted 5 lines · Yongbok Kim
target-mips/op_helper.c +0 -0
index 73a8e45..58f02cf 100644

target-mips/op_helper.c void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr, · line 2223

Quoted 9 lines
target-mips/op_helper.c +6 -0
@@ -2215,6 +2215,13 @@ void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
     int error_code = 0;
     int excp;
 
+    if (env->insn_flags & ISA_MIPS32R6) {
+        /* Release 6 provides support for misaligned memory access for
+         * all ordinary memory reference instructions
+         * */
+        return;
+    }

This should be done instead with MO_UNALN, at translate time. See target-ppc, DisasContext, default_tcg_memop_mask.

r~

#3 lore raw

Re: [Qemu-devel] [PATCH v4 1/3] target-mips: Misaligned memory accesses for R6

Quoted 20 lines · Richard Henderson
Quoted 5 lines · Yongbok Kim
target-mips/op_helper.c +0 -0
index 73a8e45..58f02cf 100644

target-mips/op_helper.c void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr, · line 2223

Quoted 9 lines
target-mips/op_helper.c +6 -0
@@ -2215,6 +2215,13 @@ void mips_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
     int error_code = 0;
     int excp;
 
+    if (env->insn_flags & ISA_MIPS32R6) {
+        /* Release 6 provides support for misaligned memory access for
+         * all ordinary memory reference instructions
+         * */
+        return;
+    }

This should be done instead with MO_UNALN, at translate time. See target-ppc, DisasContext, default_tcg_memop_mask.

Quoted 2 lines

r~

Fair enough. Actually I considered to pass the information but didn't bother as this way is so simple.

Regards,
Yongbok

#4 lore raw

Re: [Qemu-devel] [PATCH v4 1/3] target-mips: Misaligned memory accesses for R6

Quoted 2 lines · Yongbok Kim

Fair enough. Actually I considered to pass the information but didn't bother as this way is so simple.

If you ever quit relying on a separate heck for atomics, which you probably should, this would be incorrect.

r~

#5 lore raw

PATCH v4 2/3 softmmu: Add probe_write()

In reply to Yongbok Kim

Add probe_write() forces a tlb_fill if the specified guest virtual index isn't in the TCG softmmu TLB.

---
 include/exec/exec-all.h |    2 ++
 softmmu_template.h      |   21 +++++++++++++++++++++
 2 files changed, 23 insertions(+), 0 deletions(-)
include/exec/exec-all.h +2 -0
index b58cd47..af51203 100644
@@ -109,6 +109,8 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,
                              hwaddr paddr, MemTxAttrs attrs,
                              int prot, int mmu_idx, target_ulong size);
 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr);
+void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx,
+                 uintptr_t retaddr);
 #else
 static inline void tlb_flush_page(CPUState *cpu, target_ulong addr)
 {
softmmu_template.h +21 -0
index 39f571b..c175d17 100644
@@ -548,6 +548,27 @@ glue(glue(helper_st, SUFFIX), MMUSUFFIX)(CPUArchState *env, target_ulong addr,
     helper_te_st_name(env, addr, val, oi, GETRA());
 }
 
+#if DATA_SIZE == 1
+/*
+ * Force a tlb_fill if the specified guest virtual index isn't in the TCG
+ * softmmu TLB.
+ */
+void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx,
+                 uintptr_t retaddr)
+{
+    int index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
+    target_ulong tlb_addr = env->tlb_table[mmu_idx][index].addr_write;
+
+    if ((addr & TARGET_PAGE_MASK)
+        != (tlb_addr & (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {
+        /* TLB entry is for a different page */
+        if (!VICTIM_TLB_HIT(addr_write)) {
+            tlb_fill(ENV_GET_CPU(env), addr, MMU_DATA_STORE, mmu_idx, retaddr);
+        }
+    }
+    return;
+}
+#endif
 #endif /* !defined(SOFTMMU_CODE_ACCESS) */
 
 #undef READ_ACCESS_TYPE
Signature · 1 line
1.7.5.4
#6 lore raw

Re: [Qemu-devel] [PATCH v4 2/3] softmmu: Add probe_write()

softmmu_template.h glue(glue(helper_st, SUFFIX), MMUSUFFIX)(CPUArchState *env, target_ulong addr, · line 570

Quoted 3 lines · Yongbok Kim
softmmu_template.h +3 -0
+    }
+    return;
+}

Drop the useless return.

r~

#7 lore raw

PATCH v4 3/3 target-mips: Misaligned memory accesses for MSA

In reply to Yongbok Kim

MIPS SIMD Architecture vector loads and stores require misalignment support. MSA Memory access should work as an atomic operation. Therefore, it has to check validity of all addresses for a vector store access if it is spanning into two pages.

Separating helper functions for each data format as format is known in translation.
To use mmu_idx from cpu_mmu_index() instead of calculating it from hflag. Removing save_cpu_state() call in translation because it is able to use cpu_restore_state() on fault as GETRA() is passed.

---
 target-mips/helper.h    |   10 +++-
 target-mips/op_helper.c |  137 +++++++++++++++++++++++++---------------------
 target-mips/translate.c |   21 +++++---
 3 files changed, 96 insertions(+), 72 deletions(-)
target-mips/helper.h +8 -2
index 3bd0b02..c532276 100644
@@ -931,5 +931,11 @@ DEF_HELPER_4(msa_ftint_u_df, void, env, i32, i32, i32)
 DEF_HELPER_4(msa_ffint_s_df, void, env, i32, i32, i32)
 DEF_HELPER_4(msa_ffint_u_df, void, env, i32, i32, i32)
 
-DEF_HELPER_5(msa_ld_df, void, env, i32, i32, i32, s32)
-DEF_HELPER_5(msa_st_df, void, env, i32, i32, i32, s32)
+#define MSALDST_PROTO(type)                                 \
+DEF_HELPER_4(msa_ld_ ## type, void, env, i32, i32, s32)     \
+DEF_HELPER_4(msa_st_ ## type, void, env, i32, i32, s32)
+MSALDST_PROTO(b)
+MSALDST_PROTO(h)
+MSALDST_PROTO(w)
+MSALDST_PROTO(d)
+#undef MSALDST_PROTO
target-mips/op_helper.c +74 -63

146 lines not on this page · raw message

target-mips/translate.c +14 -7
index fd063a2..88b3206 100644
@@ -18402,32 +18402,39 @@ static void gen_msa(CPUMIPSState *env, DisasContext *ctx)
             int32_t s10 = sextract32(ctx->opcode, 16, 10);
             uint8_t rs = (ctx->opcode >> 11) & 0x1f;
             uint8_t wd = (ctx->opcode >> 6) & 0x1f;
-            uint8_t df = (ctx->opcode >> 0) & 0x3;
 
-            TCGv_i32 tdf = tcg_const_i32(df);
             TCGv_i32 twd = tcg_const_i32(wd);
             TCGv_i32 trs = tcg_const_i32(rs);
             TCGv_i32 ts10 = tcg_const_i32(s10);
 
             switch (MASK_MSA_MINOR(opcode)) {
             case OPC_LD_B:
+                gen_helper_msa_ld_b(cpu_env, twd, trs, ts10);
+                break;
             case OPC_LD_H:
+                gen_helper_msa_ld_h(cpu_env, twd, trs, ts10);
+                break;
             case OPC_LD_W:
+                gen_helper_msa_ld_w(cpu_env, twd, trs, ts10);
+                break;
             case OPC_LD_D:
-                save_cpu_state(ctx, 1);
-                gen_helper_msa_ld_df(cpu_env, tdf, twd, trs, ts10);
+                gen_helper_msa_ld_d(cpu_env, twd, trs, ts10);
                 break;
             case OPC_ST_B:
+                gen_helper_msa_st_b(cpu_env, twd, trs, ts10);
+                break;
             case OPC_ST_H:
+                gen_helper_msa_st_h(cpu_env, twd, trs, ts10);
+                break;
             case OPC_ST_W:
+                gen_helper_msa_st_w(cpu_env, twd, trs, ts10);
+                break;
             case OPC_ST_D:
-                save_cpu_state(ctx, 1);
-                gen_helper_msa_st_df(cpu_env, tdf, twd, trs, ts10);
+                gen_helper_msa_st_d(cpu_env, twd, trs, ts10);
                 break;
             }
 
             tcg_temp_free_i32(twd);
-            tcg_temp_free_i32(tdf);
             tcg_temp_free_i32(trs);
             tcg_temp_free_i32(ts10);
         }
Signature · 1 line
1.7.5.4
#8 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] target-mips: Misaligned memory accesses for MSA

target-mips/op_helper.c FOP_CONDN_S(sne, (float32_lt(fst1, fst0, &env->active_fpu.fp_status) · line 3574

Quoted 7 lines · Yongbok Kim
target-mips/op_helper.c +7 -0
+#define MSA_LD_DF(DF, TYPE, LD_INSN, ...)                               \
+void helper_msa_ld_ ## TYPE(CPUMIPSState *env, uint32_t wd, uint32_t rs,\
+                     int32_t s10)                                       \
+{                                                                       \
+    wr_t *pwd = &(env->active_fpu.fpr[wd].wr);                          \
+    wr_t wx;                                                            \
+    target_ulong addr = env->active_tc.gpr[rs] + (s10 << DF);           \

It would be nice to clean this up a bit more. For instance, don't pass the register number and offset. Instead, compute the address properly during translation and pass that down.

target-mips/op_helper.c FOP_CONDN_S(sne, (float32_lt(fst1, fst0, &env->active_fpu.fp_status) · line 3577

Quoted 3 lines
target-mips/op_helper.c +3 -0
+    int i;                                                              \
+    for (i = 0; i < DF_ELEMENTS(DF); i++) {                             \
+        wx.TYPE[i] = LD_INSN(env, addr + (i << DF), ##__VA_ARGS__);     \

Why the varargs? They're the same all the time.

Review

Quoted 2 lines
(quoted diff) +2 -0
+MSA_LD_DF(DF_BYTE,   b, helper_ret_ldub_mmu, \
+          make_memop_idx(MO_UNALN, cpu_mmu_index(env)), GETRA())

I would prefer it if you'd pass the full and proper TCGMemOp. I.e. MO_TE | MO_{8,16,32,64} | MO_UNALN.

And you might as well hoist that out of the load/store loops.

r~

#9 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] target-mips: Misaligned memory accesses for MSA

Quoted 6 lines · Richard Henderson

target-mips/op_helper.c FOP_CONDN_S(sne, (float32_lt(fst1, fst0, &env->active_fpu.fp_status) · line 3577

Quoted 4 lines
target-mips/op_helper.c +3 -0
+    int i;                                                              \
+    for (i = 0; i < DF_ELEMENTS(DF); i++) {                             \
+        wx.TYPE[i] = LD_INSN(env, addr + (i << DF), ##__VA_ARGS__);     \

Why the varargs? They're the same all the time.

No, they aren't. we still need to support linux user mode which don't have any extra argument.

MSA_LD_DF(DF_BYTE,   b, helper_ret_ldub_mmu, \
          make_memop_idx(MO_UNALN, cpu_mmu_index(env)), GETRA())

VS.

MSA_LD_DF(DF_BYTE, b, cpu_ldub_data)

Regards,
Yongbok

#10 lore raw

Re: [Qemu-devel] [PATCH v4 3/3] target-mips: Misaligned memory accesses for MSA

Quoted 12 lines · Yongbok Kim
Quoted 6 lines · Richard Henderson

target-mips/op_helper.c FOP_CONDN_S(sne, (float32_lt(fst1, fst0, &env->active_fpu.fp_status) · line 3577

Quoted 4 lines
target-mips/op_helper.c +3 -0
+    int i;                                                              \
+    for (i = 0; i < DF_ELEMENTS(DF); i++) {                             \
+        wx.TYPE[i] = LD_INSN(env, addr + (i << DF), ##__VA_ARGS__);     \

Why the varargs? They're the same all the time.

No, they aren't. we still need to support linux user mode which don't have any extra argument.

Quoted 6 lines
MSA_LD_DF(DF_BYTE,   b, helper_ret_ldub_mmu, \
          make_memop_idx(MO_UNALN, cpu_mmu_index(env)), GETRA())

VS.

MSA_LD_DF(DF_BYTE, b, cpu_ldub_data)

Ah, I'd missed that. Thanks,

r~