All of lore.kernel.org
 help / color / mirror / Atom feed
From: Emil Renner Berthing <kernel@esmil.dk>
To: linux-riscv@lists.infradead.org
Cc: Emil Renner Berthing <kernel@esmil.dk>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ingo Molnar <mingo@redhat.com>,
	Paul Walmsley <paul.walmsley@sifive.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Peter Zijlstra <peterz@infradead.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Jason Baron <jbaron@akamai.com>, Ard Biesheuvel <ardb@kernel.org>,
	Jisheng Zhang <jszhang@kernel.org>,
	Alexandre Ghiti <alex@ghiti.fr>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v3 8/8] riscv: Use asm/insn.h for dynamic ftrace
Date: Thu, 24 Feb 2022 16:24:56 +0100	[thread overview]
Message-ID: <20220224152456.493365-9-kernel@esmil.dk> (raw)
In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk>

This converts kernel/ftrace.c to use asm/insn.h to generate the
instructions for dynamic ftrace. This also converts the make_call macro
into a regular static function.

Signed-off-by: Emil Renner Berthing <kernel@esmil.dk>
---
 arch/riscv/kernel/ftrace.c | 69 ++++++++++++--------------------------
 1 file changed, 22 insertions(+), 47 deletions(-)

diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c
index 2cc15dc45ce0..7dd3aafa17aa 100644
--- a/arch/riscv/kernel/ftrace.c
+++ b/arch/riscv/kernel/ftrace.c
@@ -9,6 +9,7 @@
 #include <linux/uaccess.h>
 #include <linux/memory.h>
 #include <asm/cacheflush.h>
+#include <asm/insn.h>
 #include <asm/patch.h>
 
 #ifdef CONFIG_DYNAMIC_FTRACE
@@ -21,31 +22,13 @@
  * Dynamic ftrace generates probes to call sites, so we must deal with
  * both auipc and jalr at the same time.
  */
+static void make_call(unsigned long caller, unsigned long callee, unsigned int call[2])
+{
+	u32 offset = callee - caller;
 
-#define JALR_SIGN_MASK		(0x00000800)
-#define JALR_OFFSET_MASK	(0x00000fff)
-#define AUIPC_OFFSET_MASK	(0xfffff000)
-#define AUIPC_PAD		(0x00001000)
-#define JALR_SHIFT		20
-#define JALR_BASIC		(0x000080e7)
-#define AUIPC_BASIC		(0x00000097)
-#define NOP4			(0x00000013)
-
-#define make_call(caller, callee, call)					\
-do {									\
-	call[0] = to_auipc_insn((unsigned int)((unsigned long)callee -	\
-				(unsigned long)caller));		\
-	call[1] = to_jalr_insn((unsigned int)((unsigned long)callee -	\
-			       (unsigned long)caller));			\
-} while (0)
-
-#define to_jalr_insn(offset)						\
-	(((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_BASIC)
-
-#define to_auipc_insn(offset)						\
-	((offset & JALR_SIGN_MASK) ?					\
-	(((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_BASIC) :	\
-	((offset & AUIPC_OFFSET_MASK) | AUIPC_BASIC))
+	call[0] = RISCV_INSN_AUIPC | RISCV_INSN_RD_RA | riscv_insn_u_imm(offset + 0x800);
+	call[1] = RISCV_INSN_JALR  | RISCV_INSN_RD_RA | RISCV_INSN_RS1_RA | riscv_insn_i_imm(offset);
+}
 
 int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
 {
@@ -63,7 +46,7 @@ static int ftrace_check_current_call(unsigned long hook_pos,
 				     unsigned int *expected)
 {
 	unsigned int replaced[2];
-	unsigned int nops[2] = {NOP4, NOP4};
+	unsigned int nops[2] = { RISCV_INSN_NOP, RISCV_INSN_NOP };
 
 	/* we expect nops at the hook position */
 	if (!expected)
@@ -95,7 +78,7 @@ static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
 				bool enable)
 {
 	unsigned int call[2];
-	unsigned int nops[2] = {NOP4, NOP4};
+	unsigned int nops[2] = { RISCV_INSN_NOP, RISCV_INSN_NOP };
 
 	make_call(hook_pos, target, call);
 
@@ -108,39 +91,31 @@ static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
 }
 
 /*
- * Put 5 instructions with 16 bytes at the front of function within
+ * Put 4 instructions with 16 bytes at the front of function within
  * patchable function entry nops' area.
  *
  * 0: REG_S  ra, -SZREG(sp)
- * 1: auipc  ra, 0x?
- * 2: jalr   -?(ra)
+ * 1: auipc  ra, ?
+ * 2: jalr   ra, ra, ?
  * 3: REG_L  ra, -SZREG(sp)
- *
- * So the opcodes is:
- * 0: 0xfe113c23 (sd)/0xfe112e23 (sw)
- * 1: 0x???????? -> auipc
- * 2: 0x???????? -> jalr
- * 3: 0xff813083 (ld)/0xffc12083 (lw)
  */
-#if __riscv_xlen == 64
-#define INSN0	0xfe113c23
-#define INSN3	0xff813083
-#elif __riscv_xlen == 32
-#define INSN0	0xfe112e23
-#define INSN3	0xffc12083
-#endif
-
 #define FUNC_ENTRY_SIZE	16
 #define FUNC_ENTRY_JMP	4
 
 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 {
-	unsigned int call[4] = {INSN0, 0, 0, INSN3};
+	unsigned int call[4] = {
+		RISCV_INSN_REG_S | RISCV_INSN_RS2_RA | RISCV_INSN_RS1_SP |
+			riscv_insn_s_imm(-RISCV_INSN_SZREG),
+		0,
+		0,
+		RISCV_INSN_REG_L | RISCV_INSN_RD_RA  | RISCV_INSN_RS1_SP |
+			riscv_insn_i_imm(-RISCV_INSN_SZREG),
+	};
 	unsigned long target = addr;
 	unsigned long caller = rec->ip + FUNC_ENTRY_JMP;
 
-	call[1] = to_auipc_insn((unsigned int)(target - caller));
-	call[2] = to_jalr_insn((unsigned int)(target - caller));
+	make_call(caller, target, &call[1]);
 
 	if (patch_text_nosync((void *)rec->ip, call, FUNC_ENTRY_SIZE))
 		return -EPERM;
@@ -151,7 +126,7 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
 		    unsigned long addr)
 {
-	unsigned int nops[4] = {NOP4, NOP4, NOP4, NOP4};
+	unsigned int nops[4] = { RISCV_INSN_NOP, RISCV_INSN_NOP, RISCV_INSN_NOP, RISCV_INSN_NOP };
 
 	if (patch_text_nosync((void *)rec->ip, nops, FUNC_ENTRY_SIZE))
 		return -EPERM;
-- 
2.35.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

WARNING: multiple messages have this Message-ID (diff)
From: Emil Renner Berthing <kernel@esmil.dk>
To: linux-riscv@lists.infradead.org
Cc: Emil Renner Berthing <kernel@esmil.dk>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ingo Molnar <mingo@redhat.com>,
	Paul Walmsley <paul.walmsley@sifive.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Peter Zijlstra <peterz@infradead.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Jason Baron <jbaron@akamai.com>, Ard Biesheuvel <ardb@kernel.org>,
	Jisheng Zhang <jszhang@kernel.org>,
	Alexandre Ghiti <alex@ghiti.fr>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v3 8/8] riscv: Use asm/insn.h for dynamic ftrace
Date: Thu, 24 Feb 2022 16:24:56 +0100	[thread overview]
Message-ID: <20220224152456.493365-9-kernel@esmil.dk> (raw)
In-Reply-To: <20220224152456.493365-1-kernel@esmil.dk>

This converts kernel/ftrace.c to use asm/insn.h to generate the
instructions for dynamic ftrace. This also converts the make_call macro
into a regular static function.

Signed-off-by: Emil Renner Berthing <kernel@esmil.dk>
---
 arch/riscv/kernel/ftrace.c | 69 ++++++++++++--------------------------
 1 file changed, 22 insertions(+), 47 deletions(-)

diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c
index 2cc15dc45ce0..7dd3aafa17aa 100644
--- a/arch/riscv/kernel/ftrace.c
+++ b/arch/riscv/kernel/ftrace.c
@@ -9,6 +9,7 @@
 #include <linux/uaccess.h>
 #include <linux/memory.h>
 #include <asm/cacheflush.h>
+#include <asm/insn.h>
 #include <asm/patch.h>
 
 #ifdef CONFIG_DYNAMIC_FTRACE
@@ -21,31 +22,13 @@
  * Dynamic ftrace generates probes to call sites, so we must deal with
  * both auipc and jalr at the same time.
  */
+static void make_call(unsigned long caller, unsigned long callee, unsigned int call[2])
+{
+	u32 offset = callee - caller;
 
-#define JALR_SIGN_MASK		(0x00000800)
-#define JALR_OFFSET_MASK	(0x00000fff)
-#define AUIPC_OFFSET_MASK	(0xfffff000)
-#define AUIPC_PAD		(0x00001000)
-#define JALR_SHIFT		20
-#define JALR_BASIC		(0x000080e7)
-#define AUIPC_BASIC		(0x00000097)
-#define NOP4			(0x00000013)
-
-#define make_call(caller, callee, call)					\
-do {									\
-	call[0] = to_auipc_insn((unsigned int)((unsigned long)callee -	\
-				(unsigned long)caller));		\
-	call[1] = to_jalr_insn((unsigned int)((unsigned long)callee -	\
-			       (unsigned long)caller));			\
-} while (0)
-
-#define to_jalr_insn(offset)						\
-	(((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_BASIC)
-
-#define to_auipc_insn(offset)						\
-	((offset & JALR_SIGN_MASK) ?					\
-	(((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_BASIC) :	\
-	((offset & AUIPC_OFFSET_MASK) | AUIPC_BASIC))
+	call[0] = RISCV_INSN_AUIPC | RISCV_INSN_RD_RA | riscv_insn_u_imm(offset + 0x800);
+	call[1] = RISCV_INSN_JALR  | RISCV_INSN_RD_RA | RISCV_INSN_RS1_RA | riscv_insn_i_imm(offset);
+}
 
 int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex)
 {
@@ -63,7 +46,7 @@ static int ftrace_check_current_call(unsigned long hook_pos,
 				     unsigned int *expected)
 {
 	unsigned int replaced[2];
-	unsigned int nops[2] = {NOP4, NOP4};
+	unsigned int nops[2] = { RISCV_INSN_NOP, RISCV_INSN_NOP };
 
 	/* we expect nops at the hook position */
 	if (!expected)
@@ -95,7 +78,7 @@ static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
 				bool enable)
 {
 	unsigned int call[2];
-	unsigned int nops[2] = {NOP4, NOP4};
+	unsigned int nops[2] = { RISCV_INSN_NOP, RISCV_INSN_NOP };
 
 	make_call(hook_pos, target, call);
 
@@ -108,39 +91,31 @@ static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
 }
 
 /*
- * Put 5 instructions with 16 bytes at the front of function within
+ * Put 4 instructions with 16 bytes at the front of function within
  * patchable function entry nops' area.
  *
  * 0: REG_S  ra, -SZREG(sp)
- * 1: auipc  ra, 0x?
- * 2: jalr   -?(ra)
+ * 1: auipc  ra, ?
+ * 2: jalr   ra, ra, ?
  * 3: REG_L  ra, -SZREG(sp)
- *
- * So the opcodes is:
- * 0: 0xfe113c23 (sd)/0xfe112e23 (sw)
- * 1: 0x???????? -> auipc
- * 2: 0x???????? -> jalr
- * 3: 0xff813083 (ld)/0xffc12083 (lw)
  */
-#if __riscv_xlen == 64
-#define INSN0	0xfe113c23
-#define INSN3	0xff813083
-#elif __riscv_xlen == 32
-#define INSN0	0xfe112e23
-#define INSN3	0xffc12083
-#endif
-
 #define FUNC_ENTRY_SIZE	16
 #define FUNC_ENTRY_JMP	4
 
 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 {
-	unsigned int call[4] = {INSN0, 0, 0, INSN3};
+	unsigned int call[4] = {
+		RISCV_INSN_REG_S | RISCV_INSN_RS2_RA | RISCV_INSN_RS1_SP |
+			riscv_insn_s_imm(-RISCV_INSN_SZREG),
+		0,
+		0,
+		RISCV_INSN_REG_L | RISCV_INSN_RD_RA  | RISCV_INSN_RS1_SP |
+			riscv_insn_i_imm(-RISCV_INSN_SZREG),
+	};
 	unsigned long target = addr;
 	unsigned long caller = rec->ip + FUNC_ENTRY_JMP;
 
-	call[1] = to_auipc_insn((unsigned int)(target - caller));
-	call[2] = to_jalr_insn((unsigned int)(target - caller));
+	make_call(caller, target, &call[1]);
 
 	if (patch_text_nosync((void *)rec->ip, call, FUNC_ENTRY_SIZE))
 		return -EPERM;
@@ -151,7 +126,7 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
 		    unsigned long addr)
 {
-	unsigned int nops[4] = {NOP4, NOP4, NOP4, NOP4};
+	unsigned int nops[4] = { RISCV_INSN_NOP, RISCV_INSN_NOP, RISCV_INSN_NOP, RISCV_INSN_NOP };
 
 	if (patch_text_nosync((void *)rec->ip, nops, FUNC_ENTRY_SIZE))
 		return -EPERM;
-- 
2.35.1


  parent reply	other threads:[~2022-02-24 15:26 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-24 15:24 [PATCH v3 0/8] Add RISC-V asm/insn.h header Emil Renner Berthing
2022-02-24 15:24 ` Emil Renner Berthing
2022-02-24 15:24 ` [PATCH v3 1/8] riscv: Avoid unaligned access when relocating modules Emil Renner Berthing
2022-02-24 15:24   ` Emil Renner Berthing
2022-02-26  4:48   ` Samuel Bronson
2022-02-26  4:48     ` Samuel Bronson
2022-02-28 12:46     ` Emil Renner Berthing
2022-02-28 12:46       ` Emil Renner Berthing
2022-02-24 15:24 ` [PATCH v3 2/8] riscv: Remove unneeded definitions from asm/module.h Emil Renner Berthing
2022-02-24 15:24   ` Emil Renner Berthing
2022-02-24 15:24 ` [PATCH v3 3/8] riscv: Remove unneeded definitions from asm/ftrace.h Emil Renner Berthing
2022-02-24 15:24   ` Emil Renner Berthing
2022-02-24 15:24 ` [PATCH v3 4/8] riscv: Add asm/insn.h header Emil Renner Berthing
2022-02-24 15:24   ` Emil Renner Berthing
2022-02-24 15:24 ` [PATCH v3 5/8] riscv: Use asm/insn.h for module relocations Emil Renner Berthing
2022-02-24 15:24   ` Emil Renner Berthing
2022-02-24 15:24 ` [PATCH v3 6/8] riscv: Use asm/insn.h to generate plt entries Emil Renner Berthing
2022-02-24 15:24   ` Emil Renner Berthing
2022-02-24 15:24 ` [PATCH v3 7/8] riscv: Use asm/insn.h for jump labels Emil Renner Berthing
2022-02-24 15:24   ` Emil Renner Berthing
2022-02-24 15:24 ` Emil Renner Berthing [this message]
2022-02-24 15:24   ` [PATCH v3 8/8] riscv: Use asm/insn.h for dynamic ftrace Emil Renner Berthing

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220224152456.493365-9-kernel@esmil.dk \
    --to=kernel@esmil.dk \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=ardb@kernel.org \
    --cc=jbaron@akamai.com \
    --cc=jpoimboe@redhat.com \
    --cc=jszhang@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=mingo@redhat.com \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.