From nobody Mon May 13 00:41:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1709908361; cv=none; d=zohomail.com; s=zohoarc; b=UGP51GEMHfnKsh7TIDZDYefmMfoLr6VOvzTI/r2UDWKhHfll274Lf6+Lv5wDZz5QyvdrWM7xWI9rpXhdi5YtchyAwKU8vplU7XE0sPiFmgdvexGstsMxK+TxlD4260xmtSy8YkZC0yea3MvKoEpkZF959PaQ5uWuvT+27pj/Wvo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1709908361; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:Sender:Subject:Subject:To:To:Message-Id; bh=RmuVRJyUuFsUk698YQ862KDTSVHI7AFBobs2581IKEI=; b=XA8Vtv/NgcwdNJM0e37c024XdCaYnvHeeksA0UlCCOxCllameI3VE6GkXel4odbHVBUJHZjvGpu9YyKasEQnIchjF1kJSRVHsWPjdy+SkCR3TiK2SzMO38aRDRcyIMbGxkxVvKxQEvwF4o6SF+tN25sUSj8A23gIfOSnkpSAotc= ARC-Authentication-Results: i=1; mx.zohomail.com; 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1709908360879525.7484343155224; Fri, 8 Mar 2024 06:32:40 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ribGO-0000bp-Jz; Fri, 08 Mar 2024 09:31:57 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1riWZq-0004hR-2M; Fri, 08 Mar 2024 04:31:50 -0500 Received: from 60-248-80-70.hinet-ip.hinet.net ([60.248.80.70] helo=Atcsqr.andestech.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1riWZl-0000FS-UD; Fri, 08 Mar 2024 04:31:40 -0500 Received: from Atcsqr.andestech.com (localhost [127.0.0.2] (may be forged)) by Atcsqr.andestech.com with ESMTP id 4288fwTC081314; Fri, 8 Mar 2024 16:41:58 +0800 (+08) (envelope-from yumin686@andestech.com) Received: from mail.andestech.com (ATCPCS16.andestech.com [10.0.1.222]) by Atcsqr.andestech.com with ESMTP id 4288fXsh081055; Fri, 8 Mar 2024 16:41:33 +0800 (+08) (envelope-from yumin686@andestech.com) Received: from lubuntu-vb.andestech.com (10.0.12.42) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.498.0; Fri, 8 Mar 2024 16:41:33 +0800 To: , , , , , CC: , , Yu-Ming Chang Subject: [PATCH] target/riscv: raise an exception when CSRRS/CSRRC writes a read-only CSR Date: Fri, 8 Mar 2024 16:40:14 +0800 Message-ID: <20240308084014.22054-1-yumin686@andestech.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.0.12.42] X-DNSRBL: X-SPAM-SOURCE-CHECK: pass X-MAIL: Atcsqr.andestech.com 4288fwTC081314 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=lists.gnu.org; Received-SPF: pass client-ip=60.248.80.70; envelope-from=yumin686@andestech.com; helo=Atcsqr.andestech.com X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, RDNS_DYNAMIC=0.982, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, TVD_RCVD_IP=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 08 Mar 2024 09:31:54 -0500 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Yu-Ming Chang From: Yu-Ming Chang via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1709908363263100003 Content-Type: text/plain; charset="utf-8" Both CSRRS and CSRRC always read the addressed CSR and cause any read side effects regardless of rs1 and rd fields. Note that if rs1 specifies a regis= ter holding a zero value other than x0, the instruction will still attempt to w= rite the unmodified value back to the CSR and will cause any attendant side effe= cts. So if CSRRS or CSRRC tries to write a read-only CSR with rs1 which specifies a register holding a zero value, an illegal instruction exception should be raised. Signed-off-by: Yu-Ming Chang --- target/riscv/cpu.h | 3 +++ target/riscv/csr.c | 18 +++++++++++++++--- target/riscv/op_helper.c | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 5d291a7092..087ef64889 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -710,6 +710,9 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, void riscv_cpu_update_mask(CPURISCVState *env); bool riscv_cpu_is_32bit(RISCVCPU *cpu); =20 +RISCVException riscv_csrr(CPURISCVState *env, int csrno, + target_ulong *ret_value, + target_ulong new_value, target_ulong write_mask); RISCVException riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, target_ulong new_value, target_ulong write_mask= ); diff --git a/target/riscv/csr.c b/target/riscv/csr.c index d4e8ac13b9..3e49cf0df1 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -4306,7 +4306,7 @@ static RISCVException rmw_seed(CPURISCVState *env, in= t csrno, =20 static inline RISCVException riscv_csrrw_check(CPURISCVState *env, int csrno, - bool write_mask) + bool write) { /* check privileges and return RISCV_EXCP_ILLEGAL_INST if check fails = */ bool read_only =3D get_field(csrno, 0xC00) =3D=3D 3; @@ -4328,7 +4328,7 @@ static inline RISCVException riscv_csrrw_check(CPURIS= CVState *env, } =20 /* read / write check */ - if (write_mask && read_only) { + if (write && read_only) { return RISCV_EXCP_ILLEGAL_INST; } =20 @@ -4415,11 +4415,23 @@ static RISCVException riscv_csrrw_do64(CPURISCVStat= e *env, int csrno, return RISCV_EXCP_NONE; } =20 +RISCVException riscv_csrr(CPURISCVState *env, int csrno, + target_ulong *ret_value, + target_ulong new_value, target_ulong write_mask) +{ + RISCVException ret =3D riscv_csrrw_check(env, csrno, false); + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + + return riscv_csrrw_do64(env, csrno, ret_value, new_value, write_mask); +} + RISCVException riscv_csrrw(CPURISCVState *env, int csrno, target_ulong *ret_value, target_ulong new_value, target_ulong write_mask) { - RISCVException ret =3D riscv_csrrw_check(env, csrno, write_mask); + RISCVException ret =3D riscv_csrrw_check(env, csrno, true); if (ret !=3D RISCV_EXCP_NONE) { return ret; } diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index f414aaebdb..76b0f5371d 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -51,7 +51,7 @@ target_ulong helper_csrr(CPURISCVState *env, int csr) } =20 target_ulong val =3D 0; - RISCVException ret =3D riscv_csrrw(env, csr, &val, 0, 0); + RISCVException ret =3D riscv_csrr(env, csr, &val, 0, 0); =20 if (ret !=3D RISCV_EXCP_NONE) { riscv_raise_exception(env, ret, GETPC()); --=20 2.34.1