2013-12-08 06:19:09 +01:00
|
|
|
From 7b37ca7330058615efd4da075e39f482099edb5b Mon Sep 17 00:00:00 2001
|
2012-07-05 18:39:32 +02:00
|
|
|
From: Alexander Graf <agraf@suse.de>
|
|
|
|
Date: Thu, 5 Jul 2012 17:31:39 +0200
|
|
|
|
Subject: [PATCH] linux-user: lock tcg
|
2013-02-10 19:32:29 +01:00
|
|
|
MIME-Version: 1.0
|
|
|
|
Content-Type: text/plain; charset=UTF-8
|
|
|
|
Content-Transfer-Encoding: 8bit
|
2012-07-05 18:39:32 +02:00
|
|
|
|
|
|
|
The tcg code generator is not thread safe. Lock its generation between
|
|
|
|
different threads.
|
|
|
|
|
|
|
|
Signed-off-by: Alexander Graf <agraf@suse.de>
|
2013-02-10 19:32:29 +01:00
|
|
|
[AF: Rebased onto exec.c/translate-all.c split for 1.4]
|
|
|
|
Signed-off-by: Andreas Färber <afaerber@suse.de>
|
2012-07-05 18:39:32 +02:00
|
|
|
---
|
2013-10-08 20:29:33 +02:00
|
|
|
linux-user/mmap.c | 3 +++
|
|
|
|
tcg/tcg.c | 36 ++++++++++++++++++++++++++++++++++--
|
|
|
|
tcg/tcg.h | 6 ++++++
|
2013-05-14 22:46:08 +02:00
|
|
|
3 files changed, 43 insertions(+), 2 deletions(-)
|
2012-07-05 18:39:32 +02:00
|
|
|
|
|
|
|
diff --git a/linux-user/mmap.c b/linux-user/mmap.c
|
2013-07-30 14:36:48 +02:00
|
|
|
index 34a5615..7ebf953 100644
|
2012-07-05 18:39:32 +02:00
|
|
|
--- a/linux-user/mmap.c
|
|
|
|
+++ b/linux-user/mmap.c
|
|
|
|
@@ -30,6 +30,7 @@
|
|
|
|
|
|
|
|
#include "qemu.h"
|
|
|
|
#include "qemu-common.h"
|
|
|
|
+#include "tcg.h"
|
|
|
|
|
|
|
|
//#define DEBUG_MMAP
|
|
|
|
|
2013-07-30 14:36:48 +02:00
|
|
|
@@ -40,6 +41,7 @@ void mmap_lock(void)
|
2012-07-10 18:43:10 +02:00
|
|
|
{
|
2012-07-05 18:39:32 +02:00
|
|
|
if (mmap_lock_count++ == 0) {
|
|
|
|
pthread_mutex_lock(&mmap_mutex);
|
2012-07-10 18:43:10 +02:00
|
|
|
+ tcg_lock();
|
2012-07-05 18:39:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 14:36:48 +02:00
|
|
|
@@ -47,6 +49,7 @@ void mmap_unlock(void)
|
2012-07-10 18:43:10 +02:00
|
|
|
{
|
2012-07-05 18:39:32 +02:00
|
|
|
if (--mmap_lock_count == 0) {
|
|
|
|
pthread_mutex_unlock(&mmap_mutex);
|
2012-07-10 18:43:10 +02:00
|
|
|
+ tcg_unlock();
|
2012-07-05 18:39:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diff --git a/tcg/tcg.c b/tcg/tcg.c
|
2013-12-08 06:19:09 +01:00
|
|
|
index 66d3f3d..a169e53 100644
|
2012-07-05 18:39:32 +02:00
|
|
|
--- a/tcg/tcg.c
|
|
|
|
+++ b/tcg/tcg.c
|
|
|
|
@@ -40,6 +40,8 @@
|
2013-02-10 19:32:29 +01:00
|
|
|
#include "qemu/cache-utils.h"
|
|
|
|
#include "qemu/host-utils.h"
|
|
|
|
#include "qemu/timer.h"
|
2012-07-05 18:39:32 +02:00
|
|
|
+#include "config-host.h"
|
2013-02-10 19:32:29 +01:00
|
|
|
+#include "qemu/thread.h"
|
2012-07-05 18:39:32 +02:00
|
|
|
|
|
|
|
/* Note: the long term plan is to reduce the dependancies on the QEMU
|
|
|
|
CPU definitions. Currently they are used for qemu_ld/st
|
2013-12-08 06:19:09 +01:00
|
|
|
@@ -117,6 +119,29 @@ const size_t tcg_op_defs_max = ARRAY_SIZE(tcg_op_defs);
|
2012-11-27 21:42:06 +01:00
|
|
|
static TCGRegSet tcg_target_available_regs[2];
|
|
|
|
static TCGRegSet tcg_target_call_clobber_regs;
|
2012-07-05 18:39:32 +02:00
|
|
|
|
2012-07-10 18:43:10 +02:00
|
|
|
+#ifdef CONFIG_USER_ONLY
|
|
|
|
+static __thread int tcg_lock_count;
|
|
|
|
+#endif
|
2012-07-05 18:39:32 +02:00
|
|
|
+void tcg_lock(void)
|
|
|
|
+{
|
|
|
|
+#ifdef CONFIG_USER_ONLY
|
|
|
|
+ TCGContext *s = &tcg_ctx;
|
2012-07-10 18:43:10 +02:00
|
|
|
+ if (tcg_lock_count++ == 0) {
|
|
|
|
+ qemu_mutex_lock(&s->lock);
|
|
|
|
+ }
|
2012-07-05 18:39:32 +02:00
|
|
|
+#endif
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void tcg_unlock(void)
|
|
|
|
+{
|
|
|
|
+#ifdef CONFIG_USER_ONLY
|
|
|
|
+ TCGContext *s = &tcg_ctx;
|
2012-07-10 18:43:10 +02:00
|
|
|
+ if (--tcg_lock_count == 0) {
|
|
|
|
+ qemu_mutex_unlock(&s->lock);
|
|
|
|
+ }
|
2012-07-05 18:39:32 +02:00
|
|
|
+#endif
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
static inline void tcg_out8(TCGContext *s, uint8_t v)
|
|
|
|
{
|
|
|
|
*s->code_ptr++ = v;
|
2013-12-08 06:19:09 +01:00
|
|
|
@@ -295,7 +320,8 @@ void tcg_context_init(TCGContext *s)
|
2012-11-27 21:42:06 +01:00
|
|
|
|
2012-07-05 18:39:32 +02:00
|
|
|
memset(s, 0, sizeof(*s));
|
|
|
|
s->nb_globals = 0;
|
|
|
|
-
|
|
|
|
+ qemu_mutex_init(&s->lock);
|
|
|
|
+
|
|
|
|
/* Count total number of arguments and allocate the corresponding
|
|
|
|
space */
|
|
|
|
total_args = 0;
|
2013-12-08 06:19:09 +01:00
|
|
|
@@ -2599,10 +2625,12 @@ int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf)
|
2012-07-05 18:39:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
+ tcg_lock();
|
|
|
|
tcg_gen_code_common(s, gen_code_buf, -1);
|
|
|
|
|
|
|
|
/* flush instruction cache */
|
2013-12-08 06:19:09 +01:00
|
|
|
flush_icache_range((uintptr_t)gen_code_buf, (uintptr_t)s->code_ptr);
|
2012-07-05 18:39:32 +02:00
|
|
|
+ tcg_unlock();
|
|
|
|
|
|
|
|
return s->code_ptr - gen_code_buf;
|
|
|
|
}
|
2013-12-08 06:19:09 +01:00
|
|
|
@@ -2613,7 +2641,11 @@ int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf)
|
2012-07-05 18:39:32 +02:00
|
|
|
Return -1 if not found. */
|
|
|
|
int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset)
|
|
|
|
{
|
|
|
|
- return tcg_gen_code_common(s, gen_code_buf, offset);
|
|
|
|
+ int r;
|
|
|
|
+ tcg_lock();
|
|
|
|
+ r = tcg_gen_code_common(s, gen_code_buf, offset);
|
|
|
|
+ tcg_unlock();
|
|
|
|
+ return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
diff --git a/tcg/tcg.h b/tcg/tcg.h
|
2013-12-08 06:19:09 +01:00
|
|
|
index 0d9bd29..720fdd2 100644
|
2012-07-05 18:39:32 +02:00
|
|
|
--- a/tcg/tcg.h
|
|
|
|
+++ b/tcg/tcg.h
|
2013-12-08 06:19:09 +01:00
|
|
|
@@ -54,6 +54,8 @@ typedef uint64_t tcg_target_ulong;
|
2012-07-05 18:39:32 +02:00
|
|
|
#error unsupported
|
|
|
|
#endif
|
|
|
|
|
|
|
|
+#include "config-host.h"
|
2013-02-10 19:32:29 +01:00
|
|
|
+#include "qemu/thread.h"
|
2012-07-05 18:39:32 +02:00
|
|
|
#include "tcg-runtime.h"
|
|
|
|
|
2013-12-08 06:19:09 +01:00
|
|
|
#if TCG_TARGET_NB_REGS <= 32
|
|
|
|
@@ -526,6 +528,7 @@ struct TCGContext {
|
|
|
|
|
|
|
|
/* The TCGBackendData structure is private to tcg-target.c. */
|
|
|
|
struct TCGBackendData *be;
|
2012-07-05 18:39:32 +02:00
|
|
|
+ QemuMutex lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern TCGContext tcg_ctx;
|
2013-12-08 06:19:09 +01:00
|
|
|
@@ -703,6 +706,9 @@ void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1,
|
2012-07-05 18:39:32 +02:00
|
|
|
TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr, TCGArg *args,
|
|
|
|
TCGOpDef *tcg_op_def);
|
|
|
|
|
|
|
|
+extern void tcg_lock(void);
|
|
|
|
+extern void tcg_unlock(void);
|
|
|
|
+
|
|
|
|
/* only used for debugging purposes */
|
2013-12-08 06:19:09 +01:00
|
|
|
void tcg_dump_ops(TCGContext *s);
|
|
|
|
|