From patchwork Wed Nov 13 09:17:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 13873337 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 67FB3D41C20 for ; Wed, 13 Nov 2024 09:21:32 +0000 (UTC) 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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=+hUbPYoNCvzSbLtb9XM/tX3BhhJseN/C4la6EBJndIE=; b=leqiH/XiMZTHpP mSoyCpHMkWVa6WM8w7CdwUnl8m6oCccJ4YKsf/GHc/PNUxBKt2mxp7pAhbna033sy4c7MW//jHLhB RJ9dLBoACzgXyJ3F8lR5J46aiqKWl4HS2ruoDiQBoipX3JJRNahfAm9VG/oLenFmx/u3eZYNLVMDC 2iJXKyOk/jGSVinC/OSHTDCasQFtKlS3EJbMpTZkb8q+qc4fnGslyQTq5bl78SipfsmVNUeTcHNAa fPSfrs3iTzzXC5X8/Nc1OuYRrkbGUdswhbiFadY+qE43bUtOsNwBscxUwzCmaH8XiyIzJUCABft7C BLFRgdjbvuVxCNhxDL/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tB9Z1-00000006E2x-2I8p; Wed, 13 Nov 2024 09:21:27 +0000 Received: from imap5.colo.codethink.co.uk ([78.40.148.171]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tB9Uu-00000006DVs-2UeR for linux-riscv@lists.infradead.org; Wed, 13 Nov 2024 09:17:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codethink.co.uk; s=imap5-20230908; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=BAuGBA6SeGp8hx6Y+sRmy9j/6zVyRjrmgZdRmAJIH6o=; b=bHf1CG/GiYmTpAw95DwbJPi10s IQuKe8QHiI0G03Kl4dqgT+hEEsM8j5tXQeihK5qGwOZcwUhJ5AoC+cyNawxvKNtei0dLFiKxlXVhc x7jRZ0fV4MMnhE8DuudWFO8PyljMoJMrnC7FQqWunWnopKQEvr5L3y2PEUCBoeMmolsn97DsEww8F uUFMRWCj2FKpSRIDB52PfvIl0cgWJDz1Epx+gvyMG9PlyCCycdt9qPY8G3RWK2LcvRbcWIryo2061 UVA3L0hnF+z1dAw/2p3srMu/PBYpCiCegQbp9SbPeqNtVQLjrEyjWWAOO8T6J00Pe5ynxq9yVHeCn E6l2scZg==; Received: from [63.135.74.212] (helo=rainbowdash) by imap5.colo.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1tB9Ul-004RXL-9h; Wed, 13 Nov 2024 09:17:04 +0000 Received: from ben by rainbowdash with local (Exim 4.98) (envelope-from ) id 1tB9Ul-0000000D939-3qMb; Wed, 13 Nov 2024 09:17:03 +0000 From: Ben Dooks To: linux-riscv@lists.infradead.org Cc: palmer@dabbelt.com, ajones@ventanamicro.com, alex@ghiti.fr, Ben Dooks Subject: [PATCH 1/3] riscv: ptrace: add regs_set_register() Date: Wed, 13 Nov 2024 09:17:01 +0000 Message-Id: <20241113091703.3133017-2-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.37.2.352.g3c44437643 In-Reply-To: <20241113091703.3133017-1-ben.dooks@codethink.co.uk> References: <20241113091703.3133017-1-ben.dooks@codethink.co.uk> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241113_011712_701864_745A7B5D X-CRM114-Status: GOOD ( 11.34 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Since we have regs_get_register() and we could use the set counterpart for things like fixing up traps, add regs_set_register() to set a pt_regs value from offset. Signed-off-by: Ben Dooks --- arch/riscv/include/asm/ptrace.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h index b5b0adcc85c1..66fc1795141d 100644 --- a/arch/riscv/include/asm/ptrace.h +++ b/arch/riscv/include/asm/ptrace.h @@ -143,6 +143,26 @@ static inline unsigned long regs_get_register(struct pt_regs *regs, return *(unsigned long *)((unsigned long)regs + offset); } +/** + * regs_set_register() - set register value from its offset + * @regs: pt_regs from which register value is gotten + * @offset: offset of the register. + * @to: value to set register to + * + * regs_set_register sets the value @to to a register whose offset from @regs. + * The @offset is the offset of the register in struct pt_regs. + * If @offset is bigger than MAX_REG_OFFSET, this will ignore the write. + */ +static inline void regs_set_register(struct pt_regs *regs, + unsigned int offset, + unsigned long to) +{ + if (unlikely(offset > MAX_REG_OFFSET)) + return; + + *(unsigned long *)((unsigned long)regs + offset) = to; +} + /** * regs_get_kernel_argument() - get Nth function argument in kernel * @regs: pt_regs of that context From patchwork Wed Nov 13 09:17:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 13873338 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 273DFD41C20 for ; Wed, 13 Nov 2024 09:21:41 +0000 (UTC) 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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=tnPE2Md47wD+3jwS38xDiZMC6G+uhOFQSoYGPRgus/U=; b=i78VKypu9+ivKB VWCgdHdrrjWJjGoYZ/oWI2wryzDzqnO+KrY/Q/DkjkScmwUeXU4Wd9EXq8s+dppPciafEKnZL/GDO EZJwEy+yXNijpA0uPmy8GutnAO0nvjIS6AeJ8SuyIu6EXmbY5xgtLklXPR5CH48wveY/poPdR7xAL zTvMhTYmm3E8A3jq0wLP71AWnHIbSwHW9N7AzPhSJEMlEiSAmvueWPqMlaViujvLtTUCE3QKicQEe bnXyQjrodx2hydRmYGjsEqKIu9grkIaOMdj1ARCAzpWT/bK7qvb+wMiAS0PiWAfisU2qGd/CdRaLl Yjt9/yDm7a3KL3wqp0VQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tB9Z0-00000006E2S-3mqh; Wed, 13 Nov 2024 09:21:26 +0000 Received: from imap4.hz.codethink.co.uk ([188.40.203.114]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tB9Up-00000006DUL-35pO for linux-riscv@lists.infradead.org; Wed, 13 Nov 2024 09:17:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codethink.co.uk; s=imap4-20230908; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=sPFqAIYDy/tE51Vy+5d3jCKjCYegv4kJbtmLrKVG1Us=; b=k595sKahp6pWaDuOhyopiptwTe nIEc67CsPODkxUYkWWXDWY8TBauLN8ibWSxNgnmYmSUJk6ECIAfC2TRLtm+0A7c9z8PHQZpaSbihw ms8RKvUCCOW9Wm/VafxsgpxNcq+D0IBPvWlE8n1HrW4zkSVQrPu4Q4O18YztFNZQPMQtoQ96BwGW7 1hVIu2mhllnX9qT8G4thyb+1HCiGU6ivwUOOeKIz2r/QfwHOjPhFARj6zjMR4zNvUSjd+kP6NNW4r 8Kz2jTvnrhqYjj8Mb1CU3KCwzaEacozo+dCdQkH0bQhDg71T4eQ0Kr5n2Gg4lyv9TQ3wu7aNQQgaE bJrMmbBg==; Received: from [63.135.74.212] (helo=rainbowdash) by imap4.hz.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1tB9Um-003uon-0L; Wed, 13 Nov 2024 09:17:04 +0000 Received: from ben by rainbowdash with local (Exim 4.98) (envelope-from ) id 1tB9Ul-0000000D93C-3yyt; Wed, 13 Nov 2024 09:17:03 +0000 From: Ben Dooks To: linux-riscv@lists.infradead.org Cc: palmer@dabbelt.com, ajones@ventanamicro.com, alex@ghiti.fr, Ben Dooks Subject: [PATCH 2/3] riscv: traps: make insn fetch common in unknown instruction Date: Wed, 13 Nov 2024 09:17:02 +0000 Message-Id: <20241113091703.3133017-3-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.37.2.352.g3c44437643 In-Reply-To: <20241113091703.3133017-1-ben.dooks@codethink.co.uk> References: <20241113091703.3133017-1-ben.dooks@codethink.co.uk> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241113_011707_810292_74C66B46 X-CRM114-Status: GOOD ( 12.29 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Add the trapped instruction (insn) as the second argument to riscv_v_first_use_handler() from the trap handler so when we add more handlers we can do the fetch of the instruction just once. Signed-off-by: Ben Dooks --- - fixed wording of patch from rfc --- arch/riscv/include/asm/vector.h | 4 ++-- arch/riscv/kernel/traps.c | 11 ++++++++++- arch/riscv/kernel/vector.c | 11 +---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index be7d309cca8a..c9f0b02cd975 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -21,7 +21,7 @@ extern unsigned long riscv_v_vsize; int riscv_v_setup_vsize(void); -bool riscv_v_first_use_handler(struct pt_regs *regs); +bool riscv_v_first_use_handler(struct pt_regs *regs, u32 insn); void kernel_vector_begin(void); void kernel_vector_end(void); void get_cpu_vector_context(void); @@ -268,7 +268,7 @@ struct pt_regs; static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } -static inline bool riscv_v_first_use_handler(struct pt_regs *regs) { return false; } +static inline bool riscv_v_first_use_handler(struct pt_regs *regs, u32 insn) { return false; } static inline bool riscv_v_vstate_query(struct pt_regs *regs) { return false; } static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; } #define riscv_v_vsize (0) diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 51ebfd23e007..1c3fab272fd1 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -172,11 +172,20 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re bool handled; if (user_mode(regs)) { + u32 __user *epc = (u32 __user *)regs->epc; + u32 insn = (u32)regs->badaddr; + irqentry_enter_from_user_mode(regs); local_irq_enable(); - handled = riscv_v_first_use_handler(regs); + if (!insn) { + if (__get_user(insn, epc)) { + /* todo */ + } + } + + handled = riscv_v_first_use_handler(regs, insn); local_irq_disable(); diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c index 682b3feee451..b852648cb8d5 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -168,11 +168,8 @@ bool riscv_v_vstate_ctrl_user_allowed(void) } EXPORT_SYMBOL_GPL(riscv_v_vstate_ctrl_user_allowed); -bool riscv_v_first_use_handler(struct pt_regs *regs) +bool riscv_v_first_use_handler(struct pt_regs *regs, u32 insn) { - u32 __user *epc = (u32 __user *)regs->epc; - u32 insn = (u32)regs->badaddr; - if (!has_vector()) return false; @@ -184,12 +181,6 @@ bool riscv_v_first_use_handler(struct pt_regs *regs) if (riscv_v_vstate_query(regs)) return false; - /* Get the instruction */ - if (!insn) { - if (__get_user(insn, epc)) - return false; - } - /* Filter out non-V instructions */ if (!insn_is_vector(insn)) return false; From patchwork Wed Nov 13 09:17:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 13873336 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0A446D41C1E for ; Wed, 13 Nov 2024 09:21:32 +0000 (UTC) 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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=uY4IYi5Itn/fsGd+rw9ToSiXoKbsgFpTdOELvM15Nb0=; b=G/0JvBsy58pHNn RAhaBZD43Cy/BVtI/AGJfBWpZoXuHy71Anp+FQG9Mathyfcf/JXwZBBL5HptoxxMWlIlWhNW4YqNm mE+sBadNw2LzUARfAsOPP+4xUd+N6myD8r62WO0Ki7snd8RVUoFOkoXGOq1lVC2+cCB76HRls7z91 yO9Xuudw01CQhntsAUC1nbxsVQ+oPg/SkQ7jblkD1yKZCoVDhlhUunwtaJ0b5CY4e1jKVyz3WIF9i dOif3AFS9x5DBJmyLM1bwaKsaHaCq9HqkgKeMKP0YojjVehiEtklxyYPVEYVaxDNI4AnTq3hxwB8z h4H6ErC6UaH5EH8AhSzg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tB9Yz-00000006E2E-2vBl; Wed, 13 Nov 2024 09:21:25 +0000 Received: from imap4.hz.codethink.co.uk ([188.40.203.114]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tB9Up-00000006DUK-2vDN for linux-riscv@lists.infradead.org; Wed, 13 Nov 2024 09:17:09 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codethink.co.uk; s=imap4-20230908; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=ndQVvTzHXT+i+I2ixdEppGGTUj/crNj5V52A15z/KHw=; b=Cq78MvQ9Es/ZEKqr+E25u6eTL9 Gdugsx7JW93JAa2yPj6D3MgefOyqg0f9mRPyJfnwSUNDJXfY9yZKP+T3rg/EHBuRh8ECUs59ZKt1n n+xHF+xGSq1wLpTdfa6SzvaQiMpZqyf8LA7+6mpVpKmiElToTVly6hJFXBL0KhRnaBAtzan1Rx/oG tULxQd7PlYXSn2fcm1LGN1nX14A6rLGrZKNQ0wjxS6G1XpCRs8iRiT9cVOmJe88EvX3UpLl0E7Q90 UAMi+teKE608dDEOXfTxBF6cJdfYEfqYqY3JOSgZFFNoknSFpgBYNP2v7gQREOFXWw0EVan/PHkmb r5iaKCAQ==; Received: from [63.135.74.212] (helo=rainbowdash) by imap4.hz.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1tB9Um-003uoo-0L; Wed, 13 Nov 2024 09:17:04 +0000 Received: from ben by rainbowdash with local (Exim 4.98) (envelope-from ) id 1tB9Ul-0000000D93F-47ve; Wed, 13 Nov 2024 09:17:03 +0000 From: Ben Dooks To: linux-riscv@lists.infradead.org Cc: palmer@dabbelt.com, ajones@ventanamicro.com, alex@ghiti.fr, Ben Dooks Subject: [PATCH 3/3] riscv: insn: add RV_EXTRACT_FUNCT3() Date: Wed, 13 Nov 2024 09:17:03 +0000 Message-Id: <20241113091703.3133017-4-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.37.2.352.g3c44437643 In-Reply-To: <20241113091703.3133017-1-ben.dooks@codethink.co.uk> References: <20241113091703.3133017-1-ben.dooks@codethink.co.uk> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241113_011707_761025_F6BEAA44 X-CRM114-Status: UNSURE ( 8.11 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Add extraction for the func3 field of most instructions for use with anyone who needs it. Note, added this for decoding of CSR accesses for work we did looking at the RDCYCLE v RDTIME calls. Signed-off-by: Ben Dooks Reviewed-by: Andrew Jones --- arch/riscv/include/asm/insn.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.h index 09fde95a5e8f..c67f44ff2066 100644 --- a/arch/riscv/include/asm/insn.h +++ b/arch/riscv/include/asm/insn.h @@ -299,6 +299,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code) ({typeof(x) x_ = (x); \ (RV_X(x_, RVG_RD_OPOFF, RVG_RD_MASK)); }) +#define RV_EXTRACT_FUNCT3(x) \ + ({typeof(x) x_ = (x); \ + (RV_X(x_, RV_INSN_FUNCT3_OPOFF, RV_INSN_FUNCT3_MASK)); }) + #define RV_EXTRACT_UTYPE_IMM(x) \ ({typeof(x) x_ = (x); \ (RV_X(x_, RV_U_IMM_31_12_OPOFF, RV_U_IMM_31_12_MASK)); })