From patchwork Thu Jul 18 05:43:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DongdongZhang X-Patchwork-Id: 1961901 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=Pw6jMUrr; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WPhZp3w19z20FC for ; Thu, 18 Jul 2024 15:44:28 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=arRODa51EWmMz1qRpo9CFqSMKMVUoaWNIka8757rXog=; b=Pw6jMUrrJoiCAndTPICtRx8NHE rcvNyBTdY5sw6sF1rofUXaH+6TgSlhlvFMoNMUiIHQbesNUyHdOXc5kLo7LjnbE3m2Gd4WjXrmTbQ 4VWXtQ5pW88JHEqIR1a5hpbSUtWV7kDVrq3/eTybawgj7fvOp2I0gjFNdbm2wxQ8qT+PKvwUGqTxT Tm0/jeTGyAzeY6eYDa+lcyS+9wEDRQoVqrINg10uqNIiAzWeU245WUj4M6Nkbc1vBf0YFzsUUMpj4 ttN3v+evgfbwH43Dwt8UvjTAL0DpR2iN+aKODEYQithyUvMxIYI0Gfb8PUjz1v7uey1Kc7ysob36I 7mFQ4g/g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sUJwA-0000000Fr1S-2SXc; Thu, 18 Jul 2024 05:44:18 +0000 Received: from sgoci-sdnproxy-4.icoremail.net ([129.150.39.64]) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sUJw6-0000000Fr0T-3us9 for opensbi@lists.infradead.org; Thu, 18 Jul 2024 05:44:16 +0000 Received: from ubuntu18.eswin.cn (unknown [10.12.192.173]) by app1 (Coremail) with SMTP id TAJkCgBX+OSfq5hmWG4WAA--.27394S5; Thu, 18 Jul 2024 13:44:08 +0800 (CST) From: zhangdongdong@eswincomputing.com To: opensbi@lists.infradead.org Subject: [PATCH v2 1/1] lib:sbi: Enhance CSR Handling in system_opcode_insn Date: Thu, 18 Jul 2024 13:43:57 +0800 Message-Id: <20240718054357.18756-2-zhangdongdong@eswincomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240718054357.18756-1-zhangdongdong@eswincomputing.com> References: <20240718054357.18756-1-zhangdongdong@eswincomputing.com> X-CM-TRANSID: TAJkCgBX+OSfq5hmWG4WAA--.27394S5 X-Coremail-Antispam: 1UD129KBjvJXoWxWF15Wr4DXF45KF4kCw45KFg_yoWrAFy8pF s8CFn8Gr4UtFs7WayftFy5Cr1fJw4rGr47AFnxurZakw47J3s5GFy0gw4YyrZrGrW7Gr48 uF42kryUury3X37anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPj14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r1Y6r1xM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwAKzVCY07xG64k0F24lc2xSY4 AK6svPMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAF wI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc4 0Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AK xVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr 1lIxAIcVC2z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjfUO89NUUUU U X-CM-SenderInfo: x2kd0wpgrqwvxrqjqvxvzl0uprps33xlqjhudrp/1tbiAgECCmaXkbkbywABsD X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240717_224415_434871_E2734994 X-CRM114-Status: GOOD ( 11.19 ) X-Spam-Score: -0.6 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Dongdong Zhang - Completed TODO in `system_opcode_insn` to ensure CSR read/write instruction handling. - Refactored to use new macros `GET_RS1_NUM` and `GET_CSR_NUM`. - Updated `GET_RM` macro and replaced hardcoded [...] Content analysis details: (-0.6 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [129.150.39.64 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [129.150.39.64 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 1.3 RCVD_IN_VALIDITY_RPBL RBL: Relay in Validity RPBL, https://senderscore.org/blocklistlookup/ [129.150.39.64 listed in bl.score.senderscore.com] X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: ajones@ventanamicro.com, Dongdong Zhang MIME-Version: 1.0 Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Dongdong Zhang - Completed TODO in `system_opcode_insn` to ensure CSR read/write instruction handling. - Refactored to use new macros `GET_RS1_NUM` and `GET_CSR_NUM`. - Updated `GET_RM` macro and replaced hardcoded funct3 values with constants (`CSRRW`, `CSRRS`, `CSRRC`, etc.). - Removed redundant `GET_RM` from `riscv_fp.h`. - Improved validation and error handling for CSR instructions. This patch enhances the clarity and correctness of CSR handling in `system_opcode_insn`. Signed-off-by: Dongdong Zhang Reviewed-by: Anup Patel --- include/sbi/riscv_encoding.h | 19 +++++++++++++++++- include/sbi/riscv_fp.h | 1 - lib/sbi/sbi_illegal_insn.c | 37 +++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/include/sbi/riscv_encoding.h b/include/sbi/riscv_encoding.h index 477fa3a..5146654 100644 --- a/include/sbi/riscv_encoding.h +++ b/include/sbi/riscv_encoding.h @@ -947,7 +947,10 @@ #define REG_PTR(insn, pos, regs) \ (ulong *)((ulong)(regs) + REG_OFFSET(insn, pos)) -#define GET_RM(insn) (((insn) >> 12) & 7) +#define GET_RM(insn) ((insn & MASK_FUNCT3) >> SHIFT_FUNCT3) + +#define GET_RS1_NUM(insn) ((insn & MASK_RS1) >> 15) +#define GET_CSR_NUM(insn) ((insn & MASK_CSR) >> SHIFT_CSR) #define GET_RS1(insn, regs) (*REG_PTR(insn, SH_RS1, regs)) #define GET_RS2(insn, regs) (*REG_PTR(insn, SH_RS2, regs)) @@ -959,7 +962,21 @@ #define IMM_I(insn) ((s32)(insn) >> 20) #define IMM_S(insn) (((s32)(insn) >> 25 << 5) | \ (s32)(((insn) >> 7) & 0x1f)) + #define MASK_FUNCT3 0x7000 +#define MASK_RS1 0xf8000 +#define MASK_CSR 0xfff + +#define SHIFT_FUNCT3 12 +#define SHIFT_CSR 20 + + +#define CSRRW 1 +#define CSRRS 2 +#define CSRRC 3 +#define CSRRWI 5 +#define CSRRSI 6 +#define CSRRCI 7 /* clang-format on */ diff --git a/include/sbi/riscv_fp.h b/include/sbi/riscv_fp.h index 3141c1c..f523c56 100644 --- a/include/sbi/riscv_fp.h +++ b/include/sbi/riscv_fp.h @@ -15,7 +15,6 @@ #include #define GET_PRECISION(insn) (((insn) >> 25) & 3) -#define GET_RM(insn) (((insn) >> 12) & 7) #define PRECISION_S 0 #define PRECISION_D 1 diff --git a/lib/sbi/sbi_illegal_insn.c b/lib/sbi/sbi_illegal_insn.c index ed6f111..e4acf05 100644 --- a/lib/sbi/sbi_illegal_insn.c +++ b/lib/sbi/sbi_illegal_insn.c @@ -48,9 +48,10 @@ static int misc_mem_opcode_insn(ulong insn, struct sbi_trap_regs *regs) static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs) { - int do_write, rs1_num = (insn >> 15) & 0x1f; - ulong rs1_val = GET_RS1(insn, regs); - int csr_num = (u32)insn >> 20; + bool do_write = false; + int rs1_num = GET_RS1_NUM(insn); + ulong rs1_val = GET_RS1(insn, regs); + int csr_num = GET_CSR_NUM((u32)insn); ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT; ulong csr_val, new_csr_val; @@ -60,32 +61,42 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs) return SBI_EFAIL; } - /* TODO: Ensure that we got CSR read/write instruction */ + /* Ensure that we got CSR read/write instruction */ + int funct3 = GET_RM(insn); + if (funct3 == 0 || funct3 == 4) { + sbi_printf("%s: Invalid opcode for CSR read/write instruction", + __func__); + return truly_illegal_insn(insn, regs); + } if (sbi_emulate_csr_read(csr_num, regs, &csr_val)) return truly_illegal_insn(insn, regs); do_write = rs1_num; - switch (GET_RM(insn)) { - case 1: + switch (funct3) { + case CSRRW: new_csr_val = rs1_val; - do_write = 1; + do_write = true; break; - case 2: + case CSRRS: new_csr_val = csr_val | rs1_val; + do_write = (rs1_num != 0); break; - case 3: + case CSRRC: new_csr_val = csr_val & ~rs1_val; + do_write = (rs1_num != 0); break; - case 5: + case CSRRWI: new_csr_val = rs1_num; - do_write = 1; + do_write = true; break; - case 6: + case CSRRSI: new_csr_val = csr_val | rs1_num; + do_write = (rs1_num != 0); break; - case 7: + case CSRRCI: new_csr_val = csr_val & ~rs1_num; + do_write = (rs1_num != 0); break; default: return truly_illegal_insn(insn, regs);