From nobody Mon Nov 23 04:40:00 2026 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; dmarc=pass(p=quarantine dis=none) header.from=univ-grenoble-alpes.fr ARC-Seal: i=1; a=rsa-sha256; t=1793981985; cv=none; d=zohomail.com; s=zohoarc; b=f9G8jBBEr56r5wTHJx8meoSLoKEAdtZNE6LJGjLEnvOwUHwW6IXmRxiQoI8W50kDLPNABCyP/mV14xzNxtj0IpfQROmoewpG7yCIn1BUL2VuCz4lSF4NkdapuzFGgQB8xZk1cBk75w6IBoCrj2UcQHVz8Om9j/NYUylEqiYm14A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793981985; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+hGt+7XWScZ5ln3kJel6ZWiiVcEE+Dcc7fhrAuWRsMM=; b=LuVC7fZZ86UKztmehb9ts/jGckWRj0nJKOoZazDOhrSjdmlsXArtbyP2ooHBiIBohd/VVDYri53mn9hzzUMK6RRd9tOWvAUFfCfsTElqIXO5mU+dk2TcGrgowUvlTR8FtBnQTRLhVRXtltUu11HiIUn5pOXCy4qE6g55h+73qEY= 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793981984957677.5056102656359; Fri, 6 Nov 2026 08:19:44 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xPMey-0007UY-Mb; Fri, 06 Nov 2026 11:19:26 -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 1xPMeY-0007E0-6q; Fri, 06 Nov 2026 11:18:59 -0500 Received: from zm-mta-out-3.u-ga.fr ([152.77.200.56]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xPMeV-0004Dm-Jo; Fri, 06 Nov 2026 11:18:57 -0500 Received: from mailhub-1.u-ga.fr (mailhub-1.u-ga.fr [129.88.178.98]) by zm-mta-out-3.u-ga.fr (Postfix) with ESMTP id B4E0D40297; Fri, 6 Nov 2026 17:18:51 +0100 (CET) Received: from smtps.univ-grenoble-alpes.fr (smtps3.u-ga.fr [195.83.24.62]) by mailhub-1.u-ga.fr (Postfix) with ESMTP id B0D86FF856; Fri, 6 Nov 2026 17:18:51 +0100 (CET) Received: from naga.tima.u-ga.fr (naga.tima.u-ga.fr [147.171.132.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: petrotf@univ-grenoble-alpes.fr) by smtps.univ-grenoble-alpes.fr (Postfix) with ESMTPSA id 8063940056; Fri, 06 Nov 2026 17:18:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1793981931; bh=wXR1PSkq3HapoGgipk+/RTb2noP9dtU/nfFIGSFQuOY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AjSd2abPqAD8sjO5ofzkWS35Q49G3KSWqZabEP5pRQEDtZdSvfX+8Hd80dc+zSsvx wj4Nl132mO5ypNXihm7RdjBxBmdZiK5xTuhP4NXbNfDzWsN3WuCBKuLlGGDtmcjLvy tod7OkdePAbboWnZfs9iQydqyLA7pTrDKWpEy0DFH1z3xmALj/4IBGHbiR1RJwEnZm +3aFZhOo1vQ3mKoONGIuAdjeCYNh+Onsj4BdaMZLTjghFfq9SV9SYWW7eXTRBufkzS j5fecFA2TPQUfOIkP6WsyA1TUILvGEo+SQ9cljZtZvCfcwcUd33OWGDo7gG8YdOM6G yPQBSXXtY1ARQ== From: frederic.petrot@univ-grenoble-alpes.fr To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, daniel.barboza@oss.qualcomm.com, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-riscv@nongnu.org, =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9trot?= Subject: [PATCH v3 1/4] target/riscv: fix pmp sanitize error for x-rv128 Date: Fri, 6 Nov 2026 17:18:40 +0100 Message-ID: <20261106161843.2075117-2-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261106161843.2075117-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261106161843.2075117-1-frederic.petrot@univ-grenoble-alpes.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Greylist: Whitelist-UGA SMTP Authentifie (petrotf@univ-grenoble-alpes.fr) via submission-587 ACL (41) 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=lists1p.gnu.org; Received-SPF: pass client-ip=152.77.200.56; envelope-from=frederic.petrot@univ-grenoble-alpes.fr; helo=zm-mta-out-3.u-ga.fr X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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_MED=-2.3, 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-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 @univ-grenoble-alpes.fr) X-ZM-MESSAGEID: 1793981987326158500 From: Fr=C3=A9d=C3=A9ric P=C3=A9trot UndefinedBehaviorSanitizer reports an undefined behavior due to shifts larger than type in the handling of the pmp configuration registers for the x-rv128 cpu. Adding an "i128" version for the functions accessing the pmpcfg registers to properly handle the 128-bit case, and make sure only legal accesses to these pmp registers can take place. Reported-by: Peter Maydell Signed-off-by: Fr=C3=A9d=C3=A9ric P=C3=A9trot --- target/riscv/tcg/csr.c | 16 +++++++++++----- target/riscv/tcg/csr.h | 3 +++ target/riscv/tcg/pmp.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c index af444e3e6f..c3ef16cf1a 100644 --- a/target/riscv/tcg/csr.c +++ b/target/riscv/tcg/csr.c @@ -775,10 +775,12 @@ static RISCVException pmp(CPURISCVState *env, int csr= no) if (csrno <=3D max_pmpcfg) { uint32_t reg_index =3D csrno - CSR_PMPCFG0; =20 - /* TODO: RV128 restriction check */ if ((reg_index & 1) && (riscv_cpu_mxl(env) =3D=3D MXL_RV64)) { return RISCV_EXCP_ILLEGAL_INST; } + if ((reg_index & 3) && (riscv_cpu_mxl(env) =3D=3D MXL_RV128)) { + return RISCV_EXCP_ILLEGAL_INST; + } } =20 return RISCV_EXCP_NONE; @@ -6508,11 +6510,13 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { /* Physical Memory Protection */ [CSR_MSECCFG] =3D { "mseccfg", have_mseccfg, read_mseccfg, write_= mseccfg, .min_priv_ver =3D PRIV_VERSION_1_11_0 }, - [CSR_PMPCFG0] =3D { "pmpcfg0", pmp, read_pmpcfg, write_pmpcfg }, + [CSR_PMPCFG0] =3D { "pmpcfg0", pmp, read_pmpcfg, write_pmpcfg, N= ULL, + read_pmpcfg_i128, write_pmpcfg_i128 }, [CSR_PMPCFG1] =3D { "pmpcfg1", pmp, read_pmpcfg, write_pmpcfg }, [CSR_PMPCFG2] =3D { "pmpcfg2", pmp, read_pmpcfg, write_pmpcfg }, [CSR_PMPCFG3] =3D { "pmpcfg3", pmp, read_pmpcfg, write_pmpcfg }, - [CSR_PMPCFG4] =3D { "pmpcfg4", pmp, read_pmpcfg, write_pmpcfg, + [CSR_PMPCFG4] =3D { "pmpcfg4", pmp, read_pmpcfg, write_pmpcfg, N= ULL, + read_pmpcfg_i128, write_pmpcfg_i128, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, [CSR_PMPCFG5] =3D { "pmpcfg5", pmp, read_pmpcfg, write_pmpcfg, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, @@ -6520,7 +6524,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { .min_priv_ver =3D PRIV_VERSION_1_12_0 }, [CSR_PMPCFG7] =3D { "pmpcfg7", pmp, read_pmpcfg, write_pmpcfg, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, - [CSR_PMPCFG8] =3D { "pmpcfg8", pmp, read_pmpcfg, write_pmpcfg, + [CSR_PMPCFG8] =3D { "pmpcfg8", pmp, read_pmpcfg, write_pmpcfg, N= ULL, + read_pmpcfg_i128, write_pmpcfg_i128, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, [CSR_PMPCFG9] =3D { "pmpcfg9", pmp, read_pmpcfg, write_pmpcfg, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, @@ -6528,7 +6533,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { .min_priv_ver =3D PRIV_VERSION_1_12_0 }, [CSR_PMPCFG11] =3D { "pmpcfg11", pmp, read_pmpcfg, write_pmpcfg, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, - [CSR_PMPCFG12] =3D { "pmpcfg12", pmp, read_pmpcfg, write_pmpcfg, + [CSR_PMPCFG12] =3D { "pmpcfg12", pmp, read_pmpcfg, write_pmpcfg, N= ULL, + read_pmpcfg_i128, write_pmpcfg_i128, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, [CSR_PMPCFG13] =3D { "pmpcfg13", pmp, read_pmpcfg, write_pmpcfg, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, diff --git a/target/riscv/tcg/csr.h b/target/riscv/tcg/csr.h index 53ba3b4a4d..40396bafdb 100644 --- a/target/riscv/tcg/csr.h +++ b/target/riscv/tcg/csr.h @@ -91,6 +91,9 @@ extern const RISCVCSR mips_csr_list[]; void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index, target_ulong val); target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index); +void pmpcfg_csr_write_i128(CPURISCVState *env, uint32_t reg_index, + Int128 val); +Int128 pmpcfg_csr_read_i128(CPURISCVState *env, uint32_t reg_index); =20 void mseccfg_csr_write(CPURISCVState *env, uint64_t val); uint64_t mseccfg_csr_read(CPURISCVState *env); diff --git a/target/riscv/tcg/pmp.c b/target/riscv/tcg/pmp.c index 04df53c234..7ca60bf854 100644 --- a/target/riscv/tcg/pmp.c +++ b/target/riscv/tcg/pmp.c @@ -564,6 +564,28 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg= _index, } } =20 +void pmpcfg_csr_write_i128(CPURISCVState *env, uint32_t reg_index, + Int128 val) +{ + int i; + uint8_t cfg_val; + int pmpcfg_nums =3D 2 << riscv_cpu_mxl(env); + bool modified =3D false; + + trace_pmpcfg_csr_write(env->mhartid, reg_index, int128_getlo(val)); + trace_pmpcfg_csr_write(env->mhartid, reg_index, int128_gethi(val)); + + for (i =3D 0; i < pmpcfg_nums; i++) { + cfg_val =3D int128_getlo(int128_rshift(val, 8 * i)) & 0xff; + modified |=3D pmp_write_cfg(env, (reg_index * 4) + i, cfg_val); + } + + /* If PMP permission of any addr has been changed, flush TLB pages. */ + if (modified) { + pmp_update_rule_nums(env); + tlb_flush(env_cpu(env)); + } +} =20 /* * Handle a read from a pmpcfg CSR @@ -584,6 +606,22 @@ target_ulong pmpcfg_csr_read(CPURISCVState *env, uint3= 2_t reg_index) return cfg_val; } =20 +Int128 pmpcfg_csr_read_i128(CPURISCVState *env, uint32_t reg_index) +{ + int i; + Int128 cfg_val =3D int128_zero(); + target_ulong val =3D 0; + int pmpcfg_nums =3D 2 << riscv_cpu_mxl(env); + + for (i =3D 0; i < pmpcfg_nums; i++) { + val =3D pmp_read_cfg(env, (reg_index * 4) + i); + cfg_val =3D int128_or(cfg_val, int128_lshift(int128_make64(val), i= * 8)); + } + trace_pmpcfg_csr_read(env->mhartid, reg_index, int128_getlo(cfg_val)); + trace_pmpcfg_csr_read(env->mhartid, reg_index, int128_gethi(cfg_val)); + + return cfg_val; +} =20 /* * Handle a write to a pmpaddr CSR --=20 2.43.0 From nobody Mon Nov 23 04:40:00 2026 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; dmarc=pass(p=quarantine dis=none) header.from=univ-grenoble-alpes.fr ARC-Seal: i=1; a=rsa-sha256; t=1793981993; cv=none; d=zohomail.com; s=zohoarc; b=Rw7RPKaKoNqF/mpSLwDN72qpzKu1wS6h60dX9kBFWJGOS3lxGqsrt1oa5L+py3dXat+vZtoicTbovWfO9ONqjHD8nfCC2VVkYkEwGsrEsa6Uz09zHy1hcNJi9V4gtb9hmvpYFpSgWZ1pn19zFKMuA0hElzF4zvyizFJnA+h+QG0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793981993; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=yNktV+uquibgt9wWnuRVpkuXUNVAYpdDuX4xZX1Lgcw=; b=AEMEsh4WuVgnEgqs3n2JmeV4hSwvJ2bs/MffVciyv2llVUjuKu5SJby1yCm2/KYxE8GgvYhEjSYquPYtjNQPSqXVmaphd64sG1XXfn3Mq+gt626VqmKxW2+82Fy1n2btWMxHgL76quBseMkwfJ3l7kFRuKYbvcbvsoUu9ANDH/4= 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793981993841775.3144764961916; Fri, 6 Nov 2026 08:19:53 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xPMf0-0007V6-BH; Fri, 06 Nov 2026 11:19:26 -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 1xPMeZ-0007EL-P3; Fri, 06 Nov 2026 11:18:59 -0500 Received: from zm-mta-out-3.u-ga.fr ([152.77.200.56]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xPMeW-0004Dq-NQ; Fri, 06 Nov 2026 11:18:59 -0500 Received: from mailhub-2.u-ga.fr (mailhub-2.u-ga.fr [129.88.178.102]) by zm-mta-out-3.u-ga.fr (Postfix) with ESMTP id F3AE34029B; Fri, 6 Nov 2026 17:18:51 +0100 (CET) Received: from smtps.univ-grenoble-alpes.fr (smtps3.u-ga.fr [195.83.24.62]) by mailhub-2.u-ga.fr (Postfix) with ESMTP id EF589FF855; Fri, 6 Nov 2026 17:18:51 +0100 (CET) Received: from naga.tima.u-ga.fr (naga.tima.u-ga.fr [147.171.132.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: petrotf@univ-grenoble-alpes.fr) by smtps.univ-grenoble-alpes.fr (Postfix) with ESMTPSA id B5E8E40056; Fri, 06 Nov 2026 17:18:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1793981932; bh=SE3m8IlGg5jkVt+rBzd5npOuos8UeaRGV3wS+Ry2TMQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WY/5WYl8I/X8Q4oau4ASGxausJP9/SYNYXO+1soJXPHJDJqpqW3vi/rswTrM2+how LwgHpG3a8YhLu5ulSdhsAa9PQvZPpJnPfbaOG2/jgkwSbRZ08ShxfgBV/z0CxPRl2E SahnECZUx9XrTOpFXrsVC8+grICfIKgG1BMWr2FnYcJup7pSgHAAS+b69z60wRAyWs JBNdAtVZW2vkvwAfIrWSjfEitZwpb32e6jXqN2Dn1XyDc3CHalWjGuFZBFPzgDEib5 l+GJwDKxGdg4hGBjI8Xd43Ux5oXKf/Cf+WaRrbY42FAv0A533a5/BUCpkC8ocxwKj+ O9j0yIz2WYcGg== From: frederic.petrot@univ-grenoble-alpes.fr To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, daniel.barboza@oss.qualcomm.com, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-riscv@nongnu.org, =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9trot?= Subject: [PATCH v3 2/4] target/riscv: fix riscv_cpu_do_interrupt sanitize error for x-rv128 Date: Fri, 6 Nov 2026 17:18:41 +0100 Message-ID: <20261106161843.2075117-3-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261106161843.2075117-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261106161843.2075117-1-frederic.petrot@univ-grenoble-alpes.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Greylist: Whitelist-UGA SMTP Authentifie (petrotf@univ-grenoble-alpes.fr) via submission-587 ACL (41) 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=lists1p.gnu.org; Received-SPF: pass client-ip=152.77.200.56; envelope-from=frederic.petrot@univ-grenoble-alpes.fr; helo=zm-mta-out-3.u-ga.fr X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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_MED=-2.3, 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-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 @univ-grenoble-alpes.fr) X-ZM-MESSAGEID: 1793981995556158500 From: Fr=C3=A9d=C3=A9ric P=C3=A9trot UndefinedBehaviorSanitizer reports an undefined behavior when setting the Interrupt flag (msb) in the [ms]cause registers due to an out of bounds shift. To prevent this, we introduce fields to hold the interrupt flag in all "versions" of the cause register and build the expected values on the fly by oring it with the proper cause. Handling of vscause assumes it has the same xlen than cause. Reported-by: Peter Maydell Signed-off-by: Fr=C3=A9d=C3=A9ric P=C3=A9trot --- target/riscv/cpu.c | 1 + target/riscv/cpu.h | 4 ++ target/riscv/tcg/cpu_helper.c | 20 +++++++-- target/riscv/tcg/csr.c | 79 +++++++++++++++++++++++++++++++---- 4 files changed, 93 insertions(+), 11 deletions(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 362e561809..d473c94e21 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1021,6 +1021,7 @@ static void riscv_cpu_reset_hold(Object *obj, ResetTy= pe type) env->mstatus =3D set_field(env->mstatus, MSTATUS_SBE, cpu->cfg.big_end= ian); env->mstatus =3D set_field(env->mstatus, MSTATUS_UBE, cpu->cfg.big_end= ian); env->mcause =3D 0; + env->mcause_int =3D 0; env->miclaim =3D MIP_SGEIP; env->pc =3D env->resetvec; env->bins =3D 0; diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 49a1186bdb..e7fe1102c3 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -354,10 +354,12 @@ struct CPUArchState { uint64_t stvec; uint64_t sepc; uint64_t scause; + uint64_t scause_int; =20 uint64_t mtvec; uint64_t mepc; uint64_t mcause; + uint64_t mcause_int; uint64_t mtval; /* since: priv-1.10.0 */ =20 uint64_t mctrctl; @@ -417,6 +419,7 @@ struct CPUArchState { uint64_t vsscratch; uint64_t vsepc; uint64_t vscause; + uint64_t vscause_int; uint64_t vstval; uint64_t vsatp; =20 @@ -431,6 +434,7 @@ struct CPUArchState { uint64_t sscratch_hs; uint64_t sepc_hs; uint64_t scause_hs; + uint64_t scause_int_hs; uint64_t stval_hs; uint64_t satp_hs; uint64_t mstatus_hs; diff --git a/target/riscv/tcg/cpu_helper.c b/target/riscv/tcg/cpu_helper.c index 58e98e975c..a3ba7e9c42 100644 --- a/target/riscv/tcg/cpu_helper.c +++ b/target/riscv/tcg/cpu_helper.c @@ -585,7 +585,9 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env) env->sepc =3D env->sepc_hs; =20 env->vscause =3D env->scause; + env->vscause_int =3D env->scause_int; env->scause =3D env->scause_hs; + env->scause_int =3D env->scause_int_hs; =20 env->vstval =3D env->stval; env->stval =3D env->stval_hs; @@ -608,7 +610,9 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env) env->sepc =3D env->vsepc; =20 env->scause_hs =3D env->scause; + env->scause_int_hs =3D env->scause_int; env->scause =3D env->vscause; + env->scause_int =3D env->vscause_int; =20 env->stval_hs =3D env->stval; env->stval =3D env->vstval; @@ -2375,7 +2379,12 @@ void riscv_cpu_do_interrupt(CPUState *cs) } env->mstatus =3D s; sxlen =3D 16 << riscv_cpu_sxl(env); - env->scause =3D cause | ((target_ulong)async << (sxlen - 1)); + env->scause =3D cause; + if (riscv_cpu_sxl(env) !=3D MXL_RV128) { + env->scause_int =3D (target_ulong)async << (sxlen - 1); + } else { + env->scause_int =3D (target_ulong)async << 63; + } env->sepc =3D env->pc; env->stval =3D tval; env->htval =3D htval; @@ -2447,9 +2456,14 @@ void riscv_cpu_do_interrupt(CPUState *cs) s =3D set_field(s, MSTATUS_MDT, 1); } env->mstatus =3D s; - env->mcause =3D cause | ((target_ulong)async << (mxlen - 1)); + env->mcause =3D cause; + if (riscv_cpu_mxl(env) !=3D MXL_RV128) { + env->mcause_int =3D (target_ulong)async << (mxlen - 1); + } else { + env->mcause_int =3D (target_ulong)async << 63; + } if (smode_double_trap) { - env->mtval2 =3D env->mcause; + env->mtval2 =3D env->mcause_int | env->mcause; env->mcause =3D RISCV_EXCP_DOUBLE_TRAP; } else { env->mtval2 =3D mtval2; diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c index c3ef16cf1a..9549f5e6c5 100644 --- a/target/riscv/tcg/csr.c +++ b/target/riscv/tcg/csr.c @@ -3234,14 +3234,37 @@ static RISCVException write_mepc(CPURISCVState *env= , int csrno, static RISCVException read_mcause(CPURISCVState *env, int csrno, target_ulong *val) { - *val =3D env->mcause; + *val =3D env->mcause_int | env->mcause; return RISCV_EXCP_NONE; } =20 +static uint64_t cause_int_mask(RISCVMXL xl) +{ + g_assert(xl !=3D MXL_RV128); + return 1ULL << ((16 << xl) - 1); +} + static RISCVException write_mcause(CPURISCVState *env, int csrno, target_ulong val, uintptr_t ra) { - env->mcause =3D val; + env->mcause =3D val & (cause_int_mask(riscv_cpu_xlen(env)) - 1); + env->mcause_int =3D val & cause_int_mask(riscv_cpu_xlen(env)); + return RISCV_EXCP_NONE; +} + +static RISCVException read_mcause_i128(CPURISCVState *env, int csrno, + Int128 *val) +{ + *val =3D int128_make128(env->mcause, env->mcause_int); + + return RISCV_EXCP_NONE; +} + +static RISCVException write_mcause_i128(CPURISCVState *env, int csrno, + Int128 val) +{ + env->mcause =3D int128_getlo(val); + env->mcause_int =3D int128_gethi(val); return RISCV_EXCP_NONE; } =20 @@ -4494,14 +4517,31 @@ static RISCVException write_sepc(CPURISCVState *env= , int csrno, static RISCVException read_scause(CPURISCVState *env, int csrno, target_ulong *val) { - *val =3D env->scause; + *val =3D env->scause_int | env->scause; return RISCV_EXCP_NONE; } =20 static RISCVException write_scause(CPURISCVState *env, int csrno, target_ulong val, uintptr_t ra) { - env->scause =3D val; + env->scause =3D val & (cause_int_mask(riscv_cpu_sxl(env)) - 1); + env->scause_int =3D val & cause_int_mask(riscv_cpu_sxl(env)); + return RISCV_EXCP_NONE; +} + +static RISCVException read_scause_i128(CPURISCVState *env, int csrno, + Int128 *val) +{ + *val =3D int128_make128(env->scause, env->scause_int); + + return RISCV_EXCP_NONE; +} + +static RISCVException write_scause_i128(CPURISCVState *env, int csrno, + Int128 val) +{ + env->scause =3D int128_getlo(val); + env->scause_int =3D int128_gethi(val); return RISCV_EXCP_NONE; } =20 @@ -5532,14 +5572,16 @@ static RISCVException write_vsepc(CPURISCVState *en= v, int csrno, static RISCVException read_vscause(CPURISCVState *env, int csrno, target_ulong *val) { - *val =3D env->vscause; + *val =3D env->vscause_int | env->vscause; return RISCV_EXCP_NONE; } =20 static RISCVException write_vscause(CPURISCVState *env, int csrno, target_ulong val, uintptr_t ra) { - env->vscause =3D val; + /* Assumes vxlen =3D=3D sxlen */ + env->vscause =3D val & (cause_int_mask(riscv_cpu_sxl(env)) - 1); + env->vscause_int =3D val & cause_int_mask(riscv_cpu_sxl(env)); return RISCV_EXCP_NONE; } =20 @@ -5632,6 +5674,24 @@ static RISCVException write_pmpcfg(CPURISCVState *en= v, int csrno, return RISCV_EXCP_NONE; } =20 +static RISCVException read_pmpcfg_i128(CPURISCVState *env, int csrno, + Int128 *val) +{ + uint32_t reg_index =3D csrno - CSR_PMPCFG0; + + *val =3D pmpcfg_csr_read_i128(env, reg_index); + return RISCV_EXCP_NONE; +} + +static RISCVException write_pmpcfg_i128(CPURISCVState *env, int csrno, + Int128 val) +{ + uint32_t reg_index =3D csrno - CSR_PMPCFG0; + + pmpcfg_csr_write_i128(env, reg_index, val); + return RISCV_EXCP_NONE; +} + static RISCVException read_pmpaddr(CPURISCVState *env, int csrno, target_ulong *val) { @@ -6227,7 +6287,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_MSCRATCH] =3D { "mscratch", any, read_mscratch, write_mscratch, NULL, read_mscratch_i128, write_mscratch_i128 }, [CSR_MEPC] =3D { "mepc", any, read_mepc, write_mepc }, - [CSR_MCAUSE] =3D { "mcause", any, read_mcause, write_mcause }, + [CSR_MCAUSE] =3D { "mcause", any, read_mcause, write_mcause, + NULL, read_mcause_i128, write_mcause_i128 }, [CSR_MTVAL] =3D { "mtval", any, read_mtval, write_mtval }, [CSR_MIP] =3D { "mip", any, NULL, NULL, rmw_mip }, =20 @@ -6362,7 +6423,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_SSCRATCH] =3D { "sscratch", smode, read_sscratch, write_sscratch, NULL, read_sscratch_i128, write_sscratch_i128 }, [CSR_SEPC] =3D { "sepc", smode, read_sepc, write_sepc = }, - [CSR_SCAUSE] =3D { "scause", smode, read_scause, write_scause = }, + [CSR_SCAUSE] =3D { "scause", smode, read_scause, write_scause, + NULL, read_scause_i128, write_scause_i128 }, + [CSR_STVAL] =3D { "stval", smode, read_stval, write_stval = }, [CSR_SIP] =3D { "sip", smode, NULL, NULL, rmw_sip = }, [CSR_STIMECMP] =3D { "stimecmp", sstc, read_stimecmp, write_stimecmp, --=20 2.43.0 From nobody Mon Nov 23 04:40:00 2026 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; dmarc=pass(p=quarantine dis=none) header.from=univ-grenoble-alpes.fr ARC-Seal: i=1; a=rsa-sha256; t=1793982071; cv=none; d=zohomail.com; s=zohoarc; b=mS5YTkqK1giMrz7jNQY21YDthjMHUBYYp4QWUkZEI4pCNZfP9W9gz6RUf7A9I/PvB3MTUZ3HYh/I7hFvaeSCUAjkqcY+hMkyzcwC7rR+cPqWVX6itFKepXsCAKUMfFiXFBJVR0ymbrcUdxBqOWyGofW5clAos8ESv5yrFnZo5P4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793982071; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fCvdIrDoQEXsKB7pVor9oPqkicUe1RtlSOoBl8BYyAA=; b=Npzw2XwttwdUArx1QmJvI7tbw9018shVshkVslDlkbhTqp3Lo7rBC5vwZ9CoGzzVqnJG/Lh9H5GsGMJm5OQ9obz/QszJdEOnQvqBGawjD+ibly21wzTtmsk9wrSDmot3f3Q9MjFMvf3M2aicAenm/GcvaVJrwaWDwgcCw0FKQuQ= 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793982070933970.2804007940815; Fri, 6 Nov 2026 08:21:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xPMen-0007Lr-Em; Fri, 06 Nov 2026 11:19:16 -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 1xPMeZ-0007EJ-AE; Fri, 06 Nov 2026 11:18:59 -0500 Received: from zm-mta-out-3.u-ga.fr ([152.77.200.56]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xPMeV-0004E4-LH; Fri, 06 Nov 2026 11:18:58 -0500 Received: from mailhub-2.u-ga.fr (mailhub-2.u-ga.fr [129.88.178.102]) by zm-mta-out-3.u-ga.fr (Postfix) with ESMTP id 370B84029D; Fri, 6 Nov 2026 17:18:52 +0100 (CET) Received: from smtps.univ-grenoble-alpes.fr (smtps3.u-ga.fr [195.83.24.62]) by mailhub-2.u-ga.fr (Postfix) with ESMTP id 3313BFF855; Fri, 6 Nov 2026 17:18:52 +0100 (CET) Received: from naga.tima.u-ga.fr (naga.tima.u-ga.fr [147.171.132.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: petrotf@univ-grenoble-alpes.fr) by smtps.univ-grenoble-alpes.fr (Postfix) with ESMTPSA id 0002240056; Fri, 06 Nov 2026 17:18:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1793981932; bh=70m9Os0id7lXuj2YS7O+SmI2K61D9n4d7lwY0rlz3cI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G2n1ttxI0s+YlsldjOf1Mg3wDAgkgurdLHmGyNhmqJ5aGxwxXnSdQD7Yyrz68c/Bp PNVXaI4qaGNIJSrWBVfAgaqRnobarR08GbG5+yXj/bmvL/wf0uFQzqRIuDismkvtXx 5xwr53an8Xw58o7FKIkkJ4W2iSNUM5dUONNYFpCMv9wM7cLQj3U472GIjcaPXuSAa7 OcS7o9x73g8rDhhFAClME4jcRy/heMaWCRkrvW+JKYlcTljb339cX6d+HX6Xr2YDjb OQlTq/IWFyCC1FQOn2z8Q3fBl4KOVGfREmSaf187UWlG1xKDtwaKMiMWb+1nHZnkiP J6i2FWUegdAqQ== From: frederic.petrot@univ-grenoble-alpes.fr To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, daniel.barboza@oss.qualcomm.com, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-riscv@nongnu.org, =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9trot?= Subject: [PATCH v3 3/4] target/riscv: Update machine state Date: Fri, 6 Nov 2026 17:18:42 +0100 Message-ID: <20261106161843.2075117-4-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261106161843.2075117-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261106161843.2075117-1-frederic.petrot@univ-grenoble-alpes.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Greylist: Whitelist-UGA SMTP Authentifie (petrotf@univ-grenoble-alpes.fr) via submission-587 ACL (41) 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=lists1p.gnu.org; Received-SPF: pass client-ip=152.77.200.56; envelope-from=frederic.petrot@univ-grenoble-alpes.fr; helo=zm-mta-out-3.u-ga.fr X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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_MED=-2.3, 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-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 @univ-grenoble-alpes.fr) X-ZM-MESSAGEID: 1793982072566158500 From: Fr=C3=A9d=C3=A9ric P=C3=A9trot Addition of the interrupt flags of the cause registers into machine migration state. Signed-off-by: Fr=C3=A9d=C3=A9ric P=C3=A9trot --- target/riscv/machine.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/target/riscv/machine.c b/target/riscv/machine.c index 0de06e9588..080a983a19 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -114,6 +114,7 @@ static const VMStateDescription vmstate_hyper =3D { VMSTATE_UINT64(env.vsscratch, RISCVCPU), VMSTATE_UINT64(env.vsepc, RISCVCPU), VMSTATE_UINT64(env.vscause, RISCVCPU), + VMSTATE_UINT64(env.vscause_int, RISCVCPU), VMSTATE_UINT64(env.vstval, RISCVCPU), VMSTATE_UINT64(env.vsatp, RISCVCPU), VMSTATE_UINT16(env.vsiselect, RISCVCPU), @@ -126,6 +127,7 @@ static const VMStateDescription vmstate_hyper =3D { VMSTATE_UINT64(env.sscratch_hs, RISCVCPU), VMSTATE_UINT64(env.sepc_hs, RISCVCPU), VMSTATE_UINT64(env.scause_hs, RISCVCPU), + VMSTATE_UINT64(env.scause_int_hs, RISCVCPU), VMSTATE_UINT64(env.stval_hs, RISCVCPU), VMSTATE_UINT64(env.satp_hs, RISCVCPU), VMSTATE_UINT64(env.mstatus_hs, RISCVCPU), @@ -615,9 +617,11 @@ const VMStateDescription vmstate_riscv_cpu =3D { VMSTATE_UINT64(env.stvec, RISCVCPU), VMSTATE_UINT64(env.sepc, RISCVCPU), VMSTATE_UINT64(env.scause, RISCVCPU), + VMSTATE_UINT64(env.scause_int, RISCVCPU), VMSTATE_UINT64(env.mtvec, RISCVCPU), VMSTATE_UINT64(env.mepc, RISCVCPU), VMSTATE_UINT64(env.mcause, RISCVCPU), + VMSTATE_UINT64(env.mcause_int, RISCVCPU), VMSTATE_UINT64(env.mtval, RISCVCPU), VMSTATE_UINT16(env.miselect, RISCVCPU), VMSTATE_UINT16(env.siselect, RISCVCPU), --=20 2.43.0 From nobody Mon Nov 23 04:40:00 2026 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; dmarc=pass(p=quarantine dis=none) header.from=univ-grenoble-alpes.fr ARC-Seal: i=1; a=rsa-sha256; t=1793981966; cv=none; d=zohomail.com; s=zohoarc; b=i73O9HNiYyXErCas4BepxX3NjKz4l2pxlkNeSVey4muuJg1M2SBIz3l1DrPLZTy0sGnqxP3MdZnUAvLT0q3Tz9NLzST2rKZgMuEuF7xtphVPQvJf0NHJt9rcHdPiW+3+MtOAvyVq5N7QqZ45ZGREg4wAFo6Cu6e9QHm6JzXiUkY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793981966; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QZeSycisun+bok+D3hkSbd3lmJ7xK/HqSka3ujTJ1LA=; b=cQRxBs4UYnwx61eRVbf02T7Qv12EtRwYDCAkU84AxYwcN+IUWy+G+rJCwIFa7pGCLUXDsFMSM86r2RRA3CUVBcUh+hR/NNDP7cBkRLMKHmFg+hcysgD0++NctI+ykq92VAYvQfs9KJ9ZF7Fi4NL5CWhX42qUets7QA49ZHIrINE= 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793981965488231.0110234543987; Fri, 6 Nov 2026 08:19:25 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xPMej-0007Kv-HW; Fri, 06 Nov 2026 11:19:11 -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 1xPMeZ-0007EK-HI; Fri, 06 Nov 2026 11:18:59 -0500 Received: from zm-mta-out-3.u-ga.fr ([152.77.200.56]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xPMeV-0004E8-Kq; Fri, 06 Nov 2026 11:18:59 -0500 Received: from mailhub-2.u-ga.fr (mailhub-2.u-ga.fr [129.88.178.102]) by zm-mta-out-3.u-ga.fr (Postfix) with ESMTP id 6DC80402B5; Fri, 6 Nov 2026 17:18:52 +0100 (CET) Received: from smtps.univ-grenoble-alpes.fr (smtps3.u-ga.fr [195.83.24.62]) by mailhub-2.u-ga.fr (Postfix) with ESMTP id 69D86FF855; Fri, 6 Nov 2026 17:18:52 +0100 (CET) Received: from naga.tima.u-ga.fr (naga.tima.u-ga.fr [147.171.132.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: petrotf@univ-grenoble-alpes.fr) by smtps.univ-grenoble-alpes.fr (Postfix) with ESMTPSA id 383CC40056; Fri, 06 Nov 2026 17:18:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1793981932; bh=DSo6OEZ9+BpqyQqB9FFmN9tmeE9uqLngPmCiP7X0dxI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=4IqrdMfadJVLEZ3mmH/QW8JegUJKU+5rrv/xmkJPu40TLajy3eehVkb/F2v/Bola2 hM0nRFjVvIHuhKOlY3vTNX3mpRW/vYdom3tWVtWcO1zjG9uFYycydmDhjrywZKxnLX 1qZbHyZ8a0yfcVnP6S0bW5UwKr637qtuifhBoyuGW0AcG5IYcHoFJeIqA+iHpWJAN4 bEcprzxnuctDDkTMIBADu1k7iRXo7geNVxDmvVc+bD66euzOx9x7c7LuCHc9KxMTSx llrO7hlhZmdvEWNGASFCsfGp2UB57IpFjSFaH3PI8DZJ37qqmFXTFMoWni/FsaMjYU TvgDrqcjzCQFQ== From: frederic.petrot@univ-grenoble-alpes.fr To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, daniel.barboza@oss.qualcomm.com, palmer@dabbelt.com, alistair.francis@wdc.com, liwei1518@gmail.com, zhiwei_liu@linux.alibaba.com, chao.liu@processmission.com, qemu-riscv@nongnu.org, =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9trot?= Subject: [PATCH v3 4/4] target/riscv: [ms]cause tcg tests for x-rv128 Date: Fri, 6 Nov 2026 17:18:43 +0100 Message-ID: <20261106161843.2075117-5-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261106161843.2075117-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261106161843.2075117-1-frederic.petrot@univ-grenoble-alpes.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Greylist: Whitelist-UGA SMTP Authentifie (petrotf@univ-grenoble-alpes.fr) via submission-587 ACL (41) 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=lists1p.gnu.org; Received-SPF: pass client-ip=152.77.200.56; envelope-from=frederic.petrot@univ-grenoble-alpes.fr; helo=zm-mta-out-3.u-ga.fr X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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_MED=-2.3, 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-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 @univ-grenoble-alpes.fr) X-ZM-MESSAGEID: 1793981972212158500 From: Fr=C3=A9d=C3=A9ric P=C3=A9trot Add tcg tests for the rv128 mcause and scause registers. This mainly checks the behavior of the read and write instructions, as other exceptions (at least in s-mode) are catched by the semihosting mechanism. Signed-off-by: Fr=C3=A9d=C3=A9ric P=C3=A9trot --- tests/tcg/riscv64/system/meson.build | 14 ++++ tests/tcg/riscv64/test-rv128-mcause.S | 101 ++++++++++++++++++++++++++ tests/tcg/riscv64/test-rv128-scause.S | 94 ++++++++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 tests/tcg/riscv64/test-rv128-mcause.S create mode 100644 tests/tcg/riscv64/test-rv128-scause.S diff --git a/tests/tcg/riscv64/system/meson.build b/tests/tcg/riscv64/syste= m/meson.build index 3c8b018d9e..b462d5f4ab 100644 --- a/tests/tcg/riscv64/system/meson.build +++ b/tests/tcg/riscv64/system/meson.build @@ -53,6 +53,20 @@ tests +=3D { } } =20 +tests +=3D { + 'test-rv128-mcause.S': { + 'cflags': cflags, + 'qemu_args': ['-cpu', 'x-rv128', qemu_args], + } +} + +tests +=3D { + 'test-rv128-scause.S': { + 'cflags': cflags, + 'qemu_args': ['-cpu', 'x-rv128', qemu_args], + } +} + tests +=3D { 'test-vle32ff.S': { 'cflags': cflags + ['-march=3Drv64gcv'], diff --git a/tests/tcg/riscv64/test-rv128-mcause.S b/tests/tcg/riscv64/test= -rv128-mcause.S new file mode 100644 index 0000000000..5dd7b543e1 --- /dev/null +++ b/tests/tcg/riscv64/test-rv128-mcause.S @@ -0,0 +1,101 @@ +/* + * Test rv128 mcause update and access + * Author: Fr=C3=A9d=C3=A9ric P=C3=A9trot + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#define slli(rd, rs1, imm) .insn i 0b0010011, 0b001, rd, rs1, imm & 0b111= 1111 +#define srli(rd, rs1, imm) .insn i 0b0010011, 0b101, rd, rs1, imm & 0b111= 1111 + + .option norvc + + .text + .globl _start +_start: + csrr s0, misa + srli (s1, s0, 126) + li s0, 3 + bne s1, s0, fail + + # Check UXL and SXL are set to RV128 too + csrr s0, mstatus + srli (s1, s0, 32) + andi s1, s1, 0xf + li s0, 0xf + bne s1, s0, fail + + # Write some values in mcause (wlrl) + # 13 is valid for both interruptions and exceptions + li s0, 13 + csrw mcause, s0 + csrr s1, mcause + bne s1, s0, fail + + li s1, 1 + slli (s1, s1, 127) + or s1, s1, s0 + csrw mcause, s1 + csrr s0, mcause + bne s1, s0, fail + + # Write an illegal value, but as for the 32/64-bit versions, + # we keep it as it is + li s1, -1 + csrw mcause, s1 + csrr s0, mcause + bne s1, s0, fail + + # Set the trap vector + la s0, tvecall + csrw mtvec, s0 + # Do an ecall to make sure mcause is properly set + ecall + # Set the trap vector + la s0, tvbreak + csrw mtvec, s0 + # Do an ecall to make sure mcause is properly set + ebreak + li a0, 0 + j _exit + + # mtvec bit 1:0 indicate mode, 00 is Direct, what + # we want in this simple test + .align 4 +tvecall: + csrr s0, mcause + li s1, 11 # ecall from m-mode + bne s1, s0, fail + csrr s0, mepc + addi s0, s0, 4 + csrw mepc, s0 + mret + + .align 4 +tvbreak: + csrr s0, mcause + li s1, 3 # break + bne s1, s0, fail + csrr s0, mepc + addi s0, s0, 4 + csrw mepc, s0 + mret +fail: + li a0, 1 +_exit: + lla a1, semiargs + li t0, 0x20026 # ADP_Stopped_ApplicationExit + sd t0, 0(a1) + sd a0, 8(a1) + li a0, 0x20 # TARGET_SYS_EXIT_EXTENDED + + # Semihosting call sequence + .balign 16 + slli zero, zero, 0x1f + ebreak + srai zero, zero, 0x7 + j . + + .data + .balign 16 +semiargs: + .space 16 diff --git a/tests/tcg/riscv64/test-rv128-scause.S b/tests/tcg/riscv64/test= -rv128-scause.S new file mode 100644 index 0000000000..823bdbac81 --- /dev/null +++ b/tests/tcg/riscv64/test-rv128-scause.S @@ -0,0 +1,94 @@ +/* + * Test rv128 mcause update and access + * Author: Fr=C3=A9d=C3=A9ric P=C3=A9trot + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#define slli(rd, rs1, imm) .insn i 0b0010011, 0b001, rd, rs1, imm & 0b111= 1111 +#define srli(rd, rs1, imm) .insn i 0b0010011, 0b101, rd, rs1, imm & 0b111= 1111 + + .option norvc + + .text + .globl _start +_start: + # Check RV128 is running + csrr s0, misa + srli (s1, s0, 126) + li s0, 3 + bne s1, s0, fail + + # Minimal pmp settings +#define PMP_NAPOT 0x18 +#define PMP_R 0x01 +#define PMP_W 0x02 +#define PMP_X 0x04 + + li s0, -1 + csrw pmpaddr0, s0 + li s0, PMP_NAPOT | PMP_R | PMP_W | PMP_X + csrw pmpcfg0, s0 + + # Set MPP to supervisor + csrr s0, mstatus + li s1, ~0x1800 // mask + and s1, s1, s0 + li s0, 0x0800 // supervisor + or s1, s1, s0 + csrw mstatus, s1 + # Change to supervisor mode + la s0, 1f + csrw mepc, s0 + mret +1: + # Check UXL is set to RV128 + csrr s0, sstatus + srli (s1, s0, 32) + andi s1, s1, 0xf + li s0, 0x3 + bne s1, s0, fail + + # Write some values in scause (wlrl) + # 13 is valid for both interruptions and exceptions + li s0, 13 + csrw scause, s0 + csrr s1, scause + bne s1, s0, fail + + li s1, 1 + slli (s1, s1, 127) + or s1, s1, s0 + csrw scause, s1 + csrr s0, scause + bne s1, s0, fail + + # Write an illegal value, but as for the 32/64-bit versions, + # we keep it as it is + li s1, -1 + csrw scause, s1 + csrr s0, scause + bne s1, s0, fail + + li a0, 0 + j _exit + +fail: + li a0, 1 +_exit: + lla a1, semiargs + li t0, 0x20026 # ADP_Stopped_ApplicationExit + sd t0, 0(a1) + sd a0, 8(a1) + li a0, 0x20 # TARGET_SYS_EXIT_EXTENDED + + # Semihosting call sequence + .balign 16 + slli zero, zero, 0x1f + ebreak + srai zero, zero, 0x7 + j . + + .data + .balign 16 +semiargs: + .space 16 --=20 2.43.0