Skip to contentSkip to conversation

The series enables Multi-Threaded TCG on PPC64

PATCH Applied Nikunj A Dadhania 2 participants 8 messages Apr 27, 2017 – May 1, 2017

lore
Share & tools
lore
#0 lore raw

v3 cover The series enables Multi-Threaded TCG on PPC64

Patch 01: Use atomic_cmpxchg in store conditional
02: Handle first write to page during atomic operation
03: Generate memory barriers for sync/isync and load/store conditional 04: Fix CPU unplug in MTTCG
05: Enable MTTCG by default on PPC64
06: Fixes a bug in PPC where the reservation is reset
causing atomic operations to never succeed

Patches are based on ppc-for-2.10

Changelog:
v2:
* David found problem related to clang and "make check" that was root caused to a tcg bug and the patch is in mainline:

79b1af9 tcg: Initialize return value after exit_atomic

* Fixed a bug in ppc_cpu_exec_enter(), which was resetting the reserve_addr, this should be done in powerpc_excp()

v1:
* Rewrote store_conditional as suggested by Richard

Bharata B Rao (1):
cpus: Fix CPU unplug for MTTCG

Nikunj A Dadhania (5):
target/ppc: Emulate LL/SC using cmpxchg helpers
cputlb: handle first atomic write to the page target/ppc: Generate fence operations
tcg: enable MTTCG by default for PPC64 on x86 target/ppc: do not reset reserve_addr in exec_enter

 configure                   |  2 ++
 cpus.c                      |  6 ++++++
 cputlb.c                    |  8 +++++++-
 target/ppc/cpu.h            |  2 ++
 target/ppc/excp_helper.c    |  3 +++
 target/ppc/translate.c      | 37 +++++++++++++++++++++++++++++++------
 target/ppc/translate_init.c |  9 ---------
 7 files changed, 51 insertions(+), 16 deletions(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH v3 1/6 target/ppc: Emulate LL/SC using cmpxchg helpers

Emulating LL/SC with cmpxchg is not correct, since it can suffer from the ABA problem. However, portable parallel code is written assuming only cmpxchg which means that in practice this is a viable alternative.

---
 target/ppc/translate.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)
target/ppc/translate.c +23 -6
index f40b5a1..50b6d4d 100644
@@ -73,6 +73,7 @@ static TCGv cpu_cfar;
 #endif
 static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca, cpu_ov32, cpu_ca32;
 static TCGv cpu_reserve;
+static TCGv cpu_reserve_val;
 static TCGv cpu_fpscr;
 static TCGv_i32 cpu_access_type;
 
@@ -181,6 +182,9 @@ void ppc_translate_init(void)
     cpu_reserve = tcg_global_mem_new(cpu_env,
                                      offsetof(CPUPPCState, reserve_addr),
                                      "reserve_addr");
+    cpu_reserve_val = tcg_global_mem_new(cpu_env,
+                                     offsetof(CPUPPCState, reserve_val),
+                                     "reserve_val");
 
     cpu_fpscr = tcg_global_mem_new(cpu_env,
                                    offsetof(CPUPPCState, fpscr), "fpscr");
@@ -3023,7 +3027,7 @@ static void gen_##name(DisasContext *ctx)                            \
     }                                                                \
     tcg_gen_qemu_ld_tl(gpr, t0, ctx->mem_idx, memop);                \
     tcg_gen_mov_tl(cpu_reserve, t0);                                 \
-    tcg_gen_st_tl(gpr, cpu_env, offsetof(CPUPPCState, reserve_val)); \
+    tcg_gen_mov_tl(cpu_reserve_val, gpr);                            \
     tcg_temp_free(t0);                                               \
 }
 
@@ -3155,14 +3159,27 @@ static void gen_conditional_store(DisasContext *ctx, TCGv EA,
 static void gen_conditional_store(DisasContext *ctx, TCGv EA,
                                   int reg, int memop)
 {
-    TCGLabel *l1;
+    TCGLabel *l1 = gen_new_label();
+    TCGLabel *l2 = gen_new_label();
+    TCGv t0;
 
-    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
-    l1 = gen_new_label();
     tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, l1);
-    tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], CRF_EQ);
-    tcg_gen_qemu_st_tl(cpu_gpr[reg], EA, ctx->mem_idx, memop);
+
+    t0 = tcg_temp_new();
+    tcg_gen_atomic_cmpxchg_tl(t0, cpu_reserve, cpu_reserve_val,
+                              cpu_gpr[reg], ctx->mem_idx,
+                              DEF_MEMOP(memop) | MO_ALIGN);
+    tcg_gen_setcond_tl(TCG_COND_EQ, t0, t0, cpu_reserve_val);
+    tcg_gen_shli_tl(t0, t0, CRF_EQ_BIT);
+    tcg_gen_or_tl(t0, t0, cpu_so);
+    tcg_gen_trunc_tl_i32(cpu_crf[0], t0);
+    tcg_temp_free(t0);
+    tcg_gen_br(l2);
+
     gen_set_label(l1);
+    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
+
+    gen_set_label(l2);
     tcg_gen_movi_tl(cpu_reserve, -1);
 }
 #endif
Signature · 1 line
2.9.3
#2 lore raw

PATCH v3 2/6 cputlb: handle first atomic write to the page

In reply to Nikunj A Dadhania

In case where the conditional write is the first write to the page, TLB_NOTDIRTY will be set and stop_the_world is triggered. Handle this as a special case and set the dirty bit. After that fall through to the actual atomic instruction below.

---
 cputlb.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
cputlb.c +7 -1
index f5d056c..743776a 100644
@@ -930,7 +930,13 @@ static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
         tlb_addr = tlbe->addr_write;
     }
 
-    /* Notice an IO access, or a notdirty page.  */
+    /* Check notdirty */
+    if (unlikely(tlb_addr & TLB_NOTDIRTY)) {
+        tlb_set_dirty(ENV_GET_CPU(env), addr);
+        tlb_addr = tlb_addr & ~TLB_NOTDIRTY;
+    }
+
+    /* Notice an IO access  */
     if (unlikely(tlb_addr & ~TARGET_PAGE_MASK)) {
         /* There's really nothing that can be done to
            support this apart from stop-the-world.  */
Signature · 1 line
2.9.3
#3 lore raw

PATCH v3 3/6 target/ppc: Generate fence operations

In reply to Nikunj A Dadhania
---
 target/ppc/translate.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/ppc/translate.c +8 -0
index 50b6d4d..4a1f24a 100644
@@ -2971,6 +2971,7 @@ static void gen_stswx(DisasContext *ctx)
 /* eieio */
 static void gen_eieio(DisasContext *ctx)
 {
+    tcg_gen_mb(TCG_MO_LD_ST | TCG_BAR_SC);
 }
 
 #if !defined(CONFIG_USER_ONLY)
@@ -3008,6 +3009,7 @@ static void gen_isync(DisasContext *ctx)
     if (!ctx->pr) {
         gen_check_tlb_flush(ctx, false);
     }
+    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
     gen_stop_exception(ctx);
 }
 
@@ -3028,6 +3030,7 @@ static void gen_##name(DisasContext *ctx)                            \
     tcg_gen_qemu_ld_tl(gpr, t0, ctx->mem_idx, memop);                \
     tcg_gen_mov_tl(cpu_reserve, t0);                                 \
     tcg_gen_mov_tl(cpu_reserve_val, gpr);                            \
+    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);                           \
     tcg_temp_free(t0);                                               \
 }
 
@@ -3177,6 +3180,10 @@ static void gen_conditional_store(DisasContext *ctx, TCGv EA,
     tcg_gen_br(l2);
 
     gen_set_label(l1);
+
+    /* Address mismatch implies failure.  But we still need to provide the
+       memory barrier semantics of the instruction.  */
+    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
 
     gen_set_label(l2);
@@ -3308,6 +3315,7 @@ static void gen_sync(DisasContext *ctx)
     if (((l == 2) || !(ctx->insns_flags & PPC_64B)) && !ctx->pr) {
         gen_check_tlb_flush(ctx, true);
     }
+    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
 }
 
 /* wait */
Signature · 1 line
2.9.3
#4 lore raw

PATCH v3 4/6 cpus: Fix CPU unplug for MTTCG

In reply to Nikunj A Dadhania

From: Bharata B Rao <[email protected]>

Ensure that the unplugged CPU thread is destroyed and the waiting thread is notified about it. This is needed for CPU unplug to work correctly in MTTCG mode.

---
 cpus.c | 6 ++++++
 1 file changed, 6 insertions(+)
cpus.c +6 -0
index 740b8dc..79f780b 100644
@@ -1483,6 +1483,12 @@ static void *qemu_tcg_cpu_thread_fn(void *arg)
                 /* Ignore everything else? */
                 break;
             }
+        } else if (cpu->unplug) {
+            qemu_tcg_destroy_vcpu(cpu);
+            cpu->created = false;
+            qemu_cond_signal(&qemu_cpu_cond);
+            qemu_mutex_unlock_iothread();
+            return NULL;
         }
 
         atomic_mb_set(&cpu->exit_request, 0);
Signature · 1 line
2.9.3
#5 lore raw

PATCH v3 5/6 tcg: enable MTTCG by default for PPC64 on x86

In reply to Nikunj A Dadhania

This enables the multi-threaded system emulation by default for PPC64 guests using the x86_64 TCG back-end.

---
 configure        | 2 ++
 target/ppc/cpu.h | 2 ++
 2 files changed, 4 insertions(+)
configure +2 -0
index c35acf1..3814617 100755
@@ -6090,12 +6090,14 @@ case "$target_name" in
   ppc64)
     TARGET_BASE_ARCH=ppc
     TARGET_ABI_DIR=ppc
+    mttcg=yes
     gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml power-vsx.xml"
   ;;
   ppc64le)
     TARGET_ARCH=ppc64
     TARGET_BASE_ARCH=ppc
     TARGET_ABI_DIR=ppc
+    mttcg=yes
     gdb_xml_files="power64-core.xml power-fpu.xml power-altivec.xml power-spe.xml power-vsx.xml"
   ;;
   ppc64abi32)
target/ppc/cpu.h +2 -0
index e0ff041..ece535d 100644
@@ -30,6 +30,8 @@
 #define TARGET_LONG_BITS 64
 #define TARGET_PAGE_BITS 12
 
+#define TCG_GUEST_DEFAULT_MO 0
+
 /* Note that the official physical address space bits is 62-M where M
    is implementation dependent.  I've not looked up M for the set of
    cpus we emulate at the system level.  */
Signature · 1 line
2.9.3
#6 lore raw

PATCH v3 6/6 target/ppc: do not reset reserve_addr in exec_enter

In reply to Nikunj A Dadhania

In case when atomic operation is not supported, exit_atomic is called and we stop the world and execute the atomic operation. This results in a following call chain:

tcg_gen_atomic_cmpxchg_tl()
-> gen_helper_exit_atomic()

     -> HELPER(exit_atomic)
        -> cpu_loop_exit_atomic() -> EXCP_ATOMIC
           -> qemu_tcg_cpu_thread_fn() => case EXCP_ATOMIC
              -> cpu_exec_step_atomic()
                 -> cpu_step_atomic()
                    -> cc->cpu_exec_enter() = ppc_cpu_exec_enter()
                       Sets env->reserve_addr = -1;

But by the time it return back, the reservation is erased and the code fails, this continues forever and the lock is never taken.

Instead set this in powerpc_excp()

Now that ppc_cpu_exec_enter() doesn't have anything meaningful to do, let us get rid of the function.

---
 target/ppc/excp_helper.c    | 3 +++
 target/ppc/translate_init.c | 9 ---------
 2 files changed, 3 insertions(+), 9 deletions(-)
target/ppc/excp_helper.c +3 -0
index f4ee7aa..a6bcb47 100644
@@ -728,6 +728,9 @@ static inline void powerpc_excp(PowerPCCPU *cpu, int excp_model, int excp)
     cs->exception_index = POWERPC_EXCP_NONE;
     env->error_code = 0;
 
+    /* Reset the reservation */
+    env->reserve_addr = -1;
+
     /* Any interrupt is context synchronizing, check if TCG TLB
      * needs a delayed flush on ppc64
      */
target/ppc/translate_init.c +0 -9
index e82e3e6..9b048cd 100644
@@ -10436,14 +10436,6 @@ static bool ppc_cpu_has_work(CPUState *cs)
     return msr_ee && (cs->interrupt_request & CPU_INTERRUPT_HARD);
 }
 
-static void ppc_cpu_exec_enter(CPUState *cs)
-{
-    PowerPCCPU *cpu = POWERPC_CPU(cs);
-    CPUPPCState *env = &cpu->env;
-
-    env->reserve_addr = -1;
-}
-
 /* CPUClass::reset() */
 static void ppc_cpu_reset(CPUState *s)
 {
@@ -10660,7 +10652,6 @@ static void ppc_cpu_class_init(ObjectClass *oc, void *data)
     cc->get_phys_page_debug = ppc_cpu_get_phys_page_debug;
     cc->vmsd = &vmstate_ppc_cpu;
 #endif
-    cc->cpu_exec_enter = ppc_cpu_exec_enter;
 #if defined(CONFIG_SOFTMMU)
     cc->write_elf64_note = ppc64_cpu_write_elf64_note;
     cc->write_elf32_note = ppc32_cpu_write_elf32_note;
Signature · 1 line
2.9.3
Applied
#7 lore raw
In reply to Nikunj A Dadhania
Quoted 9 lines · Nikunj A Dadhania

Patch 01: Use atomic_cmpxchg in store conditional
02: Handle first write to page during atomic operation
03: Generate memory barriers for sync/isync and load/store conditional 04: Fix CPU unplug in MTTCG
05: Enable MTTCG by default on PPC64
06: Fixes a bug in PPC where the reservation is reset
causing atomic operations to never succeed

Patches are based on ppc-for-2.10

Applied to ppc-for-2.10, thanks.

Quoted 33 lines

Changelog:
v2:
* David found problem related to clang and "make check" that was root caused to a tcg bug and the patch is in mainline:

79b1af9 tcg: Initialize return value after exit_atomic

* Fixed a bug in ppc_cpu_exec_enter(), which was resetting the reserve_addr, this should be done in powerpc_excp()

v1:
* Rewrote store_conditional as suggested by Richard

Bharata B Rao (1):
cpus: Fix CPU unplug for MTTCG

Nikunj A Dadhania (5):
target/ppc: Emulate LL/SC using cmpxchg helpers
cputlb: handle first atomic write to the page target/ppc: Generate fence operations
tcg: enable MTTCG by default for PPC64 on x86 target/ppc: do not reset reserve_addr in exec_enter

 configure                   |  2 ++
 cpus.c                      |  6 ++++++
 cputlb.c                    |  8 +++++++-
 target/ppc/cpu.h            |  2 ++
 target/ppc/excp_helper.c    |  3 +++
 target/ppc/translate.c      | 37 +++++++++++++++++++++++++++++++------
 target/ppc/translate_init.c |  9 ---------
 7 files changed, 51 insertions(+), 16 deletions(-)
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson