From patchwork Fri Oct 30 00:49:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 319960 Delivered-To: patch@linaro.org Received: by 2002:a92:7b12:0:0:0:0:0 with SMTP id w18csp969304ilc; Thu, 29 Oct 2020 18:01:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw5BOv/BaHZC/uCJg+CoJAe79ZmFE/SMBeFjcF5SQ/kifG5I6QZReV6Uzm7uV9x4TXr0ZeA X-Received: by 2002:a6b:8ec7:: with SMTP id q190mr59187iod.42.1604019713467; Thu, 29 Oct 2020 18:01:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1604019713; cv=none; d=google.com; s=arc-20160816; b=AWN3fXu1HHoWfEpxvcuy3mLRNhsRMqT8IjJcUFuPkDnmshFoSTb+4GAvFz7JuVkMFg qlbYG9vO498Scr9GvECOhtU4IuKOL3f35piX9JmNRUswj34xP4R5TYcba/blzBS3o5QE cNwfhia6lsX456hK8r9TeMNTOAx2A/b8ZE1i4GBbCKO1ikXrtmWz+d4xDRgzE3XajTTI nM0qdLJJMVKmA9QV/km1S4a83SlbYhpfSYIfR0xzJMoQUFhTnrTutxVXS7ToUn0sDak6 UWkkO+4Sd8bB3QKnMKG+qmWt2ChNvBgRcIVhnDpQWHHjMtoUch6/b0OendYJvL+qa8Eh wUPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=z2KaP1xs6rRO9cwUUOTb/ugn+4CyHbulGJfJyCgsn04=; b=K2qtrhvQoZLeFeyWnHZ8JwnTAKzd2j1JZKJP7b1Sb+33ZAyf5vOf1Z6HcWdzgieHyk Lg+yxgDN9vYajdDwzFKGHD+TeyN1tckhwi9OcUc4zF0JmZbavrIZYE1hrjdJBCA8zrk+ JJfzO9nAqkQkgxrjEWapaG3m5bpE3chHfLF5tdZwZj6r9pwVWVngrJo0XOn8zmSvC+cK zMRQwLBiBd3fUWmawznYf/XTDFZNRMp9BbOtz3ehjNAHAcyvjqgUgKW6hJhJYlJOBdcF 23ZV/6n3EPmWJsCxuK1vQ4ds2UBP9tt00ydnOqano86J05QkIoK42T93qwABkHPWfH5+ ihiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=xx33lHJB; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id y17si4515118jat.17.2020.10.29.18.01.53 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 29 Oct 2020 18:01:53 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=xx33lHJB; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:33710 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kYIng-0004ZO-DT for patch@linaro.org; Thu, 29 Oct 2020 21:01:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46868) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kYIc0-0004L9-7t for qemu-devel@nongnu.org; Thu, 29 Oct 2020 20:49:48 -0400 Received: from mail-pg1-x543.google.com ([2607:f8b0:4864:20::543]:38296) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kYIbv-0001ri-Tr for qemu-devel@nongnu.org; Thu, 29 Oct 2020 20:49:47 -0400 Received: by mail-pg1-x543.google.com with SMTP id i26so3751033pgl.5 for ; Thu, 29 Oct 2020 17:49:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=z2KaP1xs6rRO9cwUUOTb/ugn+4CyHbulGJfJyCgsn04=; b=xx33lHJBzp23BGBvF1qD0vsRKsTSd2golyL79BF+ezblp2OuSSEvG2eoPCkdqUsebS BE6zIEL2kcsy4iCH/LbU5JLZI+JVnlQ2CSkzchye8ln66nKhPZnu3mK4jxcJK4kbL1Gx eYqMVR8Pk0GxhD3QstDMVGj7Hrr6Ye32YHjV7EVz5uCAA7o5zOURa0GNG2rJhpBbH01f hcDDyNG0mQWUJBx2V0xqJwZh4Bp2l5VEB6nw29n05Wid5XzLoadrfBZCkwj7PpZkRpHM geRZthL+tPxiyaLbagpV6+nuazmBJtQANQtWvsC2Mchq0m9ugmJUp5TFJ6IOX3znvZV4 Lwxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=z2KaP1xs6rRO9cwUUOTb/ugn+4CyHbulGJfJyCgsn04=; b=j30M7h/p9VBjs0foEp9Ouxf/UqQ9JtR/gNuZKAEEnysjOxZcfbS3CKxgDMb37XWosq 7DqI+xzgQRvKCIbj0fRCv/I+ljri4ZYaDeqlihU+p59JR3xvwgX2T0EcxdS+gT/TAnLa HAnu4qqRrekuA2Qc8xCz1APqoqTH+vXNdKu9VV7CK7Esoh0VwXW/fEc1di6yJaNc2eyP 8FcvoNN2Bto3/EBeKwYlY0bihv7lpn1X7FPWWKN/OreCYJwgwF7TbGfxRdcHoSjxVS4H 4Cik0WkcC3ywvqSZ33pL8jTjr3TyqEBXgy/8lRyiOg6FpIEUZ6kgilGlyJYTTb220LVh +eqw== X-Gm-Message-State: AOAM5310lt8O3nmzpDjyxWzndkYBvShaWYKElaFjsi3grLRLHU99wIGZ 0/zWHFsh5r0rDq/pLkgFTH33jzVmLQrWmQ== X-Received: by 2002:a17:90a:1690:: with SMTP id o16mr1964360pja.148.1604018982218; Thu, 29 Oct 2020 17:49:42 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id b6sm3557264pgq.58.2020.10.29.17.49.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Oct 2020 17:49:41 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org 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 Message-Id: <20201030004921.721096-15-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201030004921.721096-1-richard.henderson@linaro.org> References: <20201030004921.721096-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::543; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x543.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, j@getutm.app, laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Cribbed from code posted by Joelle van Dyne , and rearranged to a cleaner structure. Completely untested. Signed-off-by: Richard Henderson --- accel/tcg/translate-all.c | 68 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) -- 2.25.1 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 + +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");