Skip to contentSkip to conversation

target/hppa: Optimize ldcw/ldcd instruction translation

PATCH In review Helge Deller 2 participants 5 messages Sep 13, 2023 – Sep 14, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/hppa: Optimize ldcw/ldcd instruction translation

ldcw (load word and clear) is the only atomic memory instruction of the hppa architecture and thus is heavily used by the Linux and HP/UX kernel to implement locks.

Since ldcw always writes a zero, optimize it to not write zero again if the memory already contained zero (as the lock was already locked). This reduces the memory contention.
Furthermore, use a native qatomic_xchg() call to write the zero and thus give a small performace improvement and avoid additional atomic locking in the target.

target/hppa/helper.h +1 -1
index c7e35ce8c7..26d022c714 100644
@@ -16,7 +16,7 @@ DEF_HELPER_FLAGS_3(stby_b_parallel, TCG_CALL_NO_WG, void, env, tl, tr)
 DEF_HELPER_FLAGS_3(stby_e, TCG_CALL_NO_WG, void, env, tl, tr)
 DEF_HELPER_FLAGS_3(stby_e_parallel, TCG_CALL_NO_WG, void, env, tl, tr)
 
-DEF_HELPER_FLAGS_1(ldc_check, TCG_CALL_NO_RWG, void, tl)
+DEF_HELPER_FLAGS_3(ldc, TCG_CALL_NO_WG, tr, env, tl, i32)
 
 DEF_HELPER_FLAGS_4(probe, TCG_CALL_NO_WG, tr, env, tl, i32, i32)
 
target/hppa/op_helper.c +51 -1

64 lines not on this page · raw message

target/hppa/translate.c +4 -14
index c04dc15228..c96691ab62 100644
@@ -2857,9 +2857,9 @@ static bool trans_st(DisasContext *ctx, arg_ldst *a)
 
 static bool trans_ldc(DisasContext *ctx, arg_ldst *a)
 {
-    MemOp mop = MO_TE | MO_ALIGN | a->size;
-    TCGv_reg zero, dest, ofs;
+    TCGv_reg dest, ofs;
     TCGv_tl addr;
+    TCGv_i32 sz;
 
     nullify_over(ctx);
 
@@ -2874,18 +2874,8 @@ static bool trans_ldc(DisasContext *ctx, arg_ldst *a)
     form_gva(ctx, &addr, &ofs, a->b, a->x, a->scale ? a->size : 0,
              a->disp, a->sp, a->m, ctx->mmu_idx == MMU_PHYS_IDX);
 
-    /*
-     * For hppa1.1, LDCW is undefined unless aligned mod 16.
-     * However actual hardware succeeds with aligned mod 4.
-     * Detect this case and log a GUEST_ERROR.
-     *
-     * TODO: HPPA64 relaxes the over-alignment requirement
-     * with the ,co completer.
-     */
-    gen_helper_ldc_check(addr);
-
-    zero = tcg_constant_reg(0);
-    tcg_gen_atomic_xchg_reg(dest, addr, zero, ctx->mmu_idx, mop);
+    sz = tcg_constant_i32((a->size == MO_32) ? 4 : 8);
+    gen_helper_ldc(dest, cpu_env, addr, sz);
 
     if (a->m) {
         save_gpr(ctx, a->b, ofs);
#1 lore raw

Review

Quoted 2 lines · Helge Deller
(quoted diff) +2 -0
+        haddr = (uint32_t *)((uintptr_t)vaddr);
+        old = *haddr;

This is horribly incorrect, both for user-only and system mode.

target/hppa/op_helper.c void HELPER(stby_e_parallel)(CPUHPPAState *env, target_ulong addr, · line 194

Quoted 5 lines
target/hppa/op_helper.c +5 -0
+        /* if already zero, do not write 0 again to reduce memory presssure */
+        if (old == 0) {
+            return 0;
+        }
+        old = qatomic_xchg(haddr, (uint32_t) 0);

You're also dropping the required host memory barrier.

Frankly, the current tcg_gen_atomic_xchg_reg is as optimized as you'll be able to do. I really doubt the "avoid write 0" is measurable at all.

r~

#2 lore raw
Quoted 5 lines · Richard Henderson
Quoted 2 lines · Helge Deller

+        haddr = (uint32_t *)((uintptr_t)vaddr);
+        old = *haddr;

This is horribly incorrect, both for user-only and system mode.

Richard, thank you for the review!
But would you mind explaining why this is incorrect?
I thought the "vaddr = probe_access()" calculates the host address, so shouldn't it be the right address?

Quoted 7 lines
Quoted 5 lines

+        /* if already zero, do not write 0 again to reduce memory presssure */ +        if (old == 0) {
+            return 0;
+        }
+        old = qatomic_xchg(haddr, (uint32_t) 0);

You're also dropping the required host memory barrier.

?

Quoted 1 line

Frankly, the current tcg_gen_atomic_xchg_reg is as optimized as you'll be able to do.  I really doubt the "avoid write 0" is measurable at all.

Helge

#3 lore raw
Quoted 11 lines · Helge Deller
Quoted 5 lines · Richard Henderson
Quoted 2 lines · Helge Deller

+        haddr = (uint32_t *)((uintptr_t)vaddr);
+        old = *haddr;

This is horribly incorrect, both for user-only and system mode.

Richard, thank you for the review!
But would you mind explaining why this is incorrect?
I thought the "vaddr = probe_access()" calculates the host address, so shouldn't it be the right address?

The vaddr name is confusing (since it implies virtual address, which the return from probe_access is not) as are the casts, which are unnecessary.

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

+        /* if already zero, do not write 0 again to reduce memory presssure */ +        if (old == 0) {
+            return 0;
+        }
+        old = qatomic_xchg(haddr, (uint32_t) 0);

You're also dropping the required host memory barrier.

?

The path through the read+test+return, without the qatomic_xchg, has no host memory barrier to provide sequential consistency of the entire operation.

r~

#4 lore raw

Hi Richard,

Quoted 8 lines · Richard Henderson
Quoted 7 lines · Helge Deller
Quoted 5 lines · Richard Henderson
Quoted 2 lines · Helge Deller

+        haddr = (uint32_t *)((uintptr_t)vaddr);
+        old = *haddr;

This is horribly incorrect, both for user-only and system mode.

Quoted 8 lines
Quoted 4 lines

Richard, thank you for the review!
But would you mind explaining why this is incorrect?
I thought the "vaddr = probe_access()" calculates the host address, so shouldn't it be the right address?

The vaddr name is confusing (since it implies virtual address, which the return from probe_access is not) as are the casts, which are unnecessary.

Still, I think my code isn't as wrong as you said.

But I tend to agree with you on this:

Quoted 2 lines

Frankly, the current tcg_gen_atomic_xchg_reg is as optimized as you'll be able to do.

tcg_gen_atomic_xchg_reg() seems to generate on x86-64:

0000000000525160 <helper_atomic_xchgl_be>:

  525160:       53                      push   %rbx
  525161:       4c 8b 44 24 08          mov    0x8(%rsp),%r8
  525166:       89 d3                   mov    %edx,%ebx
  525168:       89 ca                   mov    %ecx,%edx
  52516a:       b9 04 00 00 00          mov    $0x4,%ecx
  52516f:       e8 1c a6 ff ff          call   51f790 <atomic_mmu_lookup>
  525174:       48 89 c2                mov    %rax,%rdx
  525177:       89 d8                   mov    %ebx,%eax
  525179:       0f c8                   bswap  %eax
  52517b:       87 02                   xchg   %eax,(%rdx)
  52517d:       5b                      pop    %rbx
  52517e:       0f c8                   bswap  %eax
  525180:       c3                      ret

and atomic_mmu_lookup() is basically the same as probe_access(), so there is probably no gain in my patch.

Please ignore my patch.

Thank you!
Helge