Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0C67DC3600B for ; Thu, 27 Mar 2025 17:31:50 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1txr4H-0002aF-GG; Thu, 27 Mar 2025 13:31:01 -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 1txr41-0002Z6-My for qemu-riscv@nongnu.org; Thu, 27 Mar 2025 13:30:46 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1txr3y-0004qS-DS for qemu-riscv@nongnu.org; Thu, 27 Mar 2025 13:30:45 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-225477548e1so27173305ad.0 for ; Thu, 27 Mar 2025 10:30:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1743096640; x=1743701440; darn=nongnu.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=4K79JuADZVxew8NZh1Y0DBPTYMqkjfLhjKMzpJ5yDl8=; b=gWy/SpM/j9XbzPUSaCu8/R7M0WYU9IFGG8+ug/HxFUCHr/+yKxuDVHFMT/FMiZEgao qhDg52jGLNAnCElIQTWyYPJdORR/EcLdmzRuynJZMm7DMz0me3zhqqc4hJ1hfa2iPWWh db7/UfJkATQj20B/gRMsMjUIl3sBfqFwSWcC5RZqf0uy78PVpfoHb09U+B0B5bs6FCpJ yYXkq1jjUw3kMWesHOg8Lp9l3nkY/gZAQHjah4MfY85lJMyG9UYRSkRImW44+C51ThAu u7OgZ6v+H5Oawa2TV9e9rGC6jJTvpCqzcN+hVDjCWy4EAr31CHkTfehSIRlZ8XvmQcmv EyWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743096640; x=1743701440; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4K79JuADZVxew8NZh1Y0DBPTYMqkjfLhjKMzpJ5yDl8=; b=iw1gPIDAd9de9clzXb4kxET95GETK650u2WOcc6hGKZqBoUpRsrMWYuF4G/QQm6qBl ooYYplpt96kcPvYvai0aEabhjYEkKG1YcIshoyVmQYmWdVPCF/UkdM2SPMCPtFB1PY9L uDPXvNcDs6q8soaqrMwftRa1p7dvtjDYU5/rac5AkujYrl31PLIXg4zV/e03RaThUxyK 7+JVfwMdngHj9VKvegHuBBi0ah833xCB0C/++cLfZHj0V05G2NiZqTplI/uAiKFR8HE+ 8nDCn8vXA+Ci+oEYPPg0C7RzmFRc5gnAZcsm8sKK2y7/9gr+DVv1BeFJqJOLbJxWkQjB dVnA== X-Forwarded-Encrypted: i=1; AJvYcCXQD790kk4wQcsdQ74nBKw7uURCPm8y8dCLhWWFqpZQ77IxiGYa1ZuFDGXr1o/zAZ4lh3Xl4NQ3aMBk@nongnu.org X-Gm-Message-State: AOJu0YzPVkl62kdQiaRFxpLioREzvOQXSdBM8FUp8WBGj7q2yV3FAk6H gjxnd5pHDYER5N1l0XtO+b5AWBHGs8aFF8U5OQ8lYl3N0OjL3U8bQC9OTw6KZSY= X-Gm-Gg: ASbGncufHYY6pjmkN7NvRqO0RDiGPqYpW20YTbBg70eYe72rp4/viAWPnLHnb3YwWfr EErXSO5dotL49AfpEjWOOL/SkG/lzWx6MVqp0gYggJY8yEPC8a4PHOScp7UhVPNOKMF3psNfHIX 3UYxqnpzTkZai3pV7gp17tPJC1SBhDXSXD8kcQsrfIdHI20b3IQ8Hclyk+I8Il7dH1fE2JuXjua xy2hcG6DTkqT57i0+IscGI4vhuoiJfJ0B+JWQvYSCAgRWaXMtxsrJSE1ND3PUvCF44Q11P+bLZJ AU3nYp7fFcy0TFJttBnTjwV38BOzPJqQZOBhQhy8J1XdPhwR34cTQzZviT4fFFhl0fYtMgQLMZi 5eCt7+49JlgxdAdXDuc57 X-Google-Smtp-Source: AGHT+IGDceHEvbz/CWhvfA6ITVTCoB7wLMNq8oMDJQ1J2t6lLQZTKm0CnL8DAwgftRQD4YC31SIDSg== X-Received: by 2002:a05:6a20:c707:b0:1f3:194b:30ae with SMTP id adf61e73a8af0-1fea2d2ca7emr8466536637.1.1743096640042; Thu, 27 Mar 2025 10:30:40 -0700 (PDT) Received: from [192.168.68.110] (201-69-66-189.dial-up.telesp.net.br. [201.69.66.189]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-af93b69f6ccsm155346a12.20.2025.03.27.10.30.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 Mar 2025 10:30:39 -0700 (PDT) Message-ID: Date: Thu, 27 Mar 2025 14:30:36 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 1/5] target/riscv: pmp: don't allow RLB to bypass rule privileges To: =?UTF-8?Q?Lo=C3=AFc_Lefort?= , qemu-devel@nongnu.org Cc: Liu Zhiwei , Weiwei Li , qemu-riscv@nongnu.org, Palmer Dabbelt , Alistair Francis References: <20250313193011.720075-1-loic@rivosinc.com> <20250313193011.720075-2-loic@rivosinc.com> Content-Language: en-US From: Daniel Henrique Barboza In-Reply-To: <20250313193011.720075-2-loic@rivosinc.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=dbarboza@ventanamicro.com; helo=mail-pl1-x632.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, 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-riscv@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-riscv-bounces+qemu-riscv=archiver.kernel.org@nongnu.org Sender: qemu-riscv-bounces+qemu-riscv=archiver.kernel.org@nongnu.org On 3/13/25 4:30 PM, Loïc Lefort wrote: > When Smepmp is supported, mseccfg.RLB allows bypassing locks when writing CSRs > but should not affect interpretation of actual PMP rules. > > This is not the case with the current implementation where pmp_hart_has_privs > 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ïc Lefort > --- Reviewed-by: Daniel Henrique Barboza > 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, uint32_t pmp_index) > return 0; > } > > +/* > + * 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 = true; > + bool readonly = true; > > if (riscv_cpu_cfg(env)->ext_smepmp) { > /* mseccfg.RLB is set */ > if (MSECCFG_RLB_ISSET(env)) { > - locked = false; > + readonly = false; > } > > /* mseccfg.MML is not set */ > - if (!MSECCFG_MML_ISSET(env) && !pmp_is_locked(env, pmp_index)) { > - locked = false; > + if (!MSECCFG_MML_ISSET(env) && !pmp_is_readonly(env, pmp_index)) { > + readonly = false; > } > > /* mseccfg.MML is set */ > if (MSECCFG_MML_ISSET(env)) { > /* not adding execute bit */ > if ((val & PMP_LOCK) != 0 && (val & PMP_EXEC) != PMP_EXEC) { > - locked = false; > + readonly = false; > } > /* shared region and not adding X bit */ > if ((val & PMP_LOCK) != PMP_LOCK && > (val & 0x7) != (PMP_WRITE | PMP_EXEC)) { > - locked = false; > + readonly = false; > } > } > } else { > - if (!pmp_is_locked(env, pmp_index)) { > - locked = false; > - } > + readonly = pmp_is_readonly(env, pmp_index); > } > > - 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 != val) { > /* If !mseccfg.MML then ignore writes with encoding RW=01 */ > if ((val & PMP_WRITE) && !(val & PMP_READ) && > @@ -524,14 +527,14 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, > uint8_t pmp_cfg = env->pmp_state.pmp[addr_index + 1].cfg_reg; > is_next_cfg_tor = PMP_AMATCH_TOR == pmp_get_a_field(pmp_cfg); > > - 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; > } > } > > - if (!pmp_is_locked(env, addr_index)) { > + if (!pmp_is_readonly(env, addr_index)) { > if (env->pmp_state.pmp[addr_index].addr_reg != val) { > env->pmp_state.pmp[addr_index].addr_reg = val; > pmp_update_rule_addr(env, addr_index); > @@ -542,7 +545,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_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,