From patchwork Tue Sep 17 13:08:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 13806168 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 91FC7C35FFF for ; Tue, 17 Sep 2024 13:33:13 +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=2/cSN/yD2vmdT7 qripjSBfb0WA2alrt/YrrvYHotKPoR87TAIImprjRwq5850me/H7/tOznMu+CweldQ3M00SM8umqJ TSCTpGz7QLfAqsdXJ4KVbCu9JS4/SaSoXBGgw4zxMEWux/gnaOW08zMZ4cC1+5uQToNtd88oviyVh r3sZqFwmXxZkZdYZ4HE1fcLMCQWvZ/1ESCv+hrTfEIzvLA7jxyp9J30CI6fIzgRqUfWUUyHVa3Fy3 aWcn32DaR2a9c0tCYEYPfehXif7jOqpftyq/EtUGimo1ULj5IDLbbeeLYZUB1ZfmCJhhd9BCP7O0Y +Et2yPWzvTy4GE+UrgxQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sqYKM-00000006El4-1MCB; Tue, 17 Sep 2024 13:33:10 +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 1sqYKJ-00000006EkU-0MWa for linux-riscv@lists.infradead.org; Tue, 17 Sep 2024 13:33:08 +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=OJb8gcLPH5kl/h9Oqnuu6YCEvh h7AEo/dT1E+oKZgS2G+9OXJwxtT9aBoYPHoHG9SPi8bGer6gk22wnU50+TBzCGg0P7GHVwlrlGxpP yqfl04uryUJqGSaOrTUMk6HgGFLytoM5HRJlvOi0hxQhz3STyE/Et1CasYN9X5BPR3w4YGLtlXmKO ZzrlyXmYUF62qZcDGXBKpamEjOwkCbCdcFkFDcZECE7GCrR+gMGtvWtlfI0vd2CZ6d7YErz9QMTye +xoAVatjS7ZVSrgVHF822es4aU141/wGUbOr7FMt4jW8LDzsSx9u04F2qrjuokKrbdZEgB+1ihGT8 z+hc/jMg==; Received: from [83.68.141.146] (helo=rainbowdash) by imap5.colo.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1sqXws-00FSG2-NZ; Tue, 17 Sep 2024 14:08:54 +0100 Received: from ben by rainbowdash with local (Exim 4.98) (envelope-from ) id 1sqXws-000000004rP-0VIV; Tue, 17 Sep 2024 14:08:54 +0100 From: Ben Dooks To: linux-riscv@lists.infradead.org Cc: Ben Dooks Subject: [PATCH 1/3] riscv: ptrace: add regs_set_register() Date: Tue, 17 Sep 2024 14:08:51 +0100 Message-Id: <20240917130853.18657-2-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.37.2.352.g3c44437643 In-Reply-To: <20240917130853.18657-1-ben.dooks@codethink.co.uk> References: <20240917130853.18657-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-20240917_063307_230200_ACFEA780 X-CRM114-Status: GOOD ( 12.05 ) 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 Tue Sep 17 13:08:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 13806194 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 E3247C3601E for ; Tue, 17 Sep 2024 14:13:59 +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=Od87KDvEGVzzQi077duqoV8Q04u7ElAxznhhYa4pQm8=; b=JJ807QgoKcunJl /GqrpyCirAXbNQvWgTqapoS1ZOW6YP3/kBhDF3U5ysvtTPewXnaXndiarL0C/pUYrF/fZMZXRZMQM a5Z1e1amzOi6if+zRERfxJ3OSiMbUOO2fflpCrQkJPtlgdmOuE9ziwEtZ7voE0Xv5We+KyiHLg87A jMJc2fIeRa6dKQmogSnqj8qPhmSxmrFmdnTXqBa4Hgbr7toBi2y8WWBwX3aYFbkyw+x5gPdVuz1O2 o6EoSKTmzhcfpPdUcJqoYWGeAlMVaz53A8Ak8Z63quAMJSlpIT8dIutn+D5DSxpHh4CjlDs7Y1FJC Q3oskdceCraTHBUze3Yg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sqYxn-00000006Hrq-2seK; Tue, 17 Sep 2024 14:13:55 +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 1sqYxl-00000006HrU-0RYg for linux-riscv@lists.infradead.org; Tue, 17 Sep 2024 14:13:54 +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=gtRmv9BrhSGoV+dH/R03FLbWHG3jzP2Ra9nhvo2NjUU=; b=hs3Up6K/Z9vcE8Sh/GdJgxIP/j Wl0C0cWL3EfxzPjwVZxMmJkPkoH73fQmiByU2SHt/78cUyxAv1KSrovlTgAaJLiy89D1OvJD5brLP BXeskyQsQhaJ1M7XnvMMOJ94ejzzQXNFaj08HzeAeL9g2tbeAor6Bd4xcorcQDo80rVBnUXb++Umy DzS0Zr+Fn1Jy/biUaCMjFzvcklDSiC7ZFNQWyQffHCvM++RXruY0OQ0w67An/7Km2TPw+wGgxZ9Zr 9ippZZ37LlDE56C+2yjahn9me1Q+sUurKhOjZkzqlR25whp7PV83E5ULcevY4P6m1BDdKhskNULug XjZbDhlA==; Received: from [83.68.141.146] (helo=rainbowdash) by imap4.hz.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1sqXws-00BR16-Io; Tue, 17 Sep 2024 14:08:55 +0100 Received: from ben by rainbowdash with local (Exim 4.98) (envelope-from ) id 1sqXws-000000004rS-0dH8; Tue, 17 Sep 2024 14:08:54 +0100 From: Ben Dooks To: linux-riscv@lists.infradead.org Cc: Ben Dooks Subject: [PATCH 2/3] riscv: traps: make insn fetch common in unknown instruction Date: Tue, 17 Sep 2024 14:08:52 +0100 Message-Id: <20240917130853.18657-3-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.37.2.352.g3c44437643 In-Reply-To: <20240917130853.18657-1-ben.dooks@codethink.co.uk> References: <20240917130853.18657-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-20240917_071353_252607_7900D3FE X-CRM114-Status: GOOD ( 12.80 ) 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 insn as the second argument to riscv_v_first_use_handler() form the trap handler so when we add more handlers we can do the fetch of the instruction just once. Signed-off-by: Ben Dooks --- 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 Tue Sep 17 13:08:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 13806174 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 BB1A2C35FFF for ; Tue, 17 Sep 2024 13:44:10 +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=bFmrixWolhXRnMCcF/McilxdYudWBhc6DO8vEPRZjGo=; b=Vl/Y8wtHgdXtAF RrzZLtR4R4ROuBFrNghdZ5e88EKJfuIGfpytjsF188Qz9t8vjyQKvfngEB5JjIXaS70p2X2K3CHHq 2Zttqeaxqoa9BrL0mu4WaUzj7tyitm7PC38VPXv18HQo2Sc9gvEDZoBlAM6UX1KHE53w6zVfYp3Fz 5PvyBf0oSymVfRXFqmhqP5ExuxIQFLCPm09QIGwStH8veQE1HtSfSke3wWzz4UAPYvgL0aavPGxlM Oid9wgArG0EGxCgsYQ3or9J4Moz9zcs5V8s5utr2FfCKz/xlfXkRPQpq6ue08T+iIy5M5AejEvBEj nZ+obWhfg5YASpRtoZDA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1sqYUw-00000006Fjc-0tFK; Tue, 17 Sep 2024 13:44:06 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sqYUv-00000006FjD-15Ff for linux-riscv@bombadil.infradead.org; Tue, 17 Sep 2024 13:44:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; 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; bh=vPT1/BO+U9Phv5a4Mw+G6VxVGbtaN7AKxVNuXrAVhRI=; b=KldsTzIdyNgzlL6BI8GkWzD2JM gjWonhomDyuEFRgGnHlRcsZPZ+vkYTs324LWfcH6GSTD3pQ9RIXwVufqkl93EJdKqdi8ktlCjvxZ8 GJk4OJF+XrA/sW93vgOcqCm2NYJA35vCrgEq9duyxfBroEf6/TavBBEp30alMnZbbFWPr2U87tEsl VTV8yDGjC4plUQ7N2VK6uT3yIN7tOUPe6NP1M1MpCUv7nabZuf1HfhMIrlkKohU2DzVOktv58lylx f75THXW6S0F2M4vz2M1S/+uncb4RU6rxPOtLCWWc6B8WWzhVRFAKcdaMu4Sxyhf6X6co48yyClPwg e9zz6cnQ==; Received: from imap4.hz.codethink.co.uk ([188.40.203.114]) by casper.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1sqYUr-00000003CDA-1RIw for linux-riscv@lists.infradead.org; Tue, 17 Sep 2024 13:44:04 +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=vPT1/BO+U9Phv5a4Mw+G6VxVGbtaN7AKxVNuXrAVhRI=; b=cO9PkAkTgHhCseu84EvUpDu4Sh HUcTYeSfDshELiR/LnkopNz36MH4w7NAc6EPNZuktjEi4ctH021XTWbj1QpLHT6t0Ey6WVpK3AH9l 4irjcf2cOAB/9dBukzr4NCxSSxW5y8xdtjXWVN8Z/QRYqj13wOfEPw/LDGHgGEdAKB6eTAcBcb0cp vUhdCOWjdNtjW0HSi8cWCyhAnrOqYNjZgTHJKkJ9qJQOxcmehAvxYUnWlP9tCFUt5d2WRxtq8wNZo ErFVUC3w1/6LuYik9vR+N7yZj4DWdF1QQ32A6B59hUIAerAqI2jdg47KYuxVMIyhDRedtg4tMNeoW ZXqpFSug==; Received: from [83.68.141.146] (helo=rainbowdash) by imap4.hz.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1sqXws-00BR17-JL; Tue, 17 Sep 2024 14:08:54 +0100 Received: from ben by rainbowdash with local (Exim 4.98) (envelope-from ) id 1sqXws-000000004rV-0lGX; Tue, 17 Sep 2024 14:08:54 +0100 From: Ben Dooks To: linux-riscv@lists.infradead.org Cc: Ben Dooks Subject: [PATCH 3/3] riscv: add trap and emulation for RDCYCLE Date: Tue, 17 Sep 2024 14:08:53 +0100 Message-Id: <20240917130853.18657-4-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.37.2.352.g3c44437643 In-Reply-To: <20240917130853.18657-1-ben.dooks@codethink.co.uk> References: <20240917130853.18657-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-20240917_144402_181664_4AC346B9 X-CRM114-Status: GOOD ( 11.25 ) 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 a trap for RDCYCLE and emulate it as RDTIME instruciton. This is an initial PoC and should probably be made more generic way of trapping and dealing with bad instructions Signed-off-by: Ben Dooks --- arch/riscv/kernel/traps.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 1c3fab272fd1..51ea28ebf54d 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -167,6 +167,35 @@ DO_ERROR_INFO(do_trap_insn_misaligned, DO_ERROR_INFO(do_trap_insn_fault, SIGSEGV, SEGV_ACCERR, "instruction access fault"); +#define is_system(__i) (((__i) & 0x7f) == RVG_OPCODE_SYSTEM) + +static bool riscv_try_csr_fixup_user(struct pt_regs *regs, u32 insn) +{ + /* expecting a 4 byte CSR instruction (*/ + if (unlikely(GET_INSN_LENGTH(insn) != 4)) + return false; + + if (is_system(insn)) { + u32 csr = insn >> RVG_SYSTEM_CSR_OFF; + u32 rd = (insn >> RVG_RD_OPOFF) & RVG_RD_MASK; + u32 rs = (insn >> RVG_RS1_OPOFF) & RVG_RS1_MASK; + u32 funct3 = (insn >> RV_INSN_FUNCT3_OPOFF) & 0x7; + + if (rs == 0 && funct3 == 2 && csr == CSR_CYCLE) { + u64 val = csr_read(CSR_TIME); + /* we've got a RDCCLYE, emulated it with CSR_TIME */ + + printk_ratelimited("PID %d: process using RDCYCLE, emulating with RDTIME\n", current->pid); + + regs_set_register(regs, rd*sizeof(unsigned long), val); + regs->epc += 4; + return true; + } + } + + return false; +} + asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs) { bool handled; @@ -186,6 +215,8 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re } handled = riscv_v_first_use_handler(regs, insn); + if (!handle) + handled = riscv_try_csr_fixup_user(regs, insn); local_irq_disable();