* [PATCH v2 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 02/11] riscv: kprobes: Move branch_funct3 " Nam Cao
` (10 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
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.
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/include/asm/insn.h | 5 +++++
arch/riscv/kernel/probes/simulate-insn.c | 5 +----
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.h
index 7c65fc8baeed..4a26cef3b5c0 100644
--- a/arch/riscv/include/asm/insn.h
+++ b/arch/riscv/include/asm/insn.h
@@ -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 */
@@ -450,6 +451,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)
({typeof(x) x_ = (x); \
(RV_X_mask(x_, RVG_RS1_OPOFF, RVG_RS1_MASK)); })
+#define RV_EXTRACT_RS2_REG(x) \
+ ({typeof(x) x_ = (x); \
+ (RV_X_mask(x_, RVG_RS2_OPOFF, RVG_RS2_MASK)); })
+
#define RV_EXTRACT_RD_REG(x) \
({typeof(x) x_ = (x); \
(RV_X_mask(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index 6c166029079c..77be381bb8b4 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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);
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 02/11] riscv: kprobes: Move branch_funct3 to insn.h
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
2025-05-14 9:38 ` [PATCH v2 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 03/11] riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM Nam Cao
` (9 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
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.
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/include/asm/insn.h | 4 ++++
arch/riscv/kernel/probes/simulate-insn.c | 5 +----
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.h
index 4a26cef3b5c0..ca9628c61885 100644
--- a/arch/riscv/include/asm/insn.h
+++ b/arch/riscv/include/asm/insn.h
@@ -447,6 +447,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)
#define RVC_RS2(insn) RV_X(insn, SH_RS2C, 5)
#define RVC_X(X, s, mask) RV_X_mask(X, s, mask)
+#define RV_EXTRACT_FUNCT3(x) \
+ ({typeof(x) x_ = (x); \
+ (RV_X_mask(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_mask(x_, RVG_RS1_OPOFF, RVG_RS1_MASK)); })
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index 77be381bb8b4..d5f74fadbc3a 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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;
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 03/11] riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
2025-05-14 9:38 ` [PATCH v2 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h Nam Cao
2025-05-14 9:38 ` [PATCH v2 02/11] riscv: kprobes: Move branch_funct3 " Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 04/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG Nam Cao
` (8 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
Use RV_EXTRACT_JTYPE_IMM, instead of reimplementing it in simulate_jal().
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/kernel/probes/simulate-insn.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index d5f74fadbc3a..b76a691d0d9a 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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;
}
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 04/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (2 preceding siblings ...)
2025-05-14 9:38 ` [PATCH v2 03/11] riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 05/11] riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM Nam Cao
` (7 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
Use RV_EXTRACT_RS1_REG instead of reimplementing its code.
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/kernel/probes/simulate-insn.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index b76a691d0d9a..625d514c4ada 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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;
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 05/11] riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (3 preceding siblings ...)
2025-05-14 9:38 ` [PATCH v2 04/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 06/11] riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM Nam Cao
` (6 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
Use RV_EXTRACT_BTYPE_IMM, instead of reimplementing it in
simulate_branch().
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/kernel/probes/simulate-insn.c | 11 +----------
1 file changed, 1 insertion(+), 10 deletions(-)
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index 625d514c4ada..3ba97e79a2a3 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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;
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 06/11] riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (4 preceding siblings ...)
2025-05-14 9:38 ` [PATCH v2 05/11] riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 07/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG Nam Cao
` (5 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
Use RVC_EXTRACT_JTYPE_IMM, instead of reimplementing it in simulate_c_j().
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/kernel/probes/simulate-insn.c | 19 ++-----------------
1 file changed, 2 insertions(+), 17 deletions(-)
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index 3ba97e79a2a3..5defbde4dd50 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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;
}
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 07/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (5 preceding siblings ...)
2025-05-14 9:38 ` [PATCH v2 06/11] riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 08/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM Nam Cao
` (4 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
Use RVC_EXTRACT_C2_RS1_REG, instead of reimplementing it in
simulate_c_jr_jalr().
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/kernel/probes/simulate-insn.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index 5defbde4dd50..f5d64613dab5 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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;
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 08/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (6 preceding siblings ...)
2025-05-14 9:38 ` [PATCH v2 07/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 09/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG Nam Cao
` (3 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
Use RVC_EXTRACT_BTYPE_IMM, instead of reimplementing it in
simulate_c_bnez_beqz().
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/kernel/probes/simulate-insn.c | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index f5d64613dab5..e670e55954d2 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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);
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 09/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (7 preceding siblings ...)
2025-05-14 9:38 ` [PATCH v2 08/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 10/11] riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM Nam Cao
` (2 subsequent siblings)
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
Use RV_EXTRACT_RD_REG, instead of reimplementing its code.
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/kernel/probes/simulate-insn.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index e670e55954d2..1717df780409 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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))
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 10/11] riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (8 preceding siblings ...)
2025-05-14 9:38 ` [PATCH v2 09/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-05-14 9:38 ` [PATCH v2 11/11] riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM Nam Cao
2025-06-04 14:33 ` [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation patchwork-bot+linux-riscv
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
Use RV_EXTRACT_UTYPE_IMM, instead of reimplementing it in simulate_auipc().
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/kernel/probes/simulate-insn.c | 13 +------------
1 file changed, 1 insertion(+), 12 deletions(-)
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index 1717df780409..2b3cd69d6f8e 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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;
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH v2 11/11] riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (9 preceding siblings ...)
2025-05-14 9:38 ` [PATCH v2 10/11] riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM Nam Cao
@ 2025-05-14 9:38 ` Nam Cao
2025-06-04 14:33 ` [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation patchwork-bot+linux-riscv
11 siblings, 0 replies; 15+ messages in thread
From: Nam Cao @ 2025-05-14 9:38 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao, Alexandre Ghiti
Use RV_EXTRACT_ITYPE_IMM, instead of re-implementing it in simulate_jalr().
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Signed-off-by: Nam Cao <namcao@linutronix.de>
---
arch/riscv/kernel/probes/simulate-insn.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
index 2b3cd69d6f8e..fa581590c1f8 100644
--- a/arch/riscv/kernel/probes/simulate-insn.c
+++ b/arch/riscv/kernel/probes/simulate-insn.c
@@ -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);
--
2.39.5
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation
2025-05-14 9:38 [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (10 preceding siblings ...)
2025-05-14 9:38 ` [PATCH v2 11/11] riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM Nam Cao
@ 2025-06-04 14:33 ` patchwork-bot+linux-riscv
2025-07-08 10:25 ` Nam Cao
11 siblings, 1 reply; 15+ messages in thread
From: patchwork-bot+linux-riscv @ 2025-06-04 14:33 UTC (permalink / raw)
To: Nam Cao; +Cc: linux-riscv, paul.walmsley, palmer, aou, alex, linux-kernel
Hello:
This series was applied to riscv/linux.git (for-next)
by Alexandre Ghiti <alexghiti@rivosinc.com>:
On Wed, 14 May 2025 11:38:39 +0200 you wrote:
> 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.
>
> [...]
Here is the summary with links:
- [v2,01/11] riscv: kprobes: Move branch_rs2_idx to insn.h
https://git.kernel.org/riscv/c/d29656bab74c
- [v2,02/11] riscv: kprobes: Move branch_funct3 to insn.h
https://git.kernel.org/riscv/c/da6de46c2eed
- [v2,03/11] riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM
https://git.kernel.org/riscv/c/6d47d903b18f
- [v2,04/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG
https://git.kernel.org/riscv/c/5cefc323f32a
- [v2,05/11] riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM
https://git.kernel.org/riscv/c/a285674909ae
- [v2,06/11] riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM
https://git.kernel.org/riscv/c/c7196c136e29
- [v2,07/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG
https://git.kernel.org/riscv/c/768007ca3fe8
- [v2,08/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM
https://git.kernel.org/riscv/c/f2c715fff676
- [v2,09/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG
https://git.kernel.org/riscv/c/284ca2a100de
- [v2,10/11] riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM
https://git.kernel.org/riscv/c/a60c2933ec74
- [v2,11/11] riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM
https://git.kernel.org/riscv/c/ee4c45f5cbeb
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation
2025-06-04 14:33 ` [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation patchwork-bot+linux-riscv
@ 2025-07-08 10:25 ` Nam Cao
2025-07-08 13:27 ` Alexandre Ghiti
0 siblings, 1 reply; 15+ messages in thread
From: Nam Cao @ 2025-07-08 10:25 UTC (permalink / raw)
To: patchwork-bot+linux-riscv
Cc: linux-riscv, paul.walmsley, palmer, aou, alex, linux-kernel
On Wed, Jun 04, 2025 at 02:33:06PM +0000, patchwork-bot+linux-riscv@kernel.org wrote:
> Hello:
>
> This series was applied to riscv/linux.git (for-next)
> by Alexandre Ghiti <alexghiti@rivosinc.com>:
>
> On Wed, 14 May 2025 11:38:39 +0200 you wrote:
> > 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.
> >
> > [...]
>
> Here is the summary with links:
> - [v2,01/11] riscv: kprobes: Move branch_rs2_idx to insn.h
> https://git.kernel.org/riscv/c/d29656bab74c
> - [v2,02/11] riscv: kprobes: Move branch_funct3 to insn.h
> https://git.kernel.org/riscv/c/da6de46c2eed
> - [v2,03/11] riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM
> https://git.kernel.org/riscv/c/6d47d903b18f
> - [v2,04/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG
> https://git.kernel.org/riscv/c/5cefc323f32a
> - [v2,05/11] riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM
> https://git.kernel.org/riscv/c/a285674909ae
> - [v2,06/11] riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM
> https://git.kernel.org/riscv/c/c7196c136e29
> - [v2,07/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG
> https://git.kernel.org/riscv/c/768007ca3fe8
> - [v2,08/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM
> https://git.kernel.org/riscv/c/f2c715fff676
> - [v2,09/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG
> https://git.kernel.org/riscv/c/284ca2a100de
> - [v2,10/11] riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM
> https://git.kernel.org/riscv/c/a60c2933ec74
> - [v2,11/11] riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM
> https://git.kernel.org/riscv/c/ee4c45f5cbeb
This series was dropped from 6.16 due to conflict. Can it be re-applied? It
applies cleanly to riscv/for-next.
The same story for:
https://lore.kernel.org/linux-riscv/174904758974.2309006.8809303189389627595.git-patchwork-notify@kernel.org/
Best regards,
Nam
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH v2 00/11] riscv: kprobes: Clean up instruction simulation
2025-07-08 10:25 ` Nam Cao
@ 2025-07-08 13:27 ` Alexandre Ghiti
0 siblings, 0 replies; 15+ messages in thread
From: Alexandre Ghiti @ 2025-07-08 13:27 UTC (permalink / raw)
To: Nam Cao, patchwork-bot+linux-riscv
Cc: linux-riscv, paul.walmsley, palmer, aou, linux-kernel
Hi Nam,
On 7/8/25 12:25, Nam Cao wrote:
> On Wed, Jun 04, 2025 at 02:33:06PM +0000, patchwork-bot+linux-riscv@kernel.org wrote:
>> Hello:
>>
>> This series was applied to riscv/linux.git (for-next)
>> by Alexandre Ghiti <alexghiti@rivosinc.com>:
>>
>> On Wed, 14 May 2025 11:38:39 +0200 you wrote:
>>> 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.
>>>
>>> [...]
>> Here is the summary with links:
>> - [v2,01/11] riscv: kprobes: Move branch_rs2_idx to insn.h
>> https://git.kernel.org/riscv/c/d29656bab74c
>> - [v2,02/11] riscv: kprobes: Move branch_funct3 to insn.h
>> https://git.kernel.org/riscv/c/da6de46c2eed
>> - [v2,03/11] riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM
>> https://git.kernel.org/riscv/c/6d47d903b18f
>> - [v2,04/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG
>> https://git.kernel.org/riscv/c/5cefc323f32a
>> - [v2,05/11] riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM
>> https://git.kernel.org/riscv/c/a285674909ae
>> - [v2,06/11] riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM
>> https://git.kernel.org/riscv/c/c7196c136e29
>> - [v2,07/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG
>> https://git.kernel.org/riscv/c/768007ca3fe8
>> - [v2,08/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM
>> https://git.kernel.org/riscv/c/f2c715fff676
>> - [v2,09/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG
>> https://git.kernel.org/riscv/c/284ca2a100de
>> - [v2,10/11] riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM
>> https://git.kernel.org/riscv/c/a60c2933ec74
>> - [v2,11/11] riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM
>> https://git.kernel.org/riscv/c/ee4c45f5cbeb
> This series was dropped from 6.16 due to conflict. Can it be re-applied? It
> applies cleanly to riscv/for-next.
>
> The same story for:
> https://lore.kernel.org/linux-riscv/174904758974.2309006.8809303189389627595.git-patchwork-notify@kernel.org/
Yes, I have both in my for-next branch, sorry I did not advertise that
(I'm working on it).
FYI, I have enabled kunit tests in the upstream kernel CI:
https://github.com/linux-riscv/linux/actions/runs/15967114709/job/45030410210?pr=571#step:5:625
Thanks,
Alex
>
> Best regards,
> Nam
>
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 15+ messages in thread