Skip to contentSkip to conversation

tcg: Make direct jump patching thread-safe

PATCH Applied Sergey Fedorov 7 participants 57 messages Apr 7, 2016 – May 10, 2016

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover tcg: Make direct jump patching thread-safe

From: Sergey Fedorov <[email protected]>

When patching translated code for direct block chaining/unchaining, modification of concurrently executing code can happen in multi-threaded execution. Currently only user-mode is affected. To make direct block patching safe, some care must be taken to make sure that the code modification is made atomic and concurrently executed code is guaranteed to be consistent.

This patch series fixes all supported TCG targets using direct patching and documents the requirement for direct jump patching be atomic and thread-safe.

The series' tree can be found in a public git repository [1].

[1] https://github.com/sergefdrv/qemu/tree/atomic-tb-patching

Sergey Fedorov (10):
pc-bios/s390-ccw: Use correct strip when cross-compiling
tci: Make direct jump patching thread-safe tcg/ppc: Make direct jump patching thread-safe tcg/i386: Make direct jump patching thread-safe tcg/s390: Make direct jump patching thread-safe tcg/arm: Make direct jump patching thread-safe tcg/aarch64: Make direct jump patching thread-safe tcg/sparc: Make direct jump patching thread-safe tcg/mips: Make direct jump patching thread-safe
tcg: Note requirement on atomic direct jump patching

Stefan Weil (1):
tci: Fix build regression

 include/exec/exec-all.h      | 32 ++++++------------------------
 pc-bios/s390-ccw/Makefile    |  2 +-
 tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
 tcg/arm/tcg-target.inc.c     | 17 ++++++++++++++++
 tcg/i386/tcg-target.inc.c    | 17 ++++++++++++++++
 tcg/mips/tcg-target.inc.c    |  3 ++-
 tcg/ppc/tcg-target.inc.c     | 22 +++++++++++++++++----
 tcg/s390/tcg-target.inc.c    |  6 ++++++
 tcg/sparc/tcg-target.inc.c   |  2 +-
 tcg/tci/tcg-target.inc.c     |  2 ++
 tci.c                        | 46 +++++++++++++++++++++++++-------------------
 translate-all.c              |  2 ++
 12 files changed, 111 insertions(+), 54 deletions(-)
Signature · 1 line
2.8.1
#1 lore raw

PATCH 1/11 tci: Fix build regression

From: Stefan Weil <[email protected]>

Commit d38ea87ac54af64ef611de434d07c12dc0399216 cleaned the include statements which resulted in a wrong order of assert.h and the definition of NDEBUG in tci.c. Normally NDEBUG modifies the definition of the assert macro, but here this definition comes too late which results in a failing build.

To fix this, a new macro tci_assert which depends on CONFIG_DEBUG_TCG is introduced. Only builds with CONFIG_DEBUG_TCG will use assertions. Even in this case, it is still possible to disable assertions by defining NDEBUG via compiler settings.

---
 tci.c | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)
tci.c +22 -19
index 7cbb39ed4b6a..82705fe77295 100644
@@ -1,7 +1,7 @@
 /*
  * Tiny Code Interpreter for QEMU
  *
- * Copyright (c) 2009, 2011 Stefan Weil
+ * Copyright (c) 2009, 2011, 2016 Stefan Weil
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,9 +19,12 @@
 
 #include "qemu/osdep.h"
 
-/* Defining NDEBUG disables assertions (which makes the code faster). */
-#if !defined(CONFIG_DEBUG_TCG) && !defined(NDEBUG)
-# define NDEBUG
+/* Enable TCI assertions only when debugging TCG (and without NDEBUG defined).
+ * Without assertions, the interpreter runs much faster. */
+#if defined(CONFIG_DEBUG_TCG)
+# define tci_assert(cond) assert(cond)
+#else
+# define tci_assert(cond) ((void)0)
 #endif
 
 #include "qemu-common.h"
@@ -56,7 +59,7 @@ static tcg_target_ulong tci_reg[TCG_TARGET_NB_REGS];
 
 static tcg_target_ulong tci_read_reg(TCGReg index)
 {
-    assert(index < ARRAY_SIZE(tci_reg));
+    tci_assert(index < ARRAY_SIZE(tci_reg));
     return tci_reg[index];
 }
 
@@ -105,9 +108,9 @@ static uint64_t tci_read_reg64(TCGReg index)
 
 static void tci_write_reg(TCGReg index, tcg_target_ulong value)
 {
-    assert(index < ARRAY_SIZE(tci_reg));
-    assert(index != TCG_AREG0);
-    assert(index != TCG_REG_CALL_STACK);
+    tci_assert(index < ARRAY_SIZE(tci_reg));
+    tci_assert(index != TCG_AREG0);
+    tci_assert(index != TCG_REG_CALL_STACK);
     tci_reg[index] = value;
 }
 
@@ -325,7 +328,7 @@ static uint64_t tci_read_ri64(uint8_t **tb_ptr)
 static tcg_target_ulong tci_read_label(uint8_t **tb_ptr)
 {
     tcg_target_ulong label = tci_read_i(tb_ptr);
-    assert(label != 0);
+    tci_assert(label != 0);
     return label;
 }
 
@@ -468,11 +471,11 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
 
     tci_reg[TCG_AREG0] = (tcg_target_ulong)env;
     tci_reg[TCG_REG_CALL_STACK] = sp_value;
-    assert(tb_ptr);
+    tci_assert(tb_ptr);
 
     for (;;) {
         TCGOpcode opc = tb_ptr[0];
-#if !defined(NDEBUG)
+#if defined(CONFIG_DEBUG_TCG) && !defined(NDEBUG)
         uint8_t op_size = tb_ptr[1];
         uint8_t *old_code_ptr = tb_ptr;
 #endif
@@ -525,7 +528,7 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             break;
         case INDEX_op_br:
             label = tci_read_label(&tb_ptr);
-            assert(tb_ptr == old_code_ptr + op_size);
+            tci_assert(tb_ptr == old_code_ptr + op_size);
             tb_ptr = (uint8_t *)label;
             continue;
         case INDEX_op_setcond_i32:
@@ -600,7 +603,7 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             t0 = tci_read_r32(&tb_ptr);
             t1 = tci_read_r(&tb_ptr);
             t2 = tci_read_s32(&tb_ptr);
-            assert(t1 != sp_value || (int32_t)t2 < 0);
+            tci_assert(t1 != sp_value || (int32_t)t2 < 0);
             *(uint32_t *)(t1 + t2) = t0;
             break;
 
@@ -725,7 +728,7 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             condition = *tb_ptr++;
             label = tci_read_label(&tb_ptr);
             if (tci_compare32(t0, t1, condition)) {
-                assert(tb_ptr == old_code_ptr + op_size);
+                tci_assert(tb_ptr == old_code_ptr + op_size);
                 tb_ptr = (uint8_t *)label;
                 continue;
             }
@@ -751,7 +754,7 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             condition = *tb_ptr++;
             label = tci_read_label(&tb_ptr);
             if (tci_compare64(tmp64, v64, condition)) {
-                assert(tb_ptr == old_code_ptr + op_size);
+                tci_assert(tb_ptr == old_code_ptr + op_size);
                 tb_ptr = (uint8_t *)label;
                 continue;
             }
@@ -885,7 +888,7 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             t0 = tci_read_r64(&tb_ptr);
             t1 = tci_read_r(&tb_ptr);
             t2 = tci_read_s32(&tb_ptr);
-            assert(t1 != sp_value || (int32_t)t2 < 0);
+            tci_assert(t1 != sp_value || (int32_t)t2 < 0);
             *(uint64_t *)(t1 + t2) = t0;
             break;
 
@@ -992,7 +995,7 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             condition = *tb_ptr++;
             label = tci_read_label(&tb_ptr);
             if (tci_compare64(t0, t1, condition)) {
-                assert(tb_ptr == old_code_ptr + op_size);
+                tci_assert(tb_ptr == old_code_ptr + op_size);
                 tb_ptr = (uint8_t *)label;
                 continue;
             }
@@ -1087,7 +1090,7 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             break;
         case INDEX_op_goto_tb:
             t0 = tci_read_i32(&tb_ptr);
-            assert(tb_ptr == old_code_ptr + op_size);
+            tci_assert(tb_ptr == old_code_ptr + op_size);
             tb_ptr += (int32_t)t0;
             continue;
         case INDEX_op_qemu_ld_i32:
@@ -1234,7 +1237,7 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             TODO();
             break;
         }
-        assert(tb_ptr == old_code_ptr + op_size);
+        tci_assert(tb_ptr == old_code_ptr + op_size);
     }
 exit:
     return next_tb;
Signature · 1 line
2.8.1
#2 lore raw

Re: [Qemu-devel] [PATCH 01/11] tci: Fix build regression

tci.c line 28

Quoted 8 lines · Sergey Fedorov
tci.c +6 -0
+/* Enable TCI assertions only when debugging TCG (and without NDEBUG defined).
+ * Without assertions, the interpreter runs much faster. */
+#if defined(CONFIG_DEBUG_TCG)
+# define tci_assert(cond) assert(cond)
+#else
+# define tci_assert(cond) ((void)0)
  #endif

Please just use tcg_debug_assert.

r~

#3 lore raw

Re: [Qemu-devel] [PATCH 01/11] tci: Fix build regression

Am 07.04.2016 um 20:15 schrieb Richard Henderson:

Quoted 14 lines · Sergey Fedorov

tci.c line 28

Quoted 9 lines

+/* Enable TCI assertions only when debugging TCG (and without NDEBUG defined).
+ * Without assertions, the interpreter runs much faster. */

tci.c +4 -0
+#if defined(CONFIG_DEBUG_TCG)
+# define tci_assert(cond) assert(cond)
+#else
+# define tci_assert(cond) ((void)0)
  #endif

Please just use tcg_debug_assert.

Quoted 1 line

r~

Hi Richard,

that's a good suggestion, but maybe a little late for 2.6-rc2. I already sent a pull request an hour ago after Michael had added his tested-by.

My first priority is fixing the build regression in 2.6. I can try to prepare a new patch, wait for reviews and send a pull request, but I am afraid this might not be finished in time for 2.6.

Regards
Stefan

#4 lore raw

Re: [Qemu-devel] [PATCH 01/11] tci: Fix build regression

Am 07.04.2016 um 21:16 schrieb Stefan Weil:

Quoted 22 lines

Am 07.04.2016 um 20:15 schrieb Richard Henderson:

Quoted 13 lines · Sergey Fedorov

tci.c line 28

Quoted 9 lines

+/* Enable TCI assertions only when debugging TCG (and without NDEBUG defined).
+ * Without assertions, the interpreter runs much faster. */

tci.c +4 -0
+#if defined(CONFIG_DEBUG_TCG)
+# define tci_assert(cond) assert(cond)
+#else
+# define tci_assert(cond) ((void)0)
  #endif

Please just use tcg_debug_assert.

Quoted 1 line

r~

Hi Richard,

that's a good suggestion, but maybe a little late for 2.6-rc2. I already sent a pull request an hour ago after Michael had added his tested-by.

Quoted 7 lines

My first priority is fixing the build regression in 2.6. I can try to prepare a new patch, wait for reviews and send a pull request, but I am afraid this might not be finished in time for 2.6.

Regards
Stefan

I just tested a variant with tcg_debug_assert. It creates less efficient code when debugging is disabled. Here is the code size for x86_64:

with normal tcg_debug_assert:

text data bss dec hex filename
8293 0 128 8421 20e5
bin/ndebug/x86_64-linux-gnu,tci/x86_64-softmmu/tci.o

In gdb I can also see assembler code for op_size at the beginning of the for loop in tcg_qemu_tb_exec. This slows down the interpreter.

with ((void)0):

text data bss dec hex filename
8135 0 128 8263 2047
bin/ndebug/x86_64-linux-gnu,tci/x86_64-softmmu/tci.o

Therefore I'd prefer using my pull request for 2.6.

Stefan

#5 lore raw

Re: [Qemu-devel] [PATCH 01/11] tci: Fix build regression

Quoted 11 lines · Stefan Weil

I just tested a variant with tcg_debug_assert. It creates less efficient code when debugging is disabled. Here is the code size for x86_64:

with normal tcg_debug_assert:

text data bss dec hex filename
8293 0 128 8421 20e5
bin/ndebug/x86_64-linux-gnu,tci/x86_64-softmmu/tci.o

In gdb I can also see assembler code for op_size at the beginning of the for loop in tcg_qemu_tb_exec. This slows down the interpreter.

Please file a gcc bug.

A branch across __builtin_unreachable is supposed to fold to a simple compile-time assert, providing extra information to the compiler but not generating code.

r~

#6 lore raw

PATCH 2/11 pc-bios/s390-ccw: Use correct strip when cross-compiling

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +1 -1
index 4208cb429593..5ce6d4ccbaf5 100644
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 
Signature · 1 line
2.8.1
Acked-by on v1 2/11
#7 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

Quoted 4 lines · On Thu, 7 Apr 2016 18:53:44 +0300 Sergey Fedorov <[email protected]>…

From: Sergey Fedorov <[email protected]>

Any reason you use two different email addresses here? :)

Quoted 9 lines
---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +0 -0
index 4208cb429593..5ce6d4ccbaf5 100644

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 8 lines
pc-bios/s390-ccw/Makefile +1 -1
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 

I can take this through my queue for 2.7, if nothing else depends on it.

#8 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

Quoted 8 lines · Cornelia Huck
Quoted 4 lines · On Thu, 7 Apr 2016 18:53:44 +0300 Sergey Fedorov <[email protected]>…

From: Sergey Fedorov <[email protected]>

Any reason you use two different email addresses here? :)

I'd like people could reach me after my @linaro.org account get disabled. That's why I put my private address. On the other hand, I'd like to signify I did this work under Linaro. That's why I put another s-o-b tag.

Thanks,
Sergey

Quoted 22 lines
Quoted 10 lines
---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +0 -0
index 4208cb429593..5ce6d4ccbaf5 100644

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 8 lines
pc-bios/s390-ccw/Makefile +1 -1
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 

I can take this through my queue for 2.7, if nothing else depends on it.

#9 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

In reply to Cornelia Huck
Quoted 2 lines · Cornelia Huck

On Thu, 7 Apr 2016 18:53:44 +0300
Sergey Fedorov <[email protected]> wrote:

(snip)

Quoted 15 lines
Quoted 15 lines
---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +1 -1
index 4208cb429593..5ce6d4ccbaf5 100644
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
Quoted 6 lines

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 2 lines
pc-bios/s390-ccw/Makefile +0 -0
 $(OBJECTS): Makefile
 

I can take this through my queue for 2.7, if nothing else depends on it.

Yes, please :)

Kind regards,
Sergey

#10 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

In reply to Sergey Fedorov
Quoted 13 lines · On Thu, 7 Apr 2016 18:53:44 +0300 Sergey Fedorov <[email protected]>…

From: Sergey Fedorov <[email protected]>

---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +0 -0
index 4208cb429593..5ce6d4ccbaf5 100644

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 8 lines
pc-bios/s390-ccw/Makefile +1 -1
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 

Thanks, applied to s390-next.

#11 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

Quoted 18 lines · On Mon, 18 Apr 2016 16:51:16 +0200 Cornelia Huck <[email protected]>…
Quoted 15 lines · On Thu, 7 Apr 2016 18:53:44 +0300 Sergey Fedorov <[email protected]>…

From: Sergey Fedorov <[email protected]>

---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +0 -0
index 4208cb429593..5ce6d4ccbaf5 100644
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
Quoted 8 lines

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 6 lines
pc-bios/s390-ccw/Makefile +1 -1
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 

Thanks, applied to s390-next.

Uhm, scratch that.

This fails to build with --disable-strip, as $STRIP is unset in that case:

Building s390-ccw/s390-ccw.elf
Stripping s390-ccw/s390-ccw.img
/bin/sh: --strip-unneeded: command not found
make[1]: *** [s390-ccw.img] Error 127

The catch is that we always want to strip that binary. Care to send a patch that deals with that?

#12 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

Quoted 33 lines · Cornelia Huck
Quoted 18 lines · On Mon, 18 Apr 2016 16:51:16 +0200 Cornelia Huck <[email protected]>…
Quoted 15 lines · On Thu, 7 Apr 2016 18:53:44 +0300 Sergey Fedorov <[email protected]>…

From: Sergey Fedorov <[email protected]>

---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +0 -0
index 4208cb429593..5ce6d4ccbaf5 100644
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
Quoted 7 lines

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 6 lines
pc-bios/s390-ccw/Makefile +1 -1
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 

Thanks, applied to s390-next.

Uhm, scratch that.

This fails to build with --disable-strip, as $STRIP is unset in that case:

Quoted 9 lines

Building s390-ccw/s390-ccw.elf
Stripping s390-ccw/s390-ccw.img
/bin/sh: --strip-unneeded: command not found
make[1]: *** [s390-ccw.img] Error 127

The catch is that we always want to strip that binary. Care to send a patch that deals with that?

I see the problem. I don't promise to fix it soon, but I could try dealing with this as I have time. I don't mind if someone else can just fix it with their own patch :)

Kind regards,
Sergey

#13 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

Quoted 40 lines · Sergey Fedorov
Quoted 39 lines · Cornelia Huck
Quoted 18 lines · On Mon, 18 Apr 2016 16:51:16 +0200 Cornelia Huck <[email protected]>…
Quoted 15 lines · On Thu, 7 Apr 2016 18:53:44 +0300 Sergey Fedorov <[email protected]>…

From: Sergey Fedorov <[email protected]>

---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +0 -0
index 4208cb429593..5ce6d4ccbaf5 100644
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
Quoted 7 lines

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 6 lines
pc-bios/s390-ccw/Makefile +1 -1
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 

Thanks, applied to s390-next.

Uhm, scratch that.

This fails to build with --disable-strip, as $STRIP is unset in that case:

Building s390-ccw/s390-ccw.elf
Stripping s390-ccw/s390-ccw.img
/bin/sh: --strip-unneeded: command not found
make[1]: *** [s390-ccw.img] Error 127

Quoted 6 lines
Quoted 3 lines

The catch is that we always want to strip that binary. Care to send a patch that deals with that?

I see the problem. I don't promise to fix it soon, but I could try dealing with this as I have time. I don't mind if someone else can just fix it with their own patch :)

It's not straightforward to fix. We need to detect a correct cross-prefix for strip. There is something in roms/Makefile:

    #
    # cross compiler auto detection
    #
    path := $(subst :, ,$(PATH))
    system := $(shell uname -s | tr "A-Z" "a-z")

    # first find cross binutils in path
    find-cross-ld = $(firstword $(wildcard $(patsubst
    %,%/$(1)-*$(system)*-ld,$(path))))
    # then check we have cross gcc too
    find-cross-gcc = $(firstword $(wildcard $(patsubst %ld,%gcc,$(call
    find-cross-ld,$(1)))))
    # finally strip off path + toolname so we get the prefix
    find-cross-prefix = $(subst gcc,,$(notdir $(call find-cross-gcc,$(1))))

    powerpc64_cross_prefix := $(call find-cross-prefix,powerpc64)
    powerpc_cross_prefix := $(call find-cross-prefix,powerpc)
    x86_64_cross_prefix := $(call find-cross-prefix,x86_64)

and then:

    $(powerpc_cross_prefix)strip <...>

However, to solve this problem, it would be enough to export ${cross_prefix} from configure to config-host.mak and do like this:

    $(CROSS_PREFIX)strip <...>

Kind regards,
Sergey

#14 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

Quoted 61 lines · Sergey Fedorov
Quoted 40 lines · Sergey Fedorov
Quoted 39 lines · Cornelia Huck
Quoted 18 lines · On Mon, 18 Apr 2016 16:51:16 +0200 Cornelia Huck <[email protected]>…
Quoted 15 lines · On Thu, 7 Apr 2016 18:53:44 +0300 Sergey Fedorov <[email protected]>…

From: Sergey Fedorov <[email protected]>

---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +0 -0
index 4208cb429593..5ce6d4ccbaf5 100644
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
Quoted 7 lines

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 6 lines
pc-bios/s390-ccw/Makefile +1 -1
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 

Thanks, applied to s390-next.

Uhm, scratch that.

This fails to build with --disable-strip, as $STRIP is unset in that case:

Building s390-ccw/s390-ccw.elf
Stripping s390-ccw/s390-ccw.img
/bin/sh: --strip-unneeded: command not found
make[1]: *** [s390-ccw.img] Error 127

Quoted 6 lines
Quoted 3 lines

The catch is that we always want to strip that binary. Care to send a patch that deals with that?

I see the problem. I don't promise to fix it soon, but I could try dealing with this as I have time. I don't mind if someone else can just fix it with their own patch :)

It's not straightforward to fix. We need to detect a correct cross-prefix for strip. There is something in roms/Makefile:

    #
    # cross compiler auto detection
    #
    path := $(subst :, ,$(PATH))
    system := $(shell uname -s | tr "A-Z" "a-z")

    # first find cross binutils in path
    find-cross-ld = $(firstword $(wildcard $(patsubst
    %,%/$(1)-*$(system)*-ld,$(path))))
    # then check we have cross gcc too
Quoted 8 lines
    find-cross-gcc = $(firstword $(wildcard $(patsubst %ld,%gcc,$(call
    find-cross-ld,$(1)))))
    # finally strip off path + toolname so we get the prefix
    find-cross-prefix = $(subst gcc,,$(notdir $(call find-cross-gcc,$(1))))

    powerpc64_cross_prefix := $(call find-cross-prefix,powerpc64)
    powerpc_cross_prefix := $(call find-cross-prefix,powerpc)
    x86_64_cross_prefix := $(call find-cross-prefix,x86_64)

Ohh that's cool. I wonder if it could be used to solve the tcg/tests problem?

Quoted 7 lines

and then:

    $(powerpc_cross_prefix)strip <...>
Quoted 8 lines

However, to solve this problem, it would be enough to export ${cross_prefix} from configure to config-host.mak and do like this:

    $(CROSS_PREFIX)strip <...>

Kind regards,
Sergey

--
Alex Bennée

#15 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

Quoted 67 lines · Alex Bennée
Quoted 65 lines · Sergey Fedorov
Quoted 40 lines · Sergey Fedorov
Quoted 39 lines · Cornelia Huck
Quoted 18 lines · On Mon, 18 Apr 2016 16:51:16 +0200 Cornelia Huck <[email protected]>…
Quoted 15 lines · On Thu, 7 Apr 2016 18:53:44 +0300 Sergey Fedorov <[email protected]>…

From: Sergey Fedorov <[email protected]>

---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +0 -0
index 4208cb429593..5ce6d4ccbaf5 100644
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
Quoted 7 lines

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 6 lines
pc-bios/s390-ccw/Makefile +1 -1
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 

Thanks, applied to s390-next.

Uhm, scratch that.

This fails to build with --disable-strip, as $STRIP is unset in that case:

Building s390-ccw/s390-ccw.elf
Stripping s390-ccw/s390-ccw.img
/bin/sh: --strip-unneeded: command not found
make[1]: *** [s390-ccw.img] Error 127

Quoted 6 lines
Quoted 3 lines

The catch is that we always want to strip that binary. Care to send a patch that deals with that?

I see the problem. I don't promise to fix it soon, but I could try dealing with this as I have time. I don't mind if someone else can just fix it with their own patch :)

It's not straightforward to fix. We need to detect a correct cross-prefix for strip. There is something in roms/Makefile:

    #
    # cross compiler auto detection
    #
    path := $(subst :, ,$(PATH))
    system := $(shell uname -s | tr "A-Z" "a-z")

    # first find cross binutils in path
    find-cross-ld = $(firstword $(wildcard $(patsubst
    %,%/$(1)-*$(system)*-ld,$(path))))
    # then check we have cross gcc too
    find-cross-gcc = $(firstword $(wildcard $(patsubst %ld,%gcc,$(call
    find-cross-ld,$(1)))))
    # finally strip off path + toolname so we get the prefix
    find-cross-prefix = $(subst gcc,,$(notdir $(call find-cross-gcc,$(1))))
Quoted 5 lines
Quoted 3 lines
    powerpc64_cross_prefix := $(call find-cross-prefix,powerpc64)
    powerpc_cross_prefix := $(call find-cross-prefix,powerpc)
    x86_64_cross_prefix := $(call find-cross-prefix,x86_64)

Ohh that's cool. I wonder if it could be used to solve the tcg/tests problem?

Which problem? :)

Kind regards,
Sergey

#16 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

In reply to Sergey Fedorov
Quoted 71 lines · On Thu, 21 Apr 2016 20:36:49 +0300 Sergey Fedorov <[email protected]> wrote:
Quoted 40 lines · Sergey Fedorov
Quoted 39 lines · Cornelia Huck
Quoted 18 lines · On Mon, 18 Apr 2016 16:51:16 +0200 Cornelia Huck <[email protected]>…
Quoted 15 lines · On Thu, 7 Apr 2016 18:53:44 +0300 Sergey Fedorov <[email protected]>…

From: Sergey Fedorov <[email protected]>

---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
pc-bios/s390-ccw/Makefile +0 -0
index 4208cb429593..5ce6d4ccbaf5 100644
@@ -20,7 +20,7 @@ s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"  Building $(TARGET_DIR)$@")
 
Quoted 7 lines

pc-bios/s390-ccw/Makefile s390-ccw.elf: $(OBJECTS) · line 26

Quoted 6 lines
pc-bios/s390-ccw/Makefile +1 -1
 s390-ccw.img: s390-ccw.elf
-	$(call quiet-command,strip --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
+	$(call quiet-command,$(STRIP) --strip-unneeded $< -o $@,"  Stripping $(TARGET_DIR)$@")
 
 $(OBJECTS): Makefile
 

Thanks, applied to s390-next.

Uhm, scratch that.

This fails to build with --disable-strip, as $STRIP is unset in that case:

Building s390-ccw/s390-ccw.elf
Stripping s390-ccw/s390-ccw.img
/bin/sh: --strip-unneeded: command not found
make[1]: *** [s390-ccw.img] Error 127

Quoted 6 lines
Quoted 3 lines

The catch is that we always want to strip that binary. Care to send a patch that deals with that?

I see the problem. I don't promise to fix it soon, but I could try dealing with this as I have time. I don't mind if someone else can just fix it with their own patch :)

It's not straightforward to fix. We need to detect a correct cross-prefix for strip. There is something in roms/Makefile:

    #
    # cross compiler auto detection
    #
    path := $(subst :, ,$(PATH))
    system := $(shell uname -s | tr "A-Z" "a-z")

    # first find cross binutils in path
    find-cross-ld = $(firstword $(wildcard $(patsubst
    %,%/$(1)-*$(system)*-ld,$(path))))
    # then check we have cross gcc too
    find-cross-gcc = $(firstword $(wildcard $(patsubst %ld,%gcc,$(call
    find-cross-ld,$(1)))))
    # finally strip off path + toolname so we get the prefix
    find-cross-prefix = $(subst gcc,,$(notdir $(call find-cross-gcc,$(1))))

    powerpc64_cross_prefix := $(call find-cross-prefix,powerpc64)
    powerpc_cross_prefix := $(call find-cross-prefix,powerpc)
    x86_64_cross_prefix := $(call find-cross-prefix,x86_64)
Quoted 3 lines

and then:

    $(powerpc_cross_prefix)strip <...>

This one has the drawback of having to add architectures manually, though.

Quoted 6 lines

However, to solve this problem, it would be enough to export ${cross_prefix} from configure to config-host.mak and do like this:

    $(CROSS_PREFIX)strip <...>

I agree. This would probably be the easiest way.

#17 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

Quoted 6 lines · Cornelia Huck
Quoted 4 lines
Quoted 4 lines

However, to solve this problem, it would be enough to export ${cross_prefix} from configure to config-host.mak and do like this:

    $(CROSS_PREFIX)strip <...>

I agree. This would probably be the easiest way.

$(CROSS_PREFIX) is a build->host cross prefix (tool running on build machine, producing output for the machine that QEMU runs on). The ones in roms/Makefile are build->target cross prefix (tool running on build machine, producing output for the machine that QEMU emulates).

So roms/Makefile is the way to go.

Paolo

#18 lore raw

Re: [Qemu-devel] [PATCH 02/11] pc-bios/s390-ccw: Use correct strip when cross-compiling

Quoted 6 lines · Paolo Bonzini
Quoted 4 lines · Cornelia Huck
Quoted 4 lines
Quoted 4 lines

However, to solve this problem, it would be enough to export ${cross_prefix} from configure to config-host.mak and do like this:

    $(CROSS_PREFIX)strip <...>
Quoted 8 lines
Quoted 2 lines

I agree. This would probably be the easiest way.

$(CROSS_PREFIX) is a build->host cross prefix (tool running on build machine, producing output for the machine that QEMU runs on). The ones in roms/Makefile are build->target cross prefix (tool running on build machine, producing output for the machine that QEMU emulates).

So roms/Makefile is the way to go.

You're right, thanks. I suspect there should be some way to move this stuff from roms/Makefile.

Kind regards,
Sergey

#19 lore raw

PATCH 3/11 tci: Make direct jump patching thread-safe

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in TCI is atomic by:
* naturally aligning a location of direct jump address;
* using atomic_read()/atomic_set() to load/store the address.

---
 include/exec/exec-all.h  | 2 +-
 tcg/tci/tcg-target.inc.c | 2 ++
 tci.c                    | 5 ++++-
 3 files changed, 7 insertions(+), 2 deletions(-)
include/exec/exec-all.h +1 -1
index 736209505a68..59709c9dd5c9 100644
@@ -302,7 +302,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     /* patch the branch destination */
-    *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
+    atomic_set((int32_t *)jmp_addr, addr - (jmp_addr + 4));
     /* no need to flush icache explicitly */
 }
 #elif defined(_ARCH_PPC)
tcg/tci/tcg-target.inc.c +2 -0
index 4afe4d7a8d59..7e6180e62898 100644
@@ -556,6 +556,8 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         if (s->tb_jmp_offset) {
             /* Direct jump method. */
             assert(args[0] < ARRAY_SIZE(s->tb_jmp_offset));
+            /* Align for atomic patching and thread safety */
+            s->code_ptr = (uint8_t *)(((uintptr_t)s->code_ptr + 3) & ~3);
             s->tb_jmp_offset[args[0]] = tcg_current_code_size(s);
             tcg_out32(s, 0);
         } else {
tci.c +4 -1
index 82705fe77295..531f5ebf2c2e 100644
@@ -1089,7 +1089,10 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             goto exit;
             break;
         case INDEX_op_goto_tb:
-            t0 = tci_read_i32(&tb_ptr);
+            /* Jump address is aligned */
+            tb_ptr = (uint8_t *)(((uintptr_t)tb_ptr + 3) & ~3);
+            t0 = atomic_read((int32_t *)tb_ptr);
+            tb_ptr += sizeof(int32_t);
             tci_assert(tb_ptr == old_code_ptr + op_size);
             tb_ptr += (int32_t)t0;
             continue;
Signature · 1 line
2.8.1
#20 lore raw

Re: [Qemu-devel] [PATCH 03/11] tci: Make direct jump patching thread-safe

Quoted 32 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in TCI is atomic by:
* naturally aligning a location of direct jump address;
* using atomic_read()/atomic_set() to load/store the address.

---
 include/exec/exec-all.h  | 2 +-
 tcg/tci/tcg-target.inc.c | 2 ++
 tci.c                    | 5 ++++-
 3 files changed, 7 insertions(+), 2 deletions(-)
include/exec/exec-all.h +1 -1
index 736209505a68..59709c9dd5c9 100644
@@ -302,7 +302,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr);
 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     /* patch the branch destination */
-    *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
+    atomic_set((int32_t *)jmp_addr, addr - (jmp_addr + 4));
     /* no need to flush icache explicitly */
 }
 #elif defined(_ARCH_PPC)
tcg/tci/tcg-target.inc.c +0 -0
index 4afe4d7a8d59..7e6180e62898 100644

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

Quoted 6 lines
tcg/tci/tcg-target.inc.c +2 -0
@@ -556,6 +556,8 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         if (s->tb_jmp_offset) {
             /* Direct jump method. */
             assert(args[0] < ARRAY_SIZE(s->tb_jmp_offset));
+            /* Align for atomic patching and thread safety */
+            s->code_ptr = (uint8_t *)(((uintptr_t)s->code_ptr + 3) &
 ~3);

Seeing this pattern is being used over and over again I wonder if we should have some utility helper functions for this? Perhaps we should steal the kernels ALIGN macros?

Quoted 9 lines
tcg/tci/tcg-target.inc.c +0 -0
             s->tb_jmp_offset[args[0]] = tcg_current_code_size(s);
             tcg_out32(s, 0);
         } else {
tci.c +0 -0
index 82705fe77295..531f5ebf2c2e 100644
@@ -1089,7 +1089,10 @@ uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr)
             goto exit;

tci.c uintptr_t tcg_qemu_tb_exec(CPUArchState *env, uint8_t *tb_ptr) · line 1098

Quoted 10 lines
tci.c +4 -1
             break;
         case INDEX_op_goto_tb:
-            t0 = tci_read_i32(&tb_ptr);
+            /* Jump address is aligned */
+            tb_ptr = (uint8_t *)(((uintptr_t)tb_ptr + 3) & ~3);
+            t0 = atomic_read((int32_t *)tb_ptr);
+            tb_ptr += sizeof(int32_t);
             tci_assert(tb_ptr == old_code_ptr + op_size);
             tb_ptr += (int32_t)t0;
             continue;

--
Alex Bennée

#21 lore raw

Re: [Qemu-devel] [PATCH 03/11] tci: Make direct jump patching thread-safe

Quoted 15 lines · Alex Bennée

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

Quoted 11 lines · Sergey Fedorov
tcg/tci/tcg-target.inc.c +2 -0
index 4afe4d7a8d59..7e6180e62898 100644
@@ -556,6 +556,8 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         if (s->tb_jmp_offset) {
             /* Direct jump method. */
             assert(args[0] < ARRAY_SIZE(s->tb_jmp_offset));
+            /* Align for atomic patching and thread safety */
+            s->code_ptr = (uint8_t *)(((uintptr_t)s->code_ptr + 3) &
 ~3);

Seeing this pattern is being used over and over again I wonder if we should have some utility helper functions for this? Perhaps we should steal the kernels ALIGN macros?

Good point, really. I see such a macro in hw/display/qxl.c and kvm-all.c. It'd be better a common definition. Any idea of where to put it?

Kind regards,
Sergey

#22 lore raw

Re: [Qemu-devel] [PATCH 03/11] tci: Make direct jump patching thread-safe

Quoted 17 lines · Sergey Fedorov
Quoted 15 lines · Alex Bennée

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

Quoted 11 lines · Sergey Fedorov
tcg/tci/tcg-target.inc.c +2 -0
index 4afe4d7a8d59..7e6180e62898 100644
@@ -556,6 +556,8 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         if (s->tb_jmp_offset) {
             /* Direct jump method. */
             assert(args[0] < ARRAY_SIZE(s->tb_jmp_offset));
+            /* Align for atomic patching and thread safety */
+            s->code_ptr = (uint8_t *)(((uintptr_t)s->code_ptr + 3) &
 ~3);

Seeing this pattern is being used over and over again I wonder if we should have some utility helper functions for this? Perhaps we should steal the kernels ALIGN macros?

Quoted 3 lines

Good point, really. I see such a macro in hw/display/qxl.c and kvm-all.c. It'd be better a common definition. Any idea of where to put it?

Somewhere inside include/qemu. osdep.h has ROUND_UP/DOWN functions maybe there makes the most sense?

Quoted 3 lines

Kind regards,
Sergey

--
Alex Bennée

#23 lore raw

Re: [Qemu-devel] [PATCH 03/11] tci: Make direct jump patching thread-safe

Quoted 18 lines · Alex Bennée
Quoted 16 lines · Sergey Fedorov
Quoted 15 lines · Alex Bennée

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

Quoted 11 lines · Sergey Fedorov
tcg/tci/tcg-target.inc.c +2 -0
index 4afe4d7a8d59..7e6180e62898 100644
@@ -556,6 +556,8 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         if (s->tb_jmp_offset) {
             /* Direct jump method. */
             assert(args[0] < ARRAY_SIZE(s->tb_jmp_offset));
+            /* Align for atomic patching and thread safety */
+            s->code_ptr = (uint8_t *)(((uintptr_t)s->code_ptr + 3) &
 ~3);

Seeing this pattern is being used over and over again I wonder if we should have some utility helper functions for this? Perhaps we should steal the kernels ALIGN macros?

Quoted 5 lines
Quoted 3 lines

Good point, really. I see such a macro in hw/display/qxl.c and kvm-all.c. It'd be better a common definition. Any idea of where to put it?

Somewhere inside include/qemu. osdep.h has ROUND_UP/DOWN functions maybe there makes the most sense?

Hmm, ROUND_UP() seems to be exactly what we need here. Though I think compiler could be smart enough to give the same code with QEMU_ALIGN_UP() as well. But we'd benefit from something like:

/* n-byte align pointer down */

#define QEMU_ALIGN_PTR_DOWN(p, n) \
    ((typeof(p))QEMU_ALIGN_DOWN((uintptr_t)(p), (n)))

/* n-byte align pointer up */

#define QEMU_ALIGN_PTR_UP(p, n) \
    ((typeof(p))QEMU_ALIGN_UP((uintptr_t)(p), (n)))

Kind regards,
Sergey

#24 lore raw

Re: [Qemu-devel] [PATCH 03/11] tci: Make direct jump patching thread-safe

Quoted 29 lines · Sergey Fedorov
Quoted 18 lines · Alex Bennée
Quoted 16 lines · Sergey Fedorov
Quoted 15 lines · Alex Bennée

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

Quoted 11 lines · Sergey Fedorov
tcg/tci/tcg-target.inc.c +2 -0
index 4afe4d7a8d59..7e6180e62898 100644
@@ -556,6 +556,8 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         if (s->tb_jmp_offset) {
             /* Direct jump method. */
             assert(args[0] < ARRAY_SIZE(s->tb_jmp_offset));
+            /* Align for atomic patching and thread safety */
+            s->code_ptr = (uint8_t *)(((uintptr_t)s->code_ptr + 3) &
 ~3);

Seeing this pattern is being used over and over again I wonder if we should have some utility helper functions for this? Perhaps we should steal the kernels ALIGN macros?

Quoted 5 lines
Quoted 3 lines

Good point, really. I see such a macro in hw/display/qxl.c and kvm-all.c. It'd be better a common definition. Any idea of where to put it?

Somewhere inside include/qemu. osdep.h has ROUND_UP/DOWN functions maybe there makes the most sense?

Hmm, ROUND_UP() seems to be exactly what we need here. Though I think compiler could be smart enough to give the same code with QEMU_ALIGN_UP() as well. But we'd benefit from something like:

Quoted 7 lines

/* n-byte align pointer down */

#define QEMU_ALIGN_PTR_DOWN(p, n) \
    ((typeof(p))QEMU_ALIGN_DOWN((uintptr_t)(p), (n)))

/* n-byte align pointer up */

#define QEMU_ALIGN_PTR_UP(p, n) \
    ((typeof(p))QEMU_ALIGN_UP((uintptr_t)(p), (n)))

Sounds good.

--
Alex Bennée

#25 lore raw

PATCH 4/11 tcg/ppc: Make direct jump patching thread-safe

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in PPC is atomic by:
* limiting translation buffer size in 32-bit mode to be addressable by Branch I-form instruction;
* using atomic_read()/atomic_set() for code patching.

---
 tcg/ppc/tcg-target.inc.c | 22 ++++++++++++++++++----
 translate-all.c          |  2 ++
 2 files changed, 20 insertions(+), 4 deletions(-)
tcg/ppc/tcg-target.inc.c +18 -4
index 8c1c2dfa9b22..a7c65fd08854 100644
@@ -1237,6 +1237,7 @@ static void tcg_out_brcond2 (TCGContext *s, const TCGArg *args,
     tcg_out_bc(s, BC | BI(7, CR_EQ) | BO_COND_TRUE, arg_label(args[5]));
 }
 
+#ifdef __powerpc64__
 void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
 {
     tcg_insn_unit i1, i2;
@@ -1265,11 +1266,18 @@ void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
     pair = (uint64_t)i2 << 32 | i1;
 #endif
 
-    /* ??? __atomic_store_8, presuming there's some way to do that
-       for 32-bit, otherwise this is good enough for 64-bit.  */
-    *(uint64_t *)jmp_addr = pair;
+    atomic_set((uint64_t *)jmp_addr, pair);
     flush_icache_range(jmp_addr, jmp_addr + 8);
 }
+#else
+void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
+{
+    intptr_t diff = addr - jmp_addr;
+    tcg_debug_assert(in_range_b(diff));
+    atomic_set((uint32_t *)jmp_addr, B | (diff & 0x3fffffc));
+    flush_icache_range(jmp_addr, jmp_addr + 4);
+}
+#endif
 
 static void tcg_out_call(TCGContext *s, tcg_insn_unit *target)
 {
@@ -1895,7 +1903,9 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         break;
     case INDEX_op_goto_tb:
         tcg_debug_assert(s->tb_jmp_offset);
-        /* Direct jump.  Ensure the next insns are 8-byte aligned. */
+        /* Direct jump. */
+#ifdef __powerpc64__
+        /* Ensure the next insns are 8-byte aligned. */
         if ((uintptr_t)s->code_ptr & 7) {
             tcg_out32(s, NOP);
         }
@@ -1904,6 +1914,10 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         s->code_ptr += 2;
         tcg_out32(s, MTSPR | RS(TCG_REG_TMP1) | CTR);
         tcg_out32(s, BCCTR | BO_ALWAYS);
+#else
+        /* To be replaced by a branch.  */
+        s->code_ptr++;
+#endif
         s->tb_next_offset[args[0]] = tcg_current_code_size(s);
         break;
     case INDEX_op_br:
translate-all.c +2 -0
index b4df1ec68fa9..9b98a4a36967 100644
@@ -464,6 +464,8 @@ static inline PageDesc *page_find(tb_page_addr_t index)
 # define MAX_CODE_GEN_BUFFER_SIZE  (2ul * 1024 * 1024 * 1024)
 #elif defined(__powerpc64__)
 # define MAX_CODE_GEN_BUFFER_SIZE  (2ul * 1024 * 1024 * 1024)
+#elif defined(__powerpc__)
+# define MAX_CODE_GEN_BUFFER_SIZE  (32u * 1024 * 1024)
 #elif defined(__aarch64__)
 # define MAX_CODE_GEN_BUFFER_SIZE  (128ul * 1024 * 1024)
 #elif defined(__arm__)
Signature · 1 line
2.8.1
Reviewed-by on v1 4/11
#26 lore raw

Re: [Qemu-devel] [PATCH 04/11] tcg/ppc: Make direct jump patching thread-safe

Quoted 32 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in PPC is atomic by:
* limiting translation buffer size in 32-bit mode to be addressable by Branch I-form instruction;
* using atomic_read()/atomic_set() for code patching.

---
 tcg/ppc/tcg-target.inc.c | 22 ++++++++++++++++++----
 translate-all.c          |  2 ++
 2 files changed, 20 insertions(+), 4 deletions(-)
tcg/ppc/tcg-target.inc.c +1 -2
index 8c1c2dfa9b22..a7c65fd08854 100644
@@ -1237,6 +1237,7 @@ static void tcg_out_brcond2 (TCGContext *s, const TCGArg *args,
     tcg_out_bc(s, BC | BI(7, CR_EQ) | BO_COND_TRUE, arg_label(args[5]));
 }
 
+#ifdef __powerpc64__
 void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
 {
     tcg_insn_unit i1, i2;
@@ -1265,11 +1266,18 @@ void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
     pair = (uint64_t)i2 << 32 | i1;
 #endif
 
-    /* ??? __atomic_store_8, presuming there's some way to do that
-       for 32-bit, otherwise this is good enough for 64-bit.  */

tcg/ppc/tcg-target.inc.c void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr) · line 1277

Quoted 10 lines
tcg/ppc/tcg-target.inc.c +7 -1
-    *(uint64_t *)jmp_addr = pair;
+    atomic_set((uint64_t *)jmp_addr, pair);
     flush_icache_range(jmp_addr, jmp_addr + 8);
 }
+#else
+void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
+{
+    intptr_t diff = addr - jmp_addr;
+    tcg_debug_assert(in_range_b(diff));
+    atomic_set((uint32_t *)jmp_addr, B | (diff & 0x3fffffc));

I'll let that slide because the rest of the code sprinkles this magic number about. However I suspect 0x3fffffc should be a clean #define.

Quoted 33 lines
tcg/ppc/tcg-target.inc.c +10 -1
+    flush_icache_range(jmp_addr, jmp_addr + 4);
+}
+#endif
 
 static void tcg_out_call(TCGContext *s, tcg_insn_unit *target)
 {
@@ -1895,7 +1903,9 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         break;
     case INDEX_op_goto_tb:
         tcg_debug_assert(s->tb_jmp_offset);
-        /* Direct jump.  Ensure the next insns are 8-byte aligned. */
+        /* Direct jump. */
+#ifdef __powerpc64__
+        /* Ensure the next insns are 8-byte aligned. */
         if ((uintptr_t)s->code_ptr & 7) {
             tcg_out32(s, NOP);
         }
@@ -1904,6 +1914,10 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
         s->code_ptr += 2;
         tcg_out32(s, MTSPR | RS(TCG_REG_TMP1) | CTR);
         tcg_out32(s, BCCTR | BO_ALWAYS);
+#else
+        /* To be replaced by a branch.  */
+        s->code_ptr++;
+#endif
         s->tb_next_offset[args[0]] = tcg_current_code_size(s);
         break;
     case INDEX_op_br:
translate-all.c +0 -0
index b4df1ec68fa9..9b98a4a36967 100644

translate-all.c static inline PageDesc *page_find(tb_page_addr_t index) · line 471

Quoted 8 lines
translate-all.c +2 -0
@@ -464,6 +464,8 @@ static inline PageDesc *page_find(tb_page_addr_t index)
 # define MAX_CODE_GEN_BUFFER_SIZE  (2ul * 1024 * 1024 * 1024)
 #elif defined(__powerpc64__)
 # define MAX_CODE_GEN_BUFFER_SIZE  (2ul * 1024 * 1024 * 1024)
+#elif defined(__powerpc__)
+# define MAX_CODE_GEN_BUFFER_SIZE  (32u * 1024 * 1024)
 #elif defined(__aarch64__)
 # define MAX_CODE_GEN_BUFFER_SIZE  (128ul * 1024 * 1024)
 #elif defined(__arm__)

--
Alex Bennée

#27 lore raw

PATCH 5/11 tcg/i386: Make direct jump patching thread-safe

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in i386 is atomic by:
* naturally aligning a location of direct jump address;
* using atomic_read()/atomic_set() for code patching.

---
 include/exec/exec-all.h   |  2 +-
 tcg/i386/tcg-target.inc.c | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
include/exec/exec-all.h +1 -1
index 59709c9dd5c9..82399175fe80 100644
@@ -312,7 +312,7 @@ void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     /* patch the branch destination */
-    stl_le_p((void*)jmp_addr, addr - (jmp_addr + 4));
+    atomic_set((int32_t *)jmp_addr, addr - (jmp_addr + 4));
     /* no need to flush icache explicitly */
 }
 #elif defined(__s390x__)
tcg/i386/tcg-target.inc.c +17 -0
index 9187d34caf6d..3ffb7b3124d8 100644
@@ -1123,6 +1123,19 @@ static void tcg_out_jmp(TCGContext *s, tcg_insn_unit *dest)
     tcg_out_branch(s, 0, dest);
 }
 
+static void tcg_out_nopn(TCGContext *s, int n)
+{
+    static const uint8_t nop1[] = { 0x90 };
+    static const uint8_t nop2[] = { 0x66, 0x90 };
+    static const uint8_t nop3[] = { 0x8d, 0x76, 0x00 };
+    static const uint8_t *const nopn[] = { nop1, nop2, nop3 };
+    int i;
+    assert(n <= ARRAY_SIZE(nopn));
+    for (i = 0; i < n; ++i) {
+        tcg_out8(s, nopn[n - 1][i]);
+    }
+}
+
 #if defined(CONFIG_SOFTMMU)
 /* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
  *                                     int mmu_idx, uintptr_t ra)
@@ -1777,6 +1790,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
     case INDEX_op_goto_tb:
         if (s->tb_jmp_offset) {
             /* direct jump method */
+            /* align jump displacement for atomic pathing */
+            if (((uintptr_t)s->code_ptr & 3) != 3) {
+                tcg_out_nopn(s, 3 - ((uintptr_t)s->code_ptr & 3));
+            }
             tcg_out8(s, OPC_JMP_long); /* jmp im */
             s->tb_jmp_offset[args[0]] = tcg_current_code_size(s);
             tcg_out32(s, 0);
Signature · 1 line
2.8.1
#28 lore raw

Re: [Qemu-devel] [PATCH 05/11] tcg/i386: Make direct jump patching thread-safe

Quoted 36 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in i386 is atomic by:
* naturally aligning a location of direct jump address;
* using atomic_read()/atomic_set() for code patching.

---
 include/exec/exec-all.h   |  2 +-
 tcg/i386/tcg-target.inc.c | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
include/exec/exec-all.h +1 -1
index 59709c9dd5c9..82399175fe80 100644
@@ -312,7 +312,7 @@ void ppc_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
 static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     /* patch the branch destination */
-    stl_le_p((void*)jmp_addr, addr - (jmp_addr + 4));
+    atomic_set((int32_t *)jmp_addr, addr - (jmp_addr + 4));
     /* no need to flush icache explicitly */
 }
 #elif defined(__s390x__)
tcg/i386/tcg-target.inc.c +2 -0
index 9187d34caf6d..3ffb7b3124d8 100644
@@ -1123,6 +1123,19 @@ static void tcg_out_jmp(TCGContext *s, tcg_insn_unit *dest)
     tcg_out_branch(s, 0, dest);
 }
 
+static void tcg_out_nopn(TCGContext *s, int n)
+{

tcg/i386/tcg-target.inc.c static void tcg_out_jmp(TCGContext *s, tcg_insn_unit *dest) · line 1137

Quoted 10 lines
tcg/i386/tcg-target.inc.c +10 -0
+    static const uint8_t nop1[] = { 0x90 };
+    static const uint8_t nop2[] = { 0x66, 0x90 };
+    static const uint8_t nop3[] = { 0x8d, 0x76, 0x00 };
+    static const uint8_t *const nopn[] = { nop1, nop2, nop3 };
+    int i;
+    assert(n <= ARRAY_SIZE(nopn));
+    for (i = 0; i < n; ++i) {
+        tcg_out8(s, nopn[n - 1][i]);
+    }
+}

*shudder* I recall x86 instruction encoding is weird. Maybe a comment
for the function to describe the 3 forms of NOP we have here?

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

Quoted 9 lines
tcg/i386/tcg-target.inc.c +2 -0
+
 #if defined(CONFIG_SOFTMMU)
 /* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
  *                                     int mmu_idx, uintptr_t ra)
@@ -1777,6 +1790,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
     case INDEX_op_goto_tb:
         if (s->tb_jmp_offset) {
             /* direct jump method */
+            /* align jump displacement for atomic pathing */

s/pathing/patching/

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

Quoted 3 lines
tcg/i386/tcg-target.inc.c +3 -0
+            if (((uintptr_t)s->code_ptr & 3) != 3) {
+                tcg_out_nopn(s, 3 - ((uintptr_t)s->code_ptr & 3));
+            }

apropos my previous comments. I think the intention could be made clearer the use of well named helper functions to check alignment and calculate number elements until next alignment.

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

Quoted 3 lines
tcg/i386/tcg-target.inc.c +0 -0
             tcg_out8(s, OPC_JMP_long); /* jmp im */
             s->tb_jmp_offset[args[0]] = tcg_current_code_size(s);
             tcg_out32(s, 0);

--
Alex Bennée

#29 lore raw

Re: [Qemu-devel] [PATCH 05/11] tcg/i386: Make direct jump patching thread-safe

Quoted 9 lines · Alex Bennée
Quoted 8 lines · Sergey Fedorov
tcg/i386/tcg-target.inc.c +0 -0
index 9187d34caf6d..3ffb7b3124d8 100644
@@ -1123,6 +1123,19 @@ static void tcg_out_jmp(TCGContext *s, tcg_insn_unit *dest)
     tcg_out_branch(s, 0, dest);
 }
 
Quoted 14 lines

tcg/i386/tcg-target.inc.c static void tcg_out_jmp(TCGContext *s, tcg_insn_unit *dest) · line 1137

Quoted 12 lines
tcg/i386/tcg-target.inc.c +12 -0
+static void tcg_out_nopn(TCGContext *s, int n)
+{
+    static const uint8_t nop1[] = { 0x90 };
+    static const uint8_t nop2[] = { 0x66, 0x90 };
+    static const uint8_t nop3[] = { 0x8d, 0x76, 0x00 };
+    static const uint8_t *const nopn[] = { nop1, nop2, nop3 };
+    int i;
+    assert(n <= ARRAY_SIZE(nopn));
+    for (i = 0; i < n; ++i) {
+        tcg_out8(s, nopn[n - 1][i]);
+    }
+}

*shudder* I recall x86 instruction encoding is weird. Maybe a comment
for the function to describe the 3 forms of NOP we have here?

Okay.

Quoted 11 lines

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

Quoted 10 lines
tcg/i386/tcg-target.inc.c +2 -0
+
 #if defined(CONFIG_SOFTMMU)
 /* helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
  *                                     int mmu_idx, uintptr_t ra)
@@ -1777,6 +1790,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
     case INDEX_op_goto_tb:
         if (s->tb_jmp_offset) {
             /* direct jump method */
+            /* align jump displacement for atomic pathing */

s/pathing/patching/

Nice catch, thanks :)

Kind regards,
Sergey

#30 lore raw

Re: [Qemu-devel] [PATCH 05/11] tcg/i386: Make direct jump patching thread-safe

In reply to Alex Bennée
Quoted 7 lines · Alex Bennée
Quoted 7 lines
tcg/i386/tcg-target.inc.c +7 -0
+static void tcg_out_nopn(TCGContext *s, int n)
+{
+    static const uint8_t nop1[] = { 0x90 };
+    static const uint8_t nop2[] = { 0x66, 0x90 };
+    static const uint8_t nop3[] = { 0x8d, 0x76, 0x00 };
+    static const uint8_t *const nopn[] = { nop1, nop2, nop3 };
+    int i;
Quoted 8 lines

tcg/i386/tcg-target.inc.c static void tcg_out_jmp(TCGContext *s, tcg_insn_unit *dest) · line 1137

Quoted 5 lines
tcg/i386/tcg-target.inc.c +5 -0
+    assert(n <= ARRAY_SIZE(nopn));
+    for (i = 0; i < n; ++i) {
+        tcg_out8(s, nopn[n - 1][i]);
+    }
+}

*shudder* I recall x86 instruction encoding is weird. Maybe a comment
for the function to describe the 3 forms of NOP we have here?

I think I'd prefer to drop the tables and do

/* Emit 1 or 2 operand size prefixes for the standard one byte nop,
xchg %eax,%eax, forming xchg %ax,%ax. All cores accept the duplicate prefix, and all of the interesting recent cores can decode and discard the duplicates in a single cycle. */

  for (i = 1; i < n; ++i) {
    tcg_out8(s, 0x66);

}
tcg_out8(s, 0x90);

r~

#31 lore raw

Re: [Qemu-devel] [PATCH 05/11] tcg/i386: Make direct jump patching thread-safe

Quoted 19 lines · Richard Henderson
Quoted 7 lines · Alex Bennée
Quoted 7 lines
tcg/i386/tcg-target.inc.c +7 -0
+static void tcg_out_nopn(TCGContext *s, int n)
+{
+    static const uint8_t nop1[] = { 0x90 };
+    static const uint8_t nop2[] = { 0x66, 0x90 };
+    static const uint8_t nop3[] = { 0x8d, 0x76, 0x00 };
+    static const uint8_t *const nopn[] = { nop1, nop2, nop3 };
+    int i;
Quoted 8 lines

tcg/i386/tcg-target.inc.c static void tcg_out_jmp(TCGContext *s, tcg_insn_unit *dest) · line 1137

Quoted 5 lines
tcg/i386/tcg-target.inc.c +5 -0
+    assert(n <= ARRAY_SIZE(nopn));
+    for (i = 0; i < n; ++i) {
+        tcg_out8(s, nopn[n - 1][i]);
+    }
+}

*shudder* I recall x86 instruction encoding is weird. Maybe a comment
for the function to describe the 3 forms of NOP we have here?

I think I'd prefer to drop the tables and do

Quoted 8 lines

/* Emit 1 or 2 operand size prefixes for the standard one byte nop,
xchg %eax,%eax, forming xchg %ax,%ax. All cores accept the duplicate prefix, and all of the interesting recent cores can decode and discard the duplicates in a single cycle. */

  for (i = 1; i < n; ++i) {
    tcg_out8(s, 0x66);

}
tcg_out8(s, 0x90);

It's fine if you are sure about that :)

Kind regards,
Sergey

#32 lore raw

PATCH 6/11 tcg/s390: Make direct jump patching thread-safe

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in s390 is atomic by:
* naturally aligning a location of direct jump address;
* using atomic_read()/atomic_set() for code patching.

---
 include/exec/exec-all.h   | 2 +-
 tcg/s390/tcg-target.inc.c | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)
include/exec/exec-all.h +1 -1
index 82399175fe80..e18cc24e50f0 100644
@@ -320,7 +320,7 @@ static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     /* patch the branch destination */
     intptr_t disp = addr - (jmp_addr - 2);
-    stl_be_p((void*)jmp_addr, disp / 2);
+    atomic_set((int32_t *)jmp_addr, disp / 2);
     /* no need to flush icache explicitly */
 }
 #elif defined(__aarch64__)
tcg/s390/tcg-target.inc.c +6 -0
index fbf97bb2e15d..84221dfb68c9 100644
@@ -219,6 +219,8 @@ typedef enum S390Opcode {
     RX_ST       = 0x50,
     RX_STC      = 0x42,
     RX_STH      = 0x40,
+
+    NOP         = 0x0707,
 } S390Opcode;
 
 #ifndef NDEBUG
@@ -1716,6 +1718,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
 
     case INDEX_op_goto_tb:
         if (s->tb_jmp_offset) {
+            /* align branch displacement for atomic pathing */
+            if (((uintptr_t)s->code_ptr & 3) == 0) {
+                tcg_out16(s, NOP);
+            }
             tcg_out16(s, RIL_BRCL | (S390_CC_ALWAYS << 4));
             s->tb_jmp_offset[args[0]] = tcg_current_code_size(s);
             s->code_ptr += 2;
Signature · 1 line
2.8.1
#33 lore raw

Re: [Qemu-devel] [PATCH 06/11] tcg/s390: Make direct jump patching thread-safe

Quoted 40 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in s390 is atomic by:
* naturally aligning a location of direct jump address;
* using atomic_read()/atomic_set() for code patching.

---
 include/exec/exec-all.h   | 2 +-
 tcg/s390/tcg-target.inc.c | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)
include/exec/exec-all.h +1 -1
index 82399175fe80..e18cc24e50f0 100644
@@ -320,7 +320,7 @@ static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     /* patch the branch destination */
     intptr_t disp = addr - (jmp_addr - 2);
-    stl_be_p((void*)jmp_addr, disp / 2);
+    atomic_set((int32_t *)jmp_addr, disp / 2);
     /* no need to flush icache explicitly */
 }
 #elif defined(__aarch64__)
tcg/s390/tcg-target.inc.c +2 -0
index fbf97bb2e15d..84221dfb68c9 100644
@@ -219,6 +219,8 @@ typedef enum S390Opcode {
     RX_ST       = 0x50,
     RX_STC      = 0x42,
     RX_STH      = 0x40,
+
+    NOP         = 0x0707,
 } S390Opcode;
 
 #ifndef NDEBUG

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

Quoted 4 lines
tcg/s390/tcg-target.inc.c +1 -0
@@ -1716,6 +1718,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
 
     case INDEX_op_goto_tb:
         if (s->tb_jmp_offset) {
+            /* align branch displacement for atomic pathing */

s/pathing/patching/

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

Quoted 3 lines
tcg/s390/tcg-target.inc.c +3 -0
+            if (((uintptr_t)s->code_ptr & 3) == 0) {
+                tcg_out16(s, NOP);
+            }

Isn't this the wrong way around? Shouldn't we insert the NOP is code_ptr & 3 == 2 (I assume 1 & 3 are impossible). Or is it that we need to be unaligned when we out the jmp so the offset itself is aligned.

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

Quoted 3 lines
tcg/s390/tcg-target.inc.c +0 -0
             tcg_out16(s, RIL_BRCL | (S390_CC_ALWAYS << 4));
             s->tb_jmp_offset[args[0]] = tcg_current_code_size(s);
             s->code_ptr += 2;

--
Alex Bennée

#34 lore raw

Re: [Qemu-devel] [PATCH 06/11] tcg/s390: Make direct jump patching thread-safe

Quoted 5 lines · Alex Bennée
Quoted 4 lines · Sergey Fedorov
include/exec/exec-all.h +0 -0
index 82399175fe80..e18cc24e50f0 100644

(snip)

Quoted 7 lines

include/exec/exec-all.h static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 1721

Quoted 5 lines
include/exec/exec-all.h +1 -0
@@ -1716,6 +1718,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
 
     case INDEX_op_goto_tb:
         if (s->tb_jmp_offset) {
+            /* align branch displacement for atomic pathing */

s/pathing/patching/

Quoted 6 lines

include/exec/exec-all.h static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 1724

Quoted 3 lines
include/exec/exec-all.h +3 -0
+            if (((uintptr_t)s->code_ptr & 3) == 0) {
+                tcg_out16(s, NOP);
+            }

Isn't this the wrong way around? Shouldn't we insert the NOP is code_ptr & 3 == 2 (I assume 1 & 3 are impossible). Or is it that we need to be unaligned when we out the jmp so the offset itself is aligned.

Yes, it is the offset itself should be aligned to patch in atomically.

Kind regards,
Sergey

Quoted 4 lines
Quoted 4 lines
include/exec/exec-all.h +0 -0
             tcg_out16(s, RIL_BRCL | (S390_CC_ALWAYS << 4));
             s->tb_jmp_offset[args[0]] = tcg_current_code_size(s);
             s->code_ptr += 2;
#35 lore raw

PATCH 7/11 tcg/arm: Make direct jump patching thread-safe

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in ARM is atomic by using atomic_read()/atomic_set() for code patching.

---
 include/exec/exec-all.h  | 25 ++-----------------------
 tcg/arm/tcg-target.inc.c | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 23 deletions(-)
include/exec/exec-all.h +2 -23
index e18cc24e50f0..6a054ee720a8 100644
@@ -327,29 +327,8 @@ static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 void aarch64_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
 #define tb_set_jmp_target1 aarch64_tb_set_jmp_target
 #elif defined(__arm__)
-static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
-{
-#if !QEMU_GNUC_PREREQ(4, 1)
-    register unsigned long _beg __asm ("a1");
-    register unsigned long _end __asm ("a2");
-    register unsigned long _flg __asm ("a3");
-#endif
-
-    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
-    *(uint32_t *)jmp_addr =
-        (*(uint32_t *)jmp_addr & ~0xffffff)
-        | (((addr - (jmp_addr + 8)) >> 2) & 0xffffff);
-
-#if QEMU_GNUC_PREREQ(4, 1)
-    __builtin___clear_cache((char *) jmp_addr, (char *) jmp_addr + 4);
-#else
-    /* flush icache */
-    _beg = jmp_addr;
-    _end = jmp_addr + 4;
-    _flg = 0;
-    __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
-#endif
-}
+void arm_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
+#define tb_set_jmp_target1 arm_tb_set_jmp_target
 #elif defined(__sparc__) || defined(__mips__)
 void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr);
 #else
tcg/arm/tcg-target.inc.c +17 -0
index 3edf6a6f971c..5c69de20bc69 100644
@@ -121,6 +121,13 @@ static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
     *code_ptr = (*code_ptr & ~0xffffff) | (offset & 0xffffff);
 }
 
+static inline void reloc_pc24_atomic(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
+{
+    ptrdiff_t offset = (tcg_ptr_byte_diff(target, code_ptr) - 8) >> 2;
+    tcg_insn_unit insn = atomic_read(code_ptr);
+    atomic_set(code_ptr, (insn & ~0xffffff) | (offset & 0xffffff));
+}
+
 static void patch_reloc(tcg_insn_unit *code_ptr, int type,
                         intptr_t value, intptr_t addend)
 {
@@ -1038,6 +1045,16 @@ static void tcg_out_call(TCGContext *s, tcg_insn_unit *addr)
     }
 }
 
+void arm_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
+{
+    tcg_insn_unit *code_ptr = (tcg_insn_unit *)jmp_addr;
+    tcg_insn_unit *target = (tcg_insn_unit *)addr;
+
+    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
+    reloc_pc24_atomic(code_ptr, target);
+    flush_icache_range(jmp_addr, jmp_addr + 4);
+}
+
 static inline void tcg_out_goto_label(TCGContext *s, int cond, TCGLabel *l)
 {
     if (l->has_value) {
Signature · 1 line
2.8.1
#36 lore raw

Re: [PATCH 07/11] tcg/arm: Make direct jump patching thread-safe

Quoted 53 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in ARM is atomic by using atomic_read()/atomic_set() for code patching.

---
 include/exec/exec-all.h  | 25 ++-----------------------
 tcg/arm/tcg-target.inc.c | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 23 deletions(-)
include/exec/exec-all.h +2 -23
index e18cc24e50f0..6a054ee720a8 100644
@@ -327,29 +327,8 @@ static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 void aarch64_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
 #define tb_set_jmp_target1 aarch64_tb_set_jmp_target
 #elif defined(__arm__)
-static inline void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
-{
-#if !QEMU_GNUC_PREREQ(4, 1)
-    register unsigned long _beg __asm ("a1");
-    register unsigned long _end __asm ("a2");
-    register unsigned long _flg __asm ("a3");
-#endif
-
-    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
-    *(uint32_t *)jmp_addr =
-        (*(uint32_t *)jmp_addr & ~0xffffff)
-        | (((addr - (jmp_addr + 8)) >> 2) & 0xffffff);
-
-#if QEMU_GNUC_PREREQ(4, 1)
-    __builtin___clear_cache((char *) jmp_addr, (char *) jmp_addr + 4);
-#else
-    /* flush icache */
-    _beg = jmp_addr;
-    _end = jmp_addr + 4;
-    _flg = 0;
-    __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
-#endif
-}
+void arm_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr);
+#define tb_set_jmp_target1 arm_tb_set_jmp_target
 #elif defined(__sparc__) || defined(__mips__)
 void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr);
 #else
tcg/arm/tcg-target.inc.c +0 -0
index 3edf6a6f971c..5c69de20bc69 100644

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 127

Quoted 7 lines
tcg/arm/tcg-target.inc.c +3 -0
@@ -121,6 +121,13 @@ static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
     *code_ptr = (*code_ptr & ~0xffffff) | (offset & 0xffffff);
 }
 
+static inline void reloc_pc24_atomic(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
+{
+    ptrdiff_t offset = (tcg_ptr_byte_diff(target, code_ptr) - 8) >>
 2;

This seems like something a tcg_debug_assert should be ensuring we don't overflow.

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 127

Quoted 1 line
tcg/arm/tcg-target.inc.c +1 -0
+    tcg_insn_unit insn = atomic_read(code_ptr);

Don't we already know what the instruction should be or could there be multiple ones?

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 128

Quoted 1 line
tcg/arm/tcg-target.inc.c +1 -0
+    atomic_set(code_ptr, (insn & ~0xffffff) | (offset & 0xffffff));

Please use deposit32 to set the offset like the aarch64 code.

Quoted 15 lines
tcg/arm/tcg-target.inc.c +2 -0
+}
+
 static void patch_reloc(tcg_insn_unit *code_ptr, int type,
                         intptr_t value, intptr_t addend)

{

tcg/arm/tcg-target.inc.c +6 -0
@@ -1038,6 +1045,16 @@ static void tcg_out_call(TCGContext *s, tcg_insn_unit *addr)
     }
 }
 
+void arm_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
+{
+    tcg_insn_unit *code_ptr = (tcg_insn_unit *)jmp_addr;
+    tcg_insn_unit *target = (tcg_insn_unit *)addr;
+
+    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the
Quoted 1 line

flush */

So why don't we?

tcg/arm/tcg-target.inc.c static void tcg_out_call(TCGContext *s, tcg_insn_unit *addr) · line 1060

Quoted 7 lines
tcg/arm/tcg-target.inc.c +4 -0
+    reloc_pc24_atomic(code_ptr, target);
+    flush_icache_range(jmp_addr, jmp_addr + 4);
+}
+
 static inline void tcg_out_goto_label(TCGContext *s, int cond, TCGLabel *l)
 {
     if (l->has_value) {

--
Alex Bennée

#37 lore raw

Re: [PATCH 07/11] tcg/arm: Make direct jump patching thread-safe

Quoted 9 lines · Alex Bennée
Quoted 8 lines · Sergey Fedorov
tcg/arm/tcg-target.inc.c +0 -0
index 3edf6a6f971c..5c69de20bc69 100644
@@ -121,6 +121,13 @@ static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
     *code_ptr = (*code_ptr & ~0xffffff) | (offset & 0xffffff);
 }
 
Quoted 5 lines

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target)

Quoted 4 lines
tcg/arm/tcg-target.inc.c +3 -0
+static inline void reloc_pc24_atomic(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
+{
+    ptrdiff_t offset = (tcg_ptr_byte_diff(target, code_ptr) - 8) >>
 2;

This seems like something a tcg_debug_assert should be ensuring we don't overflow.

Then we should probably put the same assert into reloc_pc24() as well.

Quoted 4 lines

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 127

Quoted 2 lines
tcg/arm/tcg-target.inc.c +1 -0
+    tcg_insn_unit insn = atomic_read(code_ptr);

Don't we already know what the instruction should be or could there be multiple ones?

Actually, it is always what tcg_out_b_noaddr() generates. I'm not sure it's worthwhile to introduce tcg_out_b_atomic() or something similar here.

Quoted 3 lines

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 128

Quoted 2 lines
tcg/arm/tcg-target.inc.c +1 -0
+    atomic_set(code_ptr, (insn & ~0xffffff) | (offset & 0xffffff));

Please use deposit32 to set the offset like the aarch64 code.

Will do.

Quoted 10 lines
Quoted 10 lines
tcg/arm/tcg-target.inc.c +2 -0
+}
+
 static void patch_reloc(tcg_insn_unit *code_ptr, int type,
                         intptr_t value, intptr_t addend)

{

tcg/arm/tcg-target.inc.c +0 -0
@@ -1038,6 +1045,16 @@ static void tcg_out_call(TCGContext *s, tcg_insn_unit *addr)
     }
 }
 
Quoted 8 lines
Quoted 7 lines
tcg/arm/tcg-target.inc.c +6 -0
+void arm_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
+{
+    tcg_insn_unit *code_ptr = (tcg_insn_unit *)jmp_addr;
+    tcg_insn_unit *target = (tcg_insn_unit *)addr;
+
+    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the

flush */

So why don't we?

I think because it's a bit more expensive to take this kind of branch. If we assume direct jumps are taken much more times than TB patching then it's preferable to optimize for direct jumps instead of for patching.

Kind regards,
Sergey

Quoted 9 lines
Quoted 9 lines
tcg/arm/tcg-target.inc.c +4 -0
+    reloc_pc24_atomic(code_ptr, target);
+    flush_icache_range(jmp_addr, jmp_addr + 4);
+}
+
 static inline void tcg_out_goto_label(TCGContext *s, int cond, TCGLabel *l)
 {
     if (l->has_value) {
#38 lore raw

Re: [PATCH 07/11] tcg/arm: Make direct jump patching thread-safe

Quoted 19 lines · Sergey Fedorov
Quoted 9 lines · Alex Bennée
Quoted 8 lines · Sergey Fedorov
tcg/arm/tcg-target.inc.c +0 -0
index 3edf6a6f971c..5c69de20bc69 100644
@@ -121,6 +121,13 @@ static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
     *code_ptr = (*code_ptr & ~0xffffff) | (offset & 0xffffff);
 }
 
Quoted 5 lines

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target)

Quoted 4 lines
tcg/arm/tcg-target.inc.c +3 -0
+static inline void reloc_pc24_atomic(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
+{
+    ptrdiff_t offset = (tcg_ptr_byte_diff(target, code_ptr) - 8) >>
 2;

This seems like something a tcg_debug_assert should be ensuring we don't overflow.

Then we should probably put the same assert into reloc_pc24() as well.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 127

Quoted 1 line
tcg/arm/tcg-target.inc.c +1 -0
+    tcg_insn_unit insn = atomic_read(code_ptr);

Don't we already know what the instruction should be or could there be multiple ones?

Actually, it is always what tcg_out_b_noaddr() generates. I'm not sure it's worthwhile to introduce tcg_out_b_atomic() or something similar here.

No I don't think so. It depends if the branch instruction is going to have multiple potential conditions (so requiring the read) or always be the same.

Quoted 26 lines
Quoted 4 lines

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 128

Quoted 3 lines
tcg/arm/tcg-target.inc.c +1 -0
+    atomic_set(code_ptr, (insn & ~0xffffff) | (offset & 0xffffff));

Please use deposit32 to set the offset like the aarch64 code.

Will do.

Quoted 10 lines
Quoted 10 lines
tcg/arm/tcg-target.inc.c +2 -0
+}
+
 static void patch_reloc(tcg_insn_unit *code_ptr, int type,
                         intptr_t value, intptr_t addend)

{

tcg/arm/tcg-target.inc.c +0 -0
@@ -1038,6 +1045,16 @@ static void tcg_out_call(TCGContext *s, tcg_insn_unit *addr)
     }
 }
 
Quoted 8 lines
Quoted 7 lines
tcg/arm/tcg-target.inc.c +6 -0
+void arm_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
+{
+    tcg_insn_unit *code_ptr = (tcg_insn_unit *)jmp_addr;
+    tcg_insn_unit *target = (tcg_insn_unit *)addr;
+
+    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the

flush */

So why don't we?

Quoted 4 lines

I think because it's a bit more expensive to take this kind of branch. If we assume direct jumps are taken much more times than TB patching then it's preferable to optimize for direct jumps instead of for patching.

Looking again I see the comment came from code motion so I'm not overly fussed its just comments like that always leave me hanging. "We could ...." and I want to know why we don't then ;-)

Quoted 5 lines

Kind regards,
Sergey

Quoted 1 line
Quoted 8 lines
Quoted 8 lines

tcg/arm/tcg-target.inc.c static void tcg_out_call(TCGContext *s, tcg_insn_unit *addr) · line 1060

Quoted 8 lines
tcg/arm/tcg-target.inc.c +4 -0
+    reloc_pc24_atomic(code_ptr, target);
+    flush_icache_range(jmp_addr, jmp_addr + 4);
+}
+
 static inline void tcg_out_goto_label(TCGContext *s, int cond, TCGLabel *l)
 {
     if (l->has_value) {

--
Alex Bennée

#39 lore raw

Re: [PATCH 07/11] tcg/arm: Make direct jump patching thread-safe

Quoted 19 lines · Alex Bennée
Quoted 18 lines · Sergey Fedorov
Quoted 9 lines · Alex Bennée
Quoted 8 lines · Sergey Fedorov
tcg/arm/tcg-target.inc.c +0 -0
index 3edf6a6f971c..5c69de20bc69 100644
@@ -121,6 +121,13 @@ static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
     *code_ptr = (*code_ptr & ~0xffffff) | (offset & 0xffffff);
 }
 
Quoted 5 lines

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target)

Quoted 4 lines
tcg/arm/tcg-target.inc.c +3 -0
+static inline void reloc_pc24_atomic(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
+{
+    ptrdiff_t offset = (tcg_ptr_byte_diff(target, code_ptr) - 8) >>
 2;

This seems like something a tcg_debug_assert should be ensuring we don't overflow.

Then we should probably put the same assert into reloc_pc24() as well.

Quoted 1 line
Quoted 1 line
tcg/arm/tcg-target.inc.c +1 -0
+    tcg_insn_unit insn = atomic_read(code_ptr);
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Don't we already know what the instruction should be or could there be multiple ones?

Actually, it is always what tcg_out_b_noaddr() generates. I'm not sure it's worthwhile to introduce tcg_out_b_atomic() or something similar here.

No I don't think so. It depends if the branch instruction is going to have multiple potential conditions (so requiring the read) or always be the same.

So I mean "regarding goto_tb, it's always branch immediate, unconditional". But concerning the function name, it should only patch the immediate offset of the instruction.

Quoted 22 lines
Quoted 22 lines
Quoted 3 lines

tcg/arm/tcg-target.inc.c static inline void reloc_pc24(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 128

Quoted 2 lines
tcg/arm/tcg-target.inc.c +1 -0
+    atomic_set(code_ptr, (insn & ~0xffffff) | (offset & 0xffffff));

Please use deposit32 to set the offset like the aarch64 code.

Will do.

Quoted 17 lines
Quoted 15 lines
tcg/arm/tcg-target.inc.c +2 -0
+}
+
 static void patch_reloc(tcg_insn_unit *code_ptr, int type,
                         intptr_t value, intptr_t addend)

{

tcg/arm/tcg-target.inc.c +6 -0
@@ -1038,6 +1045,16 @@ static void tcg_out_call(TCGContext *s, tcg_insn_unit *addr)
     }
 }
 
+void arm_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
+{
+    tcg_insn_unit *code_ptr = (tcg_insn_unit *)jmp_addr;
+    tcg_insn_unit *target = (tcg_insn_unit *)addr;
+
+    /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the
Quoted 1 line

flush */

So why don't we?

Quoted 7 lines
Quoted 4 lines

I think because it's a bit more expensive to take this kind of branch. If we assume direct jumps are taken much more times than TB patching then it's preferable to optimize for direct jumps instead of for patching.

Looking again I see the comment came from code motion so I'm not overly fussed its just comments like that always leave me hanging. "We could ...." and I want to know why we don't then ;-)

So let's leave it as is?

Kind regards,
Sergey

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
tcg/arm/tcg-target.inc.c +4 -0
+    reloc_pc24_atomic(code_ptr, target);
+    flush_icache_range(jmp_addr, jmp_addr + 4);
+}
+
 static inline void tcg_out_goto_label(TCGContext *s, int cond, TCGLabel *l)
 {
     if (l->has_value) {
#40 lore raw

PATCH 8/11 tcg/aarch64: Make direct jump patching thread-safe

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in AArch64 is atomic by using atomic_read()/atomic_set() for code patching.

---
 tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tcg/aarch64/tcg-target.inc.c +13 -1
index 0ed10a974121..15fdebec921f 100644
@@ -73,6 +73,18 @@ static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
     *code_ptr = deposit32(*code_ptr, 0, 26, offset);
 }
 
+static inline void reloc_pc26_atomic(tcg_insn_unit *code_ptr,
+                                     tcg_insn_unit *target)
+{
+    ptrdiff_t offset = target - code_ptr;
+    tcg_insn_unit insn;
+    assert(offset == sextract64(offset, 0, 26));
+    /* read instruction, mask away previous PC_REL26 parameter contents,
+       set the proper offset, then write back the instruction. */
+    insn = atomic_read(code_ptr);
+    atomic_set(code_ptr, deposit32(insn, 0, 26, offset));
+}
+
 static inline void reloc_pc19(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
 {
     ptrdiff_t offset = target - code_ptr;
@@ -835,7 +847,7 @@ void aarch64_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
     tcg_insn_unit *code_ptr = (tcg_insn_unit *)jmp_addr;
     tcg_insn_unit *target = (tcg_insn_unit *)addr;
 
-    reloc_pc26(code_ptr, target);
+    reloc_pc26_atomic(code_ptr, target);
     flush_icache_range(jmp_addr, jmp_addr + 4);
 }
 
Signature · 1 line
2.8.1
Reviewed-by on v1 8/11
#41 lore raw

Re: [PATCH 08/11] tcg/aarch64: Make direct jump patching thread-safe

Quoted 17 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in AArch64 is atomic by using atomic_read()/atomic_set() for code patching.

---
 tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tcg/aarch64/tcg-target.inc.c +0 -0
index 0ed10a974121..15fdebec921f 100644
@@ -73,6 +73,18 @@ static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
     *code_ptr = deposit32(*code_ptr, 0, 26, offset);

tcg/aarch64/tcg-target.inc.c static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 83

Quoted 10 lines
tcg/aarch64/tcg-target.inc.c +8 -0
 }
 
+static inline void reloc_pc26_atomic(tcg_insn_unit *code_ptr,
+                                     tcg_insn_unit *target)
+{
+    ptrdiff_t offset = target - code_ptr;
+    tcg_insn_unit insn;
+    assert(offset == sextract64(offset, 0, 26));
+    /* read instruction, mask away previous PC_REL26 parameter contents,
+       set the proper offset, then write back the instruction. */

This comment could be moved from here and reloc_pc26 and made common for the two following functions.

Quoted 8 lines
tcg/aarch64/tcg-target.inc.c +4 -0
+    insn = atomic_read(code_ptr);
+    atomic_set(code_ptr, deposit32(insn, 0, 26, offset));
+}
+
 static inline void reloc_pc19(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
 {
     ptrdiff_t offset = target - code_ptr;

tcg/aarch64/tcg-target.inc.c void aarch64_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr) · line 852

Quoted 7 lines
tcg/aarch64/tcg-target.inc.c +1 -1
@@ -835,7 +847,7 @@ void aarch64_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
     tcg_insn_unit *code_ptr = (tcg_insn_unit *)jmp_addr;
     tcg_insn_unit *target = (tcg_insn_unit *)addr;
 
-    reloc_pc26(code_ptr, target);
+    reloc_pc26_atomic(code_ptr, target);
     flush_icache_range(jmp_addr, jmp_addr + 4);
 }

Otherwise:

--
Alex Bennée

#42 lore raw

Re: [PATCH 08/11] tcg/aarch64: Make direct jump patching thread-safe

Quoted 22 lines · Alex Bennée
Quoted 19 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in AArch64 is atomic by using atomic_read()/atomic_set() for code patching.

---
  tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
tcg/aarch64/tcg-target.inc.c +0 -0
index 0ed10a974121..15fdebec921f 100644
@@ -73,6 +73,18 @@ static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
      *code_ptr = deposit32(*code_ptr, 0, 26, offset);
  }
 
Quoted 11 lines

tcg/aarch64/tcg-target.inc.c static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 83

Quoted 8 lines
tcg/aarch64/tcg-target.inc.c +8 -0
+static inline void reloc_pc26_atomic(tcg_insn_unit *code_ptr,
+                                     tcg_insn_unit *target)
+{
+    ptrdiff_t offset = target - code_ptr;
+    tcg_insn_unit insn;
+    assert(offset == sextract64(offset, 0, 26));
+    /* read instruction, mask away previous PC_REL26 parameter contents,
+       set the proper offset, then write back the instruction. */

This comment could be moved from here and reloc_pc26 and made common for the two following functions.

There's a significant amount of cleanup that ought to happen here, now that we're not re-translating TBs. I don't know if Sergey should be gated on that.

r~

#43 lore raw

Re: [PATCH 08/11] tcg/aarch64: Make direct jump patching thread-safe

Quoted 35 lines · Richard Henderson
Quoted 22 lines · Alex Bennée
Quoted 19 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in AArch64 is atomic by using atomic_read()/atomic_set() for code patching.

---
  tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
tcg/aarch64/tcg-target.inc.c +0 -0
index 0ed10a974121..15fdebec921f 100644
@@ -73,6 +73,18 @@ static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
      *code_ptr = deposit32(*code_ptr, 0, 26, offset);
  }
 
Quoted 11 lines

tcg/aarch64/tcg-target.inc.c static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 83

Quoted 8 lines
tcg/aarch64/tcg-target.inc.c +8 -0
+static inline void reloc_pc26_atomic(tcg_insn_unit *code_ptr,
+                                     tcg_insn_unit *target)
+{
+    ptrdiff_t offset = target - code_ptr;
+    tcg_insn_unit insn;
+    assert(offset == sextract64(offset, 0, 26));
+    /* read instruction, mask away previous PC_REL26 parameter contents,
+       set the proper offset, then write back the instruction. */

This comment could be moved from here and reloc_pc26 and made common for the two following functions.

Quoted 3 lines

There's a significant amount of cleanup that ought to happen here, now that we're not re-translating TBs. I don't know if Sergey should be gated on that.

Is this stuff already in the works? Otherwise we are trying to get pre-cursors to MTTCG into the code (once the tree re-opens) to keep the main diff down. This also is beneficial for linux-user stuff.

Quoted 3 lines

r~

--
Alex Bennée

#44 lore raw

Re: [PATCH 08/11] tcg/aarch64: Make direct jump patching thread-safe

Quoted 38 lines · Alex Bennée
Quoted 35 lines · Richard Henderson
Quoted 33 lines · Alex Bennée
Quoted 17 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in AArch64 is atomic by using atomic_read()/atomic_set() for code patching.

---
   tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
   1 file changed, 13 insertions(+), 1 deletion(-)
tcg/aarch64/tcg-target.inc.c +0 -0
index 0ed10a974121..15fdebec921f 100644
@@ -73,6 +73,18 @@ static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
       *code_ptr = deposit32(*code_ptr, 0, 26, offset);

tcg/aarch64/tcg-target.inc.c static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 83

Quoted 10 lines
tcg/aarch64/tcg-target.inc.c +8 -0
   }
 
+static inline void reloc_pc26_atomic(tcg_insn_unit *code_ptr,
+                                     tcg_insn_unit *target)
+{
+    ptrdiff_t offset = target - code_ptr;
+    tcg_insn_unit insn;
+    assert(offset == sextract64(offset, 0, 26));
+    /* read instruction, mask away previous PC_REL26 parameter contents,
+       set the proper offset, then write back the instruction. */

This comment could be moved from here and reloc_pc26 and made common for the two following functions.

Quoted 7 lines
Quoted 3 lines

There's a significant amount of cleanup that ought to happen here, now that we're not re-translating TBs. I don't know if Sergey should be gated on that.

Is this stuff already in the works? Otherwise we are trying to get pre-cursors to MTTCG into the code (once the tree re-opens) to keep the main diff down. This also is beneficial for linux-user stuff.

We're talking past one another.

No, it's not yet in the works. I'm saying that this patch set should not wait for it. Thus I don't care if what he adds here is a little messy; we'll clean it all up at once later. Thus don't bother refactoring reloc_pc26 and reloc_pc26_atomic.

r~

#45 lore raw

Re: [PATCH 08/11] tcg/aarch64: Make direct jump patching thread-safe

Quoted 47 lines · Richard Henderson
Quoted 38 lines · Alex Bennée
Quoted 35 lines · Richard Henderson
Quoted 33 lines · Alex Bennée
Quoted 17 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in AArch64 is atomic by using atomic_read()/atomic_set() for code patching.

---
   tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
   1 file changed, 13 insertions(+), 1 deletion(-)
tcg/aarch64/tcg-target.inc.c +0 -0
index 0ed10a974121..15fdebec921f 100644
@@ -73,6 +73,18 @@ static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
       *code_ptr = deposit32(*code_ptr, 0, 26, offset);

tcg/aarch64/tcg-target.inc.c static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 83

Quoted 10 lines
tcg/aarch64/tcg-target.inc.c +8 -0
   }
 
+static inline void reloc_pc26_atomic(tcg_insn_unit *code_ptr,
+                                     tcg_insn_unit *target)
+{
+    ptrdiff_t offset = target - code_ptr;
+    tcg_insn_unit insn;
+    assert(offset == sextract64(offset, 0, 26));
+    /* read instruction, mask away previous PC_REL26 parameter contents,
+       set the proper offset, then write back the instruction. */

This comment could be moved from here and reloc_pc26 and made common for the two following functions.

Quoted 7 lines
Quoted 3 lines

There's a significant amount of cleanup that ought to happen here, now that we're not re-translating TBs. I don't know if Sergey should be gated on that.

Is this stuff already in the works? Otherwise we are trying to get pre-cursors to MTTCG into the code (once the tree re-opens) to keep the main diff down. This also is beneficial for linux-user stuff.

Quoted 6 lines

We're talking past one another.

No, it's not yet in the works. I'm saying that this patch set should not wait for it. Thus I don't care if what he adds here is a little messy; we'll clean it all up at once later. Thus don't bother refactoring reloc_pc26 and reloc_pc26_atomic.

Ahh OK, cool ;-)

Quoted 3 lines

r~

--
Alex Bennée

#46 lore raw

Re: [PATCH 08/11] tcg/aarch64: Make direct jump patching thread-safe

In reply to Richard Henderson
Quoted 38 lines · Richard Henderson
Quoted 24 lines · Alex Bennée
Quoted 21 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in AArch64 is atomic by using atomic_read()/atomic_set() for code patching.

---
  tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
tcg/aarch64/tcg-target.inc.c +0 -0
index 0ed10a974121..15fdebec921f 100644
@@ -73,6 +73,18 @@ static inline void reloc_pc26(tcg_insn_unit
 *code_ptr, tcg_insn_unit *target)
      *code_ptr = deposit32(*code_ptr, 0, 26, offset);
  }
 
Quoted 12 lines

tcg/aarch64/tcg-target.inc.c static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 83

Quoted 9 lines
tcg/aarch64/tcg-target.inc.c +8 -0
+static inline void reloc_pc26_atomic(tcg_insn_unit *code_ptr,
+                                     tcg_insn_unit *target)
+{
+    ptrdiff_t offset = target - code_ptr;
+    tcg_insn_unit insn;
+    assert(offset == sextract64(offset, 0, 26));
+    /* read instruction, mask away previous PC_REL26 parameter
 contents,
+       set the proper offset, then write back the instruction. */

This comment could be moved from here and reloc_pc26 and made common for the two following functions.

Quoted 3 lines

There's a significant amount of cleanup that ought to happen here, now that we're not re-translating TBs. I don't know if Sergey should be gated on that.

Do you mean I'd better avoid using stuff like reloc_pc26()?

Kind regards,
Sergey

#47 lore raw

Re: [PATCH 08/11] tcg/aarch64: Make direct jump patching thread-safe

In reply to Sergey Fedorov
Quoted 16 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in AArch64 is atomic by using atomic_read()/atomic_set() for code patching.

---
 tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tcg/aarch64/tcg-target.inc.c +0 -0
index 0ed10a974121..15fdebec921f 100644

tcg/aarch64/tcg-target.inc.c static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target) · line 81

Quoted 9 lines
tcg/aarch64/tcg-target.inc.c +6 -0
@@ -73,6 +73,18 @@ static inline void reloc_pc26(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
     *code_ptr = deposit32(*code_ptr, 0, 26, offset);
 }
 
+static inline void reloc_pc26_atomic(tcg_insn_unit *code_ptr,
+                                     tcg_insn_unit *target)
+{
+    ptrdiff_t offset = target - code_ptr;
+    tcg_insn_unit insn;
+    assert(offset == sextract64(offset, 0, 26));

I'd better use tcg_debug_assert() here as in this patch:

http://patchwork.ozlabs.org/patch/613020/

Kind regards,
Sergey

Quoted 18 lines
tcg/aarch64/tcg-target.inc.c +7 -1
+    /* read instruction, mask away previous PC_REL26 parameter contents,
+       set the proper offset, then write back the instruction. */
+    insn = atomic_read(code_ptr);
+    atomic_set(code_ptr, deposit32(insn, 0, 26, offset));
+}
+
 static inline void reloc_pc19(tcg_insn_unit *code_ptr, tcg_insn_unit *target)
 {
     ptrdiff_t offset = target - code_ptr;
@@ -835,7 +847,7 @@ void aarch64_tb_set_jmp_target(uintptr_t jmp_addr, uintptr_t addr)
     tcg_insn_unit *code_ptr = (tcg_insn_unit *)jmp_addr;
     tcg_insn_unit *target = (tcg_insn_unit *)addr;
 
-    reloc_pc26(code_ptr, target);
+    reloc_pc26_atomic(code_ptr, target);
     flush_icache_range(jmp_addr, jmp_addr + 4);
 }
 
#48 lore raw

PATCH 9/11 tcg/sparc: Make direct jump patching thread-safe

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in SPARC is atomic by using atomic_read()/atomic_set() for code patching.

---
 tcg/sparc/tcg-target.inc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tcg/sparc/tcg-target.inc.c +1 -1
index 54df1bc42432..629dd7bdafff 100644
@@ -1647,6 +1647,6 @@ void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
        the code_gen_buffer can't be larger than 2GB.  */
     assert(disp == (int32_t)disp);
 
-    *ptr = CALL | (uint32_t)disp >> 2;
+    atomic_set(ptr, CALL | (uint32_t)disp >> 2);
     flush_icache_range(jmp_addr, jmp_addr + 4);
 }
Signature · 1 line
2.8.1
Reviewed-by on v1 9/11
#49 lore raw

Re: [Qemu-devel] [PATCH 09/11] tcg/sparc: Make direct jump patching thread-safe

Quoted 16 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in SPARC is atomic by using atomic_read()/atomic_set() for code patching.

---
 tcg/sparc/tcg-target.inc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tcg/sparc/tcg-target.inc.c +0 -0
index 54df1bc42432..629dd7bdafff 100644

tcg/sparc/tcg-target.inc.c void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr) · line 1650

Quoted 5 lines
tcg/sparc/tcg-target.inc.c +1 -1
@@ -1647,6 +1647,6 @@ void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
        the code_gen_buffer can't be larger than 2GB.  */
     assert(disp == (int32_t)disp);
 
-    *ptr = CALL | (uint32_t)disp >> 2;
+    atomic_set(ptr, CALL | (uint32_t)disp >> 2);

atomic_set(ptr, deposit32(CALL, 0, 30, disp >> 2))

might be a little more explicit.

Otherwise:

tcg/sparc/tcg-target.inc.c void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr) · line 1652

Quoted 2 lines
tcg/sparc/tcg-target.inc.c +0 -0
     flush_icache_range(jmp_addr, jmp_addr + 4);
 }

--
Alex Bennée

#50 lore raw

PATCH 10/11 tcg/mips: Make direct jump patching thread-safe

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

Ensure direct jump patching in MIPS is atomic by using atomic_read()/atomic_set() for code patching.

---
 tcg/mips/tcg-target.inc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tcg/mips/tcg-target.inc.c +2 -1
index 682e19897db0..e65835d2096b 100644
@@ -1886,6 +1886,7 @@ static void tcg_target_init(TCGContext *s)
 void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     uint32_t *ptr = (uint32_t *)jmp_addr;
-    *ptr = deposit32(*ptr, 0, 26, addr >> 2);
+    uint32_t insn = atomic_read(ptr);
+    atomic_write(ptr, deposit32(insn, 0, 26, addr >> 2));
     flush_icache_range(jmp_addr, jmp_addr + 4);
 }
Signature · 1 line
2.8.1
#51 lore raw

Re: [Qemu-devel] [PATCH 10/11] tcg/mips: Make direct jump patching thread-safe

Quoted 14 lines · Sergey Fedorov

Ensure direct jump patching in MIPS is atomic by using atomic_read()/atomic_set() for code patching.

---
 tcg/mips/tcg-target.inc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tcg/mips/tcg-target.inc.c +0 -0
index 682e19897db0..e65835d2096b 100644

tcg/mips/tcg-target.inc.c static void tcg_target_init(TCGContext *s) · line 1890

Quoted 6 lines
tcg/mips/tcg-target.inc.c +2 -1
@@ -1886,6 +1886,7 @@ static void tcg_target_init(TCGContext *s)
 void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     uint32_t *ptr = (uint32_t *)jmp_addr;
-    *ptr = deposit32(*ptr, 0, 26, addr >> 2);
+    uint32_t insn = atomic_read(ptr);
+    atomic_write(ptr, deposit32(insn, 0, 26, addr >> 2));

Oops. :)

Paolo

Quoted 2 lines
tcg/mips/tcg-target.inc.c +0 -0
     flush_icache_range(jmp_addr, jmp_addr + 4);
 }
#52 lore raw

Re: [Qemu-devel] [PATCH 10/11] tcg/mips: Make direct jump patching thread-safe

Quoted 24 lines · Paolo Bonzini
Quoted 14 lines · Sergey Fedorov

Ensure direct jump patching in MIPS is atomic by using atomic_read()/atomic_set() for code patching.

---
 tcg/mips/tcg-target.inc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tcg/mips/tcg-target.inc.c +0 -0
index 682e19897db0..e65835d2096b 100644

tcg/mips/tcg-target.inc.c static void tcg_target_init(TCGContext *s) · line 1890

Quoted 6 lines
tcg/mips/tcg-target.inc.c +2 -1
@@ -1886,6 +1886,7 @@ static void tcg_target_init(TCGContext *s)
 void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
 {
     uint32_t *ptr = (uint32_t *)jmp_addr;
-    *ptr = deposit32(*ptr, 0, 26, addr >> 2);
+    uint32_t insn = atomic_read(ptr);
+    atomic_write(ptr, deposit32(insn, 0, 26, addr >> 2));

Oops. :)

Quoted 1 line

Paolo

Nice catch! I think I just didn't cross-compile it for MIPS...

Thanks,
Sergey

Quoted 3 lines
Quoted 3 lines
tcg/mips/tcg-target.inc.c +0 -0
     flush_icache_range(jmp_addr, jmp_addr + 4);
 }
#53 lore raw

PATCH 11/11 tcg: Note requirement on atomic direct jump patching

In reply to Sergey Fedorov

From: Sergey Fedorov <[email protected]>

---
 include/exec/exec-all.h | 1 +
 1 file changed, 1 insertion(+)
include/exec/exec-all.h +1 -0
index 6a054ee720a8..1878e7b7856c 100644
@@ -229,6 +229,7 @@ static inline void tlb_flush_by_mmuidx(CPUState *cpu, ...)
     || defined(__sparc__) || defined(__aarch64__) \
     || defined(__s390x__) || defined(__mips__) \
     || defined(CONFIG_TCG_INTERPRETER)
+/* NOTE: Direct jump patching must be atomic and thread-safe. */
 #define USE_DIRECT_JUMP
 #endif
 
Signature · 1 line
2.8.1
Reviewed-by on v1 11/11
#54 lore raw

Re: [Qemu-devel] [PATCH 11/11] tcg: Note requirement on atomic direct jump patching

Quoted 13 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

---
 include/exec/exec-all.h | 1 +
 1 file changed, 1 insertion(+)
include/exec/exec-all.h +0 -0
index 6a054ee720a8..1878e7b7856c 100644

include/exec/exec-all.h static inline void tlb_flush_by_mmuidx(CPUState *cpu, ...) · line 232

Quoted 4 lines
include/exec/exec-all.h +1 -0
@@ -229,6 +229,7 @@ static inline void tlb_flush_by_mmuidx(CPUState *cpu, ...)
     || defined(__sparc__) || defined(__aarch64__) \
     || defined(__s390x__) || defined(__mips__) \
     || defined(CONFIG_TCG_INTERPRETER)
+/* NOTE: Direct jump patching must be atomic and thread-safe. */

Maybe:

/* NOTE: Direct jump patching must be atomic to be thread-safe. */

Otherwise:

include/exec/exec-all.h static inline void tlb_flush_by_mmuidx(CPUState *cpu, ...) · line 234

Quoted 2 lines
include/exec/exec-all.h +0 -0
 #define USE_DIRECT_JUMP
 #endif

--
Alex Bennée

#55 lore raw
In reply to Sergey Fedorov
Quoted 8 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

When patching translated code for direct block chaining/unchaining, modification of concurrently executing code can happen in multi-threaded execution. Currently only user-mode is affected. To make direct block patching safe, some care must be taken to make sure that the code modification is made atomic and concurrently executed code is guaranteed to be consistent.

Quoted 7 lines

This patch series fixes all supported TCG targets using direct patching and documents the requirement for direct jump patching be atomic and thread-safe.

The series' tree can be found in a public git repository [1].

[1] https://github.com/sergefdrv/qemu/tree/atomic-tb-patching

Sorry, the first two patches got there by mistake...

Kind regards,
Sergey

#56 lore raw
In reply to Sergey Fedorov
Quoted 8 lines · Sergey Fedorov

From: Sergey Fedorov <[email protected]>

When patching translated code for direct block chaining/unchaining, modification of concurrently executing code can happen in multi-threaded execution. Currently only user-mode is affected. To make direct block patching safe, some care must be taken to make sure that the code modification is made atomic and concurrently executed code is guaranteed to be consistent.

Quoted 6 lines

This patch series fixes all supported TCG targets using direct patching and documents the requirement for direct jump patching be atomic and thread-safe.

The series' tree can be found in a public git repository [1].

[1] https://github.com/sergefdrv/qemu/tree/atomic-tb-patching

So I already know this will need a re-base as the patches don't apply cleanly to the current master. The git tree re-based without issue so I guess that is just git am being a bit crap.

Quoted 16 lines

Sergey Fedorov (10):
pc-bios/s390-ccw: Use correct strip when cross-compiling
tci: Make direct jump patching thread-safe tcg/ppc: Make direct jump patching thread-safe tcg/i386: Make direct jump patching thread-safe tcg/s390: Make direct jump patching thread-safe tcg/arm: Make direct jump patching thread-safe tcg/aarch64: Make direct jump patching thread-safe tcg/sparc: Make direct jump patching thread-safe tcg/mips: Make direct jump patching thread-safe
tcg: Note requirement on atomic direct jump patching

Stefan Weil (1):
tci: Fix build regression

Quoted 13 lines
 include/exec/exec-all.h      | 32 ++++++------------------------
 pc-bios/s390-ccw/Makefile    |  2 +-
 tcg/aarch64/tcg-target.inc.c | 14 +++++++++++++-
 tcg/arm/tcg-target.inc.c     | 17 ++++++++++++++++
 tcg/i386/tcg-target.inc.c    | 17 ++++++++++++++++
 tcg/mips/tcg-target.inc.c    |  3 ++-
 tcg/ppc/tcg-target.inc.c     | 22 +++++++++++++++++----
 tcg/s390/tcg-target.inc.c    |  6 ++++++
 tcg/sparc/tcg-target.inc.c   |  2 +-
 tcg/tci/tcg-target.inc.c     |  2 ++
 tci.c                        | 46 +++++++++++++++++++++++++-------------------
 translate-all.c              |  2 ++
 12 files changed, 111 insertions(+), 54 deletions(-)

--
Alex Bennée