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 DEAC1CCD1A5 for ; Wed, 22 Oct 2025 02:27:38 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBOZ0-00075K-El; Tue, 21 Oct 2025 22:26:58 -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 1vBOYx-00074g-TP for qemu-riscv@nongnu.org; Tue, 21 Oct 2025 22:26:55 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBOYv-0000ye-S8 for qemu-riscv@nongnu.org; Tue, 21 Oct 2025 22:26:55 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-290ab379d48so57903545ad.2 for ; Tue, 21 Oct 2025 19:26:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761100012; x=1761704812; 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=Q4f9YnaYgyvKsd8XILEdR5MrfELcvzlciUpM11jpKgI=; b=JOJvXndhb2tRuGcc4mHApbxONhM4vTBaHFl6oCxfeu3pOwdNj5pykp7QOOqjZDiPyZ LhBQeRi4CDYqDy1vUes8+bP+5S0bbAw/kY61ywuU/5MFaAhyc7MKgiCz0RqkA8XKioql /mdWLCqnKC66FkDdrwSri2mPEi6KfsoQ1e+jWovXYNy/0QiSigMc5r0yAeVhL1lipvm4 7D1L46WAdeFxHfhA5rHssUOgI89HN/tU9hb1S8CZFq6rotkPlL74dztUr8BWO0D5QkXV Vb3wsMuvHb/DLiJSaX10gagg9IjFN4MjnEGB2rXYUtlpNhsNP/qwaX+4s2ZZkVs3iT0F 48QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761100012; x=1761704812; 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=Q4f9YnaYgyvKsd8XILEdR5MrfELcvzlciUpM11jpKgI=; b=RgodW4opSJbMMT6CIu0jf4RBWOYpl6dcjcyDpQlZ8CZrdxSWeF4jpHiSsrqZROjfCL BxIa+edwkWhuM70U77LR37tR21tecf4AGSIDrkQFWQ9HGTNePZX+9p7cVjetIf80sNzy d8TVsvbxH/vvw0+0zhcRev+HOHThpZYS91RGOaR3wO7d9kLP9HaFonmBEI0LP2gMBFdO cn+h8zmPlnegIFbQna8X1OroyLQBUcx5U8uDfbMBdmY7sy2R0sNtwZZHXt3cucMCM1V+ HP20jR0st5+1pDvQrxiurPyZSLpWjzJb1wJyl2xbZAEctqsQosezZom7lkgADH63Eqkf qo6g== X-Forwarded-Encrypted: i=1; AJvYcCW8QCxBO7Sqa/Aj2PHzJpEhCOMoupOVlt8FNsWcpJpFmNwx5gTrE6xiibfjZFq47+P7SSlxYSEoojZJ@nongnu.org X-Gm-Message-State: AOJu0YzaXPkSv/IMM0VnjS6qVZEwAZtwE16t6btSUWon3rk4hwO37Gc3 92iG3bm0RwISgOrJwRxhtJF88c3zt+eK1r5MFQ01LPSJjh0wc+tEos5e00lztZKmxHc= X-Gm-Gg: ASbGncuAiI3ZLaLshfk04nrP8IOGwia5aoY0xggT7qLjpQ4YaMtMMujbR2qTypaGpUv 5PLBHhybf1wHBooIQS6Wm76uRqdV0iLfCoEj6Mf0gs6jMW/AAwNUs7ktI3AVd7U1Gblj2M/uQIg cfMocrnQb+o7ZH445wadtA8obvD8+5gnTfT5+5u08sxxikMlElrgsFzDr8UP144pYyXGnSijeQX LK62S125fH1MbyOg+1YeTEgg50P7yCoNoTWzyKAkmDSfLW5RslIx/f9CEMmAa4pANlT4T9KCbUE SsAxCYNjqsGNliosG4w8zrMFA/z5RN53LdhBuQkRWVCWslv1WqSHy/+9ZhFMsWnIHKFvTV2ztYN kUMSeVNotfC4mV1wgKfu3AfyrIE2G1EhvZoICZsIdIm5zgfmcT6quT4Blqh4sgR0jQS4XmzfkhG YJ1M8nmPHcvWwb/yVaNgZvFTwTlZ4mNFwouo4CDZIqLhJwzSsp4Q== X-Google-Smtp-Source: AGHT+IEcffunv84UNmFLL+IM0NukBY0a+TpBTZXapGk9CCF7hVDQO0E5K5BcPjq/Kv9BzH7qQV54UA== X-Received: by 2002:a17:903:1a0e:b0:269:603f:420a with SMTP id d9443c01a7336-290c9c8932bmr223852665ad.5.1761100012425; Tue, 21 Oct 2025 19:26:52 -0700 (PDT) Received: from jchang-1875.internal.sifive.com ([136.226.240.196]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2930340092dsm4673545ad.42.2025.10.21.19.26.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 21 Oct 2025 19:26:52 -0700 (PDT) From: Jay Chang To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Jay Chang , Frank Chang , Jim Shu Subject: [PATCH v2 2/2] target/riscv: Make PMP CSRs conform to WARL constraints Date: Wed, 22 Oct 2025 10:26:28 +0800 Message-ID: <20251022022628.41307-6-jay.chang@sifive.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20251022022628.41307-1-jay.chang@sifive.com> References: <20251022022628.41307-1-jay.chang@sifive.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=jay.chang@sifive.com; helo=mail-pl1-x62b.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 This patch ensure pmpcfg and pmpaddr comply with WARL constraints. When the PMP granularity is greater than 4 bytes, NA4 mode is not valid per the spec and will be silently ignored. According to the spec, changing pmpcfg.A only affects the "read" value of pmpaddr. When G > 2 and pmpcfg.A is NAPOT, bits pmpaddr[G-2:0] read as all ones. When G > 1 and pmpcfg.A is OFF or TOR, bits pmpaddr[G-1:0] read as all zeros. This allows software to read back the correct granularity value. In addition, when updating the PMP address rule in TOR mode, the start and end addresses of the PMP region should be aligned to the PMP granularity. (The current SPEC only state in TOR mode that bits pmpaddr[G-1:0] do not affect the TOR address-matching logic.) Signed-off-by: Jay Chang Reviewed-by: Frank Chang Reviewed-by: Jim Shu --- target/riscv/pmp.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index 72f1372a49..3ef62d26ad 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -108,6 +108,17 @@ static int pmp_is_invalid_smepmp_cfg(CPURISCVState *env, uint8_t val) g_assert_not_reached(); } } +/* + * Calculate PMP granularity value 'g' + * + * The granularity value 'g' is defined as log2(granularity) - 2, where + * granularity is the minimum alignment requirement for PMP regions in bytes. + */ +static inline int pmp_get_granularity_g(CPURISCVState *env) +{ + return __builtin_ctz(riscv_cpu_cfg(env)->pmp_granularity >> 2); +} + /* * Count the number of active rules. @@ -153,6 +164,15 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val) qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpcfg write - invalid\n"); } else { + uint8_t a_field = pmp_get_a_field(val); + /* + * When granularity g >= 1 (i.e., granularity > 4 bytes), + * the NA4 (Naturally Aligned 4-byte) mode is not selectable + */ + if ((riscv_cpu_cfg(env)->pmp_granularity > + MIN_RISCV_PMP_GRANULARITY) && (a_field == PMP_AMATCH_NA4)) { + return false; + } env->pmp_state.pmp[pmp_index].cfg_reg = val; pmp_update_rule_addr(env, pmp_index); return true; @@ -199,6 +219,7 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index) target_ulong prev_addr = 0u; hwaddr sa = 0u; hwaddr ea = 0u; + int g = pmp_get_granularity_g(env); if (pmp_index >= 1u) { prev_addr = env->pmp_state.pmp[pmp_index - 1].addr_reg; @@ -211,6 +232,11 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index) break; case PMP_AMATCH_TOR: + /* Bits pmpaddr[G-1:0] do not affect the TOR address-matching logic. */ + if (g >= 1) { + prev_addr &= ~((1ULL << g) - 1ULL); + this_addr &= ~((1ULL << g) - 1ULL); + } if (prev_addr >= this_addr) { sa = ea = 0u; break; @@ -577,6 +603,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, /* * Handle a read from a pmpaddr CSR + * Change A field of pmpcfg affects the read value of pmpaddr */ target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index) { @@ -585,6 +612,25 @@ target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index) if (addr_index < pmp_regions) { val = env->pmp_state.pmp[addr_index].addr_reg; + int g = pmp_get_granularity_g(env); + switch (pmp_get_a_field(env->pmp_state.pmp[addr_index].cfg_reg)) { + case PMP_AMATCH_OFF: + /* fallthrough */ + case PMP_AMATCH_TOR: + /* Bit [g-1:0] read all zero */ + if (g >= 1 && g < TARGET_LONG_BITS) { + val &= ~((1ULL << g) - 1ULL); + } + break; + case PMP_AMATCH_NAPOT: + /* Bit [g-2:0] read all one */ + if (g >= 2 && g < TARGET_LONG_BITS) { + val |= ((1ULL << (g - 1)) - 1ULL); + } + break; + default: + break; + } trace_pmpaddr_csr_read(env->mhartid, addr_index, val); } else { qemu_log_mask(LOG_GUEST_ERROR, -- 2.48.1