tcg: Fix execution on Apple Silicon

Pages can't be both write and executable at the same time on Apple
Silicon. macOS provides public API to switch write protection [1] for
JIT applications, like TCG.

1. https://developer.apple.com/documentation/apple_silicon/porting_just-in-time_compilers_to_apple_silicon

Signed-off-by: Roman Bolshakov <r.bolshakov@yadro.com>
Message-Id: <20210103145055.11074-1-r.bolshakov@yadro.com>
This commit is contained in:
Roman Bolshakov
2021-01-03 17:50:57 +03:00
committed by osy
parent 73b368199a
commit 4a3199be61
5 changed files with 34 additions and 0 deletions
+2
View File
@@ -176,6 +176,7 @@ static inline TranslationBlock *cpu_tb_exec(CPUState *cpu,
}
#endif /* DEBUG_DISAS */
qemu_thread_jit_execute();
ret = tcg_qemu_tb_exec(env, tb_ptr);
cpu->can_do_io = 1;
/*
@@ -379,6 +380,7 @@ static inline void tb_add_jump(TranslationBlock *tb, int n,
{
uintptr_t old;
qemu_thread_jit_write();
assert(n < ARRAY_SIZE(tb->jmp_list_next));
qemu_spin_lock(&tb_next->jmp_lock);
+6
View File
@@ -1083,6 +1083,9 @@ static bool alloc_code_gen_buffer_anon(size_t size, int prot,
{
void *buf;
#if defined(__APPLE__) && defined(MAC_OS_VERSION_11_0)
flags |= MAP_JIT;
#endif
buf = mmap(NULL, size, prot, flags, -1, 0);
if (buf == MAP_FAILED) {
error_setg_errno(errp, errno,
@@ -1667,7 +1670,9 @@ static void do_tb_phys_invalidate(TranslationBlock *tb, bool rm_from_page_list)
static void tb_phys_invalidate__locked(TranslationBlock *tb)
{
qemu_thread_jit_write();
do_tb_phys_invalidate(tb, true);
qemu_thread_jit_execute();
}
/* invalidate one TB
@@ -1869,6 +1874,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
#endif
assert_memory_lock();
qemu_thread_jit_write();
phys_pc = get_page_addr_code(env, pc);
+3
View File
@@ -697,4 +697,7 @@ static inline int ios_does_not_support_system(const char *command)
}
#endif /* CONFIG_IOS */
void qemu_thread_jit_write(void);
void qemu_thread_jit_execute(void);
#endif
+1
View File
@@ -1112,6 +1112,7 @@ void tcg_prologue_init(TCGContext *s)
s->pool_labels = NULL;
#endif
qemu_thread_jit_write();
/* Generate the prologue. */
tcg_target_qemu_prologue(s);
+22
View File
@@ -606,3 +606,25 @@ writev(int fd, const struct iovec *iov, int iov_cnt)
return readv_writev(fd, iov, iov_cnt, true);
}
#endif
#if defined(__APPLE__) && defined(MAC_OS_VERSION_11_0)
static inline void qemu_thread_jit_write_protect(bool enabled)
{
if (pthread_jit_write_protect_supported_np()) {
pthread_jit_write_protect_np(enabled);
}
}
void qemu_thread_jit_execute(void)
{
qemu_thread_jit_write_protect(true);
}
void qemu_thread_jit_write(void)
{
qemu_thread_jit_write_protect(false);
}
#else
void qemu_thread_jit_write(void) {}
void qemu_thread_jit_execute(void) {}
#endif