From nobody Mon Nov 23 04:31:58 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=1795177734; cv=none; d=zohomail.com; s=zohoarc; b=VBAnbVK65UrmrSvtuV3Ew/UA/VlhrX9KSIxel9QOfviwSgAUxOenTW1S3kGVa9YpKeAuZlsHbBoa6HNo2nits++dqH75QX2UvFp5iyUmOkxVouuc3TKHxWc5xDe7DU9izKEqc8wZqa6zOcgrZYYHfeH6/kUAb6Ymp8G6h95QQx8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795177734; 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=yGWLuZ3z2vGKrSdP7y7FmQ9yp6eSCJoutQWNB0MZdXs=; b=A3zeBKDayI92WbJpk79XDSWHGOqFWa6cXyDJ4xRMy41YaGDQQ80j8qQsmxRttBUNpAFH1/7m3D7gRFBwf1aBbyoQX6wkES7cRAgBY+Hpl4+Byuvm6SJFI4FIwiSHQ+Aw7AlyHFlhHhwLq+fk92pnyZpUxy3L/bQ/Nz0/YrSu7Rg= 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 1795177733970191.50453840853106; Fri, 20 Nov 2026 04:28:53 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xUNiX-0007Pr-Li; Fri, 20 Nov 2026 07:27:50 -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 1xUNiV-0007P4-W3; Fri, 20 Nov 2026 07:27:48 -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 1xUNiT-0001rl-NN; Fri, 20 Nov 2026 07:27:47 -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 54CC6402D1; Fri, 20 Nov 2026 13:27:43 +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 4FD3CFF856; Fri, 20 Nov 2026 13:27:43 +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 2324814009A; Fri, 20 Nov 2026 13:27:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1795177663; bh=HpEAwMAk+BNDyv+85ccpf+d6FYmp6oK9imFWf9dSmAM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rxqk1g80kfMEvuMAQ7DBlknrgtKqFV6xz2ciEqD1uj6u88etXpkmTUjtgYy1whmAR yX+0T5tqYsOIS2paxiztMOGMKmjBY/9gvNSzPtc1pvDm3ArI66p9oLCGMbB3sewK8C 2HqoQbG2sS1RMrMfAqs+yCeag1cAmhM54gNg4vfz1W8LEjrfuoUIvIAg3qoNgA25fa xFXMl74dU7sZzZyksi7QtlIWi1YWec2bmhJ2Ak4EZRBBWfmTFFemhJ9XXS3R+jMDAm gAworYMHLfhLLGU1oerNoftNYUmV5Gj7F+EDUGHG9aNYdIoY4K28wAB1oYs6oxHgVc wtdIjlTS7tDQA== 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 v5 1/4] target/riscv: fix pmp sanitize error for x-rv128 Date: Fri, 20 Nov 2026 13:27:38 +0100 Message-ID: <20261120122741.33727-2-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261120122741.33727-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261120122741.33727-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: 1795177738136158500 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:31:58 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=1795177721; cv=none; d=zohomail.com; s=zohoarc; b=SeQ92c0M1PQFCJXGJS+4M9iHTHv+CW5xuKidlhRw7I1LisDh2FF7Jck68HTu8TUhhK+mSzX0jF8eqpCDOecE7dHnSscl5EhB9auIR4DTyFs3mTSR4kcMAOybIVdyjV5Wd87Q0JnS7MW+oYOtNlkSfFcRk4T7e/Yw+B/4Y7X1BJM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795177721; 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=vdN5qdE1VXLuivZkKDYpXMrEDXwRXsUjuS8/r15XqVg=; b=hL0TnLFbKeegUz2fTet0T7Lv2LRWu4PQZ44jTGImFhdbcynq1JcBlK0r5nUZVwkKkTZonVoWOibYnVtinYAgcAdLvKcBy6TA/hKyBZuLY3n/63PXhz845TKbKjxzjVkc1CDV3tLDA6tsz6W/1qgb3YW8CG2zsATbw/H4yAbP+60= 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 1795177720736438.2517127958406; Fri, 20 Nov 2026 04:28:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xUNiY-0007QW-CK; Fri, 20 Nov 2026 07:27:50 -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 1xUNiW-0007PA-7b; Fri, 20 Nov 2026 07:27:48 -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 1xUNiT-0001sr-Pn; Fri, 20 Nov 2026 07:27:47 -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 85A69402A8; Fri, 20 Nov 2026 13:27:43 +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 81B22FF853; Fri, 20 Nov 2026 13:27:43 +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 5444414009A; Fri, 20 Nov 2026 13:27:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1795177663; bh=JVtaRwl/JnoGFEJiYpnWZOcM95itPku7oGfUkJR9Tco=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oQ5yURNOTFlsB9f5lv5kYCD3myvuP5REtByckjSp/wEFDzASFW/x8SQdBpVCRhCZw aEOVc4RiaxKj5xIkD+C3kR7Z0bg43sJYGp/KEZ24Ac/9pf/sIjdCsiVdfWkIMu0KDB 0Wff2SZ3Jhh0AwcZLb/39MPIKcQOj2iSwsY0ir4OciqVN3aN1zI5abuEqiV2gA0ry1 qydMn+SHLV4AUDkbdhU+8cSs7LcMad2v0BBzDCMrc2Ttgv14YPhEPXQbPEEx7E3hs4 2Wl41Hy4CSH2/JJfw42In/xx0rv2d4BLFUJ+eALAQUXkEiR4wfqf382a7amT+PTPof h6okIKbKifCow== 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 v5 2/4] target/riscv: fix riscv_cpu_do_interrupt sanitize error for x-rv128 Date: Fri, 20 Nov 2026 13:27:39 +0100 Message-ID: <20261120122741.33727-3-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261120122741.33727-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261120122741.33727-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: 1795177725631158500 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 RV3= 2 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:31:58 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=1795177740; cv=none; d=zohomail.com; s=zohoarc; b=YbiteDrmxGVObFVYFU5mrPk0bEV6xwL1k89sPC5rvoNMODEkCtvMBaFEwK6pugldOvSmC9wyVZ+OUklyOwfp5FeHfQKRi1gETO8ii30e58fGPc63b8Ux7Hw/uyfFm/ypPz70Pj8OLQQHqRZ6MpsBHHoYA+bKz9ytGn3AskNFwRM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795177740; 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=kWjjzfLLVTQagqKfKIOL+ibCJ2wvaJO8d0ES6k3yw3w=; b=BdR/0WyjtZH/Bjcwd1G5ZqDcGIGSbiq8oIFWJxxO7qXqwqG55uiEpnn1pfOi/U4OEab1E1RFUkvna6gNycShcfe1Ro6bPVN1O6nFVAsoqCHcUu/mtRnR5Tbmia8dI4d74/acTu36p3tuBTWBO/BtNwx57j9EuMiYyieTXKRrnM4= 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 1795177739902666.4193820206794; Fri, 20 Nov 2026 04:28:59 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xUNia-0007RJ-3F; Fri, 20 Nov 2026 07:27:52 -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 1xUNiX-0007Pv-Hr; Fri, 20 Nov 2026 07:27:49 -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 1xUNiT-0001u4-Pi; Fri, 20 Nov 2026 07:27:49 -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 BB7A9402D0; Fri, 20 Nov 2026 13:27:43 +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 B6E58FF856; Fri, 20 Nov 2026 13:27:43 +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 8633D14009A; Fri, 20 Nov 2026 13:27:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1795177663; bh=S3CjdskYwdxbQq790YTMSLiPZExQwpLBCndgFohtgas=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=epRqfuPQWkRWffCBDB8Ku4NKf4wMOhys2o7o6/pH4U/Q2Y120mOu6ExEnwH8lUTYI 508gKOx6OvRQFlMCX99YDxfgMeF1K78c6OOJG1FKXzmmpN+3o/XHwLcQhqcuLnStwg 4jI/p0eRdpl0D4L8tOKkpT7gbwY4S0FU+714Ii7p7C2zoKZyuSk1astAR731tT0Tm9 fdY4C5ibArMfy2avCmYiaQhkOcN8ZQrTMd1yvINFqVvhv62I027Xt1zunJic5FfiPf bHw0pcRey4fkOgiuoIXKZoZ6xluRclUQXOLfaPXNlxbExfUic8TgN8mvC/vaY8NdYv ySSUVHCPGLQew== 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 v5 3/4] target/riscv: Update machine state Date: Fri, 20 Nov 2026 13:27:40 +0100 Message-ID: <20261120122741.33727-4-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261120122741.33727-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261120122741.33727-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: 1795177741907158500 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 | 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:31:58 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=1795177738; cv=none; d=zohomail.com; s=zohoarc; b=WqGepICGLLaA8qRDJPtJheyXak6tdlOOXIxmFAKEOtl52jn723XxzD46ICaGsn2yeHjfMv4gfTNDbq2JYS+9jOQsTESLxN7i0wf9yNF5IQp5SCAf9WPu5du8btP3y0ffkJpm3JTiyuNzAuV/b5qj/mfAHNHZp+MRT2tcykD7ZVQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795177738; 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=mJA9mcmDz074Ca22zAlqM8aRWgqlghDHqhPo3pP89a5JvFu4cGvjY4luRqL8yo5GPtEnnZZBlsbdVyDsW/TttnqwaeqKvMdbkpwP2o3xbOtwTCSnH0F/quZDvENBmQ8Mt9PQ1tJbYs1iGI7G2YFSprkBmCDGoIeMj4l/2TND1sg= 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 1795177737870386.74831481614547; Fri, 20 Nov 2026 04:28:57 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xUNia-0007RR-PB; Fri, 20 Nov 2026 07:27:52 -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 1xUNiX-0007Q6-MX; Fri, 20 Nov 2026 07:27:50 -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 1xUNiT-0001uo-PR; Fri, 20 Nov 2026 07:27:49 -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 EE9AB402D4; Fri, 20 Nov 2026 13:27:43 +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 EAA94FF853; Fri, 20 Nov 2026 13:27:43 +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 BB75614009A; Fri, 20 Nov 2026 13:27:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=univ-grenoble-alpes.fr; s=2020; t=1795177663; bh=JITvaXAjAGl/BQihGVlGKy1A2JFdSpr1zcmjdEIVI6c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f5PS1cnffuaTLGbxetdOjlKXjeyS7rshSE/J3YiUtVGwIgWkFxBDxozptmK9OAzRF VMEuTrfgmtfpguKfTVzJQOYUVJbFkpY80WAmpeMDUAAvWkXpVO38j5B1LCVhq3llhq yDshp9AAZTNrpCU6MxpRkvBf9K/NxML9U0uU/XDWfkUudAxzwV3E6KSOz5CWACcezf mnrwu5mZ7Vp+kqADZhzB5XXrAk7vSBjhpEa9JykqvizHHmWKzmTn5e5Xfm+pxuF3Cy wKfEbWgbxZvd2/KuxK+J8CMZWEE5dF0Q6FCEakOwoJeof1kiMYe8ggSSyehbc5mc6Q BQELRFzmtiiww== 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 v5 4/4] target/riscv: [ms]cause tcg tests for x-rv128 Date: Fri, 20 Nov 2026 13:27:41 +0100 Message-ID: <20261120122741.33727-5-frederic.petrot@univ-grenoble-alpes.fr> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261120122741.33727-1-frederic.petrot@univ-grenoble-alpes.fr> References: <20261120122741.33727-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: 1795177739979158500 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