From patchwork Fri Nov 6 03:28:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 320745 Delivered-To: patch@linaro.org Received: by 2002:a92:7b12:0:0:0:0:0 with SMTP id w18csp981437ilc; Thu, 5 Nov 2020 19:41:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJz5tA9CJtUgE4tczdGkJfEfMXnlxt+qr0Fa8O4q3ikePZUtDa/elk/LwWQfOwQGF6SIkpSW X-Received: by 2002:a25:c183:: with SMTP id r125mr243293ybf.267.1604634102069; Thu, 05 Nov 2020 19:41:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604634102; cv=none; d=google.com; s=arc-20160816; b=nkxVMA+6uweOjL6qBO+KSVqMR/NQoR/tgKh6PdEoPkSApIssym7kZCIw1/V+SkfVyk WL2+ghWCUftqXSdX6GRa6yvOKCPcHScIYkrQYWRRl7EljgMyMK5Si4y+H2NrmFMsz+NF ukvO98Uh2e/fPsvfYGkyHbigSR/gjU5JHQ2W3IGADmqNJCwDAFOf0OYBty1aJAeZ7Bzw s+yKLxv6F89zeQl68KorxkByDKcoVBP6vFzVj3UhdWulCr4jU8UMTHGLzVmLzmZXm5bO wykK1fsiGVTuaDleudzpXMwCc1mWc620seTpC3hACJe5qysqDm3HCPXsQHIFNEGmhk4a pwFQ== 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=lq4IkwTkjjjV6cEfjyzqrZr7JH5rLAW05aEI0Sl+9Jw=; b=Znp4IGC1NZh78B/9d/6cDSzXnik4fnFdFOItZrp4puimlutXwCdgHB906CbZ4wHFWO FGvwN7VyNeLwbL6TpEbXl2/4iaXKuEoGLQ2KH1xHQgIYwcQUurW7XGDBOhaw2oHZtsiU jpmCkP8Mc3VNLKa60h7kTV0sbEEj8JaayCiY/nwNvrLnXEV3Ec0ZookzoNUD7qrMSIP8 KtEeZjyGYQvhjP1aPY620lwuO4Pf5BLb/FJ+SWyJ6Ds+U6wnUs+iSYC4B/kzLyxyRUwW uvzO/wypSQ5hasNBHrxUUlkC5+4CG+jaJxlT2HmvTaSVLM/CzFQuvpPhOIw+oSIYzSMU Mdag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=L7gUHdwg; 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=pass (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 130si2829490yba.407.2020.11.05.19.41.41 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 05 Nov 2020 19:41:42 -0800 (PST) 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=pass header.i=@linaro.org header.s=google header.b=L7gUHdwg; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:60294 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kasdB-0005FO-IN for patch@linaro.org; Thu, 05 Nov 2020 22:41:41 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:47026) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kasRg-0004tb-27 for qemu-devel@nongnu.org; Thu, 05 Nov 2020 22:29:48 -0500 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]:44939) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kasRd-0006xO-Lu for qemu-devel@nongnu.org; Thu, 05 Nov 2020 22:29:47 -0500 Received: by mail-pf1-x442.google.com with SMTP id y7so28322pfq.11 for ; Thu, 05 Nov 2020 19:29:45 -0800 (PST) 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=lq4IkwTkjjjV6cEfjyzqrZr7JH5rLAW05aEI0Sl+9Jw=; b=L7gUHdwguNP4jAPxpnFrHEN0Zm+hJa/7DI9lwXNVrV/UmOQEC6wB/jqvRCwuELzI8/ rRARviqfWrnGWfUwAbBwHQDmNpUYk/j0mWI8rNByReuPcoqFkA8ci4GLWA9e4Zwu1U5z N1mEVstlRexLy3mhvFVjB3h88w83IB0YnCOlDD0qTtMPDk5xarHdVmbpOqGPke7sjeQg L1hUfU104zpzpTcV1FYrtaJDaprf9EYglOVaKubllZL5MFyrV4XBuSMfhwNk4K/KWVVU cVNtdvb+3UXXspiPO1gdMw/y1NCvbl1M5VTw85Vl2sxsQZYAL1vbHq2xbRLVeT+dnaIm Tt4Q== 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=lq4IkwTkjjjV6cEfjyzqrZr7JH5rLAW05aEI0Sl+9Jw=; b=pt2IjQdEci1BrLPW4r4alENKxW5Kk4Ja5TEVLys0jWl8awLvskL7A9EVUugc7lR1ou l6smo0It/5HaKXrMjv08bx/wA9BWlYAaD1luTW6KARyZ0V7dcxGSlzjPfWM9fag2FWil sRCFH3cahelxnc7izENLtLbiDKt7LYEKzHI+mHwQYLGdqO65yT2W2sfpWXRtwngriG/9 QijGgD1HZzRkSpFWLe1IOjM4rmDHLfoQ9LhHhH9BAKiSwuq73VC6ETtD8sDmbLAMeb2t v4Hwc7mLuFoJDtQ1ghfHc6fNQ9433Nm73k4PCuZJPCTjvaGxTW29MMHSslNkaLPZYner RtHQ== X-Gm-Message-State: AOAM532lHft/XKnHX/8+S6NXoW7tR8Pu21hqrbVsman/o08BUTsE3MgY REye0FSZ0p3MjMPR3/KEtXFhHVezLEuUvQ== X-Received: by 2002:aa7:96ce:0:b029:155:8c02:e74a with SMTP id h14-20020aa796ce0000b02901558c02e74amr722pfq.32.1604633384041; Thu, 05 Nov 2020 19:29:44 -0800 (PST) Received: from localhost.localdomain (76-14-210-194.or.wavecable.com. [76.14.210.194]) by smtp.gmail.com with ESMTPSA id i10sm40773pfd.60.2020.11.05.19.29.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2020 19:29:43 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v3 16/41] accel/tcg: Support split-wx for darwin/iOS with vm_remap Date: Thu, 5 Nov 2020 19:28:56 -0800 Message-Id: <20201106032921.600200-17-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201106032921.600200-1-richard.henderson@linaro.org> References: <20201106032921.600200-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::442; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x442.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: j@getutm.app 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 | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) -- 2.25.1 Reviewed-by: Joelle van Dyne diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 1931e65365..17df6c94fa 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 + +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,9 @@ 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). */ + flags |= MAP_JIT; #endif return alloc_code_gen_buffer_anon(size, prot, flags, errp);