From nobody Wed Nov 27 04:54:36 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=syntacore.com ARC-Seal: i=1; a=rsa-sha256; t=1700551157; cv=none; d=zohomail.com; s=zohoarc; b=eif0YRjsymuCDOLLt/C3mD9vKQBHF4GOzcOBzPvkMN2NoKv+XGNVwvrQ/1k344TI7t8+EMf3R10EcrDpqp50t2UkhBtnR0D/0ntiVAi07B2ZHG1MOD3n5xbOnafoQiXE2e7zzOj8u4vnWD66rn8G6FyGyyIghxRLnVxM4MnyuJc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1700551157; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xJISpx4rx9AoHbDVBMOwJVn71cS/lx+g2kjhdfZOPT4=; b=Z86zbvm5U/QA51+6vuGapKPAOyuyljZjmPrAKiRtFOoxaudgOCVuy+Fl+VUQD/+FpSiYXpLPub55STEuSUKUv5RHvCzjnL53bKvK2nq+/dgba33Oblqqh8gcazHA9eeTz/ZBuEZcmbNxe+IKl8xFovK51UKhDSwK5XbgNr2fJec= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17005511576161007.1872997316199; Mon, 20 Nov 2023 23:19:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r5L1U-0007GH-Db; Tue, 21 Nov 2023 02:18:16 -0500 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 1r5L1R-0007EP-GF; Tue, 21 Nov 2023 02:18:13 -0500 Received: from mta-04.yadro.com ([89.207.88.248]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r5L1O-0003j9-KC; Tue, 21 Nov 2023 02:18:13 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 mta-04.yadro.com C5F7FC0002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=syntacore.com; s=mta-04; t=1700551087; bh=xJISpx4rx9AoHbDVBMOwJVn71cS/lx+g2kjhdfZOPT4=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=Dejf1RVyz+z1rzVbwBFRTdoxDjNHswDPLykO1z6vEBLncjM53YhJgrC/G1T9fJrs/ j333g2u/szaPsaGSx/zt2sfdTkP6jGP8mwLU1jwQEK0Pk8J75L1gdluEedz5B0QXNG t8XubIg1u9w34Kk4XNKkbIZ8LO2nSGrVjT/ET650ktqA2IwxKIeZNRYgOqNVJtgBU/ T+tjTEQ5G22YlbbgxBW73L5e7XuoPvrY+4diKv0Ev2X/EMnEWQMBsDNw0JTmySlhIs 6bpj6/7+GOf9yZtTJBXsUcjkxB1Zch8reaeJw9TuJZEN55DeEJC9LpAJgeJizE4p29 sv2utYVERiYIg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=syntacore.com; s=mta-03; t=1700551087; bh=xJISpx4rx9AoHbDVBMOwJVn71cS/lx+g2kjhdfZOPT4=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=sfZ3PJQ2x+PtyxXwcExTIQqSLuI0jF2hKASHv0Kk6Qc5nFFH+fdIiOTMvpcKXYMEf jf2SanA4A0Ml4R/bPfDAHuj3KTJcCTOQOPxQUu6meFwD8sb5ORUsVq+D+8pExC585g JfaTwPpwqQUXuLhyfgtsbFDZz2LwjHh+d4mTrL20echrufKEdvypuNnijbwi2YInAt 4ZAS+BlOh7I3SKrMC8Ieco+94M3Rnw4Cg+MHef0gFBgJZS+OLSLBY2LYzypm7OG/d+ i5GBZMW6YSTyGX3noCjAmx7H/bl7kuvb7Smf6u2NvVABwrk7pB2+f9TFFjPNoG3EmD aDuSch3+y+szw== From: Ivan Klokov To: CC: , , , , , , , Ivan Klokov Subject: [PATCH v2 1/2] target/riscv/cpu_helper.c: Invalid exception on MMU translation stage Date: Tue, 21 Nov 2023 10:17:56 +0300 Message-ID: <20231121071757.7178-2-ivan.klokov@syntacore.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231121071757.7178-1-ivan.klokov@syntacore.com> References: <20231121071757.7178-1-ivan.klokov@syntacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: T-EXCH-08.corp.yadro.com (172.17.11.58) To S-Exch-01.corp.yadro.com (10.78.5.241) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: permerror client-ip=89.207.88.248; envelope-from=ivan.klokov@syntacore.com; helo=mta-04.yadro.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, SPF_HELO_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_PERMERROR=0.01 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @syntacore.com) (identity @syntacore.com) X-ZM-MESSAGEID: 1700551158940100002 Content-Type: text/plain; charset="utf-8" According to RISCV privileged spec sect. 5.3.2 Virtual Address Translation = Process access-fault exceptions may raise only after PMA/PMP check. Current impleme= ntation generates an access-fault for mbare mode even if there were no PMA/PMP erro= rs. This patch removes the erroneous MMU mode check and generates an access-fau= lt exception based on the pmp_violation flag only. Fixes: 1448689c7b ("target/riscv: Allow specifying MMU stage") Signed-off-by: Ivan Klokov Reviewed-by: Alistair Francis Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu_helper.c | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index b7af69de53..9ff0952e46 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1143,47 +1143,31 @@ static void raise_mmu_exception(CPURISCVState *env,= target_ulong address, bool two_stage_indirect) { CPUState *cs =3D env_cpu(env); - int page_fault_exceptions, vm; - uint64_t stap_mode; - - if (riscv_cpu_mxl(env) =3D=3D MXL_RV32) { - stap_mode =3D SATP32_MODE; - } else { - stap_mode =3D SATP64_MODE; - } - - if (first_stage) { - vm =3D get_field(env->satp, stap_mode); - } else { - vm =3D get_field(env->hgatp, stap_mode); - } - - page_fault_exceptions =3D vm !=3D VM_1_10_MBARE && !pmp_violation; =20 switch (access_type) { case MMU_INST_FETCH: if (env->virt_enabled && !first_stage) { cs->exception_index =3D RISCV_EXCP_INST_GUEST_PAGE_FAULT; } else { - cs->exception_index =3D page_fault_exceptions ? - RISCV_EXCP_INST_PAGE_FAULT : RISCV_EXCP_INST_ACCESS_FAULT; + cs->exception_index =3D pmp_violation ? + RISCV_EXCP_INST_ACCESS_FAULT : RISCV_EXCP_INST_PAGE_FAULT; } break; case MMU_DATA_LOAD: if (two_stage && !first_stage) { cs->exception_index =3D RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT; } else { - cs->exception_index =3D page_fault_exceptions ? - RISCV_EXCP_LOAD_PAGE_FAULT : RISCV_EXCP_LOAD_ACCESS_FAULT; + cs->exception_index =3D pmp_violation ? + RISCV_EXCP_LOAD_ACCESS_FAULT : RISCV_EXCP_LOAD_PAGE_FAULT; } break; case MMU_DATA_STORE: if (two_stage && !first_stage) { cs->exception_index =3D RISCV_EXCP_STORE_GUEST_AMO_ACCESS_FAUL= T; } else { - cs->exception_index =3D page_fault_exceptions ? - RISCV_EXCP_STORE_PAGE_FAULT : - RISCV_EXCP_STORE_AMO_ACCESS_FAULT; + cs->exception_index =3D pmp_violation ? + RISCV_EXCP_STORE_AMO_ACCESS_FAULT : + RISCV_EXCP_STORE_PAGE_FAULT; } break; default: --=20 2.34.1 From nobody Wed Nov 27 04:54:36 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=syntacore.com ARC-Seal: i=1; a=rsa-sha256; t=1700551157; cv=none; d=zohomail.com; s=zohoarc; b=C2Q8QGGUgAQXfyoduEOapazr+opuDxAOrpZdlwZ5pThh9y8oKaYHfIYy6OM8XF/rqFfzcZ3Q/rt7tEwI7Ecqq/+zrp6D39sUFsfwhYRluuivv8+51Q6O+lOXwJ6OH7RVmVM98rfEIOGZ+Af7qmiZd4Zw3mGsRZyqv1FNR/y0TJc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1700551157; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+tm80IOxtZrRk6/aiCijbvq3pGsJG2aqKhn9wVIaYIA=; b=KhWfOSpk2lNILoTadA3P87pMp1j/KtuudJNOnGQ80dK9HMvc7uuPWQWREIyibGD3LdNtabm9+08HiIxhMN8el099aQqy06rO9pc8T/fJIuptHX/eQqXBnihzHlQzbsB+7JL884K+lHRgTsZCn5+ienf3n3rV++4x44OzEjwT9tQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1700551157702531.7989851953189; Mon, 20 Nov 2023 23:19:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r5L1W-0007Hw-Ud; Tue, 21 Nov 2023 02:18:18 -0500 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 1r5L1S-0007F8-S4; Tue, 21 Nov 2023 02:18:15 -0500 Received: from mta-04.yadro.com ([89.207.88.248]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r5L1Q-0003jI-BB; Tue, 21 Nov 2023 02:18:14 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 mta-04.yadro.com 5071AC0002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=syntacore.com; s=mta-04; t=1700551089; bh=+tm80IOxtZrRk6/aiCijbvq3pGsJG2aqKhn9wVIaYIA=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=MplioIrU+h2ybygkNrUDiC16SM3ADAy8kFIT74EpEu9cQoceXpqPnvjOM8aTcImOx cFpNaOGWvWYGXv96nio8bmpBOtOB5lAmHmM3g4UK5+CRtZjhgDGxXsh9cjhm83WM13 YZkBIqKpxDWfOd2SqqjgpsvgLjyQKwtmoO/YPd6CXpLwHXCAH7irhCNxY+DCnonWsK ef8GcZDJVekaCg8SX7QMyRuDkhJ4IZKVCMnnukV6JuzzCtMkijPzYX0FcxZz5TIGER S8rl84+IXrXGFXiftYStFDYJLjJ4/eyo3oPBZsmwYWHe9uN92aa+8YGOpQ4Ofc0+V1 /d5SCEXOjJwnA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=syntacore.com; s=mta-03; t=1700551089; bh=+tm80IOxtZrRk6/aiCijbvq3pGsJG2aqKhn9wVIaYIA=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=TYtB53R47hfsRGHGNbdbb0ND+Se2uWrOW31vgfaB+QdxTA/Aaz5GAulOzk6YtuwEr bQtFEin48C4gY7nCfgF9zCiYEDaMJofteFPHc6y8XKa5YhRSghjmj4NuuqUVWlTiDs La0LA4ggsdACqPdK4tHUZ8eUttgPUy2nJ5xK3GQiGsVNNRvPf3/X7LrmAnv7zlxl12 U0TaLS03x7PjcPg8gQOnJnIy0oWqoAy4Bwdamp3ea9hiRBt1cK4h22fj0bHOjyVhsE CMBOclstyGuBAPQjy+Pta3y3ijdEKWStF7DDz8RUmKUuh07td8aclRcr1O6ncwsbvL 6mLLP6e1vD3CA== From: Ivan Klokov To: CC: , , , , , , , Ivan Klokov Subject: [PATCH v2 2/2] target/riscv/cpu_helper.c: Fix mxr bit behavior Date: Tue, 21 Nov 2023 10:17:57 +0300 Message-ID: <20231121071757.7178-3-ivan.klokov@syntacore.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231121071757.7178-1-ivan.klokov@syntacore.com> References: <20231121071757.7178-1-ivan.klokov@syntacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: T-EXCH-08.corp.yadro.com (172.17.11.58) To S-Exch-01.corp.yadro.com (10.78.5.241) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: permerror client-ip=89.207.88.248; envelope-from=ivan.klokov@syntacore.com; helo=mta-04.yadro.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, SPF_HELO_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_PERMERROR=0.01 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @syntacore.com) (identity @syntacore.com) X-ZM-MESSAGEID: 1700551158995100005 Content-Type: text/plain; charset="utf-8" According to RISCV Specification sect 9.5 on two stage translation when V=3D1 the vsstatus(mstatus in QEMU's terms) field MXR, which makes execute-only pages readable, only overrides VS-stage page protection. Setting MXR at HS-level(mstatus_hs), however, overrides both VS-stage and G-stage execute-only permissions. The hypervisor extension changes the behavior of MXR\MPV\MPRV bits. Due to RISCV Specification sect. 9.4.1 when MPRV=3D1, explicit memory accesses are translated and protected, and endianness is applied, as though the current virtualization mode were set to MPV and the current nominal privilege mode were set to MPP. vsstatus.MXR makes readable those pages marked executable at the VS translation stage. Fixes: 36a18664ba ("target/riscv: Implement second stage MMU") Signed-off-by: Ivan Klokov Reviewed-by: Alistair Francis Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu_helper.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 9ff0952e46..e7e23b34f4 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1032,13 +1032,29 @@ restart: prot |=3D PAGE_WRITE; } if (pte & PTE_X) { - bool mxr; + bool mxr =3D false; =20 - if (first_stage =3D=3D true) { + /* + * Use mstatus for first stage or for the second stage without + * virt_enabled (MPRV+MPV) + */ + if (first_stage || !env->virt_enabled) { mxr =3D get_field(env->mstatus, MSTATUS_MXR); - } else { - mxr =3D get_field(env->vsstatus, MSTATUS_MXR); } + + /* MPRV+MPV case, check VSSTATUS */ + if (first_stage && two_stage && !env->virt_enabled) { + mxr |=3D get_field(env->vsstatus, MSTATUS_MXR); + } + + /* + * Setting MXR at HS-level overrides both VS-stage and G-stage + * execute-only permissions + */ + if (env->virt_enabled) { + mxr |=3D get_field(env->mstatus_hs, MSTATUS_MXR); + } + if (mxr) { prot |=3D PAGE_READ; } --=20 2.34.1