Skip to contentSkip to conversation

target/xtensa: clean up unaligned access

PATCH Superseded Max Filippov 2 participants 3 messages May 17, 2021 – May 19, 2021

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 target/xtensa: clean up unaligned access

Xtensa cores may or may not have hardware support for unaligned memory access. On cores with such support pass MO_UNALN in memory access flags for all operations that would not raise an exception. Drop condition from xtensa_cpu_do_unaligned_access and replace it with assertions. Add a test.

---
Changes v2->v3:
- drop assertion for !XTENSA_OPTION_HW_ALIGNMENT from xtensa_cpu_do_unaligned_access to correctly handle acquire/release intsructions;
- add tests for acquire/release instructions.

Changes v1->v2:
- correctly handle case of !XCHAL_UNALIGNED_*_EXCEPTION in the test

 target/xtensa/helper.c             |  13 +-
 target/xtensa/translate.c          | 108 +++++++-------
 tests/tcg/xtensa/test_load_store.S | 221 +++++++++++++++++++++++++++++
 3 files changed, 281 insertions(+), 61 deletions(-)
 create mode 100644 tests/tcg/xtensa/test_load_store.S
target/xtensa/helper.c +6 -7
index eeffee297d15..f18ab383fd89 100644
@@ -270,13 +270,12 @@ void xtensa_cpu_do_unaligned_access(CPUState *cs,
     XtensaCPU *cpu = XTENSA_CPU(cs);
     CPUXtensaState *env = &cpu->env;
 
-    if (xtensa_option_enabled(env->config, XTENSA_OPTION_UNALIGNED_EXCEPTION) &&
-        !xtensa_option_enabled(env->config, XTENSA_OPTION_HW_ALIGNMENT)) {
-        cpu_restore_state(CPU(cpu), retaddr, true);
-        HELPER(exception_cause_vaddr)(env,
-                                      env->pc, LOAD_STORE_ALIGNMENT_CAUSE,
-                                      addr);
-    }
+    assert(xtensa_option_enabled(env->config,
+                                 XTENSA_OPTION_UNALIGNED_EXCEPTION));
+    cpu_restore_state(CPU(cpu), retaddr, true);
+    HELPER(exception_cause_vaddr)(env,
+                                  env->pc, LOAD_STORE_ALIGNMENT_CAUSE,
+                                  addr);
 }
 
 bool xtensa_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
target/xtensa/translate.c +54 -54

275 lines not on this page · raw message

tests/tcg/xtensa/test_load_store.S new file +221 -0

221 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw
Quoted 25 lines · Max Filippov
target/xtensa/translate.c +7 -11
-static void gen_load_store_alignment(DisasContext *dc, int shift,
-        TCGv_i32 addr, bool no_hw_alignment)
+static MemOp gen_load_store_alignment(DisasContext *dc, int shift,
+                                      TCGv_i32 addr, bool no_hw_alignment)
  {
      if (!option_enabled(dc, XTENSA_OPTION_UNALIGNED_EXCEPTION)) {
          tcg_gen_andi_i32(addr, addr, ~0 << shift);
-    } else if (option_enabled(dc, XTENSA_OPTION_HW_ALIGNMENT) &&
-            no_hw_alignment) {
-        TCGLabel *label = gen_new_label();
-        TCGv_i32 tmp = tcg_temp_new_i32();
-        tcg_gen_andi_i32(tmp, addr, ~(~0 << shift));
-        tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
-        gen_exception_cause_vaddr(dc, LOAD_STORE_ALIGNMENT_CAUSE, addr);
-        gen_set_label(label);
-        tcg_temp_free(tmp);
+    }
+    if (!no_hw_alignment && option_enabled(dc, XTENSA_OPTION_HW_ALIGNMENT)) {
+        return MO_UNALN;
+    } else {
+        return MO_ALIGN;
      }
  }

target/xtensa/translate.c static void translate_l32e(DisasContext *dc, const OpcodeArg arg[], · line 1777

Quoted 10 lines
target/xtensa/translate.c +3 -2
@@ -1784,10 +1770,11 @@ static void translate_l32e(DisasContext *dc, const OpcodeArg arg[],
                             const uint32_t par[])
  {
      TCGv_i32 addr = tcg_temp_new_i32();
+    MemOp al;
 
      tcg_gen_addi_i32(addr, arg[1].in, arg[2].imm);
-    gen_load_store_alignment(dc, 2, addr, false);
-    tcg_gen_qemu_ld_tl(arg[0].out, addr, dc->ring, MO_TEUL);
+    al = gen_load_store_alignment(dc, 2, addr, false);
+    tcg_gen_qemu_ld_tl(arg[0].out, addr, dc->ring, MO_TEUL | al);

You're duplicating the information about the size of the alignment.

I think it would be better to pass the partial MemOp into get_load_store_alignment and return the complete MemOp. E.g.:

MemOp gen_load_store_alignment(DisasContext *dc, MemOp mop,
TCGv addr)

{
    if ((mop & MO_SIZE) == MO_8) {
        return mop;
    }
    if ((mop & MO_AMASK) == 0 &&
        !option_enabled(dc, XTENSA_OPTION_HW_ALIGNMENT)) {
        mop |= MO_ALIGN;
    }

    if (!option_enabled(dc, XTENSA_OPTION_UNALIGNED_EXCEPTION)) {
        tcg_gen_andi_i32(addr, addr,
                         ~0 << get_alignment_bits(mop));
    }
    return mop;
}

Then used as

    mop = gen_load_store_alignment(dc, MO_TEUL, addr);
    tcg_gen_qemu_ld_tl(arg[0].out, addr, dc->ring, mop);

    mop = gen_load_store_alignment(dc, MO_TEUL | MO_ALIGN, addr);
    gen_check_exclusive(dc, addr, false);
    tcg_gen_qemu_ld_i32(arg[0].out, addr, dc->ring, mop);

This organization does require that you remove TARGET_ALIGNED_ONLY=y from default-configs/targets/xtensa*.mak so that MO_ALIGN has the non-zero value.

r~

#2 lore raw

Hi Richard,

Quoted 15 lines · Richard Henderson

target/xtensa/translate.c static void translate_l32e(DisasContext *dc, const OpcodeArg arg[], · line 1777

Quoted 11 lines · Max Filippov
target/xtensa/translate.c +3 -2
@@ -1784,10 +1770,11 @@ static void translate_l32e(DisasContext *dc, const OpcodeArg arg[],
                             const uint32_t par[])
  {
      TCGv_i32 addr = tcg_temp_new_i32();
+    MemOp al;
 
      tcg_gen_addi_i32(addr, arg[1].in, arg[2].imm);
-    gen_load_store_alignment(dc, 2, addr, false);
-    tcg_gen_qemu_ld_tl(arg[0].out, addr, dc->ring, MO_TEUL);
+    al = gen_load_store_alignment(dc, 2, addr, false);
+    tcg_gen_qemu_ld_tl(arg[0].out, addr, dc->ring, MO_TEUL | al);

You're duplicating the information about the size of the alignment.

Quoted 2 lines

I think it would be better to pass the partial MemOp into get_load_store_alignment and return the complete MemOp. E.g.:

That indeed looks better. Let me make another version of this patch. Thanks for taking a look!

Signature · 2 lines
Thanks.
-- Max