From nobody Thu Nov 28 08:05:23 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1693845003; cv=none; d=zohomail.com; s=zohoarc; b=FfDSP8lW6ePKo3JB1Jcq9o/Tj9iKcbGjYX1oSXNZLtAbIx0WrLUFDSXOyqU6CrrLYAl12TtWWJMRO0BI4f0qB4RgYO1TMSNYoCG0lbLFsM2P8dp95AJfn8UUOQDPKXjXw10NIGJpOKgWNPTe0TU4vGIT3YuXZ+caSxZG4Elrqq8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693845003; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=7Q80VNS5mMLiY+2ZB4hkhFDuko3FgyE8q1ZevVSDvMU=; b=Ez6d606m0OzTYvrSJjVnrMgRBBAkW81akP1KYLQPq33CCKNgGpH07cc/Viys6f/mvE/vOemPP5ONOze9NbTRmN8Vo45ewIrRCfalzf588kPzAUPrNzkTg9hzMbah8rOxO0TDnX7JT4bBfuHOIEXL7uWkBHr0Vpxp+jdUyTLFE88= 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 1693845003631630.9574964742053; Mon, 4 Sep 2023 09:30:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qdCRi-0002eZ-1T; Mon, 04 Sep 2023 12:29:02 -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 1qdCJI-0005Z8-68; Mon, 04 Sep 2023 12:20:22 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qdCJD-0000lo-BR; Mon, 04 Sep 2023 12:20:19 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1bee82fad0fso8926635ad.2; Mon, 04 Sep 2023 09:20:13 -0700 (PDT) Received: from localhost.localdomain (36-226-199-198.dynamic-ip.hinet.net. [36.226.199.198]) by smtp.gmail.com with ESMTPSA id l6-20020a170903244600b001b03f208323sm7735361pls.64.2023.09.04.09.20.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Sep 2023 09:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693844412; x=1694449212; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=7Q80VNS5mMLiY+2ZB4hkhFDuko3FgyE8q1ZevVSDvMU=; b=SvNu6+PCJG+RiWhrkbdmQ6ZWHhOsy2lHk2IBmYka5+CP3HLkbBZu45i4VScZbcTLn4 SsCx/Fr+stcBuWrDfCn3pPzEbYdUEvbzklDNoLuOwUQyL0mUS3vFErHChG92OxGJu6uo ioji5edEb5fj+aAnFw2lWbeRlTG+ncgETPgSd+nUG8HBMxA2DgpH8omEtNzXxhrHkKkE 9o9e+E8ZiJ9TXdq9xkkQshTAUzkRCQ6EVCRzpR96zptfY549lRILLqqzkode7J+7iJ9s 95qWsQ5i/Z8HGzRMsO2kGNZSM75hTOqgJtNZ/nh4LpE1vM4NkRjyURyJia+D87cc0zXz ncEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693844412; x=1694449212; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7Q80VNS5mMLiY+2ZB4hkhFDuko3FgyE8q1ZevVSDvMU=; b=ZJjYfNGE11bvw3ep0yLPcIX/udbi1eVdqbJpagPoDljeQFq/zZ3sGOa1uXDW5X2yM9 4izVRfnI6KV+uprMXbcKHaTh2ELg0AK6S3CNfOBiT6oVmt0xQvOecbZLd+FKdZLfCSlR 52VJ7HIpoV6ahDzKZqgS75fLUd9SuheTr76bdkn3uXoBtkamltBbO1WTDzDbOuDRPjQ9 j1xs/y6rqZYFAcIiO8UaKj++EJjia9+dG2OE8RNxoc3EntXLbhsrx88EPAlfLvtpudou cDIlv14ILYvtgtjhJz42ze/eIXq0k9K2DjBfDwtADbBk0bW8hADBbz8BzoQ/jNIeOF/U PuHw== X-Gm-Message-State: AOJu0YyUpWeNGQWRHLoDlJOnaeDSnSvfkx2nUfZ715+SHQ00rTxhBDvi Cm361i1AuzGvRfiqovp6ePIDTzlU/Jf9e6Rh X-Google-Smtp-Source: AGHT+IHP6JSgsfOHYEBohgn2sEQ257KS5+SQZBfYZGxFpqt/YMbkyvFrZzU85LkvIx29bGsnWvZwEg== X-Received: by 2002:a17:902:cec5:b0:1b7:e49f:1d with SMTP id d5-20020a170902cec500b001b7e49f001dmr10121261plg.62.1693844412344; Mon, 04 Sep 2023 09:20:12 -0700 (PDT) From: Alvin Chang X-Google-Original-From: Alvin Chang To: qemu-riscv@nongnu.org, qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, Alvin Chang Subject: [PATCH] target/riscv: update checks on writing pmpcfg for ePMP to version 1.0 Date: Tue, 5 Sep 2023 00:19:59 +0800 Message-Id: <20230904161959.1766-1-alvinga@andestech.com> X-Mailer: git-send-email 2.27.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=2607:f8b0:4864:20::62f; envelope-from=vivahavey@gmail.com; helo=mail-pl1-x62f.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, FREEMAIL_FROM=0.001, 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-Mailman-Approved-At: Mon, 04 Sep 2023 12:28:59 -0400 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 @gmail.com) X-ZM-MESSAGEID: 1693845005284100001 Content-Type: text/plain; charset="utf-8" Current checks on writing pmpcfg for ePMP follows ePMP version 0.9.1. However, ePMP 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 ePMP 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 ePMP truth table, and checks the rules by aforementioned specification changes. Signed-off-by: Alvin Chang --- target/riscv/pmp.c | 51 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index a08cd95658..c036ca3e70 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -99,16 +99,49 @@ static void 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) { - locked =3D false; - } - /* shared region and not adding X bit */ - if ((val & PMP_LOCK) !=3D PMP_LOCK && - (val & 0x7) !=3D (PMP_WRITE | PMP_EXEC)) { + /* + * 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 + * ePMP spec. + */ + const uint8_t epmp_operation =3D + ((val & PMP_LOCK) >> 4) | ((val & PMP_READ) << 2) | + (val & PMP_WRITE) | ((val & PMP_EXEC) >> 2); + + switch (epmp_operation) { + /* pmpcfg.L =3D 0. Neither M-mode-only nor locked Shared-R= egion */ + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + /* pmpcfg.L =3D 1 and pmpcfg.X =3D 0 (but case 10 is not a= llowed) */ + case 8: + case 12: + case 14: + /* pmpcfg.LRWX =3D 1111 */ + case 15: /* Read-only locked Shared-Region on all modes */ locked =3D false; + break; + /* Other rules which add new code regions are not allowed = */ + case 9: + case 10: /* Execute-only locked Shared-Region on all mode= s */ + case 11: + case 13: + break; + default: + g_assert_not_reached(); } } } else { --=20 2.34.1