Skip to contentSkip to conversation

riscv: kprobes: Clean up instruction simulation

PATCH Superseded Nam Cao 2 participants 16 messages May 11, 2025 – May 14, 2025 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

cover riscv: kprobes: Clean up instruction simulation

Hi,

There is some instruction-processing code in kprobes simulate code. These code should be insn.h. In fact, most of them is duplicating insn.h.

This series remove the duplicated bits and make use of macros already defined in insn.h. The non-duplicated bits are moved into insn.h.

Nam Cao (11):
riscv: kprobes: Move branch_rs2_idx to insn.h
riscv: kprobes: Move branch_funct3 to insn.h
riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM
riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG
riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM
riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM
riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG
riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM
riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG
riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM
riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM

 arch/riscv/include/asm/insn.h            |  9 +++
 arch/riscv/kernel/probes/simulate-insn.c | 94 +++++-------------------
 2 files changed, 28 insertions(+), 75 deletions(-)
Signature · 1 line
2.39.5
author
#1 lore raw

PATCH 1/11 riscv: kprobes: Move branch_rs2_idx to insn.h

Similar to other instruction-processing macros/functions, branch_rs2_idx should be in insn.h.

Move it into insn.h as RV_EXTRACT_RS2_REG. This new name matches the style in insn.h.

---
 arch/riscv/include/asm/insn.h            | 5 +++++
 arch/riscv/kernel/probes/simulate-insn.c | 5 +----
 2 files changed, 6 insertions(+), 4 deletions(-)
arch/riscv/include/asm/insn.h +5 -0
index 09fde95a5e8f..debac13a3476 100644
@@ -64,6 +64,7 @@
 #define RVG_RS2_OPOFF		20
 #define RVG_RD_OPOFF		7
 #define RVG_RS1_MASK		GENMASK(4, 0)
+#define RVG_RS2_MASK		GENMASK(4, 0)
 #define RVG_RD_MASK		GENMASK(4, 0)
 
 /* The bit field of immediate value in RVC J instruction */
@@ -295,6 +296,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)
 	({typeof(x) x_ = (x); \
 	(RV_X(x_, RVG_RS1_OPOFF, RVG_RS1_MASK)); })
 
+#define RV_EXTRACT_RS2_REG(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, RVG_RS2_OPOFF, RVG_RS2_MASK)); })
+
 #define RV_EXTRACT_RD_REG(x) \
 	({typeof(x) x_ = (x); \
 	(RV_X(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
arch/riscv/kernel/probes/simulate-insn.c +1 -4
index 6c166029079c..77be381bb8b4 100644
@@ -121,9 +121,6 @@ bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *re
 #define branch_rs1_idx(opcode) \
 	(((opcode) >> 15) & 0x1f)
 
-#define branch_rs2_idx(opcode) \
-	(((opcode) >> 20) & 0x1f)
-
 #define branch_funct3(opcode) \
 	(((opcode) >> 12) & 0x7)
 
@@ -157,7 +154,7 @@ bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r
 	unsigned long rs2_val;
 
 	if (!rv_insn_reg_get_val(regs, branch_rs1_idx(opcode), &rs1_val) ||
-	    !rv_insn_reg_get_val(regs, branch_rs2_idx(opcode), &rs2_val))
+	    !rv_insn_reg_get_val(regs, RV_EXTRACT_RS2_REG(opcode), &rs2_val))
 		return false;
 
 	offset_tmp = branch_offset(opcode);
Signature · 1 line
2.39.5
author
#5 lore raw

PATCH 2/11 riscv: kprobes: Move branch_funct3 to insn.h

In reply to Nam Cao

Similar to other instruction-processing macros/functions, branch_funct3 should be in insn.h.

Move it into insn.h as RV_EXTRACT_FUNCT3. This new name matches the style in insn.h.

---
 arch/riscv/include/asm/insn.h            | 4 ++++
 arch/riscv/kernel/probes/simulate-insn.c | 5 +----
 2 files changed, 5 insertions(+), 4 deletions(-)
arch/riscv/include/asm/insn.h +4 -0
index debac13a3476..80901b36fd22 100644
@@ -292,6 +292,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)
 #define RV_X(X, s, mask)  (((X) >> (s)) & (mask))
 #define RVC_X(X, s, mask) RV_X(X, s, mask)
 
+#define RV_EXTRACT_FUNCT3(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, RV_INSN_FUNCT3_OPOFF, RV_INSN_FUNCT3_MASK >> RV_INSN_FUNCT3_OPOFF)); })
+
 #define RV_EXTRACT_RS1_REG(x) \
 	({typeof(x) x_ = (x); \
 	(RV_X(x_, RVG_RS1_OPOFF, RVG_RS1_MASK)); })
arch/riscv/kernel/probes/simulate-insn.c +1 -4
index 77be381bb8b4..d5f74fadbc3a 100644
@@ -121,9 +121,6 @@ bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *re
 #define branch_rs1_idx(opcode) \
 	(((opcode) >> 15) & 0x1f)
 
-#define branch_funct3(opcode) \
-	(((opcode) >> 12) & 0x7)
-
 #define branch_imm(opcode) \
 	(((((opcode) >>  8) & 0xf ) <<  1) | \
 	 ((((opcode) >> 25) & 0x3f) <<  5) | \
@@ -158,7 +155,7 @@ bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r
 		return false;
 
 	offset_tmp = branch_offset(opcode);
-	switch (branch_funct3(opcode)) {
+	switch (RV_EXTRACT_FUNCT3(opcode)) {
 	case RVG_FUNCT3_BEQ:
 		offset = (rs1_val == rs2_val) ? offset_tmp : 4;
 		break;
Signature · 1 line
2.39.5
author
#6 lore raw

PATCH 3/11 riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM

In reply to Nam Cao

Use RV_EXTRACT_JTYPE_IMM, instead of reimplementing it in simulate_jal().

---
 arch/riscv/kernel/probes/simulate-insn.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
arch/riscv/kernel/probes/simulate-insn.c +3 -6
index d5f74fadbc3a..b76a691d0d9a 100644
@@ -41,19 +41,16 @@ bool __kprobes simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs
 	 *     1         10          1           8       5    JAL/J
 	 */
 	bool ret;
-	u32 imm;
+	s32 imm;
 	u32 index = (opcode >> 7) & 0x1f;
 
 	ret = rv_insn_reg_set_val(regs, index, addr + 4);
 	if (!ret)
 		return ret;
 
-	imm  = ((opcode >> 21) & 0x3ff) << 1;
-	imm |= ((opcode >> 20) & 0x1)   << 11;
-	imm |= ((opcode >> 12) & 0xff)  << 12;
-	imm |= ((opcode >> 31) & 0x1)   << 20;
+	imm = RV_EXTRACT_JTYPE_IMM(opcode);
 
-	instruction_pointer_set(regs, addr + sign_extend32((imm), 20));
+	instruction_pointer_set(regs, addr + imm);
 
 	return ret;
 }
Signature · 1 line
2.39.5
author
#7 lore raw

PATCH 4/11 riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG

In reply to Nam Cao

Use RV_EXTRACT_RS1_REG instead of reimplementing its code.

---
 arch/riscv/kernel/probes/simulate-insn.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/riscv/kernel/probes/simulate-insn.c +2 -5
index b76a691d0d9a..625d514c4ada 100644
@@ -66,7 +66,7 @@ bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *reg
 	unsigned long base_addr;
 	u32 imm = (opcode >> 20) & 0xfff;
 	u32 rd_index = (opcode >> 7) & 0x1f;
-	u32 rs1_index = (opcode >> 15) & 0x1f;
+	u32 rs1_index = RV_EXTRACT_RS1_REG(opcode);
 
 	ret = rv_insn_reg_get_val(regs, rs1_index, &base_addr);
 	if (!ret)
@@ -115,9 +115,6 @@ bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *re
 	return true;
 }
 
-#define branch_rs1_idx(opcode) \
-	(((opcode) >> 15) & 0x1f)
-
 #define branch_imm(opcode) \
 	(((((opcode) >>  8) & 0xf ) <<  1) | \
 	 ((((opcode) >> 25) & 0x3f) <<  5) | \
@@ -147,7 +144,7 @@ bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r
 	unsigned long rs1_val;
 	unsigned long rs2_val;
 
-	if (!rv_insn_reg_get_val(regs, branch_rs1_idx(opcode), &rs1_val) ||
+	if (!rv_insn_reg_get_val(regs, RV_EXTRACT_RS1_REG(opcode), &rs1_val) ||
 	    !rv_insn_reg_get_val(regs, RV_EXTRACT_RS2_REG(opcode), &rs2_val))
 		return false;
 
Signature · 1 line
2.39.5
author
#8 lore raw

PATCH 5/11 riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM

In reply to Nam Cao

Use RV_EXTRACT_BTYPE_IMM, instead of reimplementing it in simulate_branch().

---
 arch/riscv/kernel/probes/simulate-insn.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
arch/riscv/kernel/probes/simulate-insn.c +1 -10
index 625d514c4ada..3ba97e79a2a3 100644
@@ -115,15 +115,6 @@ bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *re
 	return true;
 }
 
-#define branch_imm(opcode) \
-	(((((opcode) >>  8) & 0xf ) <<  1) | \
-	 ((((opcode) >> 25) & 0x3f) <<  5) | \
-	 ((((opcode) >>  7) & 0x1 ) << 11) | \
-	 ((((opcode) >> 31) & 0x1 ) << 12))
-
-#define branch_offset(opcode) \
-	sign_extend32((branch_imm(opcode)), 12)
-
 bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *regs)
 {
 	/*
@@ -148,7 +139,7 @@ bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r
 	    !rv_insn_reg_get_val(regs, RV_EXTRACT_RS2_REG(opcode), &rs2_val))
 		return false;
 
-	offset_tmp = branch_offset(opcode);
+	offset_tmp = RV_EXTRACT_BTYPE_IMM(opcode);
 	switch (RV_EXTRACT_FUNCT3(opcode)) {
 	case RVG_FUNCT3_BEQ:
 		offset = (rs1_val == rs2_val) ? offset_tmp : 4;
Signature · 1 line
2.39.5
author
#9 lore raw

PATCH 6/11 riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM

In reply to Nam Cao

Use RVC_EXTRACT_JTYPE_IMM, instead of reimplementing it in simulate_c_j().

---
 arch/riscv/kernel/probes/simulate-insn.c | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
arch/riscv/kernel/probes/simulate-insn.c +2 -17
index 3ba97e79a2a3..5defbde4dd50 100644
@@ -170,24 +170,9 @@ bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r
 
 bool __kprobes simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs)
 {
-	/*
-	 *  15    13 12                            2 1      0
-	 * | funct3 | offset[11|4|9:8|10|6|7|3:1|5] | opcode |
-	 *     3                   11                    2
-	 */
-
-	s32 offset;
-
-	offset  = ((opcode >> 3)  & 0x7) << 1;
-	offset |= ((opcode >> 11) & 0x1) << 4;
-	offset |= ((opcode >> 2)  & 0x1) << 5;
-	offset |= ((opcode >> 7)  & 0x1) << 6;
-	offset |= ((opcode >> 6)  & 0x1) << 7;
-	offset |= ((opcode >> 9)  & 0x3) << 8;
-	offset |= ((opcode >> 8)  & 0x1) << 10;
-	offset |= ((opcode >> 12) & 0x1) << 11;
+	s32 offset = RVC_EXTRACT_JTYPE_IMM(opcode);
 
-	instruction_pointer_set(regs, addr + sign_extend32(offset, 11));
+	instruction_pointer_set(regs, addr + offset);
 
 	return true;
 }
Signature · 1 line
2.39.5
author
#10 lore raw

PATCH 7/11 riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG

In reply to Nam Cao

Use RVC_EXTRACT_C2_RS1_REG, instead of reimplementing it in simulate_c_jr_jalr().

---
 arch/riscv/kernel/probes/simulate-insn.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/probes/simulate-insn.c +1 -1
index 5defbde4dd50..f5d64613dab5 100644
@@ -188,7 +188,7 @@ static bool __kprobes simulate_c_jr_jalr(u32 opcode, unsigned long addr, struct
 
 	unsigned long jump_addr;
 
-	u32 rs1 = (opcode >> 7) & 0x1f;
+	u32 rs1 = RVC_EXTRACT_C2_RS1_REG(opcode);
 
 	if (rs1 == 0) /* C.JR is only valid when rs1 != x0 */
 		return false;
Signature · 1 line
2.39.5
author
#11 lore raw

PATCH 8/11 riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM

In reply to Nam Cao

Use RVC_EXTRACT_BTYPE_IMM, instead of reimplementing it in simulate_c_bnez_beqz().

---
 arch/riscv/kernel/probes/simulate-insn.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
arch/riscv/kernel/probes/simulate-insn.c +3 -9
index f5d64613dab5..e670e55954d2 100644
@@ -232,16 +232,10 @@ static bool __kprobes simulate_c_bnez_beqz(u32 opcode, unsigned long addr, struc
 	if (!rv_insn_reg_get_val(regs, rs1, &rs1_val))
 		return false;
 
-	if ((rs1_val != 0 && is_bnez) || (rs1_val == 0 && !is_bnez)) {
-		offset =  ((opcode >> 3)  & 0x3) << 1;
-		offset |= ((opcode >> 10) & 0x3) << 3;
-		offset |= ((opcode >> 2)  & 0x1) << 5;
-		offset |= ((opcode >> 5)  & 0x3) << 6;
-		offset |= ((opcode >> 12) & 0x1) << 8;
-		offset = sign_extend32(offset, 8);
-	} else {
+	if ((rs1_val != 0 && is_bnez) || (rs1_val == 0 && !is_bnez))
+		offset = RVC_EXTRACT_BTYPE_IMM(opcode);
+	else
 		offset = 2;
-	}
 
 	instruction_pointer_set(regs, addr + offset);
 
Signature · 1 line
2.39.5
author
#12 lore raw

PATCH 9/11 riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG

In reply to Nam Cao

Use RV_EXTRACT_RD_REG, instead of reimplementing its code.

---
 arch/riscv/kernel/probes/simulate-insn.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
arch/riscv/kernel/probes/simulate-insn.c +3 -6
index e670e55954d2..1717df780409 100644
@@ -42,7 +42,7 @@ bool __kprobes simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs
 	 */
 	bool ret;
 	s32 imm;
-	u32 index = (opcode >> 7) & 0x1f;
+	u32 index = RV_EXTRACT_RD_REG(opcode);
 
 	ret = rv_insn_reg_set_val(regs, index, addr + 4);
 	if (!ret)
@@ -65,7 +65,7 @@ bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *reg
 	bool ret;
 	unsigned long base_addr;
 	u32 imm = (opcode >> 20) & 0xfff;
-	u32 rd_index = (opcode >> 7) & 0x1f;
+	u32 rd_index = RV_EXTRACT_RD_REG(opcode);
 	u32 rs1_index = RV_EXTRACT_RS1_REG(opcode);
 
 	ret = rv_insn_reg_get_val(regs, rs1_index, &base_addr);
@@ -81,9 +81,6 @@ bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *reg
 	return ret;
 }
 
-#define auipc_rd_idx(opcode) \
-	((opcode >> 7) & 0x1f)
-
 #define auipc_imm(opcode) \
 	((((opcode) >> 12) & 0xfffff) << 12)
 
@@ -104,7 +101,7 @@ bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *re
 	 *        20       5     7
 	 */
 
-	u32 rd_idx = auipc_rd_idx(opcode);
+	u32 rd_idx = RV_EXTRACT_RD_REG(opcode);
 	unsigned long rd_val = addr + auipc_offset(opcode);
 
 	if (!rv_insn_reg_set_val(regs, rd_idx, rd_val))
Signature · 1 line
2.39.5
author
#13 lore raw

PATCH 10/11 riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM

In reply to Nam Cao

Use RV_EXTRACT_UTYPE_IMM, instead of reimplementing it in simulate_auipc().

---
 arch/riscv/kernel/probes/simulate-insn.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
arch/riscv/kernel/probes/simulate-insn.c +1 -12
index 1717df780409..2b3cd69d6f8e 100644
@@ -81,17 +81,6 @@ bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *reg
 	return ret;
 }
 
-#define auipc_imm(opcode) \
-	((((opcode) >> 12) & 0xfffff) << 12)
-
-#if __riscv_xlen == 64
-#define auipc_offset(opcode)	sign_extend64(auipc_imm(opcode), 31)
-#elif __riscv_xlen == 32
-#define auipc_offset(opcode)	auipc_imm(opcode)
-#else
-#error "Unexpected __riscv_xlen"
-#endif
-
 bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *regs)
 {
 	/*
@@ -102,7 +91,7 @@ bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *re
 	 */
 
 	u32 rd_idx = RV_EXTRACT_RD_REG(opcode);
-	unsigned long rd_val = addr + auipc_offset(opcode);
+	unsigned long rd_val = addr + (s32)RV_EXTRACT_UTYPE_IMM(opcode);
 
 	if (!rv_insn_reg_set_val(regs, rd_idx, rd_val))
 		return false;
Signature · 1 line
2.39.5
author
#14 lore raw

PATCH 11/11 riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM

In reply to Nam Cao

Use RV_EXTRACT_ITYPE_IMM, instead of re-implementing it in simulate_jalr().

---
 arch/riscv/kernel/probes/simulate-insn.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/probes/simulate-insn.c +1 -1
index 2b3cd69d6f8e..fa581590c1f8 100644
@@ -64,7 +64,7 @@ bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *reg
 	 */
 	bool ret;
 	unsigned long base_addr;
-	u32 imm = (opcode >> 20) & 0xfff;
+	u32 imm = RV_EXTRACT_ITYPE_IMM(opcode);
 	u32 rd_index = RV_EXTRACT_RD_REG(opcode);
 	u32 rs1_index = RV_EXTRACT_RS1_REG(opcode);
 
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h

In reply to Nam Cao

Hi Nam,

Quoted 25 lines · Nam Cao

Similar to other instruction-processing macros/functions, branch_rs2_idx should be in insn.h.

Move it into insn.h as RV_EXTRACT_RS2_REG. This new name matches the style in insn.h.

---
  arch/riscv/include/asm/insn.h            | 5 +++++
  arch/riscv/kernel/probes/simulate-insn.c | 5 +----
  2 files changed, 6 insertions(+), 4 deletions(-)
arch/riscv/include/asm/insn.h +1 -0
index 09fde95a5e8f..debac13a3476 100644
@@ -64,6 +64,7 @@
  #define RVG_RS2_OPOFF		20
  #define RVG_RD_OPOFF		7
  #define RVG_RS1_MASK		GENMASK(4, 0)
+#define RVG_RS2_MASK		GENMASK(4, 0)
  #define RVG_RD_MASK		GENMASK(4, 0)
 
  /* The bit field of immediate value in RVC J instruction */

arch/riscv/include/asm/insn.h static __always_inline bool riscv_insn_is_c_jalr(u32 code) · line 301

Quoted 6 lines
arch/riscv/include/asm/insn.h +3 -0
@@ -295,6 +296,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)
  	({typeof(x) x_ = (x); \
  	(RV_X(x_, RVG_RS1_OPOFF, RVG_RS1_MASK)); })
 
+#define RV_EXTRACT_RS2_REG(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, RVG_RS2_OPOFF, RVG_RS2_MASK)); })

RV_X() definition was inconsistent across multiple files, so I harmonized RV_X() it in this patch https://lore.kernel.org/linux-riscv/[email protected]/

So here you use the "old" version, would you mind rebasing on top this patchset and use RV_X_mask() instead?

If you can't, let me know and I'll find some time, I'd like to merge those cleanups in 6.16.

Thanks,

Alex

Quoted 27 lines
arch/riscv/include/asm/insn.h +1 -0
+
  #define RV_EXTRACT_RD_REG(x) \
  	({typeof(x) x_ = (x); \
  	(RV_X(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
arch/riscv/kernel/probes/simulate-insn.c +1 -4
index 6c166029079c..77be381bb8b4 100644
@@ -121,9 +121,6 @@ bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *re
  #define branch_rs1_idx(opcode) \
  	(((opcode) >> 15) & 0x1f)
 
-#define branch_rs2_idx(opcode) \
-	(((opcode) >> 20) & 0x1f)
-
  #define branch_funct3(opcode) \
  	(((opcode) >> 12) & 0x7)
 
@@ -157,7 +154,7 @@ bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r
  	unsigned long rs2_val;
 
  	if (!rv_insn_reg_get_val(regs, branch_rs1_idx(opcode), &rs1_val) ||
-	    !rv_insn_reg_get_val(regs, branch_rs2_idx(opcode), &rs2_val))
+	    !rv_insn_reg_get_val(regs, RV_EXTRACT_RS2_REG(opcode), &rs2_val))
  		return false;
 
  	offset_tmp = branch_offset(opcode);
author
#3 lore raw

Re: [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h

Hi Alex,

Quoted 9 lines · Alexandre Ghiti

arch/riscv/include/asm/insn.h static __always_inline bool riscv_insn_is_c_jalr(u32 code) · line 301

Quoted 3 lines
arch/riscv/include/asm/insn.h +3 -0
+#define RV_EXTRACT_RS2_REG(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, RVG_RS2_OPOFF, RVG_RS2_MASK)); })

RV_X() definition was inconsistent across multiple files, so I harmonized
RV_X() it in this patch https://lore.kernel.org/linux-riscv/[email protected]/

So here you use the "old" version, would you mind rebasing on top this patchset and use RV_X_mask() instead?

I will do that. Thanks for the info.

Nam

#4 lore raw

Re: [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h

Quoted 9 lines · Nam Cao

Hi Alex,

Quoted 6 lines · Alexandre Ghiti

arch/riscv/include/asm/insn.h static __always_inline bool riscv_insn_is_c_jalr(u32 code) · line 301

Quoted 3 lines
arch/riscv/include/asm/insn.h +3 -0
+#define RV_EXTRACT_RS2_REG(x) \
+	({typeof(x) x_ = (x); \
+	(RV_X(x_, RVG_RS2_OPOFF, RVG_RS2_MASK)); })

RV_X() definition was inconsistent across multiple files, so I harmonized
RV_X() it in this patch https://lore.kernel.org/linux-riscv/[email protected]/

Quoted 3 lines
Quoted 2 lines

So here you use the "old" version, would you mind rebasing on top this patchset and use RV_X_mask() instead?

I will do that. Thanks for the info.

Thanks, just give me the day to review the whole patchset before you send a new version.

Thanks again,

Alex

Quoted 7 lines

Nam

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

reviewer Reviewed-by on the v1 series
#15 lore raw
In reply to Nam Cao
Quoted 21 lines · Nam Cao

Hi,

There is some instruction-processing code in kprobes simulate code. These code should be insn.h. In fact, most of them is duplicating insn.h.

This series remove the duplicated bits and make use of macros already defined in insn.h. The non-duplicated bits are moved into insn.h.

Nam Cao (11):
riscv: kprobes: Move branch_rs2_idx to insn.h
riscv: kprobes: Move branch_funct3 to insn.h
riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM
riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG
riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM
riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM
riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG
riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM
riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG
riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM
riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM

Quoted 3 lines
  arch/riscv/include/asm/insn.h            |  9 +++
  arch/riscv/kernel/probes/simulate-insn.c | 94 +++++-------------------
  2 files changed, 28 insertions(+), 75 deletions(-)

So for the whole series, you can add:

Thanks, that's a nice cleanup!

Alex

Quoted 1 line