From nobody Thu Apr 3 10:11:41 2025 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 ARC-Seal: i=1; a=rsa-sha256; t=1741894354; cv=none; d=zohomail.com; s=zohoarc; b=DkqqFRIqSZxOGh0vxG4ufGyRlmOfqeDNOGjy6IMyg7cQdXTJ91hGwtL1Itqu+LkdRMEiF3PLci2ZdvDRCOVopZ+/+CLXWPp844wsEWBtZxP3olrE0NhZjAUSnDoe2D2VeGFMP5BBohNeqwD9XKx6E8XHpzWNmJickZ94M6HAux0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741894354; 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=pJ2zQvGxiUYLcbhnAMz/lOTZv04zTOkZOKcwj4LSpp8=; b=QY+/MiSmwjgOY/lstRI/JBfc9rpUk/cmEHR/ALURKeumbyvZiYLK9Ug59+vrM0Z7XlX2ZlEBGFznZn1KWuauqIWxNfuJrR5QwelG2zDsFYKcX4m5FbgaieZbAfuVOMao0VWFYEkKMH0Ad9twyh1kUBwiI2zrPttfx0JjLEmj+xo= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17418943549631014.8036242066613; Thu, 13 Mar 2025 12:32:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tsoGb-00020A-HH; Thu, 13 Mar 2025 15:30:53 -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 1tsoGA-0001qa-Kp for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:30 -0400 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tsoG1-0001fW-0M for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:23 -0400 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-3912baafc58so1194536f8f.1 for ; Thu, 13 Mar 2025 12:30:15 -0700 (PDT) Received: from llx1.ba.rivosinc.com (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-395c888152dsm3016421f8f.48.2025.03.13.12.30.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Mar 2025 12:30:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741894214; x=1742499014; darn=nongnu.org; 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=pJ2zQvGxiUYLcbhnAMz/lOTZv04zTOkZOKcwj4LSpp8=; b=SyNvEofDK4KfJCLeGTO7URRqFbBSE6RN/zAML7wDWm2vBkq1UkvMwDTGCMGSuEHK3l 27IGjJ3MsGbahaCjAs4Y0hd39z/LMNJPw8u670Z2UX56aYg+bn+XZY/5D04cMG8INI8Y hv7WYu6a1Ts5VwyYgIdV9iiybQMKLoynTbO98y0Wu6gh5bY5eAUMMJSAO0MNRnu38z2g iq2KHCY6gc9bVK7fWLTLyEPE/zzYksguwmmdij3thwOvJlBE9rR9p26aIAQLoOwEaoGp JVdFucr8Y3VBfj1JYIQ5vRDjU39BZzG1o32I3iC9jYzUljggPK7QXB4uWIoD9SKF9cr0 mlKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741894214; x=1742499014; 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=pJ2zQvGxiUYLcbhnAMz/lOTZv04zTOkZOKcwj4LSpp8=; b=fGxkkju8XXmdEi0HrZQcAWbU65NSjdDriTQ6aHPfH1gt3GEi2Yna68dYl31qxz9HHP ps5iUbWvOhNVngJLsyuKi6kaK0PKQLHLerw9Ys1lJIP/TqjbAsCmGyAnWWG0jscAMS4w kgUY1RRiNfWaC5dUD6v0WoQ1y0HNOST2rklNSsmBh/1eeq+Q/5ypK/UQHgNNeBk5X2gm o4fMefY11TV7/W8D0HMaUtmDXjY3rQz0XegokbfAN0bFPH5sBrf0sesIHU6/Tkash+2W TjOtBb8nDbau+u3Cc2py+rLt2Dju/GtfhDneTBC5D9V9sT9CXMQlqNqqUTkGBzkp5VKF 7R7Q== X-Gm-Message-State: AOJu0Yy1CKjkIFPSEwR7jurLILEPeOoVkPnophGOHDuazxTQpUymiDhK RD/zyvWb7MhXCx6HDDh4/x1R1huDYCCZz2QDjWLGUeqFGsPnmzH1sXKBqxVOlTSV4Q3FLEZXd+r EWpq9gmaY X-Gm-Gg: ASbGnctjvIWPxNIzD13sUPI80Ec8AzgroKpQ74k60aOOP97Jo+GlxIEf+h1mWGSG3iV 6SQJYmYqqJFJ+SbDWOBMKKFVqrliSC51lA84SmdEsETwSwfZjtzXry98LngStX/33J/2TI/88Yn ml/LSttDNPiQbwVYaggejW0tA0ztWNyhYwXRE4P5vhJJr4xXWKObsCMsRRXtEix9Em0Trx7c/VC wZxkBA0QmyZMDv5QPwY8MnGMG2Tz57CqixOwyBrUDN2wVfENHsqGP1SBMoFHhWQf7Mg7riDhdDf C/8HK9B9m2YaDRe9qr4S450J8/RLghNXlR7I4t78BnCso+KJlrRH3b3R1LD0C12W8zJIQneLb5L /FsObwWJRPXCsexcYKdC/vGOd5mWRzw== X-Google-Smtp-Source: AGHT+IEZFrxt4Idjbjow2fyvnjTXhxcf+2Ikvrzaj2PCqhYzjIfUcaexPnBNftzUv63Zhtucr1hRSQ== X-Received: by 2002:a05:6000:1f88:b0:390:dfa1:3448 with SMTP id ffacd0b85a97d-396c3838e83mr1008128f8f.43.1741894213834; Thu, 13 Mar 2025 12:30:13 -0700 (PDT) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Liu Zhiwei , Weiwei Li , qemu-riscv@nongnu.org, Palmer Dabbelt , Alistair Francis , Daniel Henrique Barboza , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH v2 1/5] target/riscv: pmp: don't allow RLB to bypass rule privileges Date: Thu, 13 Mar 2025 20:30:07 +0100 Message-ID: <20250313193011.720075-2-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250313193011.720075-1-loic@rivosinc.com> References: <20250313193011.720075-1-loic@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2a00:1450:4864:20::435; envelope-from=loic@rivosinc.com; helo=mail-wr1-x435.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1741894357307019100 When Smepmp is supported, mseccfg.RLB allows bypassing locks when writing C= SRs but should not affect interpretation of actual PMP rules. This is not the case with the current implementation where pmp_hart_has_pri= vs calls pmp_is_locked which implements mseccfg.RLB bypass. This commit implements the correct behavior by removing mseccfg.RLB bypass = from pmp_is_locked. RLB bypass when writing CSRs is implemented by adding a new pmp_is_readonly function that calls pmp_is_locked and check mseccfg.RLB. pmp_write_cfg and pmpaddr_csr_write are changed to use this new function. Signed-off-by: Lo=C3=AFc Lefort Reviewed-by: Daniel Henrique Barboza Reviewed-by: LIU Zhiwei=C2=A0 --- target/riscv/pmp.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index b0841d44f4..e1e5ca589e 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -45,11 +45,6 @@ static inline uint8_t pmp_get_a_field(uint8_t cfg) */ static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index) { - /* mseccfg.RLB is set */ - if (MSECCFG_RLB_ISSET(env)) { - return 0; - } - if (env->pmp_state.pmp[pmp_index].cfg_reg & PMP_LOCK) { return 1; } @@ -62,6 +57,15 @@ static inline int pmp_is_locked(CPURISCVState *env, uint= 32_t pmp_index) return 0; } =20 +/* + * Check whether a PMP is locked for writing or not. + * (i.e. has LOCK flag and mseccfg.RLB is unset) + */ +static int pmp_is_readonly(CPURISCVState *env, uint32_t pmp_index) +{ + return pmp_is_locked(env, pmp_index) && !MSECCFG_RLB_ISSET(env); +} + /* * Count the number of active rules. */ @@ -90,39 +94,38 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, = uint32_t pmp_index) static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t = val) { if (pmp_index < MAX_RISCV_PMPS) { - bool locked =3D true; + bool readonly =3D true; =20 if (riscv_cpu_cfg(env)->ext_smepmp) { /* mseccfg.RLB is set */ if (MSECCFG_RLB_ISSET(env)) { - locked =3D false; + readonly =3D false; } =20 /* mseccfg.MML is not set */ - if (!MSECCFG_MML_ISSET(env) && !pmp_is_locked(env, pmp_index))= { - locked =3D false; + if (!MSECCFG_MML_ISSET(env) && !pmp_is_readonly(env, pmp_index= )) { + readonly =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; + readonly =3D false; } /* shared region and not adding X bit */ if ((val & PMP_LOCK) !=3D PMP_LOCK && (val & 0x7) !=3D (PMP_WRITE | PMP_EXEC)) { - locked =3D false; + readonly =3D false; } } } else { - if (!pmp_is_locked(env, pmp_index)) { - locked =3D false; - } + readonly =3D pmp_is_readonly(env, pmp_index); } =20 - if (locked) { - qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpcfg write - locked= \n"); + if (readonly) { + qemu_log_mask(LOG_GUEST_ERROR, + "ignoring pmpcfg write - read only\n"); } else if (env->pmp_state.pmp[pmp_index].cfg_reg !=3D val) { /* If !mseccfg.MML then ignore writes with encoding RW=3D01 */ if ((val & PMP_WRITE) && !(val & PMP_READ) && @@ -524,14 +527,14 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t a= ddr_index, uint8_t pmp_cfg =3D env->pmp_state.pmp[addr_index + 1].cfg_reg; is_next_cfg_tor =3D PMP_AMATCH_TOR =3D=3D pmp_get_a_field(pmp_= cfg); =20 - if (pmp_is_locked(env, addr_index + 1) && is_next_cfg_tor) { + if (pmp_is_readonly(env, addr_index + 1) && is_next_cfg_tor) { qemu_log_mask(LOG_GUEST_ERROR, - "ignoring pmpaddr write - pmpcfg + 1 locked\= n"); + "ignoring pmpaddr write - pmpcfg+1 read only= \n"); return; } } =20 - if (!pmp_is_locked(env, addr_index)) { + if (!pmp_is_readonly(env, addr_index)) { if (env->pmp_state.pmp[addr_index].addr_reg !=3D val) { env->pmp_state.pmp[addr_index].addr_reg =3D val; pmp_update_rule_addr(env, addr_index); @@ -542,7 +545,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t add= r_index, } } else { qemu_log_mask(LOG_GUEST_ERROR, - "ignoring pmpaddr write - locked\n"); + "ignoring pmpaddr write - read only\n"); } } else { qemu_log_mask(LOG_GUEST_ERROR, --=20 2.47.2 From nobody Thu Apr 3 10:11:41 2025 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 ARC-Seal: i=1; a=rsa-sha256; t=1741894367; cv=none; d=zohomail.com; s=zohoarc; b=ahqzFP6hoWUosF4BihXyh2lVtvGE6UovisTpe5TH1z62Eja+saIdbW10x+eTc0tf0p4hGe74h30D63yOPLd3PcgBlV/Y04ZVQ685L+IeHzToSyK22UJTfrA/VfGHRnkkNEviPmg8K1k/0ubZ5D3PS5z1IXOg2ZlSOSb0t+m3OiY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741894367; 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=PKdxEPN0jl1qSlAU9JCgLxS3U91sP+GzkrHS5Ut6yAc=; b=DgERxKKwInidBOvMpBszsOS+mgq0q9LNQD/EdiYsQ33T8OsVgBMr3RugwM4NWDPsh34vEdys89cl2mEdGjGa3Z5oOK1EmCHg2+A59OL6p7UDgdg1o2EieTHg44/krVqSZZxesotlQ5/zdJLPw+PRFhGoGu4pqZ6BAqr6h4r+V4k= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1741894367526126.37809252030854; Thu, 13 Mar 2025 12:32:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tsoGb-0001zv-Ec; Thu, 13 Mar 2025 15:30:53 -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 1tsoGB-0001qo-U3 for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:30 -0400 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tsoG5-0001fr-V9 for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:27 -0400 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-43cf848528aso12609395e9.2 for ; Thu, 13 Mar 2025 12:30:16 -0700 (PDT) Received: from llx1.ba.rivosinc.com (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-395c888152dsm3016421f8f.48.2025.03.13.12.30.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Mar 2025 12:30:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741894215; x=1742499015; darn=nongnu.org; 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=PKdxEPN0jl1qSlAU9JCgLxS3U91sP+GzkrHS5Ut6yAc=; b=mtZyX2wAwrd6y4Z1H9132fIc0Gdnzqa921C9okDZ9GLzYSjgYagZu96MwthRBX+je5 i8wCLgwClFNO+eMC02QNJrTbBDKGHVO5gHKwovh9GzznjKpfFYA489JU30hvMvaII7Em 2tkX0MCCP8MoCp9CO66oYLVckJSMmX0a65uG0WFFhZIKNY3cvNUAt8P7ojBbbTgB7I9b Df5/Ru5A2WJDjtkOe9wjDXYknKtIEtYORGjYLv3ccYSyB1SBmaDH8/D5aldtFJyvKsQW xihGdrTp9Z0qSobti9WzJ2KCNPmWk45S/f+SxQC/iyypWgJ/jyg9EGvvXydgN9Pl9COl SWLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741894215; x=1742499015; 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=PKdxEPN0jl1qSlAU9JCgLxS3U91sP+GzkrHS5Ut6yAc=; b=SnznxDEEjER/52GXuput32QVreTyxkrevmBpNpkJNltAGkFYdBZCEFGJ1kXV6FteAS XUtUxGqN6LuMPMUS+/wrLOeiMO7FmBxbUHP4HsGH5ih+8NySUq6er03Qxiv5LmWNRuQ6 bAjYhJKhaWRoDeLq4ujBqI2B5GtsDySYRscz/dPe5+AI9KM9i0eK6y0ALqKY5U4ozspe TH6+Nb/SLDPFnAkhAhX/xlrDb5VN2RKM24lIKC+jaSjI9rHD8UlL4sKRMq1q/5xvh23n OlSexjFpfxphV9PDhJhp+KDy3SOH2PaznGOeeZvJ902aGjCMEcBEp59cdeLpt8jHVpe8 dDEQ== X-Gm-Message-State: AOJu0YyYDCnuMwGJ9FgYGqBRszJsxx0Q7ENfRYai5XD3FUew2A8VnXXl EhNLZ7eZo++FozaELze40N+/k3VrsiEtqgCF5mtDEnPNxYFMQKnkpuncDURDWECqKpLy5Pyepj/ wsCd3Q/Gt X-Gm-Gg: ASbGnct1t1yw7y0vtCCqFM1b+WdgO/RZB32t9NBYuYoE/dDvFAzndtmdvdhV2zxTP9m 38rNc4RfdgpA8sqxqNhJXd8CIsqAgu+uByX/VMuLSt2Lur63gE6j8Dy2oeOX+3YqdhERQefYmaP nQ4jtqMZp/xrhKVqDjP//Rw5WoEftYgVYvrTHDqVHDw8Qh38ruElN2hh9Ohc0StKpEycT4Ou2SF 9TU/X5jawAaiLPM3+gfHqdcB9BA/9qW0dJH6itgREsI92/zVCC0jOQyjqXi0x7U2LEFol32b2M5 5IFrcVd8qq1rGqANz1bIDYgPWLpTMtv62w4Hk0EivboXQdQUxymDxOPytFdAmKlODUjJgKyMBgb w5ekz9hRqEH4T2f1xnjbbl7SQdKbVCg== X-Google-Smtp-Source: AGHT+IHJU8nvG6EU4g4oS6ZIy9t0MqGjfuGpV12QHvEBIdRREKfFKnyR6hF6fpF7aMnc4zZ8q2Ezdw== X-Received: by 2002:a05:600c:1c81:b0:43c:efed:733e with SMTP id 5b1f17b1804b1-43d01be6389mr152298345e9.14.1741894214770; Thu, 13 Mar 2025 12:30:14 -0700 (PDT) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Liu Zhiwei , Weiwei Li , qemu-riscv@nongnu.org, Palmer Dabbelt , Alistair Francis , Daniel Henrique Barboza , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH v2 2/5] target/riscv: pmp: move Smepmp operation conversion into a function Date: Thu, 13 Mar 2025 20:30:08 +0100 Message-ID: <20250313193011.720075-3-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250313193011.720075-1-loic@rivosinc.com> References: <20250313193011.720075-1-loic@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2a00:1450:4864:20::332; envelope-from=loic@rivosinc.com; helo=mail-wm1-x332.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1741894369238019100 Signed-off-by: Lo=C3=AFc Lefort Reviewed-by: Daniel Henrique Barboza Reviewed-by: LIU Zhiwei --- target/riscv/pmp.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index e1e5ca589e..7d65dc24a5 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -31,6 +31,15 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t a= ddr_index, uint8_t val); static uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t addr_index); =20 +/* + * Convert the PMP permissions to match the truth table in the Smepmp spec. + */ +static inline uint8_t pmp_get_smepmp_operation(uint8_t cfg) +{ + return ((cfg & PMP_LOCK) >> 4) | ((cfg & PMP_READ) << 2) | + (cfg & PMP_WRITE) | ((cfg & PMP_EXEC) >> 2); +} + /* * Accessor method to extract address matching type 'a field' from cfg reg */ @@ -355,16 +364,6 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr add= r, const uint8_t a_field =3D pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg); =20 - /* - * Convert the PMP permissions to match the truth table in the - * Smepmp spec. - */ - const uint8_t smepmp_operation =3D - ((env->pmp_state.pmp[i].cfg_reg & PMP_LOCK) >> 4) | - ((env->pmp_state.pmp[i].cfg_reg & PMP_READ) << 2) | - (env->pmp_state.pmp[i].cfg_reg & PMP_WRITE) | - ((env->pmp_state.pmp[i].cfg_reg & PMP_EXEC) >> 2); - if (((s + e) =3D=3D 2) && (PMP_AMATCH_OFF !=3D a_field)) { /* * If the PMP entry is not off and the address is in range, @@ -383,6 +382,9 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr, /* * If mseccfg.MML Bit set, do the enhanced pmp priv check */ + const uint8_t smepmp_operation =3D + pmp_get_smepmp_operation(env->pmp_state.pmp[i].cfg_reg= ); + if (mode =3D=3D PRV_M) { switch (smepmp_operation) { case 0: --=20 2.47.2 From nobody Thu Apr 3 10:11:41 2025 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 ARC-Seal: i=1; a=rsa-sha256; t=1741894372; cv=none; d=zohomail.com; s=zohoarc; b=kAKysA6fXE8D4T6iOjBni1aLT/rwQ+oaHjnhd/nG3T0Kwnkp0AOQ0xRZDu9Xn2Ln86Qz3pZwi9qOyvFJ+f3sTvmOSKmSyTYAGEvhnunDRtlm9qToiP21KKCKG2P/xPyVbYYx9gxlO3RcU9aCGdYBy4TqE/8aWVl8njfNOYWIjio= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741894372; 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=O2n7JF8AZ9NmYBCXnfUnWio1/+gHcnlvYMH9yn19jrQ=; b=gUjhdJfr/75QJxqatp3OjA3kjVOuC/EVood7konZN4oaMJlg4meD/gOV9vrOJHlXa4WiohAVlkBl3qDCH290E2mTske9fBMdXM57+pTOfNqqW+cK5Bilwzqx7pGebKdYKN+uqeNZ6p0w2WT0sV1geW9MiqJQ1vter3NASd2o2dg= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174189437223880.97175036785586; Thu, 13 Mar 2025 12:32:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tsoGa-0001yN-Bv; Thu, 13 Mar 2025 15:30:52 -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 1tsoGB-0001qm-Tr for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:30 -0400 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tsoG5-0001g2-Ui for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:27 -0400 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-43cf0d787eeso12505455e9.3 for ; Thu, 13 Mar 2025 12:30:17 -0700 (PDT) Received: from llx1.ba.rivosinc.com (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-395c888152dsm3016421f8f.48.2025.03.13.12.30.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Mar 2025 12:30:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741894216; x=1742499016; darn=nongnu.org; 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=O2n7JF8AZ9NmYBCXnfUnWio1/+gHcnlvYMH9yn19jrQ=; b=VoQiyiKb5F/wQkH9gtEc/1Sua3H8OqXZADuNSa6DuOxkzVnNxaa/60Vgeof4lplT9h ELfQkud42rx836TJcg7Y6IWrb2yjgc5D7wMyBFv1WDumFYpHnfDOjSWiqlmGy8TdD2/0 D6urQEhP6PSSPM+4gJ9ILHSCfVzg4cgXnXT6f/Dw8KDH0cK/gNN3/DM3W+HOY8Q0uN8d rnNgVl+qHcsaU6INGjCLTTBYtYTNLuqfmWCBy2Rxvc9uN2yBpcYpbpk2gDMsE/hsJ1ZK P6T8YheB6HiKWlGN60RRdks0Z6gGsGmwc9LSX5OQv9IqZaXP3dOg6Bj6+wfdYSWQcyye ofpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741894216; x=1742499016; 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=O2n7JF8AZ9NmYBCXnfUnWio1/+gHcnlvYMH9yn19jrQ=; b=nX71YuI3TpJ5VXRe6FgLk7ZVI634FJozSAOSuqz/Y6sco8B3BXSpJjL7G1OZMFIZWO 4S39OrZ02M1t6au6+kec+uxcv0/88dsT4GeVKuTwPoTK99iZVIG1x+8VEOp64zaNOSTe dpJCBTQuGBvfpoB0yJJGpozwCFCPh5ZQHGwBK+UN75IAe0XXZBKhsO0TetaRSFSRrixx +w5vMuQNg1/mNtQ2qtUtBCb0gX+SZmzE2M8EmP8Fr5BsvQGhA8C5enidBOEDOU64M8CG heryvX9QgOIRqEpbuDgcrunK+1VN8targ954WO/9X2frnij9SIL/yQJezTRPaXFJ5zh5 ys3g== X-Gm-Message-State: AOJu0YzeBm2JVeFd83d6hOl6fzA41cwlPnSSxzgeoLXhzBDGeng5Eg7/ krBszRCO2XYVd2c8eQEPB69YaLDJuOIqsVRot39gdaec8ljDxwL3rHZlwYN2kgCs9HjevVwymuk M+ufyTANF X-Gm-Gg: ASbGnctTY/i1ZiJFJc2hCZysbp0cgT/AxQW8srtxSA1kG0NRC7kd5bUM1Yh1BFvZjR2 cH9GsYMe577tCbOTf0uJipadmYCx9KVxVvshOCtDDEGvKa86VoSgVeArhj7WPHTZkGwAd9YUcVL 9b6bVZZ1xfbBo3A+kRtnwhhZjvIKDhFVd860U3sheAPBQTGar/6i4dAhXzJhhS/OZn3dQIVlaID QoJqWtsEUni3jqZZVQLT9K+0Q0N2BgThf8DJUn9LjSPmc9amCNtQixJYjGmIUKDvKXgA+eVZPVK HAeZMTzGnUYLc0DlrBOh63dGegr9F3OorN1O1ou2hmzJkoHc9ZE75jaWQRsVbrSR1qlAum4Tpzk BDDMgux7l+VUE3a40R74= X-Google-Smtp-Source: AGHT+IEL022FRnh1FL8l5QNgk+V7HaSw2qSS+VsXVLqZENhyOz0v3BzdNiLhVtJNQn/LrklUdjPhGg== X-Received: by 2002:a05:600c:4b89:b0:43a:b0ac:b10c with SMTP id 5b1f17b1804b1-43d1dd0b000mr3381725e9.26.1741894216174; Thu, 13 Mar 2025 12:30:16 -0700 (PDT) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Liu Zhiwei , Weiwei Li , qemu-riscv@nongnu.org, Palmer Dabbelt , Alistair Francis , Daniel Henrique Barboza , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH v2 3/5] target/riscv: pmp: fix checks on writes to pmpcfg in Smepmp MML mode Date: Thu, 13 Mar 2025 20:30:09 +0100 Message-ID: <20250313193011.720075-4-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250313193011.720075-1-loic@rivosinc.com> References: <20250313193011.720075-1-loic@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2a00:1450:4864:20::332; envelope-from=loic@rivosinc.com; helo=mail-wm1-x332.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1741894374164019000 With Machine Mode Lockdown (mseccfg.MML) set and RLB not set, checks on pmp= cfg writes would match the wrong cases of Smepmp truth table. The existing code allows writes for the following cases: - L=3D1, X=3D0: cases 8, 10, 12, 14 - L=3D0, RWX!=3DWX: cases 0-2, 4-6 This leaves cases 3, 7, 9, 11, 13, 15 for which writes are ignored. From the Smepmp specification: "Adding a rule with executable privileges th= at either is M-mode-only or a locked Shared-Region is not possible (...)" This description matches cases 9-11, 13 of the truth table. This commit implements an explicit check for these cases by using pmp_get_epmp_operation to convert between PMP configuration and Smepmp truth table cases. Signed-off-by: Lo=C3=AFc Lefort Reviewed-by: Daniel Henrique Barboza Reviewed-by: LIU Zhiwei --- target/riscv/pmp.c | 79 +++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index 7d65dc24a5..c5f6cdaccb 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -75,6 +75,44 @@ static int pmp_is_readonly(CPURISCVState *env, uint32_t = pmp_index) return pmp_is_locked(env, pmp_index) && !MSECCFG_RLB_ISSET(env); } =20 +/* + * Check whether `val` is an invalid Smepmp config value + */ +static int pmp_is_invalid_smepmp_cfg(CPURISCVState *env, uint8_t val) +{ + /* No check if mseccfg.MML is not set or if mseccfg.RLB is set */ + if (!MSECCFG_MML_ISSET(env) || MSECCFG_RLB_ISSET(env)) { + return 0; + } + + /* + * Adding a rule with executable privileges that either is M-mode-only + * or a locked Shared-Region is not possible + */ + switch (pmp_get_smepmp_operation(val)) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 12: + case 14: + case 15: + return 0; + case 9: + case 10: + case 11: + case 13: + return 1; + default: + g_assert_not_reached(); + } +} + /* * Count the number of active rules. */ @@ -103,44 +141,13 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env= , uint32_t pmp_index) static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t = val) { if (pmp_index < MAX_RISCV_PMPS) { - bool readonly =3D true; - - if (riscv_cpu_cfg(env)->ext_smepmp) { - /* mseccfg.RLB is set */ - if (MSECCFG_RLB_ISSET(env)) { - readonly =3D false; - } - - /* mseccfg.MML is not set */ - if (!MSECCFG_MML_ISSET(env) && !pmp_is_readonly(env, pmp_index= )) { - readonly =3D false; - } - - /* 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) { - readonly =3D false; - } - /* shared region and not adding X bit */ - if ((val & PMP_LOCK) !=3D PMP_LOCK && - (val & 0x7) !=3D (PMP_WRITE | PMP_EXEC)) { - readonly =3D false; - } - } - } else { - readonly =3D pmp_is_readonly(env, pmp_index); - } - - if (readonly) { + if (pmp_is_readonly(env, pmp_index)) { qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpcfg write - read only\n"); - } else if (env->pmp_state.pmp[pmp_index].cfg_reg !=3D val) { - /* If !mseccfg.MML then ignore writes with encoding RW=3D01 */ - if ((val & PMP_WRITE) && !(val & PMP_READ) && - !MSECCFG_MML_ISSET(env)) { - return false; - } + } else if (pmp_is_invalid_smepmp_cfg(env, val)) { + qemu_log_mask(LOG_GUEST_ERROR, + "ignoring pmpcfg write - invalid\n"); + } else { env->pmp_state.pmp[pmp_index].cfg_reg =3D val; pmp_update_rule_addr(env, pmp_index); return true; --=20 2.47.2 From nobody Thu Apr 3 10:11:41 2025 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 ARC-Seal: i=1; a=rsa-sha256; t=1741894393; cv=none; d=zohomail.com; s=zohoarc; b=PXIRzmHcJEVoGwTnkETv8y/VEj6oPdenrV8N/qhHNvpb7TK1+xJzd/lMpwWxvZLmEyUQTb1L/KCb4VbWlJwkHKsUoqmsxU52KFCNLx2q14qW1WfJmWlzvI2uzoiX3vQSmvCxm8JxoFVhACJHwvDZ1oRpTLHo5ZDxbkrZgWAt/vg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741894393; 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=5KZ6mlLQ5iqHKgw3b50V+8bSbwFg2xs+/9RS+Up0IxU=; b=eZSLOtxkAX8d+UhgBpISZWwad8m/khBwprvFLxHhC3/naELZhtW27MedV1NSbmcjk2AQrir4e0kGVEMrTO6RvZ4Wr2tGjhAy98BbYthfDiUNpJsCkBdSK6kK7jF1p+GHWo8fQ0oSavyYJnzm+/BLBOyfL/EQw1siOUp8/YGMZkc= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1741894393394603.9012384874183; Thu, 13 Mar 2025 12:33:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tsoGe-00024d-EA; Thu, 13 Mar 2025 15:30:56 -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 1tsoGC-0001qp-6F for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:30 -0400 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tsoG5-0001g9-W6 for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:27 -0400 Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-39130ee05b0so1355474f8f.3 for ; Thu, 13 Mar 2025 12:30:18 -0700 (PDT) Received: from llx1.ba.rivosinc.com (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-395c888152dsm3016421f8f.48.2025.03.13.12.30.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Mar 2025 12:30:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741894217; x=1742499017; darn=nongnu.org; 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=5KZ6mlLQ5iqHKgw3b50V+8bSbwFg2xs+/9RS+Up0IxU=; b=JGQqX2B9+CbWV8VjppOPf7jBhO/sdm4kU7DY/lkoITDSbnYkUSDB+PAc7HTUBPQwaJ mfS+cr0q2OzW4RG3Wb1eTUflNjXld4cyhZzEwqfhRCl6jaJrZ9NwgolPXgK5i2zBORfg e1vFxtJ4SgUKW7+LY/qvgIQN1fbEebtWRLVreFtd6eyhJd2G8vtPeNTxp30W5nBL7TiB c2w9U+YS3BZLbEDeHj59LXcUl7CrKXq67Cltyz+fYrgjm57ZzftXCXvfoDYuMy7pckkh KqG3Wr+a0RN63QWo/asZA6ZfQ5cEItLtE/lP+XfoOhSawC2fh81iJHUPZcmx5raf0m8l 58ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741894217; x=1742499017; 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=5KZ6mlLQ5iqHKgw3b50V+8bSbwFg2xs+/9RS+Up0IxU=; b=CsFzNpJxQaIFFtRL/UfKH79PgYwai/nBMDQXG4pwo+7RkjwpgBqE0gCDjoI7kp3pVE 8pijDA2WfSjF5Or2hyY8TKGUzd5f8qVc2NToJWQRX+hZuMkEOX7uKQwLUikH5vjbbGKK B2yins42Nr/+nuKFGMJpcMgtPdhZractT/79wCRwaaMxHy8FzNMJa90Jv3XoojOBkLwL 8m4DgkL/4eVc4p509Uscu/Twku3U9gVS1H4PtaPN4eJpNP6RfkT9JDwT1n+utn4RcVZi +JXAn7MVhYro+5FV361zQAIDcBEG93HuIAhuJj9HaYW8qO4xOo5lHyJFBBezcfVI/oLE 9jVw== X-Gm-Message-State: AOJu0YwRWnxwjP6IPC/1A0WUXJ7mG+TeAK/5mEpDuIw3NRvitJdMz49V kAH+kHYWv8YHuxFLF3DyZ50o8xS6YLHZziauodJDtlQNvHmVQcQqkDL+TMMQkPnUzyQi7w/Eibx p0o9C7kH4 X-Gm-Gg: ASbGncvRwpkFTQfzsgh/OhQHCV3L2EmXY1jJh+jiWFA5VAF9kHMXKQGkzTvZ480q35R nkrFlK57LnonG+V4DHVMrwA6/cMPC9mCesFNSS25Y4ovrSYcBZmqDB63uvj8aueupJb5WxkPaTX Ke3FfB+8Ipll1LsoL/9b3xvY3JKbsVHT8c5H1es0FzJDgfNOmKeRFS8WVbsDo20NOCPjD2kJbNO C8/kZg6iH7jkQv/UdML8ULPoiuHnmi0m0wa9UcLJCyOb6coFac1wrwgcuox5+YC4IMakRQltylp dLMon1N6hYTtFxHYW4DJa1cd9LEWGui8+ApaF3N5+X5fIEZPYIVVVqFrXUlMACAbcucqpuHmEc6 RB6bZ6z5CS9G+fMCWVS20s4bUwvbXrQ== X-Google-Smtp-Source: AGHT+IHtdilhlkrW2izwemoMVyS8mP2IaakjlHHWZP2cFNURjZOj6mfFIxgWZly8CHmD1r0c1Hwn2Q== X-Received: by 2002:a5d:6d85:0:b0:391:2a9a:47a3 with SMTP id ffacd0b85a97d-396beefa3bdmr1218996f8f.0.1741894217618; Thu, 13 Mar 2025 12:30:17 -0700 (PDT) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Liu Zhiwei , Weiwei Li , qemu-riscv@nongnu.org, Palmer Dabbelt , Alistair Francis , Daniel Henrique Barboza , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH v2 4/5] target/riscv: pmp: exit csr writes early if value was not changed Date: Thu, 13 Mar 2025 20:30:10 +0100 Message-ID: <20250313193011.720075-5-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250313193011.720075-1-loic@rivosinc.com> References: <20250313193011.720075-1-loic@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2a00:1450:4864:20::434; envelope-from=loic@rivosinc.com; helo=mail-wr1-x434.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1741894395341019100 Signed-off-by: Lo=C3=AFc Lefort Reviewed-by: Daniel Henrique Barboza Reviewed-by: LIU Zhiwei --- target/riscv/pmp.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index c5f6cdaccb..845915e0c8 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -141,6 +141,11 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env,= uint32_t pmp_index) static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t = val) { if (pmp_index < MAX_RISCV_PMPS) { + if (env->pmp_state.pmp[pmp_index].cfg_reg =3D=3D val) { + /* no change */ + return false; + } + if (pmp_is_readonly(env, pmp_index)) { qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpcfg write - read only\n"); @@ -528,6 +533,11 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t ad= dr_index, bool is_next_cfg_tor =3D false; =20 if (addr_index < MAX_RISCV_PMPS) { + if (env->pmp_state.pmp[addr_index].addr_reg =3D=3D val) { + /* no change */ + return; + } + /* * In TOR mode, need to check the lock bit of the next pmp * (if there is a next). @@ -544,14 +554,12 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t a= ddr_index, } =20 if (!pmp_is_readonly(env, addr_index)) { - if (env->pmp_state.pmp[addr_index].addr_reg !=3D val) { - env->pmp_state.pmp[addr_index].addr_reg =3D val; - pmp_update_rule_addr(env, addr_index); - if (is_next_cfg_tor) { - pmp_update_rule_addr(env, addr_index + 1); - } - tlb_flush(env_cpu(env)); + env->pmp_state.pmp[addr_index].addr_reg =3D val; + pmp_update_rule_addr(env, addr_index); + if (is_next_cfg_tor) { + pmp_update_rule_addr(env, addr_index + 1); } + tlb_flush(env_cpu(env)); } else { qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpaddr write - read only\n"); --=20 2.47.2 From nobody Thu Apr 3 10:11:41 2025 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 ARC-Seal: i=1; a=rsa-sha256; t=1741894284; cv=none; d=zohomail.com; s=zohoarc; b=hRcafJ84Q0M0nZOIzQ9vt62AfuCqAS5IAOokqVK2JJERT4FGazRrTPv5QzmH1XGaQvmZscDndsjrWeJzu+IBa604cXDj1U6b8KdVAqkYKlLtCdNzVzVc3lo9ns9fSOeXkuaUGqX90zjYodwVvEg6GOUqlVfgLiF3pKw5TPLLEo8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741894284; 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=NDzSIj54LKWcbPZowcPmZuex48RBy5Gx23oDRyzVEhY=; b=NIhgbhPBZBMbzl8pRn6qZh/j8oiB5s3r64S79APTr56UDreOAKgRD93qdoPUB1sqRIRnMA9ZSO8s7LcwWHYTjztsMl5LRrYbr1B7go5WAaXWHh1r6TWcqVjXHiF+MD27L9xbzE6jOGXXRA4tniPBnv1JS9NvAJxjKK1vCghqjYo= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1741894284019141.06617467646913; Thu, 13 Mar 2025 12:31:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tsoGZ-0001wQ-Ts; Thu, 13 Mar 2025 15:30:52 -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 1tsoGB-0001ql-TL for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:30 -0400 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tsoG5-0001gK-VJ for qemu-devel@nongnu.org; Thu, 13 Mar 2025 15:30:27 -0400 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-3912d2c89ecso1211748f8f.2 for ; Thu, 13 Mar 2025 12:30:20 -0700 (PDT) Received: from llx1.ba.rivosinc.com (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-395c888152dsm3016421f8f.48.2025.03.13.12.30.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Mar 2025 12:30:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1741894219; x=1742499019; darn=nongnu.org; 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=NDzSIj54LKWcbPZowcPmZuex48RBy5Gx23oDRyzVEhY=; b=TJpJV/rrf3JCo9ENZ1eOn9bciZs0OcVK2KXzBS1bt0ovwZtVLm49N7gD4Yn2iKURe9 ZtWv6ssyhnExCRGEnjFlqkxg2EVcRw7eQKkZ1wMQXKLZe6uxFEILypoKnAi5Ug0FFe2Y lnvSmuATfTCgBvKbWbJDDHqzg2kxnLsCXfYWDtf3k52OGYnNE5b7idrL5ZN1THvH1tzC JFN8lkLbXrvcNdZjoAeZ4mgFX0+reNYif4grn1Ksj76NMsyJpkMgEA4HdNkxG8GH+xf4 RGzLXzlrJcfNBs3W1PBAd/idH5WIYubAGPFRVLUhT5Ctzs5LKtxZ4eFQUK0dKki5A+IO Ghgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741894219; x=1742499019; 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=NDzSIj54LKWcbPZowcPmZuex48RBy5Gx23oDRyzVEhY=; b=uVylrlPipq8WHGojW6/x23IGjw1HZDoBKNFFE/a3R4cecXeqND4+xur1fSyhTun5x6 zKpylODt+PUViPpDtVTKhePg+6E9IAIdpLSLAcjKYkrIqVZoq7+fzxE1QsgahWKJjTXu clQFSGYbVnuogvFZyhYLpT0GYOEKTQYyBUKqo1/iUcu18n5WLv7OcZOBMTkEXIogI1cO E9UC/dgMFIrms1+ASdlvjlYJMokP2nQm5NGDP/dbCwxock4rbvFTreBN8yodCJ5vUVdr s1b3mDc0ae5TBfGtlLY2dPfY4elaUkOokO8uJ0BF2ZtA4ZXF3YBbjQzLgt4KQOZMEUzh e3lA== X-Gm-Message-State: AOJu0Yx8TcST6uIN2A9OMOgHX8lCKcvUXMi6SZt9rS+vVTXmENUFGdHQ q+5Am5zHPAMVxMAJu4JlWWdwPY+ymIaLFIRssRlKa4X2/u9cbEq1D8pITwOyTMY90PBypFyxnf3 XQjxXWRqX X-Gm-Gg: ASbGncveBAPnHnqfAQTpcVSjyXOVnj/U4cEFfq9D0YNs0+IIOnvw3PPPl93fh7z4KaX tRb35f5UakMsk8HaQ8tdOUrvoyuMtl8/Ba+pnveOzTaHQiAs8QZtbcIZSlEEX7ng5CDafWurV+N yYi2tN5JB7aFYgoi0fEDwg2BED3C1ldXnWy3WuuqpAv6PlkJuDZGG777w6A2hwG0GfU6QWiyqM4 f87nekuLKX9wKpYU4C1n/aOiA3/sT+35gXoEPHdzAuTDvJUU17qS6+96YuIutcazwF1XDmdGMLC Si8BLwfdR8pmWNULoDHizZjxPOdNIjiQoCMxDtSPux9/4+/fk/Gp/Ecg2eenzXoJ4eAWA+4IuxN s7Ed0cv4cqPZKG6pWTxD17AM2cdtLOA== X-Google-Smtp-Source: AGHT+IFrOkof5kWJMTLkQqDaz6bT/YCpOjNGxPlvlBEJS/+x3v9yltsxZadzp/6f/QN0KqfyzsrhuA== X-Received: by 2002:a5d:6d8a:0:b0:391:46a6:f0db with SMTP id ffacd0b85a97d-396c3837fa2mr835070f8f.37.1741894218769; Thu, 13 Mar 2025 12:30:18 -0700 (PDT) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Liu Zhiwei , Weiwei Li , qemu-riscv@nongnu.org, Palmer Dabbelt , Alistair Francis , Daniel Henrique Barboza , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH v2 5/5] target/riscv: pmp: remove redundant check in pmp_is_locked Date: Thu, 13 Mar 2025 20:30:11 +0100 Message-ID: <20250313193011.720075-6-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250313193011.720075-1-loic@rivosinc.com> References: <20250313193011.720075-1-loic@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=2a00:1450:4864:20::435; envelope-from=loic@rivosinc.com; helo=mail-wr1-x435.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1741894287245019100 Remove useless check in pmp_is_locked, the function will return 0 in either case. Signed-off-by: Lo=C3=AFc Lefort Reviewed-by: Daniel Henrique Barboza Reviewed-by: LIU Zhiwei --- target/riscv/pmp.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index 845915e0c8..c685f7f2c5 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -58,11 +58,6 @@ static inline int pmp_is_locked(CPURISCVState *env, uint= 32_t pmp_index) return 1; } =20 - /* Top PMP has no 'next' to check */ - if ((pmp_index + 1u) >=3D MAX_RISCV_PMPS) { - return 0; - } - return 0; } =20 --=20 2.47.2