* [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
@ 2025-05-11 21:17 ` Nam Cao
2025-05-14 7:24 ` Alexandre Ghiti
2025-05-11 21:17 ` [PATCH 02/11] riscv: kprobes: Move branch_funct3 " Nam Cao
` (10 subsequent siblings)
11 siblings, 1 reply; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:17 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: 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.
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 09fde95a5e8f..debac13a3476 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 */
@@ -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)); })
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] 16+ messages in thread* Re: [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h
2025-05-11 21:17 ` [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h Nam Cao
@ 2025-05-14 7:24 ` Alexandre Ghiti
2025-05-14 7:32 ` Nam Cao
0 siblings, 1 reply; 16+ messages in thread
From: Alexandre Ghiti @ 2025-05-14 7:24 UTC (permalink / raw)
To: Nam Cao, Paul Walmsley, Palmer Dabbelt, Albert Ou, linux-riscv,
linux-kernel
Hi Nam,
On 11/05/2025 23:17, Nam Cao wrote:
> 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.
>
> 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 09fde95a5e8f..debac13a3476 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 */
> @@ -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/20250508125202.108613-3-alexghiti@rivosinc.com/
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
> +
> #define RV_EXTRACT_RD_REG(x) \
> ({typeof(x) x_ = (x); \
> (RV_X(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);
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h
2025-05-14 7:24 ` Alexandre Ghiti
@ 2025-05-14 7:32 ` Nam Cao
2025-05-14 7:35 ` Alexandre Ghiti
0 siblings, 1 reply; 16+ messages in thread
From: Nam Cao @ 2025-05-14 7:32 UTC (permalink / raw)
To: Alexandre Ghiti
Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, linux-riscv,
linux-kernel
Hi Alex,
On Wed, May 14, 2025 at 09:24:55AM +0200, Alexandre Ghiti wrote:
> > +#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/20250508125202.108613-3-alexghiti@rivosinc.com/
>
> 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
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h
2025-05-14 7:32 ` Nam Cao
@ 2025-05-14 7:35 ` Alexandre Ghiti
0 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2025-05-14 7:35 UTC (permalink / raw)
To: Nam Cao; +Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, linux-riscv,
linux-kernel
On 14/05/2025 09:32, Nam Cao wrote:
> Hi Alex,
>
> On Wed, May 14, 2025 at 09:24:55AM +0200, Alexandre Ghiti wrote:
>>> +#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/20250508125202.108613-3-alexghiti@rivosinc.com/
>>
>> 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
>
> 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] 16+ messages in thread
* [PATCH 02/11] riscv: kprobes: Move branch_funct3 to insn.h
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
2025-05-11 21:17 ` [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h Nam Cao
@ 2025-05-11 21:17 ` Nam Cao
2025-05-11 21:17 ` [PATCH 03/11] riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM Nam Cao
` (9 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:17 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: 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.
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 debac13a3476..80901b36fd22 100644
--- a/arch/riscv/include/asm/insn.h
+++ b/arch/riscv/include/asm/insn.h
@@ -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)); })
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] 16+ messages in thread* [PATCH 03/11] riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
2025-05-11 21:17 ` [PATCH 01/11] riscv: kprobes: Move branch_rs2_idx to insn.h Nam Cao
2025-05-11 21:17 ` [PATCH 02/11] riscv: kprobes: Move branch_funct3 " Nam Cao
@ 2025-05-11 21:17 ` Nam Cao
2025-05-11 21:17 ` [PATCH 04/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG Nam Cao
` (8 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:17 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao
Use RV_EXTRACT_JTYPE_IMM, instead of reimplementing it in simulate_jal().
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] 16+ messages in thread* [PATCH 04/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (2 preceding siblings ...)
2025-05-11 21:17 ` [PATCH 03/11] riscv: kprobes: Remove duplication of RV_EXTRACT_JTYPE_IMM Nam Cao
@ 2025-05-11 21:17 ` Nam Cao
2025-05-11 21:17 ` [PATCH 05/11] riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM Nam Cao
` (7 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:17 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao
Use RV_EXTRACT_RS1_REG instead of reimplementing its code.
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] 16+ messages in thread* [PATCH 05/11] riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (3 preceding siblings ...)
2025-05-11 21:17 ` [PATCH 04/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RS1_REG Nam Cao
@ 2025-05-11 21:17 ` Nam Cao
2025-05-11 21:17 ` [PATCH 06/11] riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM Nam Cao
` (6 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:17 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao
Use RV_EXTRACT_BTYPE_IMM, instead of reimplementing it in
simulate_branch().
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] 16+ messages in thread* [PATCH 06/11] riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (4 preceding siblings ...)
2025-05-11 21:17 ` [PATCH 05/11] riscv: kprobes: Remove duplication of RV_EXTRACT_BTYPE_IMM Nam Cao
@ 2025-05-11 21:17 ` Nam Cao
2025-05-11 21:17 ` [PATCH 07/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG Nam Cao
` (5 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:17 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao
Use RVC_EXTRACT_JTYPE_IMM, instead of reimplementing it in simulate_c_j().
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] 16+ messages in thread* [PATCH 07/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (5 preceding siblings ...)
2025-05-11 21:17 ` [PATCH 06/11] riscv: kproves: Remove duplication of RVC_EXTRACT_JTYPE_IMM Nam Cao
@ 2025-05-11 21:17 ` Nam Cao
2025-05-11 21:18 ` [PATCH 08/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM Nam Cao
` (4 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:17 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao
Use RVC_EXTRACT_C2_RS1_REG, instead of reimplementing it in
simulate_c_jr_jalr().
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] 16+ messages in thread* [PATCH 08/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (6 preceding siblings ...)
2025-05-11 21:17 ` [PATCH 07/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_C2_RS1_REG Nam Cao
@ 2025-05-11 21:18 ` Nam Cao
2025-05-11 21:18 ` [PATCH 09/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG Nam Cao
` (3 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:18 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao
Use RVC_EXTRACT_BTYPE_IMM, instead of reimplementing it in
simulate_c_bnez_beqz().
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] 16+ messages in thread* [PATCH 09/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (7 preceding siblings ...)
2025-05-11 21:18 ` [PATCH 08/11] riscv: kprobes: Remove duplication of RVC_EXTRACT_BTYPE_IMM Nam Cao
@ 2025-05-11 21:18 ` Nam Cao
2025-05-11 21:18 ` [PATCH 10/11] riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM Nam Cao
` (2 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:18 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao
Use RV_EXTRACT_RD_REG, instead of reimplementing its code.
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] 16+ messages in thread* [PATCH 10/11] riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (8 preceding siblings ...)
2025-05-11 21:18 ` [PATCH 09/11] riscv: kprobes: Remove duplication of RV_EXTRACT_RD_REG Nam Cao
@ 2025-05-11 21:18 ` Nam Cao
2025-05-11 21:18 ` [PATCH 11/11] riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM Nam Cao
2025-05-14 8:05 ` [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Alexandre Ghiti
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:18 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao
Use RV_EXTRACT_UTYPE_IMM, instead of reimplementing it in simulate_auipc().
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] 16+ messages in thread* [PATCH 11/11] riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (9 preceding siblings ...)
2025-05-11 21:18 ` [PATCH 10/11] riscv: kprobes: Remove duplication of RV_EXTRACT_UTYPE_IMM Nam Cao
@ 2025-05-11 21:18 ` Nam Cao
2025-05-14 8:05 ` [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Alexandre Ghiti
11 siblings, 0 replies; 16+ messages in thread
From: Nam Cao @ 2025-05-11 21:18 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Alexandre Ghiti,
linux-riscv, linux-kernel
Cc: Nam Cao
Use RV_EXTRACT_ITYPE_IMM, instead of re-implementing it in simulate_jalr().
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] 16+ messages in thread* Re: [PATCH 00/11] riscv: kprobes: Clean up instruction simulation
2025-05-11 21:17 [PATCH 00/11] riscv: kprobes: Clean up instruction simulation Nam Cao
` (10 preceding siblings ...)
2025-05-11 21:18 ` [PATCH 11/11] riscv: kprobes: Remove duplication of RV_EXTRACT_ITYPE_IMM Nam Cao
@ 2025-05-14 8:05 ` Alexandre Ghiti
11 siblings, 0 replies; 16+ messages in thread
From: Alexandre Ghiti @ 2025-05-14 8:05 UTC (permalink / raw)
To: Nam Cao, Paul Walmsley, Palmer Dabbelt, Albert Ou, linux-riscv,
linux-kernel
On 11/05/2025 23:17, Nam Cao 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.
>
> 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(-)
So for the whole series, you can add:
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Thanks, that's a nice cleanup!
Alex
>
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 16+ messages in thread