2016-09-19 19:06:58 +02:00
|
|
|
From 9d58ff5695952626bf3fb74d6fe9b5d666c43ce6 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]
|
2014-07-11 18:51:43 +02:00
|
|
|
[AF: Rebased for v2.1.0-rc0]
|
2016-01-19 19:31:32 +01:00
|
|
|
[AF: Rebased onto tcg_gen_code_common() drop for v2.5.0-rc0]
|
2016-09-19 19:06:58 +02:00
|
|
|
[AF: Rebased for v2.7.0-rc2]
|
2013-02-10 19:32:29 +01:00
|
|
|
Signed-off-by: Andreas Färber <afaerber@suse.de>
|
2012-07-05 18:39:32 +02:00
|
|
|
---
|
2014-01-17 23:04:30 +01:00
|
|
|
linux-user/mmap.c | 3 +++
|
2016-09-19 19:06:58 +02:00
|
|
|
tcg/tcg.c | 29 +++++++++++++++++++++++++++++
|
2015-03-21 12:46:36 +01:00
|
|
|
tcg/tcg.h | 6 ++++++
|
2016-09-19 19:06:58 +02:00
|
|
|
3 files changed, 38 insertions(+)
|
2012-07-05 18:39:32 +02:00
|
|
|
|
|
|
|
diff --git a/linux-user/mmap.c b/linux-user/mmap.c
|
2016-09-19 19:06:58 +02:00
|
|
|
index 68a655e..d202e45 100644
|
2012-07-05 18:39:32 +02:00
|
|
|
--- a/linux-user/mmap.c
|
|
|
|
+++ b/linux-user/mmap.c
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -22,6 +22,7 @@
|
2012-07-05 18:39:32 +02:00
|
|
|
|
|
|
|
#include "qemu.h"
|
|
|
|
#include "qemu-common.h"
|
|
|
|
+#include "tcg.h"
|
2015-07-24 15:54:26 +02:00
|
|
|
#include "translate-all.h"
|
2012-07-05 18:39:32 +02:00
|
|
|
|
|
|
|
//#define DEBUG_MMAP
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -33,6 +34,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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -40,6 +42,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
|
2016-09-19 19:06:58 +02:00
|
|
|
index 42417bd..ef6ae10 100644
|
2012-07-05 18:39:32 +02:00
|
|
|
--- a/tcg/tcg.c
|
|
|
|
+++ b/tcg/tcg.c
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -33,6 +33,8 @@
|
2016-04-05 15:18:15 +02:00
|
|
|
#include "qemu/cutils.h"
|
2013-02-10 19:32:29 +01:00
|
|
|
#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
|
|
|
|
2014-03-20 01:26:56 +01:00
|
|
|
/* Note: the long term plan is to reduce the dependencies on the QEMU
|
2012-07-05 18:39:32 +02:00
|
|
|
CPU definitions. Currently they are used for qemu_ld/st
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -120,6 +122,29 @@ static bool tcg_out_tb_finalize(TCGContext *s);
|
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
|
|
|
|
+}
|
|
|
|
+
|
2014-07-11 18:51:43 +02:00
|
|
|
#if TCG_TARGET_INSN_UNIT_SIZE == 1
|
|
|
|
static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
|
2012-07-05 18:39:32 +02:00
|
|
|
{
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -332,6 +357,7 @@ 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);
|
2016-09-19 19:06:58 +02:00
|
|
|
|
2012-07-05 18:39:32 +02:00
|
|
|
/* Count total number of arguments and allocate the corresponding
|
|
|
|
space */
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -2551,6 +2577,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
|
2016-01-19 19:31:32 +01:00
|
|
|
qemu_log("\n");
|
2012-07-05 18:39:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
+ tcg_lock();
|
2016-01-19 19:31:32 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
s->opt_time -= profile_getclock();
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -2673,6 +2700,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
|
2016-01-19 19:31:32 +01:00
|
|
|
the buffer completely. Thus we can test for overflow after
|
|
|
|
generating code without having to check during generation. */
|
|
|
|
if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
|
|
|
|
+ tcg_unlock();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -2686,6 +2714,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
|
2012-07-05 18:39:32 +02:00
|
|
|
|
|
|
|
/* flush instruction cache */
|
2014-07-11 18:51:43 +02:00
|
|
|
flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr);
|
2012-07-05 18:39:32 +02:00
|
|
|
+ tcg_unlock();
|
|
|
|
|
2014-07-11 18:51:43 +02:00
|
|
|
return tcg_current_code_size(s);
|
2012-07-05 18:39:32 +02:00
|
|
|
}
|
|
|
|
diff --git a/tcg/tcg.h b/tcg/tcg.h
|
2016-09-19 19:06:58 +02:00
|
|
|
index 1bcabca..5c2522e 100644
|
2012-07-05 18:39:32 +02:00
|
|
|
--- a/tcg/tcg.h
|
|
|
|
+++ b/tcg/tcg.h
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -29,6 +29,7 @@
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "exec/tb-context.h"
|
2014-07-11 18:51:43 +02:00
|
|
|
#include "qemu/bitops.h"
|
2013-02-10 19:32:29 +01:00
|
|
|
+#include "qemu/thread.h"
|
2014-07-11 18:51:43 +02:00
|
|
|
#include "tcg-target.h"
|
2012-07-05 18:39:32 +02:00
|
|
|
|
2016-09-19 19:06:58 +02:00
|
|
|
/* XXX: make safe guess about sizes */
|
|
|
|
@@ -697,6 +698,8 @@ struct TCGContext {
|
2016-01-19 19:31:32 +01:00
|
|
|
|
|
|
|
uint16_t gen_insn_end_off[TCG_MAX_INSNS];
|
|
|
|
target_ulong gen_insn_data[TCG_MAX_INSNS][TARGET_INSN_START_WORDS];
|
2015-03-21 12:46:36 +01:00
|
|
|
+
|
2012-07-05 18:39:32 +02:00
|
|
|
+ QemuMutex lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern TCGContext tcg_ctx;
|
2016-09-19 19:06:58 +02:00
|
|
|
@@ -904,6 +907,9 @@ TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg);
|
|
|
|
|
2015-03-21 12:46:36 +01:00
|
|
|
void tcg_optimize(TCGContext *s);
|
2012-07-05 18:39:32 +02:00
|
|
|
|
|
|
|
+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);
|
|
|
|
|