[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 23/47] accel/tcg: Support split-wx for darwin/iOS with vm_remap
From: |
Richard Henderson |
Subject: |
[PULL 23/47] accel/tcg: Support split-wx for darwin/iOS with vm_remap |
Date: |
Thu, 7 Jan 2021 10:14:24 -1000 |
Cribbed from code posted by Joelle van Dyne <j@getutm.app>,
and rearranged to a cleaner structure.
Reviewed-by: Joelle van Dyne <j@getutm.app>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
accel/tcg/translate-all.c | 67 +++++++++++++++++++++++++++++++++++++++
1 file changed, 67 insertions(+)
diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c
index 3b6804578d..c37718db4d 100644
--- a/accel/tcg/translate-all.c
+++ b/accel/tcg/translate-all.c
@@ -1166,9 +1166,71 @@ static bool alloc_code_gen_buffer_splitwx_memfd(size_t
size, Error **errp)
}
#endif /* CONFIG_POSIX */
+#ifdef CONFIG_DARWIN
+#include <mach/mach.h>
+
+extern kern_return_t mach_vm_remap(vm_map_t target_task,
+ mach_vm_address_t *target_address,
+ mach_vm_size_t size,
+ mach_vm_offset_t mask,
+ int flags,
+ vm_map_t src_task,
+ mach_vm_address_t src_address,
+ boolean_t copy,
+ vm_prot_t *cur_protection,
+ vm_prot_t *max_protection,
+ vm_inherit_t inheritance);
+
+static bool alloc_code_gen_buffer_splitwx_vmremap(size_t size, Error **errp)
+{
+ kern_return_t ret;
+ mach_vm_address_t buf_rw, buf_rx;
+ vm_prot_t cur_prot, max_prot;
+
+ /* Map the read-write portion via normal anon memory. */
+ if (!alloc_code_gen_buffer_anon(size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, errp)) {
+ return false;
+ }
+
+ buf_rw = (mach_vm_address_t)tcg_ctx->code_gen_buffer;
+ buf_rx = 0;
+ ret = mach_vm_remap(mach_task_self(),
+ &buf_rx,
+ size,
+ 0,
+ VM_FLAGS_ANYWHERE,
+ mach_task_self(),
+ buf_rw,
+ false,
+ &cur_prot,
+ &max_prot,
+ VM_INHERIT_NONE);
+ if (ret != KERN_SUCCESS) {
+ /* TODO: Convert "ret" to a human readable error message. */
+ error_setg(errp, "vm_remap for jit splitwx failed");
+ munmap((void *)buf_rw, size);
+ return false;
+ }
+
+ if (mprotect((void *)buf_rx, size, PROT_READ | PROT_EXEC) != 0) {
+ error_setg_errno(errp, errno, "mprotect for jit splitwx");
+ munmap((void *)buf_rx, size);
+ munmap((void *)buf_rw, size);
+ return false;
+ }
+
+ tcg_splitwx_diff = buf_rx - buf_rw;
+ return true;
+}
+#endif /* CONFIG_DARWIN */
+
static bool alloc_code_gen_buffer_splitwx(size_t size, Error **errp)
{
if (TCG_TARGET_SUPPORT_MIRROR) {
+#ifdef CONFIG_DARWIN
+ return alloc_code_gen_buffer_splitwx_vmremap(size, errp);
+#endif
#ifdef CONFIG_POSIX
return alloc_code_gen_buffer_splitwx_memfd(size, errp);
#endif
@@ -1201,6 +1263,11 @@ static bool alloc_code_gen_buffer(size_t size, int
splitwx, Error **errp)
#ifdef CONFIG_TCG_INTERPRETER
/* The tcg interpreter does not need execute permission. */
prot = PROT_READ | PROT_WRITE;
+#elif defined(CONFIG_DARWIN)
+ /* Applicable to both iOS and macOS (Apple Silicon). */
+ if (!splitwx) {
+ flags |= MAP_JIT;
+ }
#endif
return alloc_code_gen_buffer_anon(size, prot, flags, errp);
--
2.25.1
- [PULL 12/47] tcg: Introduce tcg_splitwx_to_{rx,rw}, (continued)
- [PULL 12/47] tcg: Introduce tcg_splitwx_to_{rx,rw}, Richard Henderson, 2021/01/07
- [PULL 14/47] tcg: Adjust tcg_out_call for const, Richard Henderson, 2021/01/07
- [PULL 13/47] tcg: Adjust TCGLabel for const, Richard Henderson, 2021/01/07
- [PULL 11/47] tcg: Add in_code_gen_buffer, Richard Henderson, 2021/01/07
- [PULL 15/47] tcg: Adjust tcg_out_label for const, Richard Henderson, 2021/01/07
- [PULL 16/47] tcg: Adjust tcg_register_jit for const, Richard Henderson, 2021/01/07
- [PULL 17/47] tcg: Adjust tb_target_set_jmp_target for split-wx, Richard Henderson, 2021/01/07
- [PULL 19/47] tcg: Make tb arg to synchronize_from_tb const, Richard Henderson, 2021/01/07
- [PULL 18/47] tcg: Make DisasContextBase.tb const, Richard Henderson, 2021/01/07
- [PULL 25/47] tcg/i386: Support split-wx code generation, Richard Henderson, 2021/01/07
- [PULL 23/47] accel/tcg: Support split-wx for darwin/iOS with vm_remap,
Richard Henderson <=
- [PULL 24/47] tcg: Return the TB pointer from the rx region from exit_tb, Richard Henderson, 2021/01/07
- [PULL 21/47] tcg: Add --accel tcg,split-wx property, Richard Henderson, 2021/01/07
- [PULL 22/47] accel/tcg: Support split-wx for linux with memfd, Richard Henderson, 2021/01/07
- [PULL 27/47] tcg/aarch64: Support split-wx code generation, Richard Henderson, 2021/01/07
- [PULL 29/47] tcg/tci: Push const down through bytecode reading, Richard Henderson, 2021/01/07
- [PULL 32/47] tcg/ppc: Use tcg_out_mem_long to reset TCG_REG_TB, Richard Henderson, 2021/01/07
- [PULL 36/47] tcg/s390: Use tcg_tbrel_diff, Richard Henderson, 2021/01/07
- [PULL 20/47] tcg: Use Error with alloc_code_gen_buffer, Richard Henderson, 2021/01/07
- [PULL 33/47] tcg/ppc: Support split-wx code generation, Richard Henderson, 2021/01/07
- [PULL 37/47] tcg/s390: Support split-wx code generation, Richard Henderson, 2021/01/07