Skip to contentSkip to conversation

Improve cmpxchg emulation.

PATCH In review Jonathan A. Kollasch 2 participants 2 messages Sep 8, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improve cmpxchg emulation.

Change the accumulator only after performing the redundant write during cmpxchg. This fixes pthreaded programs using fork() in NetBSD/i386 guests.

Quoted 1 line

From Andreas Gustafsson in https://bugs.launchpad.net/qemu/+bug/569760.

---
 target-i386/translate.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
target-i386/translate.c +8 -4
index 7b6e3c2..391830f 100644
@@ -4879,20 +4879,24 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)
             tcg_gen_sub_tl(t2, cpu_regs[R_EAX], t0);
             gen_extu(ot, t2);
             tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
+	    label2 = gen_new_label();
             if (mod == 3) {
-                label2 = gen_new_label();
                 gen_op_mov_reg_v(ot, R_EAX, t0);
                 tcg_gen_br(label2);
                 gen_set_label(label1);
                 gen_op_mov_reg_v(ot, rm, t1);
-                gen_set_label(label2);
             } else {
-                tcg_gen_mov_tl(t1, t0);
+                /* perform no-op store cycle like physical cpu; must be
+		   before changing accumulator to ensure idempotency if
+		   the store faults and the instruction is restarted
+		*/
+                gen_op_st_v(ot + s->mem_index, t0, a0);
                 gen_op_mov_reg_v(ot, R_EAX, t0);
+                tcg_gen_br(label2);
                 gen_set_label(label1);
-                /* always store */
                 gen_op_st_v(ot + s->mem_index, t1, a0);
             }
+	    gen_set_label(label2);
             tcg_gen_mov_tl(cpu_cc_src, t0);
             tcg_gen_mov_tl(cpu_cc_dst, t2);
             s->cc_op = CC_OP_SUBB + ot;
Signature · 1 line
1.6.6.2
#1 lore raw
Quoted 5 lines · Jonathan A. Kollasch

Change the accumulator only after performing the redundant write during cmpxchg. This fixes pthreaded programs using fork() in NetBSD/i386 guests.

Quoted 1 line

From Andreas Gustafsson in https://bugs.launchpad.net/qemu/+bug/569760.

This is tab damaged.

Secondly it looks as if this addresses only a small part of a general problem [1], also in a very naive and inefficient way, while also opening a hole can of worms (should real parallel execution for TCG be ever implemented)

[1] http://www.mail-archive.com/[email protected]/msg40000.html

Quoted 40 lines
---
 target-i386/translate.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
target-i386/translate.c +8 -4
index 7b6e3c2..391830f 100644
@@ -4879,20 +4879,24 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)
             tcg_gen_sub_tl(t2, cpu_regs[R_EAX], t0);
             gen_extu(ot, t2);
             tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, label1);
+	    label2 = gen_new_label();
             if (mod == 3) {
-                label2 = gen_new_label();
                 gen_op_mov_reg_v(ot, R_EAX, t0);
                 tcg_gen_br(label2);
                 gen_set_label(label1);
                 gen_op_mov_reg_v(ot, rm, t1);
-                gen_set_label(label2);
             } else {
-                tcg_gen_mov_tl(t1, t0);
+                /* perform no-op store cycle like physical cpu; must be
+		   before changing accumulator to ensure idempotency if
+		   the store faults and the instruction is restarted
+		*/
+                gen_op_st_v(ot + s->mem_index, t0, a0);
                 gen_op_mov_reg_v(ot, R_EAX, t0);
+                tcg_gen_br(label2);
                 gen_set_label(label1);
-                /* always store */
                 gen_op_st_v(ot + s->mem_index, t1, a0);
             }
+	    gen_set_label(label2);
             tcg_gen_mov_tl(cpu_cc_src, t0);
             tcg_gen_mov_tl(cpu_cc_dst, t2);
             s->cc_op = CC_OP_SUBB + ot;
Signature · 1 line
mailto:[email protected]