From nobody Mon Nov 23 04:32:02 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=1795107505; cv=none; d=zohomail.com; s=zohoarc; b=GWk3uGVd8eBqdUi/eecGJzAulFPfnIjQTY9sNpOs8sJMOz/hkpMULk4pN/QuK7SmIeK0UQ7QXkkX8XPBqXDd+DrClgGDKtClr0W0MRj9rOWjb7+XKO+JBNO3gPsmeXdScsOgmnDnzjMsjwoRiDvFf8IjhazHW1jHs9DUAx62OjQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795107505; 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=nNdIeamjxRs2SYq+l0yLEqIyyY31U5Szd9hFkMzk6v8=; b=HjpxWwWc+btBqS+yxtN31o+M3PK9DewfD8AX+dyD0sIk3Dy+Ajap4PGCBSmDq4MoQOSCQsbMVrAPRpccZMK+j397cHeXOHqfv1FJHBDI0CKp4Pn8nxAQze2fap5YZ3p71QILtJtKyvOsl794rfnG+7y9FJBqYWSQg2TSslXveOg= 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 1795107505484264.0559029757935; Thu, 19 Nov 2026 08:58:25 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU5Rz-0001qV-CG; Thu, 19 Nov 2026 11:57:31 -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 1xU5Rv-0001oi-NV; Thu, 19 Nov 2026 11:57:28 -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 1xU5Rs-0002pd-5G; Thu, 19 Nov 2026 11:57:27 -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 D4639402A9; Thu, 19 Nov 2026 17:57:19 +0100 (CET) Received: from smtps.univ-grenoble-alpes.fr (smtps2.u-ga.fr [152.77.18.2]) by mailhub-1.u-ga.fr (Postfix) with ESMTP id D0B53FF853; Thu, 19 Nov 2026 17:57:19 +0100 (CET) Received: from naga.tima.u-ga.fr (pers-185-75.vpn-inp.grenoble-inp.fr [147.171.185.75]) (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 A673214009A; Thu, 19 Nov 2026 17:57:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1795107439; bh=/Hht6LHAVZ2T77HbnJUrY2oJzbgO6GjwFYDyO2B4EpE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IZsq7ENOxxztaz+vXwJ+Orex59eAIfKq+nYvZZLdbo9FJn6+QduQFnVEIg+x21JdB mqKQbrYNu3KvFjm2v9A+LIZUtc3djqtjNp5PGpst5Ha5YuE+RRzO36sjaNCrMG/ZNI et7/fkE8LfLlFsqfBkXiwcqUFTRFIutYCri8UAsf3vFkxNqaxpLBWGkYB7UlzsQJuN uj/BVigasIBlaCgdYHua6ZUDPzJidxYE80kOQ+IUAz/0qoZY9pupTkIqf7oNro/Nmy aLGEtCare+UhFnvF7XlGufIAYbsbILZTC1u4m1lWCd89oJdkxwuVye97MslEaR5cRh 9vLDs1o35qgQQ== From: frederic.petrot@univ-grenoble-alpes.fr To: qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, peter.maydell@linaro.org, daniel.barboza@oss.qualcomm.com, palmer@dabbelt.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 v4 1/4] target/riscv: fix pmp sanitize error for x-rv128 Date: Thu, 19 Nov 2026 17:57:13 +0100 Message-ID: <20261119165716.130978-2-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261119165716.130978-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261119165716.130978-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: 1795107509340158500 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. Fixes: 444cffd37b69 ("target/riscv: Extend PMP region up to 64"). 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:32:02 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=1795107521; cv=none; d=zohomail.com; s=zohoarc; b=a9Uh05On/1oxJVGHIUEntgWMJ569I8IgFGB3l6v9wzunv7hQhfGjLrULwKZLrrCiPJAXgoTZaj/nrXGnIYHzOoEFSe3nL3h5kEyVTrxBkJ5wj7aP7H2vZHXCOhNHbPagy3QFAgAKLOgw3tfMZX7UUMRj5CEQ9GSf+vAyqQ017Pw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795107521; 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=on0hT03pFW26f6J5RjitRVENOF8/atR7dPqqTTbkAg8=; b=JbWzigj0RNmoV7HCTuotJUdvZNf8afQU7s/K2gJ1mhL8hHtP+YxwyOQLUxfUcRe1U5EkwfbLZ2lLIyYdjhw+PZPywowtt/6vx7BnS06Lpt+z5dZJ1A7B7dLbH+T9HsSQJPVFm6VO3tv2D14qlUVUPsQUfwQvZujYiiTA6W/3n6g= 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 1795107521230350.1744601857971; Thu, 19 Nov 2026 08:58:41 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU5S0-0001rI-Bn; Thu, 19 Nov 2026 11:57:32 -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 1xU5Rw-0001p2-37; Thu, 19 Nov 2026 11:57:28 -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 1xU5Rs-0002pg-70; Thu, 19 Nov 2026 11:57:27 -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 124A540282; Thu, 19 Nov 2026 17:57:20 +0100 (CET) Received: from smtps.univ-grenoble-alpes.fr (smtps2.u-ga.fr [152.77.18.2]) by mailhub-1.u-ga.fr (Postfix) with ESMTP id 0F0E0FF853; Thu, 19 Nov 2026 17:57:20 +0100 (CET) Received: from naga.tima.u-ga.fr (pers-185-75.vpn-inp.grenoble-inp.fr [147.171.185.75]) (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 D550114009A; Thu, 19 Nov 2026 17:57:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1795107440; bh=twfUhREwN/LUmEgM8KCzQQc2kGIVEKnG8e17hhue0bE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U9aLbYMqqLSHe5Ru28cSp+QEgpFSkg827QJayESBazAwZY6T3bh46oJaZllzcC1h6 g2nQWyircmOIIg5+t8425ntLVpoUOQbyMViSV/+aaIm+It4XHYHpk9ZdRT5QGpktml SlBxNNbWLYfxx1TTk0m218jYtYa5rHva4bAwBKDpZXdCNJ4X12O/BGO028uGEc+u9Q N1H2KN5fRGM2P4gHMcwN5ll/ubS0LaZvCknICVONW3pJveGzc/I8H/76Zvu2NtLJJP gne6phVdZx0ChSTEbjvtH87F9zhHu3yry3rPst7qITQ/X72rhKgzKt8G7r/KVAB6L+ fYpWSz0z4Oz/w== From: frederic.petrot@univ-grenoble-alpes.fr To: qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, peter.maydell@linaro.org, daniel.barboza@oss.qualcomm.com, palmer@dabbelt.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 v4 2/4] target/riscv: fix riscv_cpu_do_interrupt sanitize error for x-rv128 Date: Thu, 19 Nov 2026 17:57:14 +0100 Message-ID: <20261119165716.130978-3-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261119165716.130978-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261119165716.130978-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: 1795107523019158500 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. Fixes: 58597bfeab45 ("target/riscv: Correct mcause/scause bit width for RV32 in RV64 QEMU"). 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 15c27f4ce4..3e2f739c4e 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:32:02 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=1795107491; cv=none; d=zohomail.com; s=zohoarc; b=TTFZjWsNPfaZuNwpDCqhu3sfSmDz4ifm9RhFXKb8GgQuCTxD1GplkfDHYjQUPWkrVI12Z/djUTAiIWAJBj93/XWX7BbPxu8xCKIqAq0KXcQYCw82J23lIQgknsENHxfi53inY7BmNG8jWbJW3aJk9ycNjct9xGNW+P9D2AzULg0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795107491; 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=QZYXlOhHJEowwaDDDO8tXGMMdtIAiQNTXQxAbYj1H48=; b=cjRcbYnon5/yHWM4qS0F+Yeav1pLT4D0aWUI/QtSoGcWS5CZm3QLRg3eKCrqy/aN3XDo3XpGeMCdqH7EaBfjH5zaZm4AeapzIUS82Ftscj5JxSwHldGOykB/LhScHyGfqB7eDSu/DT1M0E/ineV3l7zDqNkgs4Dt2JKVY1gR6OM= 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 1795107490990104.12155783249216; Thu, 19 Nov 2026 08:58:10 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU5Rz-0001qR-C4; Thu, 19 Nov 2026 11:57:31 -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 1xU5Rv-0001oh-ND; Thu, 19 Nov 2026 11:57:28 -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 1xU5Rs-0002pk-4X; Thu, 19 Nov 2026 11:57:27 -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 459D1402AD; Thu, 19 Nov 2026 17:57:20 +0100 (CET) Received: from smtps.univ-grenoble-alpes.fr (smtps2.u-ga.fr [152.77.18.2]) by mailhub-2.u-ga.fr (Postfix) with ESMTP id 41743FF856; Thu, 19 Nov 2026 17:57:20 +0100 (CET) Received: from naga.tima.u-ga.fr (pers-185-75.vpn-inp.grenoble-inp.fr [147.171.185.75]) (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 14EB014009A; Thu, 19 Nov 2026 17:57:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1795107440; bh=z6p7JaQbZKeuc/u6rQZZbIFc3VvMd1bMQtvtqLKgvXw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VvmVNKH1W+o6xNfamHeRgP5AJMc2JkS5qjmSJpZeoYbfZMbPfO7mf864bR6Qly7o4 +vp8tUWD0iBy2K/mYt5rssRmnl/djSwhVMfo8mquR9lNsF0c/wBX/N28U9GLybg5zh jzEt3ZyLS10b7HpMRK/JcL+oqQuDeXG0bvgraa57TsXyI9fUPA2xfKS3PPc5nRvIa7 TrCL+Zytq5y65BcCukgsjUHWHQPfKug2VyZZBydWbY9aUrFbflH4DazBB6/v1x2y1z YiiQjpTCPVw+uOsSC7gb3G4/B+wH2L0LlTeknlRCRQZrx8Q6AGBxc4tOQEGvAtPmPK iFUkUEYEt3fEg== From: frederic.petrot@univ-grenoble-alpes.fr To: qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, peter.maydell@linaro.org, daniel.barboza@oss.qualcomm.com, palmer@dabbelt.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 v4 3/4] target/riscv: Update machine state Date: Thu, 19 Nov 2026 17:57:15 +0100 Message-ID: <20261119165716.130978-4-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261119165716.130978-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261119165716.130978-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: 1795107496014158500 From: Fr=C3=A9d=C3=A9ric P=C3=A9trot Addition of the interrupt flags of the cause registers into machine migration state. Fixes: b938bdf58f9b (target/riscv: fix riscv_cpu_do_interrupt sanitize error for x-rv128) Signed-off-by: Fr=C3=A9d=C3=A9ric P=C3=A9trot --- target/riscv/machine.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/target/riscv/machine.c b/target/riscv/machine.c index 0de06e9588..0443841ec4 100644 --- a/target/riscv/machine.c +++ b/target/riscv/machine.c @@ -88,8 +88,8 @@ static bool hyper_needed(void *opaque) =20 static const VMStateDescription vmstate_hyper =3D { .name =3D "cpu/hyper", - .version_id =3D 5, - .minimum_version_id =3D 5, + .version_id =3D 6, + .minimum_version_id =3D 6, .needed =3D hyper_needed, .fields =3D (const VMStateField[]) { VMSTATE_UINT64(env.hstatus, RISCVCPU), @@ -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), @@ -574,8 +576,8 @@ static const VMStateDescription vmstate_mseccfg =3D { =20 const VMStateDescription vmstate_riscv_cpu =3D { .name =3D "cpu", - .version_id =3D 12, - .minimum_version_id =3D 12, + .version_id =3D 13, + .minimum_version_id =3D 13, #ifdef CONFIG_KVM .pre_load =3D riscv_cpu_kvm_pre_load, #endif @@ -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:32:02 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=1795107492; cv=none; d=zohomail.com; s=zohoarc; b=IJJhDMy33K3KDBrj/UVLnB7vJCOl67oB2gnTow/97gDj0GSPSpa6FzCMJqWXoDxnZHJUdg5b9bevZPwgSGBamo8qTdl1fziQRbGHki+Xr9XWQw3jPxg+QxA/M4E8DYVYTTlMztqfMEnimPkm1sn+c1Q1A+Y47ElHFEXHtrJpN1Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795107492; 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=lmgXH4Ams0xabp+W6Z5CrKdFVLs4fUvk6gjBAwqEvtQ=; b=VIk8RJNhdMTLK4p/NyNPlFpVV3yRla6j0qBrHzuc2lKMQv5FOK4L1LSz95H7gygIbNy53J5WzcUv5oWsEGGffcjm8zRW1rNarmvv/Gx6o8etXkHbTyn5aHhxKF0IDiRQunGcJcLDa8SBV5cZ8SrUoM65b4uLDy3A8T6QNNLzLKc= 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 1795107491585517.2397300419622; Thu, 19 Nov 2026 08:58:11 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xU5Ry-0001qA-91; Thu, 19 Nov 2026 11:57:30 -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 1xU5Ru-0001oN-BK; Thu, 19 Nov 2026 11:57:26 -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 1xU5Rs-0002pn-1F; Thu, 19 Nov 2026 11:57:26 -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 77EF9402B3; Thu, 19 Nov 2026 17:57:20 +0100 (CET) Received: from smtps.univ-grenoble-alpes.fr (smtps2.u-ga.fr [152.77.18.2]) by mailhub-2.u-ga.fr (Postfix) with ESMTP id 73AD1FF856; Thu, 19 Nov 2026 17:57:20 +0100 (CET) Received: from naga.tima.u-ga.fr (pers-185-75.vpn-inp.grenoble-inp.fr [147.171.185.75]) (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 467C414009A; Thu, 19 Nov 2026 17:57:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1795107440; bh=JITvaXAjAGl/BQihGVlGKy1A2JFdSpr1zcmjdEIVI6c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vjtOYFYXluyJxCIaSg7IFJiHqg9zMk6T3BGwbr4BQOqCRKjgNPWrUnsXyRGgGlwPr Zhr4+GyEXo6INsMJr7ArIPoigjZLxoW42grxK3A1Dwiiu2S8KVrIq+GIm3Khs2oKXX vJw4XCij7FazBTNewbHFeYzONKguLgGAQJKtijDBqvafBFvCD60mQ4GCMXI4ZGioil XFkBeAZz5F1SrH4GtnqIkORK58dK7UTpSA2MXgNbwcheXrjjTrE7ifdncfyqi1nWjF POha8CONDcrEAXg6PSJ75fpvWWL1cd5f4PkAeEoRThWHgvrJowcaZRo1+yWoqcK6gh uYJQlJpyeM4Kw== From: frederic.petrot@univ-grenoble-alpes.fr To: qemu-devel@nongnu.org Cc: alistair.francis@wdc.com, peter.maydell@linaro.org, daniel.barboza@oss.qualcomm.com, palmer@dabbelt.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 v4 4/4] target/riscv: [ms]cause tcg tests for x-rv128 Date: Thu, 19 Nov 2026 17:57:16 +0100 Message-ID: <20261119165716.130978-5-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261119165716.130978-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261119165716.130978-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: 1795107496077158500 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 9edab1fd7a..e267fb25ab 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