[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 14/19] RFC: accel/tcg: Support split-rwx for darwin/iOS with v
From: |
Richard Henderson |
Subject: |
[PATCH v2 14/19] RFC: accel/tcg: Support split-rwx for darwin/iOS with vm_remap |
Date: |
Thu, 29 Oct 2020 17:49:16 -0700 |
Cribbed from code posted by Joelle van Dyne <j@getutm.app>,
and rearranged to a cleaner structure. Completely untested.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
accel/tcg/translate-all.c | 68 ++++++++++++++++++++++++++++++++++++++-
1 file changed, 67 insertions(+), 1 deletion(-)
diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c
index 3e69ebd1d3..bf8263fdb4 100644
--- a/accel/tcg/translate-all.c
+++ b/accel/tcg/translate-all.c
@@ -1093,6 +1093,11 @@ static bool alloc_code_gen_buffer_anon(size_t size, int
prot, Error **errp)
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
void *buf;
+#ifdef CONFIG_DARWIN
+ /* Applicable to both iOS and macOS (Apple Silicon). */
+ flags |= MAP_JIT;
+#endif
+
buf = mmap(NULL, size, prot, flags, -1, 0);
if (buf == MAP_FAILED) {
error_setg_errno(errp, errno,
@@ -1182,13 +1187,74 @@ static bool alloc_code_gen_buffer_mirror_memfd(size_t
size, Error **errp)
qemu_madvise(buf_rx, size, QEMU_MADV_HUGEPAGE);
return true;
}
-#endif
+#endif /* CONFIG_LINUX */
+
+#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_mirror_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, errp)) {
+ return false;
+ }
+
+ buf_rw = tcg_ctx->code_gen_buffer;
+ buf_rx = 0;
+ ret = mach_vm_remap(mach_task_self(),
+ &buf_rx,
+ size,
+ 0,
+ VM_FLAGS_ANYWHERE | VM_FLAGS_RANDOM_ADDR,
+ 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 mirror 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 mirror");
+ munmap((void *)buf_rx, size);
+ munmap((void *)buf_rw, size);
+ return false;
+ }
+
+ tcg_rx_mirror_diff = buf_rx - buf_rw;
+ return true;
+}
+#endif /* CONFIG_DARWIN */
static bool alloc_code_gen_buffer_mirror(size_t size, Error **errp)
{
if (TCG_TARGET_SUPPORT_MIRROR) {
#ifdef CONFIG_LINUX
return alloc_code_gen_buffer_mirror_memfd(size, errp);
+#endif
+#ifdef CONFIG_DARWIN
+ return alloc_code_gen_buffer_mirror_vmremap(size, errp);
#endif
}
error_setg(errp, "jit split-rwx not supported");
--
2.25.1
- [PATCH v2 05/19] tcg: Adjust tcg_out_call for const, (continued)
- [PATCH v2 05/19] tcg: Adjust tcg_out_call for const, Richard Henderson, 2020/10/29
- [PATCH v2 06/19] tcg: Adjust tcg_out_label for const, Richard Henderson, 2020/10/29
- [PATCH v2 03/19] tcg: Move tcg epilogue pointer out of TCGContext, Richard Henderson, 2020/10/29
- [PATCH v2 08/19] tcg: Adjust tb_target_set_jmp_target for split rwx, Richard Henderson, 2020/10/29
- [PATCH v2 09/19] tcg: Make DisasContextBase.tb const, Richard Henderson, 2020/10/29
- [PATCH v2 02/19] tcg: Move tcg prologue pointer out of TCGContext, Richard Henderson, 2020/10/29
- [PATCH v2 04/19] tcg: Introduce tcg_mirror_rw_to_rx/tcg_mirror_rx_to_rw, Richard Henderson, 2020/10/29
- [PATCH v2 10/19] tcg: Make tb arg to synchronize_from_tb const, Richard Henderson, 2020/10/29
- [PATCH v2 11/19] tcg: Use Error with alloc_code_gen_buffer, Richard Henderson, 2020/10/29
- [PATCH v2 12/19] tcg: Add --accel tcg,split-rwx property, Richard Henderson, 2020/10/29
- [PATCH v2 14/19] RFC: accel/tcg: Support split-rwx for darwin/iOS with vm_remap,
Richard Henderson <=
- [PATCH v2 15/19] tcg: Return the rx mirror of TranslationBlock from exit_tb, Richard Henderson, 2020/10/29
- [PATCH v2 17/19] tcg/aarch64: Use B not BL for tcg_out_goto_long, Richard Henderson, 2020/10/29
- [PATCH v2 07/19] tcg: Adjust tcg_register_jit for const, Richard Henderson, 2020/10/29
- [PATCH v2 13/19] accel/tcg: Support split-rwx for linux with memfd, Richard Henderson, 2020/10/29
- [PATCH v2 16/19] tcg/i386: Support split-rwx code generation, Richard Henderson, 2020/10/29
- [PATCH v2 19/19] tcg/aarch64: Support split-rwx code generation, Richard Henderson, 2020/10/29
- [PATCH v2 18/19] tcg/aarch64: Implement flush_idcache_range manually, Richard Henderson, 2020/10/29
- Re: [PATCH v2 00/19] Mirror map JIT memory for TCG, no-reply, 2020/10/29