From nobody Thu Apr 3 10:45:39 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=1740499391; cv=none; d=zohomail.com; s=zohoarc; b=Pneae1K7hySzk9MQVaLVpgAtJ1uO1HJOGAstQAXmIw11FUUl2MGaeorAw2mt/TXP/US8qqXRMMPISJ7MIKhNtGmF6ki+e9A7O1GfntzoWQOboO9eZE49PPjQJYOgWMiCmpY0+y6jbY8gtgwZ/Hg4sCsTgvmE1/5YlIYvheuGVM4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740499391; 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=b1upG3QxbU0TZ9a6nDq0D3oxnkr9zAA+ZcBirmsMASI=; b=KeEuThwZYiB4hi1AtJqUYGU+ss4D7g4TPT9EKkycz21oeVZiQY3tDDHxEEA9Rt5N4VNzUFcVF2DDY1T1Xdeb2BCsv3snT5rpyqLNU8iTScVWgRPvYR6Arbp2dIZkaPUvlVCFdw2hmgIFHTPtRpnGxnp1Oa2hggr54V6pmIjhPfU= 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 1740499390911663.2766787569738; Tue, 25 Feb 2025 08:03:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tmxOk-0000TY-Oo; Tue, 25 Feb 2025 11:03:06 -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 1tmxN2-0007HB-D8 for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:20 -0500 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tmxMt-0001wo-VY for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:19 -0500 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-43989226283so39924775e9.1 for ; Tue, 25 Feb 2025 08:00:59 -0800 (PST) Received: from llx1.cad.pilog.net (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ab1546df4sm30530755e9.17.2025.02.25.08.00.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 08:00:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1740499257; x=1741104057; 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=b1upG3QxbU0TZ9a6nDq0D3oxnkr9zAA+ZcBirmsMASI=; b=qVwGRwBqeO4MztVFXbgJF/8zxBDThIkdqU47Fmyf2Cdnj6HZDeFThERTIAQmZP17Kj 0YthyhB7KKcHgpn/cru8yxLP+51Vsokxq5hF7DyxXDDIHxSKRehwqgpLe8ED0cKkgIah bBlcn/AnOQ14uklhvIOKQ3+pB2eCuu9uw52JKmV/2s39l6mW63zTwD9uIsf2HgbeQfa5 sc2vYA0BTIKgdbyweR0pTMciWxCMQvxYiHCP+O0yov8rCPQ660Z8woHVkejAipPbfYtG rXmAhZlK2o1lHOiAvu1Nj3z05ep6mzEMjZzE2BlEvO/BfPQT/haconha77JWruJyHhjJ OLzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740499257; x=1741104057; 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=b1upG3QxbU0TZ9a6nDq0D3oxnkr9zAA+ZcBirmsMASI=; b=g2ZrMGTo47A00s3M+WJ0b2ROHIahLGPtqMRsy3JJoY3NMIIkog8M0o/8UcKzTct+cw mUbIScRLdwLGmBUY++0mBolE+oPPw3JVYJQNjSswAA689Qjf7pZNOr/v3B8343GtcSBj cj62bv/mgEluhTZmyqIzffriKkbKymEY3ahTMlFVeOQcEC5ShS/RZk/YqeLlCS24JpEw usXG/B77Da2oE38fBZ+JgvblaLN4ZOaaNc/MQCIBbDlnVC+dwCM3cIrnm5EO1ghJ8azr Ig6F76FcUEK+uEM7vrovTAnSgwgyd+zZAwcx/GHBSEqXiy85b8EYFlyjtPxyXJX3IiIh P9uA== X-Gm-Message-State: AOJu0YxDqL/OwoUAQIzcMGdqYlQ1tGVs9fOGSWwttfIoymDcnTEmto3M DSc8y/xiKo9GG0Yt6h0O/ssfL/jLjxFRqHMU5lvN+piGjhzO/n2YhpJ04xHE1bsJWHOQKty+Tve ODe67TCBC X-Gm-Gg: ASbGncsbChScumn3LxvqhfjQETgc9MxO/42J4Bi0cCH6vkMaDxASiIumMaGNkcedPyJ z0pGNlAv/j920tDGdMfvuTgJw5bELAqsNX0ypsxyxaJMCFkWFggtDC0AUAkjNae5sbjcIQZ2l9y oHyprul5uA+4c4RFshecuY3uQnOfpb4HK9mY3ci/AmC/Tslhr2Vghl2CH7luu2tRxiNPcbXZMxV vr8rI0roYiYdXMJ4v0dqgz2YqaGuLAbd7rZH2b4ryTCfnylDkkaqalHAlaeELd2mNMuDu0vnwF3 84NC+6zuPF6qMVvTRNXQuCH6alk6kw66fJPMAXyNX0f3e2QdM6arFnFnorMPhRdqs4hYrwhhow= = X-Google-Smtp-Source: AGHT+IF8IT2SVbKmiK7fu/uyxWlVtw67ma6/xAsxErSdZ9xHZoKERocGG4ZsYy0TuTmJHJ+D/+3QDA== X-Received: by 2002:a05:600c:5253:b0:439:a093:fffe with SMTP id 5b1f17b1804b1-439ae1e9601mr159030085e9.7.1740499255764; Tue, 25 Feb 2025 08:00:55 -0800 (PST) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Alistair Francis , Palmer Dabbelt , Weiwei Li , qemu-riscv@nongnu.org, Daniel Henrique Barboza , Liu Zhiwei , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH 1/5] target/riscv: pmp: don't allow RLB to bypass rule privileges Date: Tue, 25 Feb 2025 17:00:48 +0100 Message-ID: <20250225160052.39564-2-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250225160052.39564-1-loic@rivosinc.com> References: <20250225160052.39564-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::32c; envelope-from=loic@rivosinc.com; helo=mail-wm1-x32c.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: 1740499392143019100 When Smepmp is supported, RLB allows bypassing locks when writing CSRs but should not affect interpretation of actual PMP rules. pmp_is_locked is changed to only check LOCK bit and a new pmp_is_readonly function is added that checks both LOCK bit and mseccfg.RLB. pmp_write_cfg = and pmpaddr_csr_write are changed to use pmp_is_readonly while pmp_hart_has_pri= vs keeps using pmp_is_locked. Signed-off-by: Lo=C3=AFc Lefort --- target/riscv/pmp.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index 85ab270dad..ddb7e0d23c 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,40 @@ 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; + if (!pmp_is_readonly(env, pmp_index)) { + readonly =3D false; } } =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 +529,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 +547,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:45:39 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=1740499341; cv=none; d=zohomail.com; s=zohoarc; b=OtyClF/ubPOYf3zCn6l5YxGeLYyZW0RRhredvoDEokQQxoSxm1n4Whg/eZpn5J88pSJ2PAphxc1cSiaUJDRXD3LXle9xhqeMU9gBPf0I5hPOf2rY5KwCzdJME/MPyKfkrKS5mKLWIDnahYfyGY2byvnUGLf4x9mR0/Mikbgxg3o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740499341; 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=dajlQOoRvoQV8eXeQsdm+0Ep1VZ3BLLQlzfHq44Vuqc=; b=DVKl/bG2heH60v1A3HG9qr0UH8g2ZxzWpgNxV1By8DJHazC1fYLFXggFfnfrg2gx89wJPEq41EdvSjc1tY2AvQ3z00GZUiY+rVoQsh5yrNiea9tG6BV+YvQR2wTuR4vXzeBkCw88zg6HU2hrxzAMgxzdTQnqllKnDYHVhk77vFE= 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 174049934137060.441948397627584; Tue, 25 Feb 2025 08:02:21 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tmxNe-0007NK-KH; Tue, 25 Feb 2025 11:02:00 -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 1tmxMz-0007Fk-PU for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:17 -0500 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tmxMt-0001wi-DH for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:17 -0500 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-439846bc7eeso36864935e9.3 for ; Tue, 25 Feb 2025 08:00:58 -0800 (PST) Received: from llx1.cad.pilog.net (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ab1546df4sm30530755e9.17.2025.02.25.08.00.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 08:00:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1740499257; x=1741104057; 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=dajlQOoRvoQV8eXeQsdm+0Ep1VZ3BLLQlzfHq44Vuqc=; b=QpOLiPYeMG2eJ7ud/9ztyh6+BYNj2vj4y+712QrrvikdbU8JSaiLEWb+L0EJHM4pzS AAMrV6h55jQuufZ+/C0tIzwQ5REDyPuXDma+oafts9J0gaviTkWd+mj/frnKqz0NwSU4 KG+FUqqOOwTiVpGv1vcaLFganRgBpHgMjDZHYz9Zv3/VMxJa8V9RB76nTpRr5nFHgJzy dtDN024UjJ9Lcm5tv3S5fEHa59rhdc5R/RA1BitDroCHzt2cct1FSrIloa+m+pIP83xR 3I2fBJPb3g1GFoBt4NSjI9q28LguTbiAhqvm5fKymQHJikUwvqHrxz1vrwr8v4IvS8KV h3QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740499257; x=1741104057; 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=dajlQOoRvoQV8eXeQsdm+0Ep1VZ3BLLQlzfHq44Vuqc=; b=OxuUtBDW2PiyC70yMnU79npa2vKPCHQNYmOx3wU3pEp/Ez4q4/w0W+Httb9j5lTSSG PUfMNXZdpSW3QbH5mEP/h4sBk7BSf1G+N/EdHIVFTKAZdkAOTkdQlQTzduSpRaTZZcfd foICM0ALrGGnqw1zkHugeJAZafJ1MnKydepc2aQEsFCpB7adRwsRorHmyiq0BP3LNZZp S12At7yFkdr/PT7GNPjP423wAvHxoZlaMUGtXpQ00I9stoopWQ4Ppla9lVZpYUcRyV7j 9PIw7stSscN/FtiVIvoWHu89P+I7jvjTAInjcKQ1qddI6E/eOfdh+/XCals4kSAmZEkI UhuA== X-Gm-Message-State: AOJu0YzAf+AVaz/se/ZTYbwjC6R6pJTkz4inMk2e675NySP40jsVFnZO hi+BLMj7BGR/KocX+wLPyr9BqntXnqzXPPnVQYuB/S6n1Q6cX4pbgWQUOnidGsUzecGe+wHjqi7 gnIUIVg9a X-Gm-Gg: ASbGncvVaukqr84Lep2KwKbMpWHhXEeJuJN6e2PvLjShdUGby/q08MWlrMlDJetrsKP lGBxERTKJKoV8P014jYI82/R8S/h8T8NNSc9oSwEc+b1KxqDz1aJ6zBKRSMMuGqmFJNXc/2Lu/U 4g8X31sCiTidFzms85waLNUAFXWCGvqxhcZq3VbUL/xEpMwRCcsyL0QIYb3p7BVWUeErOWcZOAm 0z4RMXLO5JoHLbiY0Pjcw36oSziFuOX99RSSew7mQwGd8zoDQYYVix3q8ubEfkb+TCFM66r4Yix 9fWMpj0CjpLyTJ+tqRh85G/rE5lkcaV1yC23FMz38dFBJq0dUqowMmLNHxE2CcLkFEklcf1cyA= = X-Google-Smtp-Source: AGHT+IFUr/WlrbZLLx9gxZm7xca9g+gDD17sYS3lfk02dfE1n8u+q6vptoQT3QiGSyBQtVFlTy2DKQ== X-Received: by 2002:a05:600c:190d:b0:439:685e:d4c8 with SMTP id 5b1f17b1804b1-43ab8fe9461mr730105e9.15.1740499256670; Tue, 25 Feb 2025 08:00:56 -0800 (PST) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Alistair Francis , Palmer Dabbelt , Weiwei Li , qemu-riscv@nongnu.org, Daniel Henrique Barboza , Liu Zhiwei , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH 2/5] target/riscv: pmp: move Smepmp operation conversion into a function Date: Tue, 25 Feb 2025 17:00:49 +0100 Message-ID: <20250225160052.39564-3-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250225160052.39564-1-loic@rivosinc.com> References: <20250225160052.39564-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::32d; envelope-from=loic@rivosinc.com; helo=mail-wm1-x32d.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: 1740499343793019100 Signed-off-by: Lo=C3=AFc Lefort Reviewed-by: Daniel Henrique Barboza --- 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 ddb7e0d23c..b7f1430ff8 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 */ @@ -357,16 +366,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, @@ -385,6 +384,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:45:39 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=1740499324; cv=none; d=zohomail.com; s=zohoarc; b=HGarfUQHx3WklVX/qg9z50/s/1CG7GG1dX2pXyvbCQi8cs9D10UZQFRuMZzD3icbVwJEY6Lv0+eK5/iFgtXBtDOCKWqAMWXxQsuqYN8s2kY5utBotLQhJVKhIXGivT5PXXcJ/A74BOIF/gwRQ+128d+YkkLNplcdyyP6kNotjuc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740499324; 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=HSlxDFH/ObyXVckt8oRUa9wf2y151knGbEMsdZQomEY=; b=d0KgzxWHFSSeyXtSSRoJUEv79BvyDhk51Ws8p33+KH4nofhHkXeR4Zdmob+1L4vs3S0x7/haysD3lL095qxe5S2tLztLQvltAO3zC1KPRxJO1ZYdoZ4zxkgmKDo+MhiGA8MpI6Saab9ECe/eDPx3EX7+ueIMbadoP8fXvzbivTc= 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 1740499324179576.9711765171354; Tue, 25 Feb 2025 08:02:04 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tmxNR-0007Lp-NG; Tue, 25 Feb 2025 11:01:46 -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 1tmxMx-0007FY-OJ for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:17 -0500 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tmxMt-0001ww-KU for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:15 -0500 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-4398c8c8b2cso58658175e9.2 for ; Tue, 25 Feb 2025 08:00:59 -0800 (PST) Received: from llx1.cad.pilog.net (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ab1546df4sm30530755e9.17.2025.02.25.08.00.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 08:00:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1740499258; x=1741104058; 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=HSlxDFH/ObyXVckt8oRUa9wf2y151knGbEMsdZQomEY=; b=u8+9lnvO2z0r4JqzdAUkZ1gcPoSt13z110I30MusEQcnzqeqPmbDtjH3/kHCRJtuWF zORpa6YRyCokilyUxAk62mvnGw5R3NHu+kJHFfU1CPK1O77RGgGmI+T595BhFPT1UvR0 y7WyzaDWkN1yG0VuxGL2AO+/SNYLFKd9W8WsV84XuvvIq5zqE5U3AvVno75asG1gCE6c nu/u2cwUpD90gxt57DQZCzFVZVtTJo3W6LUuM9qOZdLKwnS0cGhlmZq3Vo20ZX02iTiV NJziz8wQTS+hVjV97V0ewyi3h9O2Zv9JXGJ3nXjoRA46sJsJ/sr3YhIPkW0hMv/T0tAo 0aWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740499258; x=1741104058; 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=HSlxDFH/ObyXVckt8oRUa9wf2y151knGbEMsdZQomEY=; b=uwZJQgE6mXvxr9TOzuEEcUhk7to06vN+uAL98bFaEWYkdz6bzO5FxVKoXhJJYHipHO PTGKluhpD1AMvsEdU03VXbiivcVq1ToBF6L6ft49un8v1+lzgCQ3ISXlPZodby9wVp6a f/nj091NZeyeffvC5h+ydyfybIk4VVTt3G8sNcCr9UN01egQ/M+5iLzp6uyQf/LJt4lR VseCTZU/Rg1r3/Ze32i9BqqIt1ygTlJgLmG/fyiaQz+beWNeOD1wcpf0n2GFGHoSTwYv z0l3iFHzqpHnxKDn5qUeckWd2m5aqJdIu4+xbKMuUK5ap0PF+wLWXATjojGDlRuDDAtT +3Dg== X-Gm-Message-State: AOJu0YwuPe7ylsYkaTcIVPqLb5+qWQ+U5N6pIQS1gBAbmSySPPhWxW/I YB1gL6/m6O3HTmaVgJKhX/o0xTaKx5X/YWQAyetbjcLtrdmZmbrwT2/b72gAV6B2Au4NCCfXH/A sYZoB1Hb1 X-Gm-Gg: ASbGncuWGK+S3UF7N+QHmwCFJsKoE62XoGOWMyPBWKtbfvdQyWbuL/rSS/nawS7ReMc AJLNp7Fciwe6B2HVAQIaB4AuoRp4YZfiqRwPFPtjfC43D8Jhd7xuNpFOfdm1kgKnvR8iC0SKfd5 qF4BKpTdjok6qXzrwHnJjZImK+ICSOiGCq2DHIqfyfV2PNpwTvuFFFUbDL9d3DVl2fjjF25Eiha b8ncuLs0NcGCKjyOxwnbTdA35teeokjYROArnY6mz4B6xojaJNrO0ghU2MsQyngkD2BxtzQyb5x uZKqrKuEgWhB4AKOsXAaNife8RkYlsrNgq1nC1/voU6szGRGtyRh2bGbwv9lWdyJ7vMipgsqOw= = X-Google-Smtp-Source: AGHT+IFFomsqx67Lys9xn577NNBX51qZjNRGPtr/fLIRAQw1Uhgu6AAHxTm800E9cMVvZ2y/UsTljg== X-Received: by 2002:a05:600c:a01:b0:439:9106:c09 with SMTP id 5b1f17b1804b1-43ab0f6fba7mr38933725e9.26.1740499257743; Tue, 25 Feb 2025 08:00:57 -0800 (PST) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Alistair Francis , Palmer Dabbelt , Weiwei Li , qemu-riscv@nongnu.org, Daniel Henrique Barboza , Liu Zhiwei , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH 3/5] target/riscv: pmp: fix checks on writes to pmpcfg in Smepmp MML mode Date: Tue, 25 Feb 2025 17:00:50 +0100 Message-ID: <20250225160052.39564-4-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250225160052.39564-1-loic@rivosinc.com> References: <20250225160052.39564-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::335; envelope-from=loic@rivosinc.com; helo=mail-wm1-x335.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: 1740499325035019000 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 --- target/riscv/pmp.c | 81 ++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index b7f1430ff8..e0ea436f8e 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,46 +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 { - if (!pmp_is_readonly(env, pmp_index)) { - readonly =3D false; - } - } - - 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:45:39 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=1740499390; cv=none; d=zohomail.com; s=zohoarc; b=GVP+dsi0sNq5T2lu6UkQMEwgAoRkY+dw/Buee8wMna9G9s6mUfMFPcGvU7rVSxtIyvZEE3UYEHgpQVgwHiPGkvpAc6T+eLpMP08vsp640d+mFS7rfSLG56TL9OysEzhF0WyScFxwoXcJ8IfNy5oK9XFA7956rG0iTxwN5EB6bkU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740499390; 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=d+TDZqvkgY5ij9r5M1+7oHx+8x9yD312lxwT4tfm9xs=; b=AtGr1lJw/JmBJ96wLjoPu9MkpyeBg1NVSNYZOwktjX9Yw8h3YQDhpk1yHLi8/TzPxIucmEhSOJGs670xfCWkK6PyPactfH3Y6UF+U8LLzVxwTm9vgc3fK6IpvbAMnJUb89HOxeOhv4Lxb83FvYRoVkm77ULXX7JKS5WfrHeBHz4= 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 1740499390499658.3298748236201; Tue, 25 Feb 2025 08:03:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tmxOc-0008FW-98; Tue, 25 Feb 2025 11:02:58 -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 1tmxN0-0007GC-8S for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:18 -0500 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tmxMs-0001x6-OY for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:17 -0500 Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-38f378498b0so4818316f8f.0 for ; Tue, 25 Feb 2025 08:01:00 -0800 (PST) Received: from llx1.cad.pilog.net (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ab1546df4sm30530755e9.17.2025.02.25.08.00.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 08:00:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1740499259; x=1741104059; 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=d+TDZqvkgY5ij9r5M1+7oHx+8x9yD312lxwT4tfm9xs=; b=g1gWvKoLoF3LrzWYtMRWXtvDwqn2+uDWG7OZIf4VQbAPwRow8z4V4ZAtbTT/O/bLq4 XkJZCo5fBjWYkBQU4NSFgOWXJglWcnGh0FgCx9KFLUAroZf64vSFJM90wNhgrv8OD8CJ MDwfvF1xdus6bk6erII0dBs+Bgyo3qhidwqrHfSquJWVkMkZML08hd+Q6wrP2UGpFMG4 Zh1nR76L+56vcVraCH57I4AUP29lhucbUpxADgnfUbwcjvBAPdJjON37n8qU7jIcurFo NhUHW412ndoJsue6qwCmGXKWB2NZQI5VnveitVj/v7c7cmSIsbIQo2AW8XT1JyfnmtCa 8Fyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740499259; x=1741104059; 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=d+TDZqvkgY5ij9r5M1+7oHx+8x9yD312lxwT4tfm9xs=; b=wrdESecqgl0wtCxLVzNNYU8zQ8OgmGuvbbUtXrO3KXpFdXUF2JMUMS5JwIX56NC4iz pwYwD/wU1c4JK7h9h6xjIIQFhVPEiurOGdGBzel1GAf6J9BUxYpnKhJY2Zm6SDZZ84Gx n9Y1+M2B+zdJXECbN80VmQLwANYPMRtx446rdLml09q26JLcp6uiUXhJUlYx0LjsiJJd 7TqqK2ItQGW2MIOjvvHQPhZsHH4eGRXGol/dNMjtdD5qk3XXNpriKPrOf0ntRfZuovzp MJxLNr/RhWeMjA2mSUkLwRDT9NvpD2Kbj1ad+nWsoO5paBBzK9UjW8hw81qwLbsFDfFp DdZQ== X-Gm-Message-State: AOJu0Ywvqq9RXh/hSkmz0JgA7mMXkLJsy1lxII0fP+uVp6bGFTBdhNiC 3LPbEsQNwav3BpzVAx4PfPtTPr1wRBdpO/QF7oJ1OjNU08RtgnNGto8z4+dvUpGCE0jWaoaJQR2 oI2JpaZbQ X-Gm-Gg: ASbGnctOCJhkNuIbN0nG52q2VBQ4LIBEDkOOdBqd5v6/oVl6vbBlVJRU60T67XCst5K 7Q/p560ukYGYh6J2r4y/hH+CuCvMSm1rqVSfXSm1b82CcXjCaWLFrZWsRSd/kl4c6jZZM+cu42y 7ZVnWJRuKSuAlwLV15JNb3FPGYe4brjoYRDrV0YnlS38zZ+0mWUuXr+wWEXIQa2iDig4lGk/lmO jAo9byUY/INlVmveTUynrNH7taZLLYDKLXOyUs7CzI8LliJiV3UNaWhikghCrWzIq+92lBi0lIl zh1PPLX5+sQmJ4uuwzuXIZyWnLIdSsYF4wNfQgEgJaWzhvsRE7cYtP9m54Bq8iFlWCNZhq+J3A= = X-Google-Smtp-Source: AGHT+IHuIaJbWn54WqI7K/p5z1g0TwWQC/jpTrAVwCWtLU3+79KsYmrITHDvEGCsbZgdBCDLL7WoLA== X-Received: by 2002:a5d:59a5:0:b0:385:f7d9:99f5 with SMTP id ffacd0b85a97d-38f6f0c3b6fmr15386446f8f.51.1740499259008; Tue, 25 Feb 2025 08:00:59 -0800 (PST) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Alistair Francis , Palmer Dabbelt , Weiwei Li , qemu-riscv@nongnu.org, Daniel Henrique Barboza , Liu Zhiwei , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH 4/5] target/riscv: pmp: exit csr writes early if value was not changed Date: Tue, 25 Feb 2025 17:00:51 +0100 Message-ID: <20250225160052.39564-5-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250225160052.39564-1-loic@rivosinc.com> References: <20250225160052.39564-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::42e; envelope-from=loic@rivosinc.com; helo=mail-wr1-x42e.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: 1740499391418019000 Signed-off-by: Lo=C3=AFc Lefort Reviewed-by: Daniel Henrique Barboza --- 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 e0ea436f8e..e4fee10d93 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:45:39 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=1740499409; cv=none; d=zohomail.com; s=zohoarc; b=E7Vejrmf0UA/kU+rBO/JatD/N2U7q1RKeaS5RZbkWaD5DX8c+oxfYa3suW6vtPnF3M5iCVcSRJ6NKSK15EjR1jutCjt7Lxy5QQBoyHWn1+bUa7fMEXP4XwlXp010aJt6U+gF1yByRU04LXyEbaPlj8Z6ydFX7NNv6gX32ftFJQI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740499409; 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=gFwyJsZ9aRSgNbolC4VPZjvH7EqpCkFhCwlagWn/q9A=; b=Oho4JGMtYAUSViGym+L2v10fow/DqrnjchYcl1GcXx9uUo5/+WO60Wl+R+AtOCblxjY0btIqaZLQnVM6EkCD//5X80AsgRA4WC8xxsgfX6osJPnPzaAuJc14YFXbf3HeFiciC+a3B8Uj8Ay1GSDb37ocKeA5RfpLqHuF7oZpeNc= 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 1740499409745935.1458663209172; Tue, 25 Feb 2025 08:03:29 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tmxOq-00011h-Sw; Tue, 25 Feb 2025 11:03:13 -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 1tmxN1-0007Gi-75 for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:19 -0500 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tmxMt-0001xE-UY for qemu-devel@nongnu.org; Tue, 25 Feb 2025 11:01:18 -0500 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-4394a0c65fcso58745215e9.1 for ; Tue, 25 Feb 2025 08:01:01 -0800 (PST) Received: from llx1.cad.pilog.net (lfbn-mar-1-50-94.w86-229.abo.wanadoo.fr. [86.229.28.94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43ab1546df4sm30530755e9.17.2025.02.25.08.00.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 08:00:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1740499260; x=1741104060; 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=gFwyJsZ9aRSgNbolC4VPZjvH7EqpCkFhCwlagWn/q9A=; b=d1lq1bT3TWyNTG2U2qOjnIl/N8TdFcn6qBSioVbhZSYtwlh1TS1Ir9JffHo/5hglUM 5QXfpz+vgAgRdY7jS6AjN1YIo95NtNGOnnZdRA4HX1qZpaGPrv71/sGAYGM/zjHfV8M+ YS7AFkvQhjc03qfRVMkhQ+yggEQ3QRNOO+lRB73WjlwqDjRiwOrK2oXvkj/xvWaG2IJf 4Hspx7w+s3mBAE0A3UgUh/7YRWfUfBLDSEvh8vWeMIerdNDMP99wJmdtzjHpDHyOHx22 xAfi0Fc6n6IcPf56gapU/NfEzQgj0Jh84EC/K075VzgZ9yMcM1ogMuNbLuitaniRqM69 NBmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740499260; x=1741104060; 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=gFwyJsZ9aRSgNbolC4VPZjvH7EqpCkFhCwlagWn/q9A=; b=N05cEdzZwyxYJ5qIOYJfyydCf73+t5EhMkT64XhGAgZ5ofsBxQG3VQ+kzh2Tixy0xi BQbisjo4I5ifXYPfBdJcZ/mAVhHRSVSMlGdagMERfHd7UkWqVhfB8rD48ZERr/uGRjRZ rGPqXDj9I1nVmcLkUG//tU83Z4K7FocM9t0C1/PnMOJy75LoUU73ioU6gz3F5yD7CGth 0RjazRkJkpokKUzdthCMyHcBkH+n9oTfbKBkwZtF0666ZbHrxf6h9ooxIhRLB2DlZZyw gcc78noE4D/tznOS/oWwf5bk2eUVJsIuFDAJ9ndSsbVCJxcbYA9u9MQp1oaMLue2Td6b e4mw== X-Gm-Message-State: AOJu0Yy2ndFZzjMeBsE1DLpf3TVUXn9VLRJAhwDZWvn5OMUmmhsV3rSA aUoOC/NXrSfJCTgYQSYLKFZ572SG2YgNY0HgjEpi1+dGXnmxepqqZkEYWrygQiG6JelVUQZGn/B vdkddK+cs X-Gm-Gg: ASbGncsN2hjBNWdiL7776/abNvVNCm36+wN1m0Wg1dSL/dRZECuyufkND1e7wG+SJ4j 7Eu2di//kaUsA90KsZ86n7MpU6nk/blFt32LWeeGMB0A6UsxAoFoPnmsvhfqI65B26GML6NYZ1x yb+A7y3BWZfkSvZfI6LIkgMVzZR7QSFDg/agpKVI2016nSsmaBROSeUJZiMk/uc8TaXxDdXYn+o wXaTkpYJJEmWTB83AjX7LCZwak2fvf5iaoCDA+CykTKRppQX9un134cyv7sf4GaMJY5iTtJbOxT dqvgpaK9k/FNwf4k149TC3Xuwrac2ZTPA444WUWtIrNZm9MXYoRhQCmpwD5ud2hU7P2XQK6m6w= = X-Google-Smtp-Source: AGHT+IF9zC9j0LMpWwQJ9hPqqKtvjzOhmUmKjZiUsWMVEOysBPz5Aa6LQedkEf8EJOFeKLdogLqPKA== X-Received: by 2002:a05:600c:1d8c:b0:439:98ca:e39b with SMTP id 5b1f17b1804b1-43ab44b58c3mr27814935e9.29.1740499260185; Tue, 25 Feb 2025 08:01:00 -0800 (PST) From: =?UTF-8?q?Lo=C3=AFc=20Lefort?= To: qemu-devel@nongnu.org Cc: Alistair Francis , Palmer Dabbelt , Weiwei Li , qemu-riscv@nongnu.org, Daniel Henrique Barboza , Liu Zhiwei , =?UTF-8?q?Lo=C3=AFc=20Lefort?= Subject: [PATCH 5/5] target/riscv: pmp: remove redundant check in pmp_is_locked Date: Tue, 25 Feb 2025 17:00:52 +0100 Message-ID: <20250225160052.39564-6-loic@rivosinc.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250225160052.39564-1-loic@rivosinc.com> References: <20250225160052.39564-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::329; envelope-from=loic@rivosinc.com; helo=mail-wm1-x329.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: 1740499411384019000 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 --- target/riscv/pmp.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index e4fee10d93..81c8bd71ce 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