From nobody Mon Apr 7 00:59:53 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=1736513748; cv=none; d=zohomail.com; s=zohoarc; b=aAk6LpOYhvG9iPynCDk038X6+y3smWVMm1AaKUrWjz+/TRj/VmpqrRWdbdC2MQaIa6R51jtaLIRwrVHQd8i8hKT/4Ct7KU3OmDFT8Ou3XZN01wSku/YT3/m2cW9o7eT+hpiec7dq4Vjf5eEvWBVjvDCyU0YMzaEr2tLlQ6SAe9M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1736513748; 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=PgHUvqU6GWN9Ix2AskIh91fh+T6XRSITDIwDhxfjXYg=; b=LNneWleoD3TupNn9lqrrEYVgiW8P9PEuKPCkEr7IUEu1nDz1SIpXYr9A4m8wzfBSr/D3dDpPlxV1vwBZhIA8s+wBUZmKmIjUWCtstFsHtJahnFU0sKVIhermp9RW1h4Tb1gNmvYiQu4ndXv4V8SXNGs/v8Dx7T4NTuOriYefaNw= 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 173651374834491.9402941421962; Fri, 10 Jan 2025 04:55:48 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tWEY4-0004TV-8L; Fri, 10 Jan 2025 07:55:42 -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 1tWEXT-00049C-M2 for qemu-devel@nongnu.org; Fri, 10 Jan 2025 07:55:00 -0500 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tWEXP-0000ya-LD for qemu-devel@nongnu.org; Fri, 10 Jan 2025 07:54:58 -0500 Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-3862d6d5765so1193914f8f.3 for ; Fri, 10 Jan 2025 04:54:54 -0800 (PST) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a8e4c1c01sm4548921f8f.97.2025.01.10.04.54.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Jan 2025 04:54:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1736513693; x=1737118493; 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=PgHUvqU6GWN9Ix2AskIh91fh+T6XRSITDIwDhxfjXYg=; b=VOqmPBkglQr1XEKj/eW/kiHuHobzNyJ+3vGnWFiu0peHzuiwaQdlyk8xivQK761IlO /7Swzj5epKRHrgx/T8ZcCeZUNymaEAg/xubjZ1CVnyuBzLmiYJ6AXMkxDTC79mh19rhX 9ijEO0KIVBpRCRLC2xHT+/gLkk2tKQCqjrtgyasdz7nDRxyqnzdrFKrBtKFCo53BYQHF zSq61Ze0iofb89YwolSR78f65KeWTo+rM3/dHYNZ+Q3KbVbvIRHfZ89D4tP6+D67HHUy bmFqj69Hin9pYUMURNv0gdMQ46RT02LZ7LfTHMKZLWThyXZxOUO4K6Wv8u2Dr7WqCsQW 31JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736513693; x=1737118493; 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=PgHUvqU6GWN9Ix2AskIh91fh+T6XRSITDIwDhxfjXYg=; b=qaXCuTKQPQ6f79Rxk0X4Xm+NFXjazsuQvglZxR9XpwuYhru07/cvfUANZxPb8Z2ArO 2Cpd6JqjBOn+ezasXIphLzc+YpM3V20maK8BN/738MpXbGIJtNiXRyi5TrgfPN6IFJZr MCZBdL2iUBBo2w3yKUHlHLcJTDuQbK0x6ZqkcVJA4ABgzHrF4ZIOmEkv9E6MNqDNVWfM HT1frRprChzSsrHYPs4rG2xRaP8zm7nIzNwMre0SRIcNEkur3yBPWcioZn+yXHET/eU7 QGIHQj5ZrLhDKRmO+Ke7nprPBsTe15gGliuWH9euXvfqQPgWfp6WP15JoiTKlTdPogIk E5lg== X-Forwarded-Encrypted: i=1; AJvYcCWOtGl1NevI5xizXT6ow+0SgaakoZB8T5qVyA+MP1mLqzJix+c3xyJPEsTEIQUveqG3b9Es1LMEA8n9@nongnu.org X-Gm-Message-State: AOJu0YzCndngoAGW3h3tPm3b3QwH5c+PfIapEGmDqgG22/yUfVu5nPEt n2w9SlrCR6/cwFw0FTKQWXGhCztBzCtgqgtrOouZX926zTGDn7CzSzvC8QulgQw= X-Gm-Gg: ASbGnctmWBu4qeYyy7qqh9fbdhyPJxkHJBAo3cN+zrmzHQOYx6Sc+z9UGTAlymXgLh7 uSrIpFJQwbpAjjx5fN6q6qx5TeX6BiOu5jgEwyTxV10URJDKzx/CXJm8auZzyE9HzJxynANeMCR NT2obCIGwbaGZK80AzvlFA0pbSAX/LLAF4F/ConhRTNOLAC5Nt0eexvNghusyKE1l67G6y15gcd GNZPpQMe2KWCee2VndWQizNMbweSjnwb2JQpSlFfAoWWtYUVZD3tPT8Bg== X-Google-Smtp-Source: AGHT+IF6lmoR05kEtXtgFW+twu5IayZgr8/uJKQtjy9TD0gZYw2Y+6+vhmzzf9xyxnfzLYQiMPS4Pg== X-Received: by 2002:a05:6000:490e:b0:386:32cb:4aa with SMTP id ffacd0b85a97d-38a8730ef59mr10362844f8f.45.1736513692954; Fri, 10 Jan 2025 04:54:52 -0800 (PST) From: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= To: qemu-riscv@nongnu.org, Palmer Dabbelt , Alistair Francis , Bin Meng Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Ved Shanbhogue , Atish Patra , qemu-devel@nongnu.org, Frank Chang Subject: [PATCH v8 1/9] target/riscv: Fix henvcfg potentially containing stale bits Date: Fri, 10 Jan 2025 13:54:32 +0100 Message-ID: <20250110125441.3208676-2-cleger@rivosinc.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250110125441.3208676-1-cleger@rivosinc.com> References: <20250110125441.3208676-1-cleger@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::433; envelope-from=cleger@rivosinc.com; helo=mail-wr1-x433.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: 1736513750643116600 With the current implementation, if we had the following scenario: - Set bit x in menvcfg - Set bit x in henvcfg - Clear bit x in menvcfg then, the internal variable env->henvcfg would still contain bit x due to both a wrong menvcfg mask used in write_henvcfg() as well as a missing update of henvcfg upon menvcfg update. This can lead to some wrong interpretation of the context. In order to update henvcfg upon menvcfg writing, call write_henvcfg() after writing menvcfg. Clearing henvcfg upon writing the new value is also needed in write_henvcfg() as well as clearing henvcfg upper part when writing it with write_henvcfgh(). Signed-off-by: Cl=C3=A9ment L=C3=A9ger Reviewed-by: Daniel Henrique Barboza Reviewed-by: Alistair Francis --- target/riscv/csr.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/target/riscv/csr.c b/target/riscv/csr.c index eddcf5a5d0..279293b86d 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -2946,6 +2946,8 @@ static RISCVException read_menvcfg(CPURISCVState *env= , int csrno, return RISCV_EXCP_NONE; } =20 +static RISCVException write_henvcfg(CPURISCVState *env, int csrno, + target_ulong val); static RISCVException write_menvcfg(CPURISCVState *env, int csrno, target_ulong val) { @@ -2974,6 +2976,7 @@ static RISCVException write_menvcfg(CPURISCVState *en= v, int csrno, } } env->menvcfg =3D (env->menvcfg & ~mask) | (val & mask); + write_henvcfg(env, CSR_HENVCFG, env->henvcfg); =20 return RISCV_EXCP_NONE; } @@ -2985,6 +2988,8 @@ static RISCVException read_menvcfgh(CPURISCVState *en= v, int csrno, return RISCV_EXCP_NONE; } =20 +static RISCVException write_henvcfgh(CPURISCVState *env, int csrno, + target_ulong val); static RISCVException write_menvcfgh(CPURISCVState *env, int csrno, target_ulong val) { @@ -2996,6 +3001,7 @@ static RISCVException write_menvcfgh(CPURISCVState *e= nv, int csrno, uint64_t valh =3D (uint64_t)val << 32; =20 env->menvcfg =3D (env->menvcfg & ~mask) | (valh & mask); + write_henvcfgh(env, CSR_HENVCFGH, env->henvcfg >> 32); =20 return RISCV_EXCP_NONE; } @@ -3101,7 +3107,7 @@ static RISCVException write_henvcfg(CPURISCVState *en= v, int csrno, } } =20 - env->henvcfg =3D (env->henvcfg & ~mask) | (val & mask); + env->henvcfg =3D val & mask; =20 return RISCV_EXCP_NONE; } @@ -3134,7 +3140,7 @@ static RISCVException write_henvcfgh(CPURISCVState *e= nv, int csrno, return ret; } =20 - env->henvcfg =3D (env->henvcfg & ~mask) | (valh & mask); + env->henvcfg =3D (env->henvcfg & 0xFFFFFFFF) | (valh & mask); return RISCV_EXCP_NONE; } =20 --=20 2.47.1