From patchwork Wed Apr 12 11:43:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 672622 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:184:0:0:0:0 with SMTP id p4csp288486wrx; Wed, 12 Apr 2023 04:47:32 -0700 (PDT) X-Google-Smtp-Source: AKy350bCTJCfjd+/4jyYORZG2pSG5Rgazvgtqw5GK9Upv1KOOcivq/ofRPQiSsuOiLEzQ2A9hDoa X-Received: by 2002:a05:6214:224a:b0:5c2:a8b0:d71a with SMTP id c10-20020a056214224a00b005c2a8b0d71amr24065392qvc.43.1681300051971; Wed, 12 Apr 2023 04:47:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681300051; cv=none; d=google.com; s=arc-20160816; b=nWd6PyY+piCpxhLgU+wHzCbW6KAUoXDacd+gVrbRHTwxZepR7MPjtHF+f/7FImo4kI QS+UweuL7HmW0D4GGqYqS2TudaGYPBgIKsRJmvSvI1+3HFMfaWYu2YVpws9DQ+UiZ2Px 7K9zmuRu7t9keGX24jONoJXZ1NLccpheIghulpTtmYZFfs1E1eytvH47F1YgfhVM7OSm WT9jiEpCkbZQnM8Xjxd371ydXfmoIwsCcRlz/2P0UXxjv2RIEAh6sSNxMmf6yWbrZRn3 zSUry9zFo6g5KlHw+Yk2dErE9hxhI4ARExp3HbgfRhSQGbZjmpXwPELsMNZj9fApSnxK ag5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=M8HoEQ5oqUBzgSiPkg41KaPcHxMWLCXEpmxQRlUQgmQ=; b=xJxzdHd+unqm5FDHyMQMUS8LZfdohNNmM2WGDwk2zVbkY3bevEmSWO34myk8H758vT Vvq7RWaxgTA5spOJ6+CFqtOxt44if+ZnJKMhhohWFtyCOTQ2b/a4fzq6LzE4kFIX+mRX 0LEQ1euAlmEp5aQNaezJv3iZgA6AEURt8xWo1/NXAVmt1ZWQnH2APfWuuVfbZyxuMbDf 16G5vhSK6xa/CqO2ez9g9v9V6nkUBPlJEcBkSteogA/2AvkcsU3ZAIo72VE6UZrHdFS+ dTwzrh6tnKxlwMWWqnA240t4Na6+vinMFhIiQ/jCTOLxBNQlOI9eRb9AbgAy0/PxR48H dDlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="nJzOtn/+"; 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 i188-20020a37b8c5000000b00746bb25f27esi10241256qkf.104.2023.04.12.04.47.31 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 12 Apr 2023 04:47:31 -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=pass header.i=@linaro.org header.s=google header.b="nJzOtn/+"; 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] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pmYtm-0006mO-MT; Wed, 12 Apr 2023 07:44:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pmYtM-000530-55 for qemu-devel@nongnu.org; Wed, 12 Apr 2023 07:44:03 -0400 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pmYtK-0002NF-9r for qemu-devel@nongnu.org; Wed, 12 Apr 2023 07:43:59 -0400 Received: by mail-wr1-x436.google.com with SMTP id s2so7310659wra.7 for ; Wed, 12 Apr 2023 04:43:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681299837; x=1683891837; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=M8HoEQ5oqUBzgSiPkg41KaPcHxMWLCXEpmxQRlUQgmQ=; b=nJzOtn/+ToFW+ybfxz8jOABM1E3vPQ8vyaeAoTbeRgrWMCEhinWPmUJNMK+tdZbeWZ Wq2WD8dWA/QCd1r/dsSZtbKlj8b4WBBZIdaHmm+Q0C1+Ud+nwzuzjj315NCyhoWOXAtl QUFi6Rs/Rt6pB56atBvvG76E9LWLbRSvz4tL6ZG9rJOf4hdWaIvoiNbM5MrDRmwAYsDj DUhygJWmP3MPUkOYyA0FgAHGw1g6OMH/QYrlYlzRzB2pScY7IpdSN/X2ZR0NWbs4AHXp rJhRx+4b00O118umgVvsMOWmY25p/CLB2CD7JuYOX/saT4o5O9T+ozvKN/9zDpAXyaDv WqCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681299837; x=1683891837; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M8HoEQ5oqUBzgSiPkg41KaPcHxMWLCXEpmxQRlUQgmQ=; b=KQgORmg/S2ZxPvV2/QWYR8CvKIbnj92WVXrIQV4c4NVY5aObvIHu0VfCKB4EeKtH+1 3pE4eLmH2ezS9AlyVxY/Bi4js5ZYdsXSyWVTwbfoQY7/QFGCPsm8JkdWOaI88LqWEwS2 616/fMitHWhMxGnioM06IWZuM7RHbLaoXS2O5PAnxNI4MHmjbRz9WE2hKYsz8Q9K/pkU TQviAONKo5DLmm07CBlD685kgvwQrdJi+4fWNGMlPt5RZIRwRNphPxoo80WLFJpdSLFW 1WU8ucnCtgc9FfApYQld5vqH5/J0GHovzRbGV4Eco29MIpgxxc68uiKL20SsRhTpbMAW 67vw== X-Gm-Message-State: AAQBX9eq+Lda1Le0JAkkVtLWqHGeff95c1UN89RzpP+FDLczoLODUomP cb2xnBq8Fs/w4f24jC0aFuHKGljZV/Kx0ZKoaoQoQ29v X-Received: by 2002:adf:e441:0:b0:2e4:bfa0:8c30 with SMTP id t1-20020adfe441000000b002e4bfa08c30mr4769296wrm.47.1681299837424; Wed, 12 Apr 2023 04:43:57 -0700 (PDT) Received: from stoup.hotel.gast (ip-037-024-010-236.um08.pools.vodafone-ip.de. [37.24.10.236]) by smtp.gmail.com with ESMTPSA id k9-20020a5d4289000000b002d21379bcabsm16980826wrq.110.2023.04.12.04.43.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 04:43:57 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alistair23@gmail.com, qemu-riscv@nongnu.org, Alistair Francis , Weiwei Li , Daniel Henrique Barboza Subject: [PATCH v7 24/25] target/riscv: Reorg access check in get_physical_address Date: Wed, 12 Apr 2023 13:43:32 +0200 Message-Id: <20230412114333.118895-25-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230412114333.118895-1-richard.henderson@linaro.org> References: <20230412114333.118895-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::436; envelope-from=richard.henderson@linaro.org; helo=mail-wr1-x436.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org We were effectively computing the protection bits twice, once while performing access checks and once while returning the valid bits to the caller. Reorg so we do this once. Move the computation of mxr close to its single use. Signed-off-by: Richard Henderson Reviewed-by: Alistair Francis Reviewed-by: Weiwei Li Tested-by: Daniel Henrique Barboza Message-Id: <20230325105429.1142530-25-richard.henderson@linaro.org> --- target/riscv/cpu_helper.c | 69 ++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index c7c384bae3..7849e18554 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -747,7 +747,7 @@ static int get_physical_address_pmp(CPURISCVState *env, int *prot, * @is_debug: Is this access from a debugger or the monitor? */ static int get_physical_address(CPURISCVState *env, hwaddr *physical, - int *prot, vaddr addr, + int *ret_prot, vaddr addr, target_ulong *fault_pte_addr, int access_type, int mmu_idx, bool first_stage, bool two_stage, @@ -779,20 +779,14 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, if (mode == PRV_M || !riscv_cpu_cfg(env)->mmu) { *physical = addr; - *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; + *ret_prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; return TRANSLATE_SUCCESS; } - *prot = 0; + *ret_prot = 0; hwaddr base; - int levels, ptidxbits, ptesize, vm, sum, mxr, widened; - - if (first_stage == true) { - mxr = get_field(env->mstatus, MSTATUS_MXR); - } else { - mxr = get_field(env->vsstatus, MSTATUS_MXR); - } + int levels, ptidxbits, ptesize, vm, sum, widened; if (first_stage == true) { if (use_background) { @@ -835,7 +829,7 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, levels = 5; ptidxbits = 9; ptesize = 8; break; case VM_1_10_MBARE: *physical = addr; - *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; + *ret_prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; return TRANSLATE_SUCCESS; default: g_assert_not_reached(); @@ -970,6 +964,27 @@ restart: return TRANSLATE_FAIL; } + int prot = 0; + if (pte & PTE_R) { + prot |= PAGE_READ; + } + if (pte & PTE_W) { + prot |= PAGE_WRITE; + } + if (pte & PTE_X) { + bool mxr; + + if (first_stage == true) { + mxr = get_field(env->mstatus, MSTATUS_MXR); + } else { + mxr = get_field(env->vsstatus, MSTATUS_MXR); + } + if (mxr) { + prot |= PAGE_READ; + } + prot |= PAGE_EXEC; + } + if ((pte & PTE_U) && ((mode != PRV_U) && (!sum || access_type == MMU_INST_FETCH))) { /* @@ -982,17 +997,9 @@ restart: /* Supervisor PTE flags when not S mode */ return TRANSLATE_FAIL; } - if (access_type == MMU_DATA_LOAD && - !((pte & PTE_R) || ((pte & PTE_X) && mxr))) { - /* Read access check failed */ - return TRANSLATE_FAIL; - } - if (access_type == MMU_DATA_STORE && !(pte & PTE_W)) { - /* Write access check failed */ - return TRANSLATE_FAIL; - } - if (access_type == MMU_INST_FETCH && !(pte & PTE_X)) { - /* Fetch access check failed */ + + if (!((prot >> access_type) & 1)) { + /* Access check failed */ return TRANSLATE_FAIL; } @@ -1057,20 +1064,16 @@ restart: (vpn & (((target_ulong)1 << ptshift) - 1)) ) << PGSHIFT) | (addr & ~TARGET_PAGE_MASK); - /* set permissions on the TLB entry */ - if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) { - *prot |= PAGE_READ; - } - if (pte & PTE_X) { - *prot |= PAGE_EXEC; - } /* - * Add write permission on stores or if the page is already dirty, - * so that we TLB miss on later writes to update the dirty bit. + * Remove write permission unless this is a store, or the page is + * already dirty, so that we TLB miss on later writes to update + * the dirty bit. */ - if ((pte & PTE_W) && (access_type == MMU_DATA_STORE || (pte & PTE_D))) { - *prot |= PAGE_WRITE; + if (access_type != MMU_DATA_STORE && !(pte & PTE_D)) { + prot &= ~PAGE_WRITE; } + *ret_prot = prot; + return TRANSLATE_SUCCESS; }