From patchwork Thu Mar 11 00:21:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 397473 Delivered-To: patch@linaro.org Received: by 2002:a17:906:a383:0:0:0:0 with SMTP id k3csp10420ejz; Wed, 10 Mar 2021 16:44:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJwvmbT7GHdbPQiqDEfe3FDw5pa8aQYZ1gU9n2mFCmfWFCqRDFVLDys/nwb2WVsEHKj4Rqng X-Received: by 2002:a6b:7c4a:: with SMTP id b10mr4435166ioq.170.1615423442809; Wed, 10 Mar 2021 16:44:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615423442; cv=none; d=google.com; s=arc-20160816; b=nJUpiOgqw18rbIXi0thYBXZbWe6bkXlZGJ0W0O+mYCu7m4EWwh/gdMukxlydOukna/ K+vGv8hjvY0dEwNw64Pn+xazUAbQ/0WUzMBF6+nig4dRlHusxj7chCdLPMXpmF0YOR8k e+JxXeRV1rHi+LI/61iBFxJGq+XxduG940FDscI2X50RdkfkrZ9VYzRttX32QVn39fHh 1RYQFi5yM9QYEZhEVLglUMIeSd9/4A02GbxTcKukIFLB6YyQtGtJuArDd6Nda4vH5gCE Ja67P5ZxG06zRVdd2bfNnP/9JdCT4QQH1mIluTvIsmEfrx/xeSl+WJ+/aat3YPFAJF0C mLvw== 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=N5ebAqjISG3PFrvVB60Fr8uPDBumPcXXPBy24caHmmA=; b=Fz4kl0c8d1PexcTA7LpyNpJKGeKDzj+PWLvGrGwBmJ7nxtkZ4DBZEXh2/LTCJBRJO1 VMCNhddg6FHtlMRTGKwkd+8QRLg1JMpfYSzPCiPmTrEaCcyH+k/safaXECqG2oxCQiHZ pYG4xSM72Q0VO6tIdiP3eSpvxtYo5Br5Qu51UybCmL6mCehgKUw8jdcXzEMLs+8KW/sP gNiafa6KEXOn3/JKbzgPFQWes+U5pRVafoj49ScWlnJ8J/Wb8PDtnb8LIC9zjdNoUC2p Cr8Y4rkfFdy8rt2dL68p62DDrv5/qiAA9tS92TOcN3yONtKZRG0go0v17yyNgtjQlSlw cJOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=ixZ4ZPOx; 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 b5si489316iok.57.2021.03.10.16.44.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 10 Mar 2021 16:44:02 -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=fail header.i=@linaro.org header.s=google header.b=ixZ4ZPOx; 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]:48692 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lK9Qn-0002Jr-Pp for patch@linaro.org; Wed, 10 Mar 2021 19:44:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55286) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lK961-000587-RR for qemu-devel@nongnu.org; Wed, 10 Mar 2021 19:22:33 -0500 Received: from mail-ot1-x332.google.com ([2607:f8b0:4864:20::332]:36352) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lK95y-0001RG-Ex for qemu-devel@nongnu.org; Wed, 10 Mar 2021 19:22:33 -0500 Received: by mail-ot1-x332.google.com with SMTP id t16so18307027ott.3 for ; Wed, 10 Mar 2021 16:22:29 -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=N5ebAqjISG3PFrvVB60Fr8uPDBumPcXXPBy24caHmmA=; b=ixZ4ZPOxXJNH6Tomd5TMawuXBmZblxZXjOZ+Cyof5Mrlre+WHdKrL5xYwmaJrzTUj0 vVXuyJWjA3zhTZeaueLnQT+AWhLnRtuu1ZByYLtPS+9rvfstZPqsGjF1W9PuKuYZXXa3 gCB2JVVea3ClfTQCQgjRIuTqKJgZBidACT2ZgfhMcDwoU0bQy8QKbEM6jBsk45FCq1CX uA7Ei0D4NpRHVj3nYwx9yJIz+bqun7Ku2LmIXn9Czq546d+DIBPMEpNUSfAZzeC3F5Es aqxIf683Ov6+r4dllkcl7FoucJ/a04XNi4WsSdrL4LO59sHDph5VHzZUESmtjZFyhUGJ EX8Q== 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=N5ebAqjISG3PFrvVB60Fr8uPDBumPcXXPBy24caHmmA=; b=OfEN12v/XIuSEqkJS9+btnVGMr2BuV/GSrcHYsjNHdoOe+7KKVIr9rdkFJdyYa9c/i t2vhsbujCDrex6mwVc5wx/4/42xxLkZexOa5Hz4rq6+qVYFlxYqnqO8wjt3JbC0dMEsX e2Ned32hvuCAm3053TVKxaaKVsDm7lRzGnXfJO3CDq3d2MtRGhMhqqqWiQhoVAnxPo9G OKXtO/dQ6wP9BBfSO5jj4+qt0KAqzCDPPWiutX/o6DgxIwgSok/s2Cs6/fsgolv3Hgud F3WC6O75bl//ksK1PeY9MtvQiBSBBKWcnR1mvsL5ZEheze5SiBVBW6ngccRPBQU0pHQ1 0hDw== X-Gm-Message-State: AOAM530vfuKnsX7AgNnF9GfHENEvO2wrEcH5ZyHGn6AEPXFhWN3JtTFo 82TYMz1p1WOotQbe2XYMLdPyNn/qAq5FOTGC X-Received: by 2002:a05:6830:149:: with SMTP id j9mr4627024otp.66.1615422149295; Wed, 10 Mar 2021 16:22:29 -0800 (PST) Received: from localhost.localdomain (fixed-187-189-51-144.totalplay.net. [187.189.51.144]) by smtp.gmail.com with ESMTPSA id a6sm300962otq.79.2021.03.10.16.22.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Mar 2021 16:22:28 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 25/26] tcg: Merge buffer protection and guard page protection Date: Wed, 10 Mar 2021 18:21:55 -0600 Message-Id: <20210311002156.253711-26-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210311002156.253711-1-richard.henderson@linaro.org> References: <20210311002156.253711-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::332; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x332.google.com 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: r.bolshakov@yadro.com, j@getutm.app Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Do not handle protections on a case-by-case basis in the various alloc_code_gen_buffer instances; do it within a single loop in tcg_region_init. Signed-off-by: Richard Henderson --- tcg/region.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) -- 2.25.1 diff --git a/tcg/region.c b/tcg/region.c index 93d03076d1..5b46172fb5 100644 --- a/tcg/region.c +++ b/tcg/region.c @@ -514,11 +514,6 @@ static int alloc_code_gen_buffer(size_t tb_size, int splitwx, Error **errp) } #endif - if (qemu_mprotect_rwx(buf, size)) { - error_setg_errno(errp, errno, "mprotect of jit buffer"); - return false; - } - region.start_aligned = buf; region.total_size = size; @@ -802,8 +797,7 @@ void tcg_region_init(size_t tb_size, int splitwx, unsigned max_cpus) { size_t page_size; size_t region_size; - size_t i; - int have_prot; + int have_prot, need_prot; have_prot = alloc_code_gen_buffer(size_code_gen_buffer(tb_size), splitwx, &error_fatal); @@ -844,14 +838,38 @@ void tcg_region_init(size_t tb_size, int splitwx, unsigned max_cpus) /* init the region struct */ qemu_mutex_init(®ion.lock); - /* Set guard pages. No need to do this for the rx_buf, only the rw_buf. */ - for (i = 0; i < region.n; i++) { + /* + * Set guard pages. No need to do this for the rx_buf, only the rw_buf. + * Work with the page protections set up with the initial mapping. + */ + need_prot = PAGE_READ | PAGE_WRITE; +#ifndef CONFIG_TCG_INTERPRETER + if (tcg_splitwx_diff == 0) { + need_prot |= PAGE_EXEC; + } +#endif + for (size_t i = 0, n = region.n; i < n; i++) { void *start, *end; int rc; tcg_region_bounds(i, &start, &end); - rc = qemu_mprotect_none(end, page_size); - g_assert(!rc); + if (have_prot != need_prot) { + if (need_prot == (PAGE_READ | PAGE_WRITE | PAGE_EXEC)) { + rc = qemu_mprotect_rwx(start, end - start); + } else if (need_prot == (PAGE_READ | PAGE_WRITE)) { + rc = qemu_mprotect_rw(start, end - start); + } else { + g_assert_not_reached(); + } + if (rc) { + error_setg_errno(&error_fatal, errno, + "mprotect of jit buffer"); + } + } + if (have_prot != 0) { + /* If guard-page permissions don't change, it isn't fatal. */ + (void)qemu_mprotect_none(end, page_size); + } } tcg_region_trees_init();