Skip to content

Instantly share code, notes, and snippets.

@KhaosT

KhaosT/tcg.patch Secret

Created December 10, 2020 02:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KhaosT/9b6ac0a12a8503a1eb8b9a07723ddf4a to your computer and use it in GitHub Desktop.
Save KhaosT/9b6ac0a12a8503a1eb8b9a07723ddf4a to your computer and use it in GitHub Desktop.
From b1f9d4395319e8ccab1e4fa8eb81a35075f18fb2 Mon Sep 17 00:00:00 2001
From: Khaos Tian <khaos.tian@gmail.com>
Date: Thu, 26 Nov 2020 16:45:39 -0800
Subject: [PATCH] TCG
---
accel/tcg/cpu-exec.c | 4 ++++
accel/tcg/tcg-all.c | 4 ++--
accel/tcg/translate-all.c | 7 ++++++-
3 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c
index 58aea605d8..1294c9d908 100644
--- a/accel/tcg/cpu-exec.c
+++ b/accel/tcg/cpu-exec.c
@@ -365,6 +365,7 @@ static inline void tb_add_jump(TranslationBlock *tb, int n,
{
uintptr_t old;
+ pthread_jit_write_protect_np(false);
assert(n < ARRAY_SIZE(tb->jmp_list_next));
qemu_spin_lock(&tb_next->jmp_lock);
@@ -393,10 +394,12 @@ static inline void tb_add_jump(TranslationBlock *tb, int n,
"] index %d -> %p [" TARGET_FMT_lx "]\n",
tb->tc.ptr, tb->pc, n,
tb_next->tc.ptr, tb_next->pc);
+ pthread_jit_write_protect_np(true);
return;
out_unlock_next:
qemu_spin_unlock(&tb_next->jmp_lock);
+ pthread_jit_write_protect_np(true);
return;
}
@@ -654,6 +657,7 @@ static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
uintptr_t ret;
int32_t insns_left;
+ pthread_jit_write_protect_np(true);
trace_exec_tb(tb, tb->pc);
ret = cpu_tb_exec(cpu, tb);
tb = (TranslationBlock *)(ret & ~TB_EXIT_MASK);
diff --git a/accel/tcg/tcg-all.c b/accel/tcg/tcg-all.c
index fa1208158f..72d414a970 100644
--- a/accel/tcg/tcg-all.c
+++ b/accel/tcg/tcg-all.c
@@ -101,11 +101,11 @@ bool mttcg_enabled;
static int tcg_init(MachineState *ms)
{
TCGState *s = TCG_STATE(current_accel());
-
+ pthread_jit_write_protect_np(false);
tcg_exec_init(s->tb_size * 1024 * 1024);
mttcg_enabled = s->mttcg_enabled;
cpus_register_accel(&tcg_cpus);
-
+ pthread_jit_write_protect_np(true);
return 0;
}
diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c
index 4572b4901f..7287ea4275 100644
--- a/accel/tcg/translate-all.c
+++ b/accel/tcg/translate-all.c
@@ -1068,12 +1068,13 @@ static inline void *alloc_code_gen_buffer(void)
static inline void *alloc_code_gen_buffer(void)
{
int prot = PROT_WRITE | PROT_READ | PROT_EXEC;
- int flags = MAP_PRIVATE | MAP_ANONYMOUS;
+ int flags = MAP_JIT | MAP_PRIVATE | MAP_ANONYMOUS;
size_t size = tcg_ctx->code_gen_buffer_size;
void *buf;
buf = mmap(NULL, size, prot, flags, -1, 0);
if (buf == MAP_FAILED) {
+ fprintf(stderr, "mmap failed: %d\n", errno);
return NULL;
}
@@ -1485,7 +1486,9 @@ static void do_tb_phys_invalidate(TranslationBlock *tb, bool rm_from_page_list)
static void tb_phys_invalidate__locked(TranslationBlock *tb)
{
+ pthread_jit_write_protect_np(false);
do_tb_phys_invalidate(tb, true);
+ pthread_jit_write_protect_np(true);
}
/* invalidate one TB
@@ -1675,6 +1678,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
target_ulong pc, target_ulong cs_base,
uint32_t flags, int cflags)
{
+ pthread_jit_write_protect_np(false);
CPUArchState *env = cpu->env_ptr;
TranslationBlock *tb, *existing_tb;
tb_page_addr_t phys_pc, phys_page2;
@@ -1918,6 +1922,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
return existing_tb;
}
tcg_tb_insert(tb);
+ pthread_jit_write_protect_np(true);
return tb;
}
--
2.27.0
@evennotodd
Copy link

How would I apply this patch?

@VollyBird
Copy link

which qemu version corresponding to this patch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment