Skip to contentSkip to conversation

tcg: Add fence gen support

RFC PATCH Superseded Pranith Kumar 6 participants 61 messages May 31, 2016 – Jun 16, 2016

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover tcg: Add fence gen support

Hello,

The following series adds fence instruction generation support to TCG. The current work has been rebased on-top of Richard's patch series.

This has been tested and confirmed to fix ordering issues on a x86 host with MTTCG enabled ARMv7 guest using KVM unit tests.

Pranith Kumar (13):
Introduce TCGOpcode for memory barrier
tcg/i386: Add support for fence
tcg/aarch64: Add support for fence
tcg/arm: Add support for fence
tcg/ia64: Add support for fence
tcg/mips: Add support for fence
tcg/ppc: Add support for fence
tcg/s390: Add support for fence
tcg/sparc: Add support for fence
tcg/tci: Add support for fence
target-arm: Generate fences in ARMv7 frontend
target-alpha: Generate fence op
tcg: Generate fences only for SMP MTTCG guests

 target-alpha/translate.c     |  5 +++--
 target-arm/translate.c       |  7 +++++--
 tcg/README                   | 17 +++++++++++++++++
 tcg/aarch64/tcg-target.inc.c |  7 +++++++
 tcg/arm/tcg-target.inc.c     | 12 ++++++++++++
 tcg/i386/tcg-target.inc.c    | 35 +++++++++++++++++++++++++++++++++++
 tcg/ia64/tcg-target.inc.c    |  5 +++++
 tcg/mips/tcg-target.inc.c    |  6 ++++++
 tcg/ppc/tcg-target.inc.c     |  8 ++++++++
 tcg/s390/tcg-target.inc.c    |  9 +++++++++
 tcg/sparc/tcg-target.inc.c   |  8 ++++++++
 tcg/tcg-op.c                 |  9 +++++++++
 tcg/tcg-op.h                 |  2 ++
 tcg/tcg-opc.h                |  2 ++
 tcg/tcg.h                    |  8 ++++++++
 tcg/tci/tcg-target.inc.c     |  3 +++
 tci.c                        |  3 +++
 17 files changed, 142 insertions(+), 4 deletions(-)
Signature · 1 line
2.8.3
#1 lore raw

RFC PATCH v2 1/13 Introduce TCGOpcode for memory barrier

This commit introduces the TCGOpcode for memory barrier instruction.

This opcode takes an argument which is the type of memory barrier which should be generated.

---
 tcg/README    | 17 +++++++++++++++++
 tcg/tcg-op.c  |  6 ++++++
 tcg/tcg-op.h  |  2 ++
 tcg/tcg-opc.h |  2 ++
 tcg/tcg.h     |  8 ++++++++
 5 files changed, 35 insertions(+)
tcg/README +17 -0
index f4a8ac1..cfe79d7 100644
@@ -402,6 +402,23 @@ double-word product T0.  The later is returned in two single-word outputs.
 
 Similar to mulu2, except the two inputs T1 and T2 are signed.
 
+********* Memory Barrier support
+
+* mb <$arg>
+
+Generate a target memory barrier instruction to ensure memory ordering as being
+enforced by a corresponding guest memory barrier instruction. The ordering
+enforced by the backend may be stricter than the ordering required by the guest.
+It cannot be weaker. This opcode takes an optional constant argument if required
+to generate the appropriate barrier instruction. The backend should take care to
+emit the target barrier instruction only when necessary i.e., for SMP guests and
+when MTTCG is enabled.
+
+The guest translators should generate this opcode for all guest instructions
+which have ordering side effects.
+
+Please see docs/atomics.txt for more information on memory barriers.
+
 ********* 64-bit guest on 32-bit host support
 
 The following opcodes are internal to TCG.  Thus they are to be implemented by
tcg/tcg-op.c +6 -0
index f554b86..a6f01a7 100644
@@ -143,6 +143,12 @@ void tcg_gen_op6(TCGContext *ctx, TCGOpcode opc, TCGArg a1, TCGArg a2,
     tcg_emit_op(ctx, opc, pi);
 }
 
+void tcg_gen_mb(TCGArg a)
+{
+    /* ??? Enable only when MTTCG is enabled.  */
+    tcg_gen_op1(&tcg_ctx, INDEX_op_mb, 0);
+}
+
 /* 32 bit ops */
 
 void tcg_gen_addi_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
tcg/tcg-op.h +2 -0
index c446d3d..40920fb 100644
@@ -261,6 +261,8 @@ static inline void tcg_gen_br(TCGLabel *l)
     tcg_gen_op1(&tcg_ctx, INDEX_op_br, label_arg(l));
 }
 
+void tcg_gen_mb(TCGArg a);
+
 /* Helper calls. */
 
 /* 32 bit ops */
tcg/tcg-opc.h +2 -0
index 6d0410c..c0f3e83 100644
@@ -42,6 +42,8 @@ DEF(br, 0, 0, 1, TCG_OPF_BB_END)
 # define IMPL64  TCG_OPF_64BIT
 #endif
 
+DEF(mb, 0, 1, 0, 0)
+
 DEF(mov_i32, 1, 1, 0, TCG_OPF_NOT_PRESENT)
 DEF(movi_i32, 1, 0, 1, TCG_OPF_NOT_PRESENT)
 DEF(setcond_i32, 1, 2, 1, 0)
tcg/tcg.h +8 -0
index a46d17c..a1d59f7 100644
@@ -385,6 +385,14 @@ static inline intptr_t QEMU_ARTIFICIAL GET_TCGV_PTR(TCGv_ptr t)
 #define TCG_CALL_DUMMY_TCGV     MAKE_TCGV_I32(-1)
 #define TCG_CALL_DUMMY_ARG      ((TCGArg)(-1))
 
+/* TCGOpmb args */
+#define TCG_MB_FULL             ((TCGArg)(0))
+#define TCG_MB_READ             ((TCGArg)(1))
+#define TCG_MB_WRITE            ((TCGArg)(2))
+#define TCG_MB_ACQUIRE          ((TCGArg)(3))
+#define TCG_MB_RELEASE          ((TCGArg)(4))
+
+
 /* Conditions.  Note that these are laid out for easy manipulation by
    the functions below:
      bit 0 is used for inverting;
Signature · 1 line
2.8.3
#2 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

tcg/README double-word product T0. The later is returned in two single-word outputs. · line 407

Quoted 3 lines · Pranith Kumar
tcg/README +3 -0
+********* Memory Barrier support
+
+* mb <$arg>

Document what $arg should be.

tcg/README double-word product T0. The later is returned in two single-word outputs. · line 413

Quoted 5 lines
tcg/README +5 -0
+Generate a target memory barrier instruction to ensure memory ordering as being
+enforced by a corresponding guest memory barrier instruction. The ordering
+enforced by the backend may be stricter than the ordering required by the guest.
+It cannot be weaker. This opcode takes an optional constant argument if required
+to generate the appropriate barrier instruction. The backend should take care to

The argument is *not* optional.

tcg/tcg-op.c void tcg_gen_op6(TCGContext *ctx, TCGOpcode opc, TCGArg a1, TCGArg a2, · line 149

Quoted 4 lines
tcg/tcg-op.c +4 -0
+void tcg_gen_mb(TCGArg a)
+{
+    /* ??? Enable only when MTTCG is enabled.  */
+    tcg_gen_op1(&tcg_ctx, INDEX_op_mb, 0);

Pass A to tcg_gen_op1, not 0.

tcg/tcg.h static inline intptr_t QEMU_ARTIFICIAL GET_TCGV_PTR(TCGv_ptr t) · line 393

Quoted 6 lines
tcg/tcg.h +6 -0
+/* TCGOpmb args */
+#define TCG_MB_FULL             ((TCGArg)(0))
+#define TCG_MB_READ             ((TCGArg)(1))
+#define TCG_MB_WRITE            ((TCGArg)(2))
+#define TCG_MB_ACQUIRE          ((TCGArg)(3))
+#define TCG_MB_RELEASE          ((TCGArg)(4))

This is, IMO, confused. Either we should use the C++11 barrier types, or the Linux barrier types, but not both.

r~

#3 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Hi Richard,

Thanks for the review. I will make the changes you pointed out. One point below:

Quoted 12 lines · Richard Henderson

tcg/tcg.h static inline intptr_t QEMU_ARTIFICIAL GET_TCGV_PTR(TCGv_ptr t) · line 393

Quoted 6 lines · Pranith Kumar
tcg/tcg.h +6 -0
+/* TCGOpmb args */
+#define TCG_MB_FULL             ((TCGArg)(0))
+#define TCG_MB_READ             ((TCGArg)(1))
+#define TCG_MB_WRITE            ((TCGArg)(2))
+#define TCG_MB_ACQUIRE          ((TCGArg)(3))
+#define TCG_MB_RELEASE          ((TCGArg)(4))

This is, IMO, confused. Either we should use the C++11 barrier types, or the Linux barrier types, but not both.

This part of the design is still being fleshed out. The above listing is all the kinds of barriers we can encounter during translation. May be I am missing something, but C++11 and Linux barrier types have higher level semantics which is difficult to infer at the instruction level.

All we want to do here is map a barrier instruction from guest to a barrier instruction on hist. This mapping is 1:1 if the host has barrier instructions with matching semantics. Otherwise we generate a multi-op instruction sequence. Some examples are: load acquire(ldar) on ARM64 guest will map to dmb+load on ARMv7 target, store fence(sfence) on x86 guest will map to dmb on ARMv7 target.

So we need to support all kinds of barrier types we expect the guest to have, then map them to the host.

Thanks,

Signature · 1 line
Pranith
#4 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 8 lines · Pranith Kumar
Quoted 2 lines

This is, IMO, confused. Either we should use the C++11 barrier types, or the Linux barrier types, but not both.

This part of the design is still being fleshed out. The above listing is all the kinds of barriers we can encounter during translation. May be I am missing something, but C++11 and Linux barrier types have higher level semantics which is difficult to infer at the instruction level.

Fair enough.

Quoted 6 lines

All we want to do here is map a barrier instruction from guest to a barrier instruction on hist. This mapping is 1:1 if the host has barrier instructions with matching semantics. Otherwise we generate a multi-op instruction sequence. Some examples are: load acquire(ldar) on ARM64 guest will map to dmb+load on ARMv7 target, store fence(sfence) on x86 guest will map to dmb on ARMv7 target.

Perhaps we should model this like the Sparc membar instruction, with individual bits for all combinations of Load x Store. One can then describe exactly what the target semantics are for each barrier.

r~

#5 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 11 lines · Richard Henderson
Quoted 6 lines · Pranith Kumar

All we want to do here is map a barrier instruction from guest to a barrier instruction on hist. This mapping is 1:1 if the host has barrier instructions with matching semantics. Otherwise we generate a multi-op instruction sequence. Some examples are: load acquire(ldar) on ARM64 guest will map to dmb+load on ARMv7 target, store fence(sfence) on x86 guest will map to dmb on ARMv7 target.

Perhaps we should model this like the Sparc membar instruction, with individual bits for all combinations of Load x Store. One can then describe exactly what the target semantics are for each barrier.

Seconded. Given that we don't support Alpha host we can ignore its unique "Dependent loads reordered" feature. I suppose we're not going to add Alpha host support :)

Kind regards,
Sergey

#6 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

In reply to Pranith Kumar
Quoted 12 lines · Pranith Kumar
tcg/README +4 -0
index f4a8ac1..cfe79d7 100644
@@ -402,6 +402,23 @@ double-word product T0.  The later is returned in two single-word outputs.
 
 Similar to mulu2, except the two inputs T1 and T2 are signed.
 
+********* Memory Barrier support
+
+* mb <$arg>
+

tcg/README double-word product T0. The later is returned in two single-word outputs. · line 418

Quoted 10 lines
tcg/README +10 -0
+Generate a target memory barrier instruction to ensure memory ordering as being
+enforced by a corresponding guest memory barrier instruction. The ordering
+enforced by the backend may be stricter than the ordering required by the guest.
+It cannot be weaker. This opcode takes an optional constant argument if required
+to generate the appropriate barrier instruction. The backend should take care to
+emit the target barrier instruction only when necessary i.e., for SMP guests and
+when MTTCG is enabled.
+
+The guest translators should generate this opcode for all guest instructions
+which have ordering side effects.

I think we need to extend TCG load/store instruction attributes to provide information about guest ordering requirements and leave this TCG operation only for explicit barrier instruction translation.

Quoted 11 lines
tcg/README +3 -0
+
+Please see docs/atomics.txt for more information on memory barriers.
+
 ********* 64-bit guest on 32-bit host support
 
 The following opcodes are internal to TCG.  Thus they are to be implemented by
tcg/tcg-op.c +0 -0
index f554b86..a6f01a7 100644

tcg/tcg-op.c void tcg_gen_op6(TCGContext *ctx, TCGOpcode opc, TCGArg a1, TCGArg a2, · line 149

Quoted 7 lines
tcg/tcg-op.c +4 -0
@@ -143,6 +143,12 @@ void tcg_gen_op6(TCGContext *ctx, TCGOpcode opc, TCGArg a1, TCGArg a2,
     tcg_emit_op(ctx, opc, pi);
 }
 
+void tcg_gen_mb(TCGArg a)
+{
+    /* ??? Enable only when MTTCG is enabled.  */
+    tcg_gen_op1(&tcg_ctx, INDEX_op_mb, 0);

Yes, this could be a right place to check for MTTCG enabled and number of CPUs emulated. If we do it here, then we should adjust the documentation stating that the backend should take care of it.

Kind regards,
Sergey

#7 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 19 lines · Sergey Fedorov
Quoted 18 lines · Pranith Kumar
tcg/README +10 -0
index f4a8ac1..cfe79d7 100644
@@ -402,6 +402,23 @@ double-word product T0.  The later is returned in two single-word outputs.
 
 Similar to mulu2, except the two inputs T1 and T2 are signed.
 
+********* Memory Barrier support
+
+* mb <$arg>
+
+Generate a target memory barrier instruction to ensure memory ordering as being
+enforced by a corresponding guest memory barrier instruction. The ordering
+enforced by the backend may be stricter than the ordering required by the guest.
+It cannot be weaker. This opcode takes an optional constant argument if required
+to generate the appropriate barrier instruction. The backend should take care to
+emit the target barrier instruction only when necessary i.e., for SMP guests and
Quoted 9 lines

tcg/README double-word product T0. The later is returned in two single-word outputs. · line 418

Quoted 4 lines
tcg/README +4 -0
+when MTTCG is enabled.
+
+The guest translators should generate this opcode for all guest instructions
+which have ordering side effects.

I think we need to extend TCG load/store instruction attributes to provide information about guest ordering requirements and leave this TCG operation only for explicit barrier instruction translation.

Yes, I am working on adding flag argument to the TCG MemOp which indicates this.

Quoted 14 lines
Quoted 14 lines
tcg/README +3 -0
+
+Please see docs/atomics.txt for more information on memory barriers.
+
 ********* 64-bit guest on 32-bit host support
 
 The following opcodes are internal to TCG.  Thus they are to be implemented by
tcg/tcg-op.c +0 -0
index f554b86..a6f01a7 100644
@@ -143,6 +143,12 @@ void tcg_gen_op6(TCGContext *ctx, TCGOpcode opc, TCGArg a1, TCGArg a2,
     tcg_emit_op(ctx, opc, pi);
 }
 
Quoted 9 lines

tcg/tcg-op.c void tcg_gen_op6(TCGContext *ctx, TCGOpcode opc, TCGArg a1, TCGArg a2, · line 149

Quoted 4 lines
tcg/tcg-op.c +4 -0
+void tcg_gen_mb(TCGArg a)
+{
+    /* ??? Enable only when MTTCG is enabled.  */
+    tcg_gen_op1(&tcg_ctx, INDEX_op_mb, 0);

Yes, this could be a right place to check for MTTCG enabled and number of CPUs emulated. If we do it here, then we should adjust the documentation stating that the backend should take care of it.

I added the check in Patch 13. I will update the documentation to reflect this.

Thanks,

Signature · 1 line
Pranith
#8 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 2 lines · Pranith Kumar

Yes, I am working on adding flag argument to the TCG MemOp which indicates this.

Please don't. I don't think this is the right way to go.

r~

#9 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

In reply to Sergey Fedorov
Quoted 3 lines · Sergey Fedorov

I think we need to extend TCG load/store instruction attributes to provide information about guest ordering requirements and leave this TCG operation only for explicit barrier instruction translation.

I do not agree. I think separate barriers are much cleaner and easier to manage and reason with.

r~

#10 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 8 lines · Richard Henderson
Quoted 3 lines · Sergey Fedorov

I think we need to extend TCG load/store instruction attributes to provide information about guest ordering requirements and leave this TCG operation only for explicit barrier instruction translation.

I do not agree. I think separate barriers are much cleaner and easier to manage and reason with.

How are we going to emulate strongly-ordered guests on weakly-ordered hosts then? I think if every load/store operation must specify which ordering it implies then this task would be quite simple.

Kind regards,
Sergey

#11 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 6 lines · Sergey Fedorov
Quoted 5 lines · Richard Henderson
Quoted 3 lines · Sergey Fedorov

I think we need to extend TCG load/store instruction attributes to provide information about guest ordering requirements and leave this TCG operation only for explicit barrier instruction translation.

Quoted 7 lines
Quoted 3 lines

I do not agree. I think separate barriers are much cleaner and easier to manage and reason with.

How are we going to emulate strongly-ordered guests on weakly-ordered hosts then? I think if every load/store operation must specify which ordering it implies then this task would be quite simple.

Hum. That does seem helpful-ish. But I'm not certain how helpful it is to complicate the helper functions even further.

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32 // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64 // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128 // prevent later loads from crossing current op
MO_BAR_ST_A = 256 // prevent later stores from crossing current op

MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A

// Match Sparc MEMBAR as the most flexible host.
TCG_BAR_LD_LD = 1 // #LoadLoad barrier
TCG_BAR_ST_LD = 2 // #StoreLoad barrier
TCG_BAR_LD_ST = 4 // #LoadStore barrier
TCG_BAR_ST_ST = 8 // #StoreStore barrier
TCG_BAR_SYNC = 64 // SEQ_CST barrier

where

tcg_gen_qemu_ld_i32(x, y, i, m | MO_BAR_LD_BEFORE | MO_BAR_ST_AFTER)

emits

mb TCG_BAR_LD_LD
qemu_ld_i32 x, y, i, m
mb TCG_BAR_LD_ST

We can then add an optimization pass which folds barriers with no memory operations in between, so that duplicates are eliminated.

r~

#12 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 41 lines · Richard Henderson
Quoted 7 lines · Sergey Fedorov
Quoted 6 lines · Richard Henderson
Quoted 4 lines · Sergey Fedorov

I think we need to extend TCG load/store instruction attributes to provide information about guest ordering requirements and leave this TCG
operation only for explicit barrier instruction translation.

Quoted 7 lines
Quoted 3 lines

I do not agree. I think separate barriers are much cleaner and easier to manage and reason with.

How are we going to emulate strongly-ordered guests on weakly-ordered hosts then? I think if every load/store operation must specify which ordering it implies then this task would be quite simple.

Hum. That does seem helpful-ish. But I'm not certain how helpful it is to complicate the helper functions even further.

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32 // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64 // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128 // prevent later loads from crossing current op
MO_BAR_ST_A = 256 // prevent later stores from crossing current op

MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1    // #LoadLoad barrier
TCG_BAR_ST_LD = 2    // #StoreLoad barrier
TCG_BAR_LD_ST = 4    // #LoadStore barrier
TCG_BAR_ST_ST = 8    // #StoreStore barrier

TCG_BAR_SYNC = 64 // SEQ_CST barrier

where

tcg_gen_qemu_ld_i32(x, y, i, m | MO_BAR_LD_BEFORE | MO_BAR_ST_AFTER)

Quoted 8 lines

emits

mb TCG_BAR_LD_LD
qemu_ld_i32 x, y, i, m
mb TCG_BAR_LD_ST

We can then add an optimization pass which folds barriers with no memory operations in between, so that duplicates are eliminated.

It would give us three TCG operations for each memory operation instead of one. But then we might like to combine these barrier operations back with memory operations in each backend. If we propagate memory ordering semantics up to the backend, it can decide itself what instructions are best to generate.

So I would just focus on translating only explicit memory barrier operations for now.

Kind regards,
Sergey

#13 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 48 lines · Sergey Fedorov
Quoted 47 lines · Richard Henderson
Quoted 7 lines · Sergey Fedorov
Quoted 6 lines · Richard Henderson
Quoted 4 lines · Sergey Fedorov

I think we need to extend TCG load/store instruction attributes to provide information about guest ordering requirements and leave this TCG
operation only for explicit barrier instruction translation.

Quoted 7 lines
Quoted 3 lines

I do not agree. I think separate barriers are much cleaner and easier to manage and reason with.

How are we going to emulate strongly-ordered guests on weakly-ordered hosts then? I think if every load/store operation must specify which ordering it implies then this task would be quite simple.

Hum. That does seem helpful-ish. But I'm not certain how helpful it is to complicate the helper functions even further.

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32 // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64 // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128 // prevent later loads from crossing current op
MO_BAR_ST_A = 256 // prevent later stores from crossing current op

MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1    // #LoadLoad barrier
TCG_BAR_ST_LD = 2    // #StoreLoad barrier
TCG_BAR_LD_ST = 4    // #LoadStore barrier
TCG_BAR_ST_ST = 8    // #StoreStore barrier

TCG_BAR_SYNC = 64 // SEQ_CST barrier

where

tcg_gen_qemu_ld_i32(x, y, i, m | MO_BAR_LD_BEFORE | MO_BAR_ST_AFTER)

emits

mb TCG_BAR_LD_LD
qemu_ld_i32 x, y, i, m
mb TCG_BAR_LD_ST

Quoted 8 lines
Quoted 2 lines

We can then add an optimization pass which folds barriers with no memory operations in between, so that duplicates are eliminated.

It would give us three TCG operations for each memory operation instead of one. But then we might like to combine these barrier operations back with memory operations in each backend. If we propagate memory ordering semantics up to the backend, it can decide itself what instructions are best to generate.

A strongly ordered target would generally only set BEFORE bits or AFTER bits, but not both (and I suggest we canonicalize on AFTER for all such targets). Thus a strongly ordered target would produce only 2 opcodes per memory op.

I supplied both to make it easier to handle a weakly ordered target with acquire/release bits.

I would *not* combine the barrier operations back with memory operations in the backend. Only armv8 and ia64 can do that, and given the optimization level at which we generate code, I doubt it would really make much difference above separate barriers.

Quoted 2 lines

So I would just focus on translating only explicit memory barrier operations for now.

Then why did you bring it up?

r~

#14 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 64 lines · Richard Henderson
Quoted 49 lines · Sergey Fedorov
Quoted 48 lines · Richard Henderson
Quoted 7 lines · Sergey Fedorov
Quoted 6 lines · Richard Henderson
Quoted 4 lines · Sergey Fedorov

I think we need to extend TCG load/store instruction attributes to provide information about guest ordering requirements and leave this TCG
operation only for explicit barrier instruction translation.

Quoted 8 lines
Quoted 4 lines

I do not agree. I think separate barriers are much cleaner and easier
to manage and reason with.

How are we going to emulate strongly-ordered guests on weakly-ordered hosts then? I think if every load/store operation must specify which ordering it implies then this task would be quite simple.

Hum. That does seem helpful-ish. But I'm not certain how helpful it is to complicate the helper functions even further.

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32 // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64 // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128 // prevent later loads from crossing current op
MO_BAR_ST_A = 256 // prevent later stores from crossing current op

MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1    // #LoadLoad barrier
TCG_BAR_ST_LD = 2    // #StoreLoad barrier
TCG_BAR_LD_ST = 4    // #LoadStore barrier
TCG_BAR_ST_ST = 8    // #StoreStore barrier

TCG_BAR_SYNC = 64 // SEQ_CST barrier

where

tcg_gen_qemu_ld_i32(x, y, i, m | MO_BAR_LD_BEFORE | MO_BAR_ST_AFTER)

emits

mb TCG_BAR_LD_LD
qemu_ld_i32 x, y, i, m
mb TCG_BAR_LD_ST

Quoted 8 lines
Quoted 2 lines

We can then add an optimization pass which folds barriers with no memory operations in between, so that duplicates are eliminated.

It would give us three TCG operations for each memory operation instead of one. But then we might like to combine these barrier operations back with memory operations in each backend. If we propagate memory ordering semantics up to the backend, it can decide itself what instructions are best to generate.

A strongly ordered target would generally only set BEFORE bits or AFTER bits, but not both (and I suggest we canonicalize on AFTER for all such targets). Thus a strongly ordered target would produce only 2 opcodes per memory op.

Quoted 7 lines

I supplied both to make it easier to handle a weakly ordered target with acquire/release bits.

I would *not* combine the barrier operations back with memory operations in the backend. Only armv8 and ia64 can do that, and given the optimization level at which we generate code, I doubt it would really make much difference above separate barriers.

So your suggestion is to generate different TCG opcode sequences depending on the underlying target architecture? And you are against forwarding this task further, to the backend code?

Quoted 5 lines
Quoted 3 lines

So I would just focus on translating only explicit memory barrier operations for now.

Then why did you bring it up?

I'm not sure I got the question right. I suggested to avoid using this TCG operation to emulate guest's memory ordering requirements for loads/stores that can be supplied with memory ordering requirement information which each backend can decide how to translate together with the load/store (possible just ignore it as it is the case for strongly-ordered hosts). I think we just need to translate explicit memory barrier instructions.

For example, emulating ARM guest on x86 host requires ARM dmb instruction to be translated to x86 mfence instruction to prevent store-after-load reordering. At the same time, we don't have to generate anything special for loads/stores since x86 is a strongly-ordered architecture.

Kind regards,
Sergey

#15 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 4 lines · Sergey Fedorov

On 03/06/16 04:08, Richard Henderson wrote:
So your suggestion is to generate different TCG opcode sequences depending on the underlying target architecture? And you are against forwarding this task further, to the backend code?

Yes, I would prefer to have, in the opcode stream, a separate opcode for barriers. This aids both the common case, where most of our hosts require separate barriers, as well as simplicity.

I am not opposed to letting the translators describe the memory model with barrier data along with memory operations, but I'd really prefer that those be split apart during initial opcode generation.

Quoted 13 lines
Quoted 4 lines
Quoted 2 lines

So I would just focus on translating only explicit memory barrier operations for now.

Then why did you bring it up?

I'm not sure I got the question right. I suggested to avoid using this TCG operation to emulate guest's memory ordering requirements for loads/stores that can be supplied with memory ordering requirement information which each backend can decide how to translate together with the load/store (possible just ignore it as it is the case for strongly-ordered hosts). I think we just need to translate explicit memory barrier instructions.

Quoted 5 lines

For example, emulating ARM guest on x86 host requires ARM dmb instruction to be translated to x86 mfence instruction to prevent store-after-load reordering. At the same time, we don't have to generate anything special for loads/stores since x86 is a strongly-ordered architecture.

Ah, so you'd prefer that we not think about optimizing barriers at the moment. Fine, but I'd prefer to think about *how* they might be optimized now, so that we *can* later.

r~

#16 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 33 lines · Richard Henderson
Quoted 4 lines · Sergey Fedorov

On 03/06/16 04:08, Richard Henderson wrote:
So your suggestion is to generate different TCG opcode sequences depending on the underlying target architecture? And you are against forwarding this task further, to the backend code?

Yes, I would prefer to have, in the opcode stream, a separate opcode for barriers. This aids both the common case, where most of our hosts require separate barriers, as well as simplicity.

I am not opposed to letting the translators describe the memory model with barrier data along with memory operations, but I'd really prefer that those be split apart during initial opcode generation.

Quoted 13 lines
Quoted 4 lines
Quoted 2 lines

So I would just focus on translating only explicit memory barrier operations for now.

Then why did you bring it up?

I'm not sure I got the question right. I suggested to avoid using this TCG operation to emulate guest's memory ordering requirements for loads/stores that can be supplied with memory ordering requirement information which each backend can decide how to translate together with the load/store (possible just ignore it as it is the case for strongly-ordered hosts). I think we just need to translate explicit memory barrier instructions.

Quoted 5 lines

For example, emulating ARM guest on x86 host requires ARM dmb instruction to be translated to x86 mfence instruction to prevent store-after-load reordering. At the same time, we don't have to generate anything special for loads/stores since x86 is a strongly-ordered architecture.

Quoted 3 lines

Ah, so you'd prefer that we not think about optimizing barriers at the moment. Fine, but I'd prefer to think about *how* they might be optimized now, so that we *can* later.

Not exactly. We need to have a TCG operation for various types of explicit barriers in order to translate guest explicit barrier instructions. I like your idea to follow Sparc's way to specify membar instruction attributes which can be used by the backed for generating optimal instructions. I think we also need to associate memory ordering attributes with load/store TCG operations. I'm not sure how would be best to handle load/store implicit memory ordering requirements, but it is probably out of the scope of this series. I'd propagate this attribute up to the backend and let it decide what kind of instructions to generate. I'd prefer to see only explicit barrier operations in the TCG opcode stream.

Kind regards,
Sergey

#17 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

In reply to Richard Henderson
Quoted 15 lines · Richard Henderson
Quoted 7 lines · Sergey Fedorov

It would give us three TCG operations for each memory operation instead of one. But then we might like to combine these barrier operations back with memory operations in each backend. If we propagate memory ordering semantics up to the backend, it can decide itself what instructions are best to generate.

A strongly ordered target would generally only set BEFORE bits or AFTER bits, but not both (and I suggest we canonicalize on AFTER for all such targets). Thus a strongly ordered target would produce only 2 opcodes per memory op.

Quoted 8 lines

I supplied both to make it easier to handle a weakly ordered target with acquire/release bits.

I would *not* combine the barrier operations back with memory operations in the backend. Only armv8 and ia64 can do that, and given the optimization level at which we generate code, I doubt it would really make much difference above separate barriers.

On armv8, using load_acquire/store_release instructions makes a significant difference in performance when compared to plain dmb+memory instruction sequence. So I would really like to keep the option of generating acq/rel instructions(by combining barrier and memory or some other way) open.

Thanks,

Signature · 1 line
Pranith
#18 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 22 lines · Pranith Kumar
Quoted 13 lines · Richard Henderson
Quoted 6 lines · Sergey Fedorov

It would give us three TCG operations for each memory operation instead of one. But then we might like to combine these barrier operations back with memory operations in each backend. If we propagate memory ordering semantics up to the backend, it can decide itself what instructions are best to generate.

A strongly ordered target would generally only set BEFORE bits or AFTER bits, but not both (and I suggest we canonicalize on AFTER for all such targets). Thus a strongly ordered target would produce only 2 opcodes per memory op.

Quoted 8 lines

I supplied both to make it easier to handle a weakly ordered target with acquire/release bits.

I would *not* combine the barrier operations back with memory operations in the backend. Only armv8 and ia64 can do that, and given the optimization level at which we generate code, I doubt it would really make much difference above separate barriers.

Quoted 5 lines

On armv8, using load_acquire/store_release instructions makes a significant difference in performance when compared to plain dmb+memory instruction sequence. So I would really like to keep the option of generating acq/rel instructions(by combining barrier and memory or some other way) open.

I'm not so sure about acq/rel flags. Is there any architecture which has explicit acq/rel barriers? I suppose acq/rel memory access instructions are always load-link and store-conditional and thus rely on exclusive memory monitor to support that "conditional" behaviour. To emulate this behaviour we need something more special like "Slow-path for atomic instruction translation" [1].

[1] http://thread.gmane.org/gmane.comp.emulators.qemu/407419

Kind regards,
Sergey

#19 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 4 lines · Sergey Fedorov

I'm not so sure about acq/rel flags. Is there any architecture which has explicit acq/rel barriers? I suppose acq/rel memory access instructions are always load-link and store-conditional and thus rely on exclusive memory monitor to support that "conditional" behaviour.

This doesn't sound right (at least not for ARM). You can have load-acquire and store-release insns which aren't exclusives, you can have exclusives which aren't acq/rel, and you can have accesses which are both exclusives and acq/rel (and just to complete the set, obviously there are accesses which are neither). The exclusive semantics require the monitor, but acq/rel is just an ordering constraint (sort of like an implicit barrier, but not quite).

thanks
-- PMM

#20 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 5 lines · Peter Maydell
Quoted 4 lines · Sergey Fedorov

I'm not so sure about acq/rel flags. Is there any architecture which has explicit acq/rel barriers? I suppose acq/rel memory access instructions are always load-link and store-conditional and thus rely on exclusive memory monitor to support that "conditional" behaviour.

Quoted 8 lines

This doesn't sound right (at least not for ARM). You can have load-acquire and store-release insns which aren't exclusives, you can have exclusives which aren't acq/rel, and you can have accesses which are both exclusives and acq/rel (and just to complete the set, obviously there are accesses which are neither). The exclusive semantics require the monitor, but acq/rel is just an ordering constraint (sort of like an implicit barrier, but not quite).

Thanks for clarifying this, Peter.

Regards,
Sergey

#21 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

In reply to Sergey Fedorov
Quoted 29 lines · Sergey Fedorov
Quoted 22 lines · Pranith Kumar
Quoted 13 lines · Richard Henderson
Quoted 6 lines · Sergey Fedorov

It would give us three TCG operations for each memory operation instead of one. But then we might like to combine these barrier operations back with memory operations in each backend. If we propagate memory ordering semantics up to the backend, it can decide itself what instructions are best to generate.

A strongly ordered target would generally only set BEFORE bits or AFTER bits, but not both (and I suggest we canonicalize on AFTER for all such targets). Thus a strongly ordered target would produce only 2 opcodes per memory op.

Quoted 8 lines

I supplied both to make it easier to handle a weakly ordered target with acquire/release bits.

I would *not* combine the barrier operations back with memory operations in the backend. Only armv8 and ia64 can do that, and given the optimization level at which we generate code, I doubt it would really make much difference above separate barriers.

Quoted 5 lines

On armv8, using load_acquire/store_release instructions makes a significant difference in performance when compared to plain dmb+memory instruction sequence. So I would really like to keep the option of generating acq/rel instructions(by combining barrier and memory or some other way) open.

Quoted 4 lines

I'm not so sure about acq/rel flags. Is there any architecture which has explicit acq/rel barriers? I suppose acq/rel memory access instructions are always load-link and store-conditional and thus rely on exclusive memory monitor to support that "conditional" behaviour.

Nope, you can have acq/rel memory operations without exclusive operations (see ARMv8 ldar and stlr). The exclusive operations also have ordered and non-ordered variants (ldxr, strx).

Quoted 8 lines

To emulate this
behaviour we need something more special like "Slow-path for atomic instruction translation" [1].

[1] http://thread.gmane.org/gmane.comp.emulators.qemu/407419

Kind regards,
Sergey

--
Alex Bennée

#22 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

In reply to Richard Henderson
Quoted 15 lines · Richard Henderson

Hum. That does seem helpful-ish. But I'm not certain how helpful it is to complicate the helper functions even further.

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32        // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64        // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128       // prevent later loads from crossing current op
MO_BAR_ST_A = 256       // prevent later stores from crossing current op
MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A
Quoted 6 lines

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1       // #LoadLoad barrier
TCG_BAR_ST_LD = 2       // #StoreLoad barrier
TCG_BAR_LD_ST = 4       // #LoadStore barrier
TCG_BAR_ST_ST = 8       // #StoreStore barrier
TCG_BAR_SYNC  = 64      // SEQ_CST barrier

I really like this format. I would also like to add to the frontend:

MO_BAR_ACQUIRE
MO_BAR_RELEASE

and the following to the backend:

TCG_BAR_ACQUIRE
TCG_BAR_RELEASE

since these are one-way barriers and the previous barrier types do not cover them.

Quoted 5 lines

where

tcg_gen_qemu_ld_i32(x, y, i, m | MO_BAR_LD_BEFORE | MO_BAR_ST_AFTER)

Quoted 9 lines

emits

  mb            TCG_BAR_LD_LD
  qemu_ld_i32   x, y, i, m
  mb            TCG_BAR_LD_ST

We can then add an optimization pass which folds barriers with no memory operations in between, so that duplicates are eliminated.

Yes, folding/eliding these barriers in an optimization pass sounds like a good idea.

Thanks,

Signature · 1 line
Pranith
#23 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 27 lines · Pranith Kumar
Quoted 15 lines · Richard Henderson
Quoted 15 lines

Hum. That does seem helpful-ish. But I'm not certain how helpful it is to complicate the helper functions even further.

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32        // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64        // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128       // prevent later loads from crossing current op
MO_BAR_ST_A = 256       // prevent later stores from crossing current op
MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A
Quoted 6 lines
Quoted 6 lines

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1       // #LoadLoad barrier
TCG_BAR_ST_LD = 2       // #StoreLoad barrier
TCG_BAR_LD_ST = 4       // #LoadStore barrier
TCG_BAR_ST_ST = 8       // #StoreStore barrier
TCG_BAR_SYNC  = 64      // SEQ_CST barrier

I really like this format. I would also like to add to the frontend:

MO_BAR_ACQUIRE
MO_BAR_RELEASE

Quoted 8 lines

and the following to the backend:

TCG_BAR_ACQUIRE
TCG_BAR_RELEASE

since these are one-way barriers and the previous barrier types do not cover them.

Well, my last comment about acq/rel barriers should really go here [Message-Id: [email protected]]

Regards,
Sergey

#24 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

In reply to Pranith Kumar
Quoted 26 lines · Pranith Kumar
Quoted 14 lines · Richard Henderson

Hum. That does seem helpful-ish. But I'm not certain how helpful it is to complicate the helper functions even further.

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32        // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64        // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128       // prevent later loads from crossing current op
MO_BAR_ST_A = 256       // prevent later stores from crossing current op
MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A
Quoted 6 lines

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1       // #LoadLoad barrier
TCG_BAR_ST_LD = 2       // #StoreLoad barrier
TCG_BAR_LD_ST = 4       // #LoadStore barrier
TCG_BAR_ST_ST = 8       // #StoreStore barrier
TCG_BAR_SYNC  = 64      // SEQ_CST barrier

I really like this format. I would also like to add to the frontend:

MO_BAR_ACQUIRE
MO_BAR_RELEASE

Quoted 7 lines

and the following to the backend:

TCG_BAR_ACQUIRE
TCG_BAR_RELEASE

since these are one-way barriers and the previous barrier types do not cover them.

Actually, the acquire barrier is a combined load-load + load-store barrier; and the release barrier is a combo of load-store + store-store barriers.

Kind regards,
Sergey

Quoted 18 lines
Quoted 5 lines

where

tcg_gen_qemu_ld_i32(x, y, i, m | MO_BAR_LD_BEFORE | MO_BAR_ST_AFTER)

Quoted 9 lines

emits

  mb            TCG_BAR_LD_LD
  qemu_ld_i32   x, y, i, m
  mb            TCG_BAR_LD_ST

We can then add an optimization pass which folds barriers with no memory operations in between, so that duplicates are eliminated.

Yes, folding/eliding these barriers in an optimization pass sounds like a good idea.

Thanks,

#25 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 20 lines · Sergey Fedorov
Quoted 19 lines · Pranith Kumar
Quoted 18 lines · Richard Henderson

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32        // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64        // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128       // prevent later loads from crossing current op
MO_BAR_ST_A = 256       // prevent later stores from crossing current op
MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1       // #LoadLoad barrier
TCG_BAR_ST_LD = 2       // #StoreLoad barrier
TCG_BAR_LD_ST = 4       // #LoadStore barrier
TCG_BAR_ST_ST = 8       // #StoreStore barrier
TCG_BAR_SYNC  = 64      // SEQ_CST barrier
Quoted 7 lines
Quoted 2 lines

I really like this format. I would also like to add to the frontend:

Actually, the acquire barrier is a combined load-load + load-store barrier; and the release barrier is a combo of load-store + store-store barriers.

All the above are two-way barriers. Acquire/Release are one-way barriers. So we cannot combine the above to get acquire/release semantics without being conservative.

Thanks,

Signature · 1 line
Pranith
#26 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 22 lines · Pranith Kumar
Quoted 21 lines · Sergey Fedorov
Quoted 20 lines · Pranith Kumar
Quoted 11 lines · Richard Henderson

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32        // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64        // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128       // prevent later loads from crossing current op
MO_BAR_ST_A = 256       // prevent later stores from crossing current op
MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A
Quoted 6 lines

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1       // #LoadLoad barrier
TCG_BAR_ST_LD = 2       // #StoreLoad barrier
TCG_BAR_LD_ST = 4       // #LoadStore barrier
TCG_BAR_ST_ST = 8       // #StoreStore barrier
TCG_BAR_SYNC  = 64      // SEQ_CST barrier

I really like this format. I would also like to add to the frontend:

Quoted 7 lines
Quoted 4 lines

Actually, the acquire barrier is a combined load-load + load-store barrier; and the release barrier is a combo of load-store + store-store barriers.

All the above are two-way barriers. Acquire/Release are one-way barriers. So we cannot combine the above to get acquire/release semantics without being conservative.

Do you mean *barriers* or *memory access* operations implying memory ordering?

Regards,
Sergey

#27 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 27 lines · Sergey Fedorov
Quoted 26 lines · Pranith Kumar
Quoted 25 lines · Sergey Fedorov
Quoted 12 lines · Pranith Kumar
Quoted 11 lines · Richard Henderson

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32        // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64        // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128       // prevent later loads from crossing current op
MO_BAR_ST_A = 256       // prevent later stores from crossing current op
MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A
Quoted 8 lines
Quoted 6 lines

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1       // #LoadLoad barrier
TCG_BAR_ST_LD = 2       // #StoreLoad barrier
TCG_BAR_LD_ST = 4       // #LoadStore barrier
TCG_BAR_ST_ST = 8       // #StoreStore barrier
TCG_BAR_SYNC  = 64      // SEQ_CST barrier

I really like this format. I would also like to add to the frontend:

Actually, the acquire barrier is a combined load-load + load-store barrier; and the release barrier is a combo of load-store + store-store barriers.

Quoted 6 lines
Quoted 3 lines

All the above are two-way barriers. Acquire/Release are one-way barriers. So we cannot combine the above to get acquire/release semantics without being conservative.

Do you mean *barriers* or *memory access* operations implying memory ordering?

I meant the latter. I know no arch which has acquire/release barriers. Sorry for the confusion.

Thanks,

Signature · 1 line
Pranith
#28 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 28 lines · Pranith Kumar
Quoted 27 lines · Sergey Fedorov
Quoted 26 lines · Pranith Kumar
Quoted 25 lines · Sergey Fedorov
Quoted 12 lines · Pranith Kumar
Quoted 11 lines · Richard Henderson

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32        // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64        // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128       // prevent later loads from crossing current op
MO_BAR_ST_A = 256       // prevent later stores from crossing current op
MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A
Quoted 8 lines
Quoted 6 lines

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1       // #LoadLoad barrier
TCG_BAR_ST_LD = 2       // #StoreLoad barrier
TCG_BAR_LD_ST = 4       // #LoadStore barrier
TCG_BAR_ST_ST = 8       // #StoreStore barrier
TCG_BAR_SYNC  = 64      // SEQ_CST barrier

I really like this format. I would also like to add to the frontend:

Actually, the acquire barrier is a combined load-load + load-store barrier; and the release barrier is a combo of load-store + store-store barriers.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

All the above are two-way barriers. Acquire/Release are one-way barriers. So we cannot combine the above to get acquire/release semantics without being conservative.

Do you mean *barriers* or *memory access* operations implying memory ordering?

I meant the latter. I know no arch which has acquire/release barriers. Sorry for the confusion.

So I am. By the way, what's the difference between sequential consistent *barrier* and a combination of all the other barriers?

Kind regards,
Sergey

#29 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 32 lines · Sergey Fedorov
Quoted 31 lines · Pranith Kumar
Quoted 30 lines · Sergey Fedorov
Quoted 29 lines · Pranith Kumar
Quoted 19 lines · Sergey Fedorov
Quoted 18 lines · Pranith Kumar
Quoted 17 lines · Richard Henderson

What if we have tcg_canonicalize_memop (or some such) split off the barriers into separate opcodes. E.g.

MO_BAR_LD_B = 32        // prevent earlier loads from crossing current op
MO_BAR_ST_B = 64        // prevent earlier stores from crossing current op
MO_BAR_LD_A = 128       // prevent later loads from crossing current op
MO_BAR_ST_A = 256       // prevent later stores from crossing current op
MO_BAR_LDST_B = MO_BAR_LD_B | MO_BAR_ST_B
MO_BAR_LDST_A = MO_BAR_LD_A | MO_BAR_ST_A
MO_BAR_MASK = MO_BAR_LDST_B | MO_BAR_LDST_A

// Match Sparc MEMBAR as the most flexible host.

TCG_BAR_LD_LD = 1       // #LoadLoad barrier
TCG_BAR_ST_LD = 2       // #StoreLoad barrier
TCG_BAR_LD_ST = 4       // #LoadStore barrier
TCG_BAR_ST_ST = 8       // #StoreStore barrier
TCG_BAR_SYNC  = 64      // SEQ_CST barrier
Quoted 6 lines
Quoted 2 lines

I really like this format. I would also like to add to the frontend:

Actually, the acquire barrier is a combined load-load + load-store barrier; and the release barrier is a combo of load-store + store-store barriers.

All the above are two-way barriers. Acquire/Release are one-way barriers. So we cannot combine the above to get acquire/release semantics without being conservative.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Do you mean *barriers* or *memory access* operations implying memory ordering?

I meant the latter. I know no arch which has acquire/release barriers. Sorry for the confusion.

So I am. By the way, what's the difference between sequential consistent *barrier* and a combination of all the other barriers?

If I read it correctly TCG_BAR_SYNC is equivalent to OR of all the other four barriers. I am not sure if we can just construct SYNC like this or if we need to define it explicitly though.

Signature · 1 line
Pranith
#30 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 3 lines · Pranith Kumar

If I read it correctly TCG_BAR_SYNC is equivalent to OR of all the other four barriers. I am not sure if we can just construct SYNC like this or if we need to define it explicitly though.

AFAICS, sparc membar #sync is stronger.

Compare PowerPC hwsync and lwsync. I would define lwsync as the OR of the other 4 barriers, but hwsync as TCG_BAR_SYNC.

r~

#31 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 8 lines · Richard Henderson
Quoted 4 lines · Pranith Kumar

If I read it correctly TCG_BAR_SYNC is equivalent to OR of all the other four barriers. I am not sure if we can just construct SYNC like this or if we need to define it explicitly though.

AFAICS, sparc membar #sync is stronger.

I tried looking it up but it's not clear. How is it stronger? And do we need those strong guarantees in our front-end/back-end?

Thanks,

Signature · 1 line
Pranith
#32 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 9 lines · Pranith Kumar
Quoted 6 lines · Richard Henderson
Quoted 3 lines · Pranith Kumar

If I read it correctly TCG_BAR_SYNC is equivalent to OR of all the other four barriers. I am not sure if we can just construct SYNC like this or if we need to define it explicitly though.

AFAICS, sparc membar #sync is stronger.

I tried looking it up but it's not clear. How is it stronger? And do we need those strong guarantees in our front-end/back-end?

That is not clear for me either :( AFAIU, PPC's lwsync does allow stores to be reordered after loads but hwsync - not. I suspect Sparc's membar #Sync is used to ensure that some system operations are complete before proceeding with execution. I'm not sure we need to introduce this into TCG. It needs to be clear what is it and how to use it.

Kind regards,
Sergey

#33 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 7 lines · Sergey Fedorov
Quoted 6 lines · Pranith Kumar
Quoted 5 lines · Richard Henderson
Quoted 3 lines · Pranith Kumar

If I read it correctly TCG_BAR_SYNC is equivalent to OR of all the other four barriers. I am not sure if we can just construct SYNC like this or if we need to define it explicitly though.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

AFAICS, sparc membar #sync is stronger.

I tried looking it up but it's not clear. How is it stronger? And do we need those strong guarantees in our front-end/back-end?

That is not clear for me either :( AFAIU, PPC's lwsync does allow stores to be reordered after loads but hwsync - not.

Yes, from the PoV of the other CPU. That is, for write-then-read by CPU 0, CPU 0 will always read what it wrote, but other CPUs don't necessarily see the write before the read is satisfied.
https://www.cl.cam.ac.uk/~pes20/ppc-supplemental/test7.pdf
describes the difference in sections 3.2 and 3.3, and has an example in section 6 of a situation which requires a full (hw)sync and an lwsync is not sufficient.

Quoted 4 lines

I suspect Sparc's membar
#Sync is used to ensure that some system operations are complete before proceeding with execution. I'm not sure we need to introduce this into TCG. It needs to be clear what is it and how to use it.

My reading of the manual is that the SPARC "membar #Sync" is like ARM ISB -- it enforces that any instruction (whether a memory access or not) before it must finish before anything after it can start. It only affects the CPU that issues it (assuming you didn't also specify any of the bits requesting memory barriers!) Since TCG doesn't attempt to reorder instructions, we likely don't need to do anything except maybe end the current TB. Also if we're still trying to do TLB operations on other CPUs asynchronously we need to wait for them to finish; I forget what the conclusion was on that idea.
PPC equivalent insn is isync I think.

thanks
-- PMM

#34 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 01/13] Introduce TCGOpcode for memory barrier

Quoted 24 lines · Peter Maydell
Quoted 11 lines · Sergey Fedorov
Quoted 8 lines · Pranith Kumar
Quoted 5 lines · Richard Henderson
Quoted 3 lines · Pranith Kumar

If I read it correctly TCG_BAR_SYNC is equivalent to OR of all the other four barriers. I am not sure if we can just construct SYNC like this or if we need to define it explicitly though.

AFAICS, sparc membar #sync is stronger.

I tried looking it up but it's not clear. How is it stronger? And do we need those strong guarantees in our front-end/back-end?

That is not clear for me either :( AFAIU, PPC's lwsync does allow stores to be reordered after loads but hwsync - not.

Yes, from the PoV of the other CPU. That is, for write-then-read by CPU 0, CPU 0 will always read what it wrote, but other CPUs don't necessarily see the write before the read is satisfied.
https://www.cl.cam.ac.uk/~pes20/ppc-supplemental/test7.pdf
describes the difference in sections 3.2 and 3.3, and has an example in section 6 of a situation which requires a full (hw)sync and an lwsync is not sufficient.

Quoted 4 lines

I suspect Sparc's membar
#Sync is used to ensure that some system operations are complete before proceeding with execution. I'm not sure we need to introduce this into TCG. It needs to be clear what is it and how to use it.

Quoted 11 lines

My reading of the manual is that the SPARC "membar #Sync" is like ARM ISB -- it enforces that any instruction (whether a memory access or not) before it must finish before anything after it can start. It only affects the CPU that issues it (assuming you didn't also specify any of the bits requesting memory barriers!) Since TCG doesn't attempt to reorder instructions, we likely don't need to do anything except maybe end the current TB. Also if we're still trying to do TLB operations on other CPUs asynchronously we need to wait for them to finish; I forget what the conclusion was on that idea.
PPC equivalent insn is isync I think.

Thanks for commenting this, Peter. AFAIU, a sequential consistency barrier is stronger than a release-aquire barrier because it provides "transitivity/commutativity" [1]. This is what general barrier guarantees in Linux [2]. I especially like this piece of description from [2]:

... if this example runs on a system where CPUs 1 and 2 share a store buffer or a level of cache, CPU 2 might have early access to CPU 1's writes. General barriers are therefore required to ensure that all CPUs agree on the combined order of CPU 1's and CPU 2's accesses.

Current Linux kernel implements Sparc's smp_mb()/__smp_mb()/mb() with "membar #StoreLoad" [3]. So we'll probably be fine with just RR, RW, WR, and WW bits in the TCG memory barrier operation attribute.

Kind regards,
Sergey

[1] https://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync#Overall_Summary
[2]
http://lxr.free-electrons.com/source/Documentation/memory-barriers.txt#L1268
[3]
http://lxr.free-electrons.com/source/arch/sparc/include/asm/barrier_64.h#L36

#35 lore raw

RFC PATCH v2 2/13 tcg/i386: Add support for fence

In reply to Pranith Kumar

Generate mfence instruction on SSE2 enabled processors. For older processors, generate a 'lock orl $0,0(%esp)' instruction which has similar ordering semantics.

[rth: Check for sse2, fallback to locked memory op otherwise.]
---
 tcg/i386/tcg-target.inc.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
tcg/i386/tcg-target.inc.c +35 -0
index 8fd37f4..1fd5a99 100644
@@ -121,6 +121,16 @@ static bool have_cmov;
 # define have_cmov 0
 #endif
 
+/* For 32-bit, we are going to attempt to determine at runtime whether
+   sse2 support is available.  */
+#if TCG_TARGET_REG_BITS == 64 || defined(__SSE2__)
+# define have_sse2 1
+#elif defined(CONFIG_CPUID_H) && defined(bit_SSE2)
+static bool have_sse2;
+#else
+# define have_sse2 0
+#endif
+
 /* If bit_MOVBE is defined in cpuid.h (added in GCC version 4.6), we are
    going to attempt to determine at runtime whether movbe is available.  */
 #if defined(CONFIG_CPUID_H) && defined(bit_MOVBE)
@@ -686,6 +696,21 @@ static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val)
     }
 }
 
+static inline void tcg_out_mb(TCGContext *s)
+{
+    if (have_sse2) {
+        /* mfence */
+        tcg_out8(s, 0x0f);
+        tcg_out8(s, 0xae);
+        tcg_out8(s, 0xf0);
+    } else {
+        /* lock orl $0,0(%esp) */
+        tcg_out8(s, 0xf0);
+        tcg_out_modrm_offset(s, OPC_ARITH_EvIb, ARITH_OR, TCG_REG_ESP, 0);
+        tcg_out8(s, 0);
+    }
+}
+
 static inline void tcg_out_push(TCGContext *s, int reg)
 {
     tcg_out_opc(s, OPC_PUSH_r32 + LOWREGMASK(reg), 0, reg, 0);
@@ -2114,6 +2139,9 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         }
         break;
 
+    case INDEX_op_mb:
+        tcg_out_mb(s);
+        break;
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
@@ -2179,6 +2207,8 @@ static const TCGTargetOpDef x86_op_defs[] = {
     { INDEX_op_add2_i32, { "r", "r", "0", "1", "ri", "ri" } },
     { INDEX_op_sub2_i32, { "r", "r", "0", "1", "ri", "ri" } },
 
+    { INDEX_op_mb, { "r" } },
+
 #if TCG_TARGET_REG_BITS == 32
     { INDEX_op_brcond2_i32, { "r", "r", "ri", "ri" } },
     { INDEX_op_setcond2_i32, { "r", "r", "r", "ri", "ri" } },
@@ -2356,6 +2386,11 @@ static void tcg_target_init(TCGContext *s)
            available, we'll use a small forward branch.  */
         have_cmov = (d & bit_CMOV) != 0;
 #endif
+#ifndef have_sse2
+        /* Likewise, almost all hardware supports SSE2, but we do
+           have a locked memory operation to use as a substitute.  */
+        have_sse2 = (d & bit_SSE2) != 0;
+#endif
 #ifndef have_movbe
         /* MOVBE is only available on Intel Atom and Haswell CPUs, so we
            need to probe for it.  */
Signature · 1 line
2.8.3
#36 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 02/13] tcg/i386: Add support for fence

tcg/i386/tcg-target.inc.c static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 2143

Quoted 2 lines · Pranith Kumar
tcg/i386/tcg-target.inc.c +2 -0
+    case INDEX_op_mb:
+        tcg_out_mb(s);

You need to look at the barrier type and DTRT. In particular, the Linux smp_rmb and smp_wmb types need not emit any code.

Quoted 1 line

+ { INDEX_op_mb, { "r" } },

You certainly do *not* need the constant argument loaded into a register. This should remain { }.

r~

#37 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 02/13] tcg/i386: Add support for fence

Quoted 8 lines · Richard Henderson

tcg/i386/tcg-target.inc.c static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 2143

Quoted 3 lines · Pranith Kumar
tcg/i386/tcg-target.inc.c +2 -0
+    case INDEX_op_mb:
+        tcg_out_mb(s);

You need to look at the barrier type and DTRT. In particular, the Linux smp_rmb and smp_wmb types need not emit any code.

These are converted to 'lfence' and 'sfence' instructions. Based on the target backend, I think we still need to emit barrier instructions. For example, if target backend is ARMv7 we need to emit 'dmb' instruction for both x86 fence instructions. I am not sure why they do not emit any code?

Quoted 7 lines
Quoted 2 lines

+ { INDEX_op_mb, { "r" } },

You certainly do *not* need the constant argument loaded into a register. This should remain { }.

OK, I will fix this.

Thanks,

Signature · 1 line
Pranith
#38 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 02/13] tcg/i386: Add support for fence

Quoted 7 lines · Pranith Kumar
Quoted 6 lines · Richard Henderson
Quoted 3 lines · Pranith Kumar
tcg/i386/tcg-target.inc.c +2 -0
+    case INDEX_op_mb:
+        tcg_out_mb(s);
Quoted 8 lines
Quoted 2 lines

You need to look at the barrier type and DTRT. In particular, the Linux smp_rmb and smp_wmb types need not emit any code.

These are converted to 'lfence' and 'sfence' instructions. Based on the target backend, I think we still need to emit barrier instructions. For example, if target backend is ARMv7 we need to emit 'dmb' instruction for both x86 fence instructions. I am not sure why they do not emit any code?

Because x86 has a strong memory model.

It does not require barriers to keep normal loads and stores in order. The primary reason for the *fence instructions is to order the "non-temporal" memory operations that are part of the SSE instruction set, which we're not using at all.

This is why you'll find

/*
* Because of the strongly ordered storage model, wmb() and rmb() are nops
* here (a compiler barrier only). QEMU doesn't do accesses to write-combining
* qemu memory or non-temporal load/stores from C code.
*/
#define smp_wmb() barrier()
#define smp_rmb() barrier()

for x86 and s390.

r~

#39 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 02/13] tcg/i386: Add support for fence

Quoted 19 lines · Richard Henderson

Because x86 has a strong memory model.

It does not require barriers to keep normal loads and stores in order. The primary reason for the *fence instructions is to order the "non-temporal" memory operations that are part of the SSE instruction set, which we're not using at all.

This is why you'll find

/*
* Because of the strongly ordered storage model, wmb() and rmb() are nops
* here (a compiler barrier only). QEMU doesn't do accesses to
write-combining
* qemu memory or non-temporal load/stores from C code.
*/
#define smp_wmb() barrier()
#define smp_rmb() barrier()

Quoted 1 line

for x86 and s390.

OK. For x86 target, that is true. I think I got the context confused. On x86 target, we can elide the read and write barriers. But we still need to generate 'mfence' to prevent store-after-load reordering. I will refine this in the next version.

Thanks,

Signature · 1 line
Pranith
#40 lore raw

RFC PATCH v2 3/13 tcg/aarch64: Add support for fence

In reply to Pranith Kumar
---
 tcg/aarch64/tcg-target.inc.c | 7 +++++++
 1 file changed, 7 insertions(+)
tcg/aarch64/tcg-target.inc.c +7 -0
index 08efdf4..c361a5c 100644
@@ -360,6 +360,9 @@ typedef enum {
     I3510_EOR       = 0x4a000000,
     I3510_EON       = 0x4a200000,
     I3510_ANDS      = 0x6a000000,
+
+    /* System instructions.  */
+    DMB_ISH         = 0xd5033bbf,
 } AArch64Insn;
 
 static inline uint32_t tcg_in32(TCGContext *s)
@@ -1625,6 +1628,9 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tcg_out_insn(s, 3508, SMULH, TCG_TYPE_I64, a0, a1, a2);
         break;
 
+    case INDEX_op_mb:
+        tcg_out32(s, DMB_ISH);
+        break;
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
@@ -1749,6 +1755,7 @@ static const TCGTargetOpDef aarch64_op_defs[] = {
     { INDEX_op_muluh_i64, { "r", "r", "r" } },
     { INDEX_op_mulsh_i64, { "r", "r", "r" } },
 
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
Signature · 1 line
2.8.3
Acked-by on v2 3/13
#41 lore raw

Re: [RFC v2 PATCH 03/13] tcg/aarch64: Add support for fence

Quoted 40 lines · Pranith Kumar
Cc Claudio Fontana <[email protected] <javascript:;>>
Signed-off-by Richard Henderson <[email protected] <javascript:;>>
Signed-off-by Pranith Kumar <[email protected] <javascript:;>>
---
 tcg/aarch64/tcg-target.inc.c | 7 +++++++
 1 file changed, 7 insertions(+)
tcg/aarch64/tcg-target.inc.c +7 -1
index 08efdf4..c361a5c 100644
@@ -360,6 +360,9 @@ typedef enum {
     I3510_EOR       = 0x4a000000,
     I3510_EON       = 0x4a200000,
     I3510_ANDS      = 0x6a000000,
+
+    /* System instructions.  */
+    DMB_ISH         = 0xd5033bbf,
 } AArch64Insn;
 
 static inline uint32_t tcg_in32(TCGContext *s)
@@ -1625,6 +1628,9 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tcg_out_insn(s, 3508, SMULH, TCG_TYPE_I64, a0, a1, a2);
         break;
 
+    case INDEX_op_mb:
+        tcg_out32(s, DMB_ISH);
+        break;
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
@@ -1749,6 +1755,7 @@ static const TCGTargetOpDef aarch64_op_defs[] = {
     { INDEX_op_muluh_i64, { "r", "r", "r" } },
     { INDEX_op_mulsh_i64, { "r", "r", "r" } },
 
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
--
Quoted 3 lines

2.8.3

--

#42 lore raw

Re: [RFC v2 PATCH 03/13] tcg/aarch64: Add support for fence

In reply to Pranith Kumar
Quoted 2 lines · Pranith Kumar
tcg/aarch64/tcg-target.inc.c +2 -0
+    /* System instructions.  */
+    DMB_ISH         = 0xd5033bbf,

...

tcg/aarch64/tcg-target.inc.c static void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 1633

Quoted 3 lines
tcg/aarch64/tcg-target.inc.c +3 -0
+    case INDEX_op_mb:
+        tcg_out32(s, DMB_ISH);
+        break;

With the flags argument, this needs to be split.

DMB_ISH = 0xd5033b8f
DMB_RD = 0x00000010
DMB_WR = 0x00000020

	if (a0 == TCG_MB_READ) {
		a0 = DMB_RD;
	} else if (a0 == TCG_MB_WRITE) {
		a0 = DMB_WR;
	} else {
		a0 = DMB_RD | DMB_WR;
	}
	tcg_out32(s, DMB_ISH | a0);

r~

#43 lore raw

Re: [RFC v2 PATCH 03/13] tcg/aarch64: Add support for fence

Hi Richard,

Quoted 7 lines · Richard Henderson
Quoted 3 lines · Pranith Kumar
tcg/aarch64/tcg-target.inc.c +2 -0
+    /* System instructions.  */
+    DMB_ISH         = 0xd5033bbf,

...

Quoted 1 line
Quoted 8 lines

tcg/aarch64/tcg-target.inc.c static void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 1633

Quoted 3 lines
tcg/aarch64/tcg-target.inc.c +3 -0
+    case INDEX_op_mb:
+        tcg_out32(s, DMB_ISH);
+        break;

With the flags argument, this needs to be split.

DMB_ISH = 0xd5033b8f

I just checked this. Shouldn't this be as follows:

DMB_ISH = 0xd50338bf
DMB_RD = 0x00000100
DMB_WR = 0x00000200

The logic seems to be ok.

Thanks!

Quoted 15 lines

DMB_RD = 0x00000010
DMB_WR = 0x00000020

        if (a0 == TCG_MB_READ) {
                a0 = DMB_RD;
        } else if (a0 == TCG_MB_WRITE) {
                a0 = DMB_WR;
        } else {
                a0 = DMB_RD | DMB_WR;
        }
        tcg_out32(s, DMB_ISH | a0);

r~

Signature · 1 line
Pranith
#44 lore raw

RFC PATCH v2 4/13 tcg/arm: Add support for fence

In reply to Pranith Kumar
---
 tcg/arm/tcg-target.inc.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tcg/arm/tcg-target.inc.c +12 -0
index a914762..e88d8ce 100644
@@ -305,6 +305,10 @@ typedef enum {
     INSN_LDRD_REG  = 0x000000d0,
     INSN_STRD_IMM  = 0x004000f0,
     INSN_STRD_REG  = 0x000000f0,
+
+    INSN_DMB_ISH   = 0x5bf07ff5,
+    INSN_DMB_MCR   = 0xba0f07ee,
+
 } ARMInsn;
 
 #define SHIFT_IMM_LSL(im)	(((im) << 7) | 0x00)
@@ -1905,6 +1909,13 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tcg_out_udiv(s, COND_AL, args[0], args[1], args[2]);
         break;
 
+    case INDEX_op_mb:
+        if (use_armv7_instructions) {
+            tcg_out32(s, INSN_DMB_ISH);
+        } else if (use_armv6_instructions) {
+            tcg_out32(s, INSN_DMB_MCR);
+        }
+        break;
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
     case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
@@ -1979,6 +1990,7 @@ static const TCGTargetOpDef arm_op_defs[] = {
     { INDEX_op_div_i32, { "r", "r", "r" } },
     { INDEX_op_divu_i32, { "r", "r", "r" } },
 
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
Signature · 1 line
2.8.3
#45 lore raw

RFC PATCH v2 5/13 tcg/ia64: Add support for fence

In reply to Pranith Kumar
---
 tcg/ia64/tcg-target.inc.c | 5 +++++
 1 file changed, 5 insertions(+)
tcg/ia64/tcg-target.inc.c +5 -0
index 261861f..88cc560 100644
@@ -247,6 +247,7 @@ enum {
     OPC_LD4_M3                = 0x0a080000000ull,
     OPC_LD8_M1                = 0x080c0000000ull,
     OPC_LD8_M3                = 0x0a0c0000000ull,
+    OPC_MF_M24                = 0x00110000000ull,
     OPC_MUX1_I3               = 0x0eca0000000ull,
     OPC_NOP_B9                = 0x04008000000ull,
     OPC_NOP_F16               = 0x00008000000ull,
@@ -2213,6 +2214,9 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tcg_out_qemu_st(s, args);
         break;
 
+    case INDEX_op_mb:
+        tcg_out_bundle(s, mmI, OPC_MF_M24, INSN_NOP_M, INSN_NOP_I);
+        break;
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
@@ -2326,6 +2330,7 @@ static const TCGTargetOpDef ia64_op_defs[] = {
     { INDEX_op_qemu_st_i32, { "SZ", "r" } },
     { INDEX_op_qemu_st_i64, { "SZ", "r" } },
 
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
Signature · 1 line
2.8.3
#46 lore raw

RFC PATCH v2 6/13 tcg/mips: Add support for fence

In reply to Pranith Kumar
---
 tcg/mips/tcg-target.inc.c | 6 ++++++
 1 file changed, 6 insertions(+)
tcg/mips/tcg-target.inc.c +6 -0
index b2a839a..fc9c7fb 100644
@@ -292,6 +292,7 @@ typedef enum {
     OPC_JALR     = OPC_SPECIAL | 0x09,
     OPC_MOVZ     = OPC_SPECIAL | 0x0A,
     OPC_MOVN     = OPC_SPECIAL | 0x0B,
+    OPC_SYNC     = OPC_SPECIAL | 0x0F,
     OPC_MFHI     = OPC_SPECIAL | 0x10,
     OPC_MFLO     = OPC_SPECIAL | 0x12,
     OPC_MULT     = OPC_SPECIAL | 0x18,
@@ -1636,6 +1637,9 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
                         const_args[4], const_args[5], true);
         break;
 
+    case INDEX_op_mb:
+        tcg_out32(s, OPC_SYNC);
+        break;
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
     case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
@@ -1716,6 +1720,8 @@ static const TCGTargetOpDef mips_op_defs[] = {
     { INDEX_op_qemu_ld_i64, { "L", "L", "lZ", "lZ" } },
     { INDEX_op_qemu_st_i64, { "SZ", "SZ", "SZ", "SZ" } },
 #endif
+
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
Signature · 1 line
2.8.3
#47 lore raw

RFC PATCH v2 7/13 tcg/ppc: Add support for fence

In reply to Pranith Kumar
---
 tcg/ppc/tcg-target.inc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tcg/ppc/tcg-target.inc.c +8 -0
index 1039407..45a667f 100644
@@ -469,6 +469,9 @@ static int tcg_target_const_match(tcg_target_long val, TCGType type,
 #define STHX   XO31(407)
 #define STWX   XO31(151)
 
+#define HWSYNC XO31(598)
+#define LWSYNC (HWSYNC | (1u << 21))
+
 #define SPR(a, b) ((((a)<<5)|(b))<<11)
 #define LR     SPR(8, 0)
 #define CTR    SPR(9, 0)
@@ -2425,6 +2428,10 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         tcg_out32(s, MULHD | TAB(args[0], args[1], args[2]));
         break;
 
+    case INDEX_op_mb:
+        /* ??? Do we want SEQ_CST or ACQ_REL memory model.  */
+        tcg_out32(s, HWSYNC);
+        break;
     case INDEX_op_mov_i32:   /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32:  /* Always emitted via tcg_out_movi.  */
@@ -2572,6 +2579,7 @@ static const TCGTargetOpDef ppc_op_defs[] = {
     { INDEX_op_qemu_st_i64, { "S", "S", "S", "S" } },
 #endif
 
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
Signature · 1 line
2.8.3
#48 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 07/13] tcg/ppc: Add support for fence

Quoted 2 lines · Pranith Kumar
tcg/ppc/tcg-target.inc.c +2 -0
+#define HWSYNC XO31(598)
+#define LWSYNC (HWSYNC | (1u << 21))

...

tcg/ppc/tcg-target.inc.c static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args, · line 2434

Quoted 4 lines
tcg/ppc/tcg-target.inc.c +4 -0
+    case INDEX_op_mb:
+        /* ??? Do we want SEQ_CST or ACQ_REL memory model.  */
+        tcg_out32(s, HWSYNC);
+        break;

With the flags argument, this needs to be

#define EIEIO XO31(854)

	a0 = args[0];
	if (a0 == TCG_MB_WRITE) {
		tcg_out32(s, EIEIO);
	} else if (a1 == TCG_MB_READ) {
		tcg_out32(s, LWSYNC);
	} else {
		tcg_out32(s, HWSYNC);
	}

r~

#49 lore raw

RFC PATCH v2 8/13 tcg/s390: Add support for fence

In reply to Pranith Kumar
---
 tcg/s390/tcg-target.inc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
tcg/s390/tcg-target.inc.c +9 -0
index e95b04b..b4f14bc 100644
@@ -341,6 +341,7 @@ static tcg_insn_unit *tb_ret_addr;
 #define FACILITY_EXT_IMM	(1ULL << (63 - 21))
 #define FACILITY_GEN_INST_EXT	(1ULL << (63 - 34))
 #define FACILITY_LOAD_ON_COND   (1ULL << (63 - 45))
+#define FACILITY_FAST_BCR_SER   FACILITY_LOAD_ON_COND
 
 static uint64_t facilities;
 
@@ -2157,6 +2158,13 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tgen_deposit(s, args[0], args[2], args[3], args[4]);
         break;
 
+    case INDEX_op_mb:
+        /* The host memory model is quite strong, we simply need to
+           serialize the instruction stream.  */
+        tcg_out_insn(s, RR, BCR,
+		     facilities & FACILITY_FAST_BCR_SER ? 14 : 15, 0);
+        break;
+
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
@@ -2278,6 +2286,7 @@ static const TCGTargetOpDef s390_op_defs[] = {
     { INDEX_op_movcond_i64, { "r", "r", "rC", "r", "0" } },
     { INDEX_op_deposit_i64, { "r", "0", "r" } },
 
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
Signature · 1 line
2.8.3
#50 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 08/13] tcg/s390: Add support for fence

Quoted 20 lines · Pranith Kumar
---
 tcg/s390/tcg-target.inc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
tcg/s390/tcg-target.inc.c +1 -0
index e95b04b..b4f14bc 100644
@@ -341,6 +341,7 @@ static tcg_insn_unit *tb_ret_addr;
 #define FACILITY_EXT_IMM	(1ULL << (63 - 21))
 #define FACILITY_GEN_INST_EXT	(1ULL << (63 - 34))
 #define FACILITY_LOAD_ON_COND   (1ULL << (63 - 45))
+#define FACILITY_FAST_BCR_SER   FACILITY_LOAD_ON_COND
 
 static uint64_t facilities;
 

tcg/s390/tcg-target.inc.c static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 2167

Quoted 10 lines
tcg/s390/tcg-target.inc.c +7 -0
@@ -2157,6 +2158,13 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tgen_deposit(s, args[0], args[2], args[3], args[4]);
         break;
 
+    case INDEX_op_mb:
+        /* The host memory model is quite strong, we simply need to
+           serialize the instruction stream.  */
+        tcg_out_insn(s, RR, BCR,
+		     facilities & FACILITY_FAST_BCR_SER ? 14 : 15, 0);
+        break;
+

Do we? What does that mean?

Kind regards,
Sergey

Quoted 11 lines
tcg/s390/tcg-target.inc.c +1 -0
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
@@ -2278,6 +2286,7 @@ static const TCGTargetOpDef s390_op_defs[] = {
     { INDEX_op_movcond_i64, { "r", "r", "rC", "r", "0" } },
     { INDEX_op_deposit_i64, { "r", "0", "r" } },
 
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
#51 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 08/13] tcg/s390: Add support for fence

Quoted 24 lines · Sergey Fedorov
Quoted 23 lines · Pranith Kumar
---
 tcg/s390/tcg-target.inc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
tcg/s390/tcg-target.inc.c +1 -0
index e95b04b..b4f14bc 100644
@@ -341,6 +341,7 @@ static tcg_insn_unit *tb_ret_addr;
 #define FACILITY_EXT_IMM	(1ULL << (63 - 21))
 #define FACILITY_GEN_INST_EXT	(1ULL << (63 - 34))
 #define FACILITY_LOAD_ON_COND   (1ULL << (63 - 45))
+#define FACILITY_FAST_BCR_SER   FACILITY_LOAD_ON_COND
 
 static uint64_t facilities;
 
@@ -2157,6 +2158,13 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         tgen_deposit(s, args[0], args[2], args[3], args[4]);
         break;
 
Quoted 9 lines

tcg/s390/tcg-target.inc.c static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 2167

Quoted 7 lines
tcg/s390/tcg-target.inc.c +7 -0
+    case INDEX_op_mb:
+        /* The host memory model is quite strong, we simply need to
+           serialize the instruction stream.  */
+        tcg_out_insn(s, RR, BCR,
+		     facilities & FACILITY_FAST_BCR_SER ? 14 : 15, 0);
+        break;
+

Do we? What does that mean?

It's the difference between ACQ_REL and SEQ_CST for s390x. FWIW.

r~

#52 lore raw

RFC PATCH v2 9/13 tcg/sparc: Add support for fence

In reply to Pranith Kumar
---
 tcg/sparc/tcg-target.inc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tcg/sparc/tcg-target.inc.c +8 -0
index a611885..81f263f 100644
@@ -249,6 +249,8 @@ static const int tcg_target_call_oarg_regs[] = {
 #define STWA       (INSN_OP(3) | INSN_OP3(0x14))
 #define STXA       (INSN_OP(3) | INSN_OP3(0x1e))
 
+#define MEMBAR     (INSN_OP(2) | INSN_OP3(0x28) | INSN_RS1(15) | (1 << 13))
+
 #ifndef ASI_PRIMARY_LITTLE
 #define ASI_PRIMARY_LITTLE 0x88
 #endif
@@ -1450,6 +1452,11 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc,
 	tcg_out_arithc(s, a0, TCG_REG_G0, a1, const_args[1], c);
 	break;
 
+    case INDEX_op_mb:
+        /* membar #LoadLoad|#LoadStore|#StoreStore|#StoreLoad */
+        tcg_out32(s, MEMBAR | 15);
+        break;
+
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
@@ -1551,6 +1558,7 @@ static const TCGTargetOpDef sparc_op_defs[] = {
     { INDEX_op_qemu_st_i32, { "sZ", "A" } },
     { INDEX_op_qemu_st_i64, { "SZ", "A" } },
 
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
Signature · 1 line
2.8.3
#53 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 09/13] tcg/sparc: Add support for fence

tcg/sparc/tcg-target.inc.c static void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 1458

Quoted 4 lines · Pranith Kumar
tcg/sparc/tcg-target.inc.c +4 -0
+    case INDEX_op_mb:
+        /* membar #LoadLoad|#LoadStore|#StoreStore|#StoreLoad */
+        tcg_out32(s, MEMBAR | 15);
+        break;

With the argument, this needs to be

	if (a0 == TCG_MB_WRITE) {
		/* #StoreStore | #StoreLoad */
		a0 = 0xa;
	} else if (a0 == TCG_MB_READ) {
		/* #LoadStore | #LoadLoad */
		a0 = 0x5;
	} else {
		/* #StoreStore | #LoadStore | #StoreLoad | #LoadLoad */
		a0 = 0xf;
	}
	tcg_out32(s, MEMBAR | a0);

r~

#54 lore raw

RFC PATCH v2 10/13 tcg/tci: Add support for fence

In reply to Pranith Kumar
---
 tcg/tci/tcg-target.inc.c | 3 +++
 tci.c                    | 3 +++
 2 files changed, 6 insertions(+)
tcg/tci/tcg-target.inc.c +3 -0
index 4e91687..a507ceb 100644
@@ -255,6 +255,7 @@ static const TCGTargetOpDef tcg_target_op_defs[] = {
     { INDEX_op_bswap32_i32, { R, R } },
 #endif
 
+    { INDEX_op_mb, { "r" } },
     { -1 },
 };
 
@@ -798,6 +799,8 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         }
         tcg_out_i(s, *args++);
         break;
+    case INDEX_op_mb:
+        break;
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
tci.c +3 -0
index 7cbb39e..d35b24f 100644
@@ -1230,6 +1230,9 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
                 tcg_abort();
             }
             break;
+        case INDEX_op_mb:
+            smp_mb();
+            break;
         default:
             TODO();
             break;
Signature · 1 line
2.8.3
#55 lore raw

RFC PATCH v2 11/13 target-arm: Generate fences in ARMv7 frontend

In reply to Pranith Kumar
---
 target-arm/translate.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
target-arm/translate.c +5 -2
index c946c0e..e1b16c0 100644
@@ -7980,9 +7980,11 @@ static void disas_arm_insn(DisasContext *s, unsigned int insn)
                 gen_clrex(s);
                 return;
             case 4: /* dsb */
+                ARCH(7);
+                return;
             case 5: /* dmb */
                 ARCH(7);
-                /* We don't emulate caches so these are a no-op.  */
+                tcg_gen_mb(TCG_MB_FULL);
                 return;
             case 6: /* isb */
                 /* We need to break the TB after this insn to execute
@@ -10330,8 +10332,9 @@ static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw
                             gen_clrex(s);
                             break;
                         case 4: /* dsb */
+                            break;
                         case 5: /* dmb */
-                            /* These execute as NOPs.  */
+                            tcg_gen_mb(TCG_MB_FULL);
                             break;
                         case 6: /* isb */
                             /* We need to break the TB after this insn
Signature · 1 line
2.8.3
#56 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 11/13] target-arm: Generate fences in ARMv7 frontend

Quoted 11 lines · Pranith Kumar
---
 target-arm/translate.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
target-arm/translate.c +0 -0
index c946c0e..e1b16c0 100644

target-arm/translate.c static void disas_arm_insn(DisasContext *s, unsigned int insn) · line 7983

Quoted 4 lines
target-arm/translate.c +1 -0
@@ -7980,9 +7980,11 @@ static void disas_arm_insn(DisasContext *s, unsigned int insn)
                 gen_clrex(s);
                 return;
             case 4: /* dsb */
+                ARCH(7);

ARMv7 ARM says: "A DSB behaves as a DMB with the same arguments, and also has the additional properties...".

Kind regards,
Sergey

Quoted 19 lines
target-arm/translate.c +4 -2
+                return;
             case 5: /* dmb */
                 ARCH(7);
-                /* We don't emulate caches so these are a no-op.  */
+                tcg_gen_mb(TCG_MB_FULL);
                 return;
             case 6: /* isb */
                 /* We need to break the TB after this insn to execute
@@ -10330,8 +10332,9 @@ static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw
                             gen_clrex(s);
                             break;
                         case 4: /* dsb */
+                            break;
                         case 5: /* dmb */
-                            /* These execute as NOPs.  */
+                            tcg_gen_mb(TCG_MB_FULL);
                             break;
                         case 6: /* isb */
                             /* We need to break the TB after this insn
#57 lore raw

Re: [Qemu-devel] [RFC v2 PATCH 11/13] target-arm: Generate fences in ARMv7 frontend

Quoted 7 lines · Sergey Fedorov
Quoted 6 lines · Pranith Kumar
---
 target-arm/translate.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
Quoted 13 lines

target-arm/translate.c static void disas_arm_insn(DisasContext *s, unsigned int insn) · line 7983

Quoted 9 lines
target-arm/translate.c +1 -0
index c946c0e..e1b16c0 100644
@@ -7980,9 +7980,11 @@ static void disas_arm_insn(DisasContext *s, unsigned int insn)
                 gen_clrex(s);
                 return;
             case 4: /* dsb */
+                ARCH(7);

ARMv7 ARM says: "A DSB behaves as a DMB with the same arguments, and also has the additional properties...".

OK. I was just focusing on dmb for now. I will add translation for all other barriers too in the next version.

Thanks!

Signature · 1 line
Pranith
#58 lore raw

RFC PATCH v2 12/13 target-alpha: Generate fence op

In reply to Pranith Kumar
---
 target-alpha/translate.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target-alpha/translate.c +3 -2
index 5b86992..17b68f5 100644
@@ -2329,11 +2329,12 @@ static ExitStatus translate_one(DisasContext *ctx, uint32_t insn)
             break;
         case 0x4000:
             /* MB */
-            /* No-op */
+            tcg_gen_mb(TCG_MB_FULL);
             break;
         case 0x4400:
             /* WMB */
-            /* No-op */
+            /* TODO: Change this to write barrier */
+            tcg_gen_mb(TCG_MB_FULL);
             break;
         case 0x8000:
             /* FETCH */
Signature · 1 line
2.8.3
#59 lore raw

RFC PATCH v2 13/13 tcg: Generate fences only for SMP MTTCG guests

In reply to Pranith Kumar

We need to generate fence instructions only for SMP MTTCG guests. This patch enforces that.

---
 tcg/tcg-op.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tcg/tcg-op.c +5 -2
index a6f01a7..eeb0d0c 100644
@@ -36,6 +36,8 @@ extern TCGv_i32 TCGV_HIGH_link_error(TCGv_i64);
 #define TCGV_HIGH TCGV_HIGH_link_error
 #endif
 
+extern int smp_cpus;
+
 /* Note that this is optimized for sequential allocation during translate.
    Up to and including filling in the forward link immediately.  We'll do
    proper termination of the end of the list after we finish translation.  */
@@ -145,8 +147,9 @@ void tcg_gen_op6(TCGContext *ctx, TCGOpcode opc, TCGArg a1, TCGArg a2,
 
 void tcg_gen_mb(TCGArg a)
 {
-    /* ??? Enable only when MTTCG is enabled.  */
-    tcg_gen_op1(&tcg_ctx, INDEX_op_mb, 0);
+    if (qemu_tcg_mttcg_enabled() && smp_cpus > 1) {
+        tcg_gen_op1(&tcg_ctx, INDEX_op_mb, 0);
+    }
 }
 
 /* 32 bit ops */
Signature · 1 line
2.8.3
#60 lore raw
In reply to Pranith Kumar

Added correct email for Sergey in CC.

I apologize for getting Sergey's email wrong. Please drop/correct his email when replying to the patches in this series otherwise you will see an email bounce.

Quoted 46 lines · Pranith Kumar

Hello,

The following series adds fence instruction generation support to TCG. The current work has been rebased on-top of Richard's patch series.

This has been tested and confirmed to fix ordering issues on a x86 host with MTTCG enabled ARMv7 guest using KVM unit tests.

Pranith Kumar (13):
Introduce TCGOpcode for memory barrier
tcg/i386: Add support for fence
tcg/aarch64: Add support for fence
tcg/arm: Add support for fence
tcg/ia64: Add support for fence
tcg/mips: Add support for fence
tcg/ppc: Add support for fence
tcg/s390: Add support for fence
tcg/sparc: Add support for fence
tcg/tci: Add support for fence
target-arm: Generate fences in ARMv7 frontend
target-alpha: Generate fence op
tcg: Generate fences only for SMP MTTCG guests

 target-alpha/translate.c     |  5 +++--
 target-arm/translate.c       |  7 +++++--
 tcg/README                   | 17 +++++++++++++++++
 tcg/aarch64/tcg-target.inc.c |  7 +++++++
 tcg/arm/tcg-target.inc.c     | 12 ++++++++++++
 tcg/i386/tcg-target.inc.c    | 35 +++++++++++++++++++++++++++++++++++
 tcg/ia64/tcg-target.inc.c    |  5 +++++
 tcg/mips/tcg-target.inc.c    |  6 ++++++
 tcg/ppc/tcg-target.inc.c     |  8 ++++++++
 tcg/s390/tcg-target.inc.c    |  9 +++++++++
 tcg/sparc/tcg-target.inc.c   |  8 ++++++++
 tcg/tcg-op.c                 |  9 +++++++++
 tcg/tcg-op.h                 |  2 ++
 tcg/tcg-opc.h                |  2 ++
 tcg/tcg.h                    |  8 ++++++++
 tcg/tci/tcg-target.inc.c     |  3 +++
 tci.c                        |  3 +++
 17 files changed, 142 insertions(+), 4 deletions(-)

--
2.8.3

Signature · 1 line
Pranith