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 89976E7717D for ; Mon, 9 Dec 2024 19:34:23 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tKjW8-0007HI-Mj; Mon, 09 Dec 2024 14:34:04 -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 1tKjW6-0007FS-Ch for qemu-riscv@nongnu.org; Mon, 09 Dec 2024 14:34:02 -0500 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tKjW3-0004qi-L6 for qemu-riscv@nongnu.org; Mon, 09 Dec 2024 14:34:02 -0500 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-725d9f57d90so1523765b3a.1 for ; Mon, 09 Dec 2024 11:33:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1733772837; x=1734377637; 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=9k9Pg+8y3c3UnD4hkh++q33Ep4J6N5eaCzFv/CJ1XXQ=; b=SOfXiJasxp1ZZcTfmD6Ac6p4A9dVm3RvE9zFyt78Qi6amhhHhPo1uBMtFzRcQK/0MH tLV9HchwNRjsFm/75qg8gfSmxSbjUcYCqlBU+H9wvWRXtpnR5FkLfxfRWZdIxavpoyv/ CDjEp3TCvFNkoizxlNYs8wlS7YcYazaY2CbGRWxQzOLOftPirYlNvnxqnY/fTNJkVbPq FKT7oYh0SovBzTB2SRfym3LjRsSco87QaxTK4hNhF4Jh/05QFUZPjkIxMulOfT350jsL JO0Dmdkb8Cs4pRsakjU1KXyW9DBnVKicJZzpTNQScv1m7760Oc56lNgVwAuu3elYDaEi 1qrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733772837; x=1734377637; 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=9k9Pg+8y3c3UnD4hkh++q33Ep4J6N5eaCzFv/CJ1XXQ=; b=v9fEtiXkhyCr3RPvW6xNIfyrjgGgXAr2K6rhzUllSAxSvYQD6Cfn5w/wBeHMj9h+en kaf7YTUKPiDBvnbG/1jjFc4q06t6F0pYnjfhcO8nQwsnRYOPPGx90zBL1n+VK412JE+d Bl6EgJEvZCI54h59yKpez3QiW7NhwKoVn7++XChn3B2fj+c+0/yRULl3LdKtNL+ZsRE1 MWDmI2cPkzoUaNqXUMlYlCAAz7OoGSlgPmxSHOG92u9fZqAGk33LwNxwvPgEQiqZA7Cp Q0mX/rVO4ACpH4m8Bso+M5NiRjkAn/WzKGdKq8wVvpX8Nr/AfoqXWErJWpdXXXQ7izG7 QgYg== X-Forwarded-Encrypted: i=1; AJvYcCW7PthJbAyRPBCAjTtqqI5l0dhIh3mm0FcVW0YaJWAaHicAfscqt784a6oPiMYjXtOMqBaCBC1c+mrF@nongnu.org X-Gm-Message-State: AOJu0YxSCV9ZZHPMLw+xzVhlznR4VSKYy976dkCDpGk+KpkcfrUC9YhR Uv+yVAvNtPagzi0L8etSaA9Jyisrxn8llug9gQ7jR2Pu10tbfyyT5jJEM7r1/7s= X-Gm-Gg: ASbGncv6oIFujUVibJu0AshFx/kZGL1DB3NxdZBSp76tIoOvu5MUqUmsadlVaMLfZm7 /x+wzO+J+k4l819wG2nbdN1mx6r00NKxpSg4UoW2Yd8sByJoxTiVVnsd1I77i/85u/vGYHdACuD M3++JPj2sS2xX7PjJ/YaI/W5KqrzwnKq3Z2CeaYUYcOfmwZt+aMSV1QgqmFQPgYFzGir1gYs6wG SrgiugWJXwJL345XLdueN2Lu5964vXJ3s14QVB3RgdKnDavLmmd39YSpbwrouvZ1G512zn7IKwZ EPtT2S7ZPWh393hFu9zQOKjS14LJ X-Google-Smtp-Source: AGHT+IGfL1Uh2J5eG5iR8/oZnE4ckhqCcaCENSziSfsgAYw3MN8tLn350cgtMsJ0gl+lbbXQoGQZrQ== X-Received: by 2002:a05:6a00:b42:b0:727:3c37:d5f9 with SMTP id d2e1a72fcca58-7273c37d849mr2452909b3a.26.1733772837543; Mon, 09 Dec 2024 11:33:57 -0800 (PST) Received: from ?IPV6:2804:7f0:bcc0:5756:767e:ee4e:f9a8:ee18? ([2804:7f0:bcc0:5756:767e:ee4e:f9a8:ee18]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-725e2f2d5f0sm3360965b3a.40.2024.12.09.11.33.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Dec 2024 11:33:57 -0800 (PST) Message-ID: <9261868b-40a4-4ed5-a887-0b56fe0c8fed@ventanamicro.com> Date: Mon, 9 Dec 2024 16:33:53 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v6 1/9] target/riscv: fix henvcfg potentially containing stale bits To: =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= , qemu-riscv@nongnu.org, Palmer Dabbelt , Alistair Francis , Bin Meng Cc: Weiwei Li , Liu Zhiwei , Ved Shanbhogue , Atish Patra , qemu-devel@nongnu.org, Frank Chang References: <20241128141230.284320-1-cleger@rivosinc.com> <20241128141230.284320-2-cleger@rivosinc.com> Content-Language: en-US From: Daniel Henrique Barboza In-Reply-To: <20241128141230.284320-2-cleger@rivosinc.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::431; envelope-from=dbarboza@ventanamicro.com; helo=mail-pf1-x431.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 11/28/24 11:12 AM, Clément Léger wrote: > 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ément Léger > --- Reviewed-by: Daniel Henrique Barboza > 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 5d8d0d7514..98c683df60 100644 > --- a/target/riscv/csr.c > +++ b/target/riscv/csr.c > @@ -2383,6 +2383,8 @@ static RISCVException read_menvcfg(CPURISCVState *env, int csrno, > return RISCV_EXCP_NONE; > } > > +static RISCVException write_henvcfg(CPURISCVState *env, int csrno, > + target_ulong val); > static RISCVException write_menvcfg(CPURISCVState *env, int csrno, > target_ulong val) > { > @@ -2403,6 +2405,7 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno, > } > } > env->menvcfg = (env->menvcfg & ~mask) | (val & mask); > + write_henvcfg(env, CSR_HENVCFG, env->henvcfg); > > return RISCV_EXCP_NONE; > } > @@ -2414,6 +2417,8 @@ static RISCVException read_menvcfgh(CPURISCVState *env, int csrno, > return RISCV_EXCP_NONE; > } > > +static RISCVException write_henvcfgh(CPURISCVState *env, int csrno, > + target_ulong val); > static RISCVException write_menvcfgh(CPURISCVState *env, int csrno, > target_ulong val) > { > @@ -2424,6 +2429,7 @@ static RISCVException write_menvcfgh(CPURISCVState *env, int csrno, > uint64_t valh = (uint64_t)val << 32; > > env->menvcfg = (env->menvcfg & ~mask) | (valh & mask); > + write_henvcfgh(env, CSR_HENVCFGH, env->henvcfg >> 32); > > return RISCV_EXCP_NONE; > } > @@ -2513,7 +2519,7 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno, > } > } > > - env->henvcfg = (env->henvcfg & ~mask) | (val & mask); > + env->henvcfg = val & mask; > > return RISCV_EXCP_NONE; > } > @@ -2546,7 +2552,7 @@ static RISCVException write_henvcfgh(CPURISCVState *env, int csrno, > return ret; > } > > - env->henvcfg = (env->henvcfg & ~mask) | (valh & mask); > + env->henvcfg = (env->henvcfg & 0xFFFFFFFF) | (valh & mask); > return RISCV_EXCP_NONE; > } >