From nobody Tue Feb 10 20:09:50 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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 1649435225355998.2865326787374; Fri, 8 Apr 2022 09:27:05 -0700 (PDT) Received: from localhost ([::1]:49568 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ncrRv-0002S1-OD for importer@patchew.org; Fri, 08 Apr 2022 12:27:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46464) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ncrQD-0001W0-QV; Fri, 08 Apr 2022 12:25:18 -0400 Received: from pb-smtp20.pobox.com ([173.228.157.52]:63180) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ncrQC-00007l-0H; Fri, 08 Apr 2022 12:25:17 -0400 Received: from pb-smtp20.pobox.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id 68D4F18B5FD; Fri, 8 Apr 2022 12:25:13 -0400 (EDT) (envelope-from nico@fluxnic.net) Received: from pb-smtp20.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id 5AC3118B5FB; Fri, 8 Apr 2022 12:25:13 -0400 (EDT) (envelope-from nico@fluxnic.net) Received: from yoda.home (unknown [96.21.170.108]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp20.pobox.com (Postfix) with ESMTPSA id B280618B5F9; Fri, 8 Apr 2022 12:25:09 -0400 (EDT) (envelope-from nico@fluxnic.net) Received: from xanadu.home (xanadu.home [192.168.2.2]) by yoda.home (Postfix) with ESMTPSA id CAD8E123A6B; Fri, 8 Apr 2022 12:25:07 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=date:from :to:cc:subject:message-id:mime-version:content-type; s=sasl; bh= r3DHX++uw68MVMaaIUNgL8Ywwi1dA17a5EDRfCbG1VE=; b=LHOW/8cuHCt/sI0y OmXYVT26xGXrwBeqYKVvBq4p9N6oy9uPGvTEp1Ma7Bz3/S2dsbSKvTXqXfYM83T1 z3as6UiL9TXo74pnX1WM544CpZ/Yy7RenHaErjE+n2A6vOn4cagW968Fl1f+b53t +/vf0KG+TrvyAPjxop5IFiZMw5I= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=fluxnic.net; h=date:from:to:cc:subject:message-id:mime-version:content-type; s=2016-12.pbsmtp; bh=r3DHX++uw68MVMaaIUNgL8Ywwi1dA17a5EDRfCbG1VE=; b=oaFdGxNK5e0pfwoK9X1OWVK297zTPcQ6TIx/A5KyvqDuRxCJur05p7gXmYTaUh3mtWha3KtK1VtMdivFU8Rwt9Af5k+IcVINW3mD4xVFj6E5IiC743NvePeuL/AnSTlRyE4OzRxlq9GJb2PDIqEmUWvcEZ2bhb8bG3RwQbi/DNg= Date: Fri, 8 Apr 2022 12:25:07 -0400 (EDT) From: Nicolas Pitre To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Subject: [PATCH] target/riscv/pmp: fix NAPOT range computation overflow Message-ID: MIME-Version: 1.0 X-Pobox-Relay-ID: 7578596A-B758-11EC-9263-C85A9F429DF0-78420484!pb-smtp20.pobox.com 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=173.228.157.52; envelope-from=nico@fluxnic.net; helo=pb-smtp20.pobox.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: , Cc: Alistair Francis , Bin Meng , Palmer Dabbelt Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (found 2 invalid signatures) X-ZM-MESSAGEID: 1649435227490100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There is an overflow with the current code where a pmpaddr value of=20 0x1fffffff is decoded as sa=3D0 and ea=3D0 whereas it should be sa=3D0 and=20 ea=3D0xffffffff. Fix that by simplifying the computation. There is in fact no need for=20 ctz64() nor special case for -1 to achieve proper results. Signed-off-by: Nicolas Pitre Reviewed-by: Alistair Francis --- This is in fact the same patch I posted yesterday but turns out its=20 scope is far more important than I initially thought. diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index 81b61bb65c..151da3fa08 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -141,17 +141,9 @@ static void pmp_decode_napot(target_ulong a, target_ul= ong *sa, target_ulong *ea) 0111...1111 2^(XLEN+2)-byte NAPOT range 1111...1111 Reserved */ - if (a =3D=3D -1) { - *sa =3D 0u; - *ea =3D -1; - return; - } else { - target_ulong t1 =3D ctz64(~a); - target_ulong base =3D (a & ~(((target_ulong)1 << t1) - 1)) << 2; - target_ulong range =3D ((target_ulong)1 << (t1 + 3)) - 1; - *sa =3D base; - *ea =3D base + range; - } + a =3D (a << 2) | 0x3; + *sa =3D a & (a + 1); + *ea =3D a | (a + 1); } =20 void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)