From patchwork Sun Dec 1 10:27:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 13889452 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 EF522D49762 for ; Sun, 1 Dec 2024 10:28:31 +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=DkY50LEf2t0aek9wRQu1zD4dSfCGwaREF61y9RVENeA=; b=HVwbYGkKQXgVGE K3NNDFGZymMbDX63K4TXeKeUuQW2HAiDPV7JnKoWQgKcKe9Ir9ZpKA3ewYFKPkjM5XZt72cHVH2vO iUiOPNBG+yc4orijS5FhGKO6c/j0gpm+iRZGW9bK9R4/xDqNRPpPQWj9MHmWB6vhRl09d8GX020Rc 4EXZ+OiQpU2BLS/FXVj31Q7KtJvYoxrWmK1/xiCxxvv6cpwnYNM0cP7wCdQqn3yy1EIiWbEAMBxkm TGWaqOCPbOe9Gd/zojqjfuJYJNTajLICyjZ5UMSdeRLbaXFfP9V59KxdU8gYFoidxN2/lAlxvJ6MN bzk8xGY/GLE9N/mKfkxA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tHhBU-00000003INp-0KHI; Sun, 01 Dec 2024 10:28:12 +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 1tHhBQ-00000003IMR-3odt for linux-riscv@lists.infradead.org; Sun, 01 Dec 2024 10:28:10 +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=yw5hif6R2xr8gQiHoZQCNSDbVLkDYEqGBRYYvkoSF24=; b=LHB8KC2ceCUaLl8HnzUR3b1Grj BZ9cWpI4lgSkVPfTu18s994++ksTwOa4xCTC7FEflRLHfXY6/8M0HdctVQ1KXraGVqNFKjzgN4MgL ozDUpvZL298+ycf4KdQ0sV2Ar787ecGTuGCNmFT7FVar0yX1C2KvGhPyPhtRel9iz5q0twlbl8O/m axOGyaJSZ8e/mjcHPXWE78oX5w3Y4Eh2VyAedO33W4eMhQvm7oEiPVXztwbi2Djs2fhZtfMOhNBxp 5djzNopQvxW5GyrM0TIURiR+3gbqgiBGADvH3Jb9RHO1pqTQwkCSApD2A7dja93lRqbNcKwYVrP7S uFNt3PlQ==; Received: from [167.98.27.226] (helo=rainbowdash) by imap5.colo.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1tHhBI-00FjrP-RS; Sun, 01 Dec 2024 10:28:02 +0000 Received: from ben by rainbowdash with local (Exim 4.98) (envelope-from ) id 1tHhBJ-00000000vYn-2pUh; Sun, 01 Dec 2024 10:28:01 +0000 From: Ben Dooks To: linux-riscv@lists.infradead.org Cc: ajones@ventanamicro.com, palmer@dabbelt.com, Ben Dooks Subject: [PATCH v2 1/3] riscv: ptrace: add regs_set_register() Date: Sun, 1 Dec 2024 10:27:57 +0000 Message-Id: <20241201102759.221176-2-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.37.2.352.g3c44437643 In-Reply-To: <20241201102759.221176-1-ben.dooks@codethink.co.uk> References: <20241201102759.221176-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-20241201_022809_121774_184CB21D X-CRM114-Status: GOOD ( 11.37 ) 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 --- v2: - fixed Andrew Jones' suggestions for comments --- 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..45c503b592c8 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 at its offset + * @regs: pt_regs to which register value is set + * @offset: offset of the register. + * @value: 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 value) +{ + if (unlikely(offset > MAX_REG_OFFSET)) + return; + + *(unsigned long *)((unsigned long)regs + offset) = value; +} + /** * regs_get_kernel_argument() - get Nth function argument in kernel * @regs: pt_regs of that context From patchwork Sun Dec 1 10:27:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 13889453 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 CD28FD49761 for ; Sun, 1 Dec 2024 10:28:34 +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=+IqgdooJS5CpVfu/nBKwlsEQXMDlLJXlaCPq1uIfTXM=; b=o4yKkx1a46zLJb q0rj+LGTy5LhtZdXWI+iAcOzxE3NfqoXZ9Xj0swWftTQx6yxTAgO/UI8d85DslrBbi4q+HXKxPYxy QGa7pz8vf7KPWyoDYb+hTH9ZUTThWcmvZzqoA/LTCp1gq7tI3FO471sGkZHmu94M2J28974vNxmP6 x0r4tPGoP4XffzlV41c8oZglFGmFnyGTbscUhNgvEIpk9wxXPCGOstElx5N4ZlF2u+Drdrx9guPp3 kmJbN8vj3Yind+C6xMF9/MRICCkYC+OVfg9ZcDyiVGJz10JKwaf1xojHuZDqV1juf+tRAU70M5ifz TsOpBtprEw4yZcBteYxA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tHhBV-00000003IO1-1CdG; Sun, 01 Dec 2024 10:28:13 +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 1tHhBQ-00000003IMU-3yAu for linux-riscv@lists.infradead.org; Sun, 01 Dec 2024 10:28:11 +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=rziJ/igp4rrah2Qo06FBEcCbvzrdQxCbCn2xe/IEfkc=; b=Ifn5RY8KywWfjv2DYpfT/Xte/K vlBSHKlM/5sjIYSZ3FC4lWKvwBdL2rJra09WlE43AR2iQsPscyELQXzzvJyKmM5FjdidYdctSQ5Kc p7Z82ZLmmk16R4LfFe9PjmKjE4SuqnvSBZ5vPBF8CfGCbGooUB2vaBCzHT1QbHWAAb38QcdsPYjno 77Rnm5oM2fxBHIuLUzMs9bgJD7y/pbXqWl9qm0x25UDBmsSwjTMECfQ6cE+rORxphBYpFaoPxoiMg czIizL5w60WJ+2z/ksP5Lr/16G3yCn4aAZrLB8PGxheLiKGA2Xx5kLdIEQnr29PK0n+J9xMAe70mT Hl/AjJnA==; Received: from [167.98.27.226] (helo=rainbowdash) by imap4.hz.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1tHhBJ-00DpKm-BM; Sun, 01 Dec 2024 10:28:03 +0000 Received: from ben by rainbowdash with local (Exim 4.98) (envelope-from ) id 1tHhBJ-00000000vYr-3Ctu; Sun, 01 Dec 2024 10:28:01 +0000 From: Ben Dooks To: linux-riscv@lists.infradead.org Cc: ajones@ventanamicro.com, palmer@dabbelt.com, Ben Dooks Subject: [PATCH v2 2/3] riscv: traps: make insn fetch common in unknown instruction Date: Sun, 1 Dec 2024 10:27:58 +0000 Message-Id: <20241201102759.221176-3-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.37.2.352.g3c44437643 In-Reply-To: <20241201102759.221176-1-ben.dooks@codethink.co.uk> References: <20241201102759.221176-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-20241201_022809_165604_3AE5C679 X-CRM114-Status: GOOD ( 13.30 ) 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 v2: - fixed todo by going to illegal instruction error if get_user fails - added pointer print for failed read - fixed issues with rebasing onto main branch --- arch/riscv/include/asm/vector.h | 4 ++-- arch/riscv/kernel/traps.c | 14 +++++++++++++- arch/riscv/kernel/vector.c | 11 +---------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index c7c023afbacd..9ec2473c1b73 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -22,7 +22,7 @@ extern unsigned long riscv_v_vsize; int riscv_v_setup_vsize(void); bool insn_is_vector(u32 insn_buf); -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); @@ -270,7 +270,7 @@ struct pt_regs; static inline int riscv_v_setup_vsize(void) { return -EOPNOTSUPP; } static __always_inline bool has_vector(void) { return false; } static __always_inline bool insn_is_vector(u32 insn_buf) { 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..9662138ba45c 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -172,11 +172,23 @@ 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)) { + printk_ratelimited(KERN_ERR "traps: failed to read instruction at user %px\n", epc); + handled = false; + insn = 0; + } + } + + if (insn) + 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 821818886fab..08164a9121fe 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 Sun Dec 1 10:27:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 13889454 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 4ED0FD49761 for ; Sun, 1 Dec 2024 10:28:43 +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=v8OQAfZyQNbK2nc/8vRYmXGsLl5eHEYtf/CgqsSUVv0=; b=Si3PdgLlMiVqmj s2G0z1TFxIa6+VUiM56fysLvB6/oCS7hs2D9QkAiidF4vmy3s5O1pxX3tEu313vqBX649m89MIfEd KZe4264KLcOl0O5iJwBYwA+WQvfvuqbGy/sAaIRuL7sggm6+Iq5AQOQEa0FKElPvX6ZAfgyKxLCW4 qSPETHc87ewmWTayoq1vexmWKaCJU+ZUsRvQd4o+25B53aMxCoe4o8gRXd4fWDjcKr7y1emtu2VPY nEU8HvFydqSw+7RT/jcI5bhYjwzTCyuQprWqJLMi2J/k/ufxs3hOfZbfNFrnAcpoQL91dsQUMEhPe fZhPkGH8LeNvLZxSZUWg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tHhBT-00000003INg-26eL; Sun, 01 Dec 2024 10:28:11 +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 1tHhBQ-00000003IMK-3IVO for linux-riscv@lists.infradead.org; Sun, 01 Dec 2024 10:28:09 +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=nfGXRLkquFvg92946tM0Zd0jB72ZUCP2hBvLATnXeSA=; b=oJAVQ4A5087W7FUIdboGYQ6WJ9 9jRF7VWrg+c17zvdIMUMes2L8z3zBFzGxiqaheP2cbQFIxZvNIF7J/6w3OOBEDJbALml+jqtLpbK9 eTOURTIl2nTFEXwXrt3jYlTWUvkexSiryhiS89rlKFwhIXzi1y7IlBMUH+RF7Rs/p6hcbWgeA8tp8 mI3+f3E0QNmtXBoePzWlLP+NLTtiVCT+fHrXBQbEpuc21OATvdA0TXbg5acmVvNwiU+5tqJyiYskK I4IwEwzhTHxW6x8SkXroGe4v30884IRIyZBWW+dvr8lYegyGYU0p/aQl4RY+P4RlhSS2Jb3VTDIq4 bvNP1T2A==; Received: from [167.98.27.226] (helo=rainbowdash) by imap5.colo.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1tHhBJ-00FjrQ-1n; Sun, 01 Dec 2024 10:28:02 +0000 Received: from ben by rainbowdash with local (Exim 4.98) (envelope-from ) id 1tHhBJ-00000000vYv-3XqN; Sun, 01 Dec 2024 10:28:01 +0000 From: Ben Dooks To: linux-riscv@lists.infradead.org Cc: ajones@ventanamicro.com, palmer@dabbelt.com, Ben Dooks Subject: [PATCH v2 3/3] riscv: insn: add RV_EXTRACT_FUNCT3() Date: Sun, 1 Dec 2024 10:27:59 +0000 Message-Id: <20241201102759.221176-4-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.37.2.352.g3c44437643 In-Reply-To: <20241201102759.221176-1-ben.dooks@codethink.co.uk> References: <20241201102759.221176-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-20241201_022808_902661_272C651C X-CRM114-Status: UNSURE ( 8.01 ) 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)); })