From nobody Fri May 17 09:18:43 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1699928666; cv=none; d=zohomail.com; s=zohoarc; b=lUioWjsSGxe/y4/devcR5etMAFnNAoFND82C6cqe3zql2tRQhZW8flKBzkpKSP0f/Y8rbdel88oRA8e6Q3aam6dtsq4qCtF9edj4z0uyXXKNH26LYmKxuV16gDh74zInFnNgvYAUImQnIHW1N23vGnUAxY+3mr2jMUMcPvnSo88= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699928666; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=7OQ7ViQE0g91AIRd0f9zOevJiCon7okfTUhDPWl5t9o=; b=I17kE31ppdX/MHd4pfAlqq998yDUwE1vrE2aPGHALwny4P2VC0mRv7dkP5/zA52442nP1DMgkDpknVx8Z9lOKDD0OvaBg0zu3ODuSbi9nRwEro722lf7GE9RguFFF48Tv1BZ7mo4B+1NdZil64/mUan3OiSG2jCoNsVahDJDa1Q= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 1699928666570839.6257966349747; Mon, 13 Nov 2023 18:24:26 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r2j5Q-0005Xm-9B; Mon, 13 Nov 2023 21:23:32 -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 1r2j5O-0005XY-T3; Mon, 13 Nov 2023 21:23:30 -0500 Received: from 60-248-80-70.hinet-ip.hinet.net ([60.248.80.70] helo=Atcsqr.andestech.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r2j5L-0001qn-VD; Mon, 13 Nov 2023 21:23:30 -0500 Received: from mail.andestech.com (ATCPCS16.andestech.com [10.0.1.222]) by Atcsqr.andestech.com with ESMTP id 3AE2N9fY043538; Tue, 14 Nov 2023 10:23:09 +0800 (+08) (envelope-from alvinga@andestech.com) Received: from alvinga-VirtualBox.andestech.com (10.0.13.65) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.498.0; Tue, 14 Nov 2023 10:23:07 +0800 To: , CC: , , Alvin Chang Subject: [PATCH v5] target/riscv: update checks on writing pmpcfg for Smepmp to version 1.0 Date: Tue, 14 Nov 2023 10:22:54 +0800 Message-ID: <20231114022254.294711-1-alvinga@andestech.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.0.13.65] X-DNSRBL: X-SPAM-SOURCE-CHECK: pass X-MAIL: Atcsqr.andestech.com 3AE2N9fY043538 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: pass client-ip=60.248.80.70; envelope-from=alvinga@andestech.com; helo=Atcsqr.andestech.com X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, RDNS_DYNAMIC=0.982, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, TVD_RCVD_IP=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: , Reply-to: Alvin Chang From: Alvin Chang via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1699928668941100001 Content-Type: text/plain; charset="utf-8" Current checks on writing pmpcfg for Smepmp follows Smepmp version 0.9.1. However, Smepmp specification has already been ratified, and there are some differences between version 0.9.1 and 1.0. In this commit we update the checks of writing pmpcfg to follow Smepmp version 1.0. When mseccfg.MML is set, the constraints to modify PMP rules are: 1. Locked rules cannot be removed or modified until a PMP reset, unless mseccfg.RLB is set. 2. From Smepmp specification version 1.0, chapter 2 section 4b: Adding a rule with executable privileges that either is M-mode-only or a locked Shared-Region is not possible and such pmpcfg writes are ignored, leaving pmpcfg unchanged. The commit transfers the value of pmpcfg into the index of the Smepmp truth table, and checks the rules by aforementioned specification changes. Signed-off-by: Alvin Chang --- Changes from v4: Rebase on master. Changes from v3: Modify "epmp_operation" to "smepmp_operation". Changes from v2: Adopt switch case ranges and numerical order. Changes from v1: Convert ePMP over to Smepmp. target/riscv/pmp.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index 162e88a90a..4069514069 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -102,16 +102,40 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_= t pmp_index, uint8_t val) locked =3D false; } =20 - /* mseccfg.MML is set */ - if (MSECCFG_MML_ISSET(env)) { - /* not adding execute bit */ - if ((val & PMP_LOCK) !=3D 0 && (val & PMP_EXEC) !=3D PMP_E= XEC) { + /* + * mseccfg.MML is set. Locked rules cannot be removed or modif= ied + * until a PMP reset. Besides, from Smepmp specification versi= on 1.0 + * , chapter 2 section 4b says: + * Adding a rule with executable privileges that either is + * M-mode-only or a locked Shared-Region is not possible and s= uch + * pmpcfg writes are ignored, leaving pmpcfg unchanged. + */ + if (MSECCFG_MML_ISSET(env) && !pmp_is_locked(env, pmp_index)) { + /* + * Convert the PMP permissions to match the truth table in= the + * Smepmp spec. + */ + const uint8_t smepmp_operation =3D + ((val & PMP_LOCK) >> 4) | ((val & PMP_READ) << 2) | + (val & PMP_WRITE) | ((val & PMP_EXEC) >> 2); + + switch (smepmp_operation) { + case 0 ... 8: locked =3D false; - } - /* shared region and not adding X bit */ - if ((val & PMP_LOCK) !=3D PMP_LOCK && - (val & 0x7) !=3D (PMP_WRITE | PMP_EXEC)) { + break; + case 9 ... 11: + break; + case 12: + locked =3D false; + break; + case 13: + break; + case 14: + case 15: locked =3D false; + break; + default: + g_assert_not_reached(); } } } else { --=20 2.34.1