From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CFD0606A31F for ; Sun, 6 Dec 2026 02:56:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=DZt0adjL Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xa2PH-0000IG-Lv; Sat, 05 Dec 2026 21:55:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xa2PG-0000Ho-Ir for qemu-devel@nongnu.org; Sat, 05 Dec 2026 21:55:18 -0500 Received: from mail-ot1-x334.google.com ([2607:f8b0:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xa2PE-0008HW-Lv for qemu-devel@nongnu.org; Sat, 05 Dec 2026 21:55:18 -0500 Received: by mail-ot1-x334.google.com with SMTP id 46e09a7af769-8479f46ecd8so1014436a34.0 for ; Sat, 05 Dec 2026 18:55:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796525715; x=1797130515; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=+K9+4ZhXdlD98zFZ97mzz7cGjPTh689UVCCO8uhhsJc=; b=DZt0adjLs1rr6AtXsD2MDh/wivAJ8k3glRpcQIjKbr84EGFd1Etl5sLHa9W61MC16j xKpyamrjD3+6DhCvDYNgHjDUZag6zsGHWFzAKL0ka3DWrkZk27y7ysjx+iOx2jT5biuK p5HDogPqUklQBAxwMjQW2JI0bEkKGXF29XoFTOJjFBk+84IG1m2KQbko1nwxte4sRv9p pxbsWBp1hp97cT/y9cW2GxZZPRaJ0tTtmGzgnHBVNbhq3HNhAjrk132kleb25KmoXaF9 E9avpKtl0q4uQNVoUEze5YyosuWMtpv2Ds5rrf0/RZIARabJbReTRX0kzHubUZ1R9DIq AEsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796525715; x=1797130515; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+K9+4ZhXdlD98zFZ97mzz7cGjPTh689UVCCO8uhhsJc=; b=OUIMTDRO9bXFgTjf57ebtsGfp09X9pe5o5TZYD2TYt92aRRo/kIc5BVOf/A9/5RG5q sctzwZFbSUrYwqdwKxanxE11QDpToQgDxmlHr4wXIYyqvgmXXFjLYaQcEC/qI3VJ7eUR g+ZStTmm5K4ybcGmfFDRLiKNDX2lU3pte5W6NpPaS2QcjFJQGzx1JnO0fT6JFuXA9EXR Nl+mjioG886ktWFxRkRoaXpk9SDwITLZIHUI25MowSGi7rOGJjBNj6EIKyGUXUGDWStR 9y3HVv6osPe757olcbz28v2Bhsi9tET76kqdH1ByeR0THzqVoGNID83spFSKe9FJYZGF LLog== X-Gm-Message-State: AB4q+uHtLsWNer16m5r0OyUXcrUdZNOmIRPhtVL1GBEolZ1qDsGgIl+V jFm3dTN8js4bRSd38+7jY4WUl4i24bIHthhzlfLHHrXpLUSVrqHSKCF5BKWainh/ X-Gm-Gg: AZFdTZf4It5iYBp+BOazLLX2QmZQ/ZIs5oiwzp9dnYpjuigaA+t/Wc9R6taSb/fEUte kJtMk75vlmb2yxBHHuF6+wS4ysueEeGHv/ysRY+UhvdlqnH/kJ39baAgQmD0UdEgbeiqqenQlbJ T3C4IDX+hGWU0XwEAFr4SRF9N1TLjVOTLzigqbfgAwhm7wbRW0oHq55Wkl6zea11fTYdPpMmYQ/ kowip7JaE/ndr90aInbADbfsUV/flXGcb7uAluV0KE/2e+Aqa2F/7yHEdFEdSx9t7SvK42j2YxM RzdlkQ3yq9OREDn9k/BBf4xbbDarui47AE2VeH8B9+BAt2gpRu0xZkFDpdVJLF74SfRcIWFfhDO 36Mi0nverMyMSO5CIJdy9p4sBW9XDkp80H4C4z2vjPRWHpvjQgZEACnrFwkeGIzY9MpsZd7Iaqg pb+DcO8W5cK1Lh7fEypnuYrrorZOcaI32HJjoMwaVyued5QKqXRxWGoiKnxF5qAZ+TebP5EZwXX 2nq5Fu6dBaRIzzxAxNrcUVCNwbSeNQ88eLaOuozG65ZWk/wk86bPJYyW3/gESIMW7Iyt2uFeX+G wXE8Cz4OhHlu94Feyx6wYd3PFYNPVnDDKAooQ4lghK+svWo9DvI4W0rM6cOxhoRRmUaRtMypDMO IrXmnojyzG1yangJPmgDK X-Received: by 2002:a05:6871:14e:b0:4ad:8707:dafa with SMTP id 586e51a60fabf-4e3d6616a9bmr5957442fac.28.1796525714986; Sat, 05 Dec 2026 18:55:14 -0800 (PST) Received: from larptup ([2605:a601:81cd:b600:7492:ffff:752b:ae59]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4e3c163707esm4442411fac.15.2026.12.05.18.55.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 18:55:13 -0800 (PST) From: Juliet Mathis To: qemu-devel@nongnu.org Cc: LIU Zhiwei , Weiwei Li , Daniel Henrique Barboza , Alistair Francis , qemu-riscv@nongnu.org, Chao Liu , Palmer Dabbelt , Juliet Mathis , Samarth Sarda , Dana Coates Subject: [PATCH v2] target/riscv: fix VS-mode sireg access to a counter exception Date: Sat, 5 Dec 2026 20:55:10 -0600 Message-ID: <20261206025510.245-1-julietmathis2004@gmail.com> X-Mailer: git-send-email 2.56.1.windows.1 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::334; envelope-from=julietmathis2004@gmail.com; helo=mail-ot1-x334.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, 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: qemu development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-21.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_PASS,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 When counter delegation is disabled, the counter range is unimplemented. Any access to a counter while in VS-mode should raise an illegal instruction exception. Any failure to access a register in VS-mode raises a virtual instruction exception without considering the value of menvcfg.CDE. Our fix makes the registers unavailable in their CSR predicates instead. Accesses to vsireg* (and sireg*, which VS-mode reaches through vsireg*) now raise an illegal instruction exception when menvcfg.CDE = 0 and the selected index (vsiselect, or siselect outside VS-mode) is within the counter delegation range. Additionally, had to insert a forward declaration of xiselect_cd_range() in order to minimize the diff and ensure functionality. Fixes: 5e33a2082715 ("target/riscv: Support generic CSR indirect access") Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4830 Signed-off-by: Samarth Sarda Signed-off-by: Dana Coates Signed-off-by: Juliet Mathis --- v2: moved the check from rmw_xireg_csrind() into the csrind predicates and covered sireg* too, per Daniel's review. target/riscv/tcg/csr.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c index af444e3e6f..cce7405a59 100644 --- a/target/riscv/tcg/csr.c +++ b/target/riscv/tcg/csr.c @@ -431,21 +431,44 @@ static bool csrind_or_aia_extensions_present(CPURISCVState *env) return csrind_extensions_present(env) || aia_extensions_present(env); } +/* Forward declaration to minimize diff */ +static bool xiselect_cd_range(uint16_t isel); + static RISCVException csrind_smode(CPURISCVState *env, int csrno) { + target_ulong isel; + if (!csrind_extensions_present(env)) { return RISCV_EXCP_ILLEGAL_INST; } + /* In VS-mode a sireg* access is redirected to vsireg* via vsiselect */ + isel = env->virt_enabled ? env->vsiselect : env->siselect; + if (xiselect_cd_range(isel) && !get_field(env->menvcfg, MENVCFG_CDE)) { + return RISCV_EXCP_ILLEGAL_INST; + } + return smode(env, csrno); } static RISCVException csrind_or_aia_smode(CPURISCVState *env, int csrno) { + target_ulong isel; + if (!csrind_or_aia_extensions_present(env)) { return RISCV_EXCP_ILLEGAL_INST; } + /* siselect is shared with this predicate: only sireg is affected. */ + if (csrno == CSR_SIREG) { + /* In VS-mode a sireg access is redirected to vsireg via vsiselect */ + isel = env->virt_enabled ? env->vsiselect : env->siselect; + if (xiselect_cd_range(isel) && + !get_field(env->menvcfg, MENVCFG_CDE)) { + return RISCV_EXCP_ILLEGAL_INST; + } + } + return smode(env, csrno); } @@ -474,6 +497,15 @@ static RISCVException csrind_hmode(CPURISCVState *env, int csrno) return RISCV_EXCP_ILLEGAL_INST; } + /* + * menvcfg.CDE = 0 makes the counter range unimplemented, so a + * vsireg* access selecting it is an illegal instruction. + */ + if (xiselect_cd_range(env->vsiselect) && + !get_field(env->menvcfg, MENVCFG_CDE)) { + return RISCV_EXCP_ILLEGAL_INST; + } + return hmode(env, csrno); } @@ -483,6 +515,17 @@ static RISCVException csrind_or_aia_hmode(CPURISCVState *env, int csrno) return RISCV_EXCP_ILLEGAL_INST; } + /* + * vsiselect is shared with this predicate: only vsireg is affected. + * With menvcfg.CDE = 0 the counter range is unimplemented, so the + * access is an illegal instruction regardless of privilege mode. + */ + if (csrno == CSR_VSIREG && + xiselect_cd_range(env->vsiselect) && + !get_field(env->menvcfg, MENVCFG_CDE)) { + return RISCV_EXCP_ILLEGAL_INST; + } + return hmode(env, csrno); } -- 2.56.1.windows.1