Skip to contentSkip to conversation

riscv: kprobes: simulate some instructions

PATCH Applied Nam Cao 4 participants 13 messages Jul 30, 2023 – Aug 30, 2023 RISC-V

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

cover riscv: kprobes: simulate some instructions

Simulate some currently rejected instructions. Still to be simulated are:
- c.jal
- c.ebreak

Nam Cao (3):
riscv: kprobes: simulate c.j instruction
riscv: kprobes: simulate c.jr and c.jalr instructions
riscv: kprobes: simulate c.beqz and c.bnez

 arch/riscv/kernel/probes/decode-insn.c   |  11 +-
 arch/riscv/kernel/probes/simulate-insn.c | 105 +++++++++
 arch/riscv/kernel/probes/simulate-insn.h |   5 +
 drivers/test_kprobe/Makefile             |   3 +
 drivers/test_kprobe/test_kprobe.c        | 265 +++++++++++++++++++++++
 5 files changed, 384 insertions(+), 5 deletions(-)
 create mode 100644 drivers/test_kprobe/Makefile
 create mode 100644 drivers/test_kprobe/test_kprobe.c
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 riscv: kprobes: simulate c.j instruction

kprobes currently rejects c.j instruction. Implement it.

---
 arch/riscv/kernel/probes/decode-insn.c   |  3 ++-
 arch/riscv/kernel/probes/simulate-insn.c | 24 ++++++++++++++++++++++++
 arch/riscv/kernel/probes/simulate-insn.h |  1 +
 3 files changed, 27 insertions(+), 1 deletion(-)
arch/riscv/kernel/probes/decode-insn.c +2 -1
index 64f6183b4717..39adb07a342d 100644
@@ -29,13 +29,14 @@ riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)
 	 * TODO: the REJECTED ones below need to be implemented
 	 */
 #ifdef CONFIG_RISCV_ISA_C
-	RISCV_INSN_REJECTED(c_j,		insn);
 	RISCV_INSN_REJECTED(c_jr,		insn);
 	RISCV_INSN_REJECTED(c_jal,		insn);
 	RISCV_INSN_REJECTED(c_jalr,		insn);
 	RISCV_INSN_REJECTED(c_beqz,		insn);
 	RISCV_INSN_REJECTED(c_bnez,		insn);
 	RISCV_INSN_REJECTED(c_ebreak,		insn);
+
+	RISCV_INSN_SET_SIMULATE(c_j,		insn);
 #endif
 
 	RISCV_INSN_SET_SIMULATE(jal,		insn);
arch/riscv/kernel/probes/simulate-insn.c +24 -0
index 7441ac8a6843..3ba45c612cd8 100644
@@ -188,3 +188,27 @@ bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r
 
 	return true;
 }
+
+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;
+
+	instruction_pointer_set(regs, addr + sign_extend32(offset, 11));
+
+	return true;
+}
arch/riscv/kernel/probes/simulate-insn.h +1 -0
index 61e35db31001..4bd6c266e7d3 100644
@@ -24,5 +24,6 @@ bool simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs);
 
 #endif /* _RISCV_KERNEL_PROBES_SIMULATE_INSN_H */
Signature · 7 lines
2.34.1


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

Re: [PATCH 1/3] riscv: kprobes: simulate c.j instruction

Quoted 48 lines · Nam Cao

kprobes currently rejects c.j instruction. Implement it.

---
 arch/riscv/kernel/probes/decode-insn.c   |  3 ++-
 arch/riscv/kernel/probes/simulate-insn.c | 24 ++++++++++++++++++++++++
 arch/riscv/kernel/probes/simulate-insn.h |  1 +
 3 files changed, 27 insertions(+), 1 deletion(-)
arch/riscv/kernel/probes/decode-insn.c +2 -1
index 64f6183b4717..39adb07a342d 100644
@@ -29,13 +29,14 @@ riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)
 	 * TODO: the REJECTED ones below need to be implemented
 	 */
 #ifdef CONFIG_RISCV_ISA_C
-	RISCV_INSN_REJECTED(c_j,		insn);
 	RISCV_INSN_REJECTED(c_jr,		insn);
 	RISCV_INSN_REJECTED(c_jal,		insn);
 	RISCV_INSN_REJECTED(c_jalr,		insn);
 	RISCV_INSN_REJECTED(c_beqz,		insn);
 	RISCV_INSN_REJECTED(c_bnez,		insn);
 	RISCV_INSN_REJECTED(c_ebreak,		insn);
+
+	RISCV_INSN_SET_SIMULATE(c_j,		insn);
 #endif
 
 	RISCV_INSN_SET_SIMULATE(jal,		insn);
arch/riscv/kernel/probes/simulate-insn.c +11 -0
index 7441ac8a6843..3ba45c612cd8 100644
@@ -188,3 +188,27 @@ bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r
 
 	return true;
 }
+
+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;
+

arch/riscv/kernel/probes/simulate-insn.c bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r · line 211

Quoted 10 lines
arch/riscv/kernel/probes/simulate-insn.c +10 -0
+	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;
+
+	instruction_pointer_set(regs, addr + sign_extend32(offset, 11));

Can you use riscv_insn_insert_jtype_imm() from insn.h since it is already created? It will also sign extend for you. Don't worry about creating a similar function for the branches, I am in the process of refactoring the insn.h file.

Quoted 22 lines
arch/riscv/kernel/probes/simulate-insn.c +3 -0
+
+	return true;
+}
arch/riscv/kernel/probes/simulate-insn.h +1 -0
index 61e35db31001..4bd6c266e7d3 100644
@@ -24,5 +24,6 @@ bool simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs);
 
 #endif /* _RISCV_KERNEL_PROBES_SIMULATE_INSN_H */
Signature · 7 lines
2.34.1


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

Thanks,
Charlie

Reviewed-by
#3 lore raw

Re: [PATCH 1/3] riscv: kprobes: simulate c.j instruction

Quoted 81 lines · Charlie Jenkins
Quoted 48 lines · Nam Cao

kprobes currently rejects c.j instruction. Implement it.

---
 arch/riscv/kernel/probes/decode-insn.c   |  3 ++-
 arch/riscv/kernel/probes/simulate-insn.c | 24 ++++++++++++++++++++++++
 arch/riscv/kernel/probes/simulate-insn.h |  1 +
 3 files changed, 27 insertions(+), 1 deletion(-)
arch/riscv/kernel/probes/decode-insn.c +2 -1
index 64f6183b4717..39adb07a342d 100644
@@ -29,13 +29,14 @@ riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)
 	 * TODO: the REJECTED ones below need to be implemented
 	 */
 #ifdef CONFIG_RISCV_ISA_C
-	RISCV_INSN_REJECTED(c_j,		insn);
 	RISCV_INSN_REJECTED(c_jr,		insn);
 	RISCV_INSN_REJECTED(c_jal,		insn);
 	RISCV_INSN_REJECTED(c_jalr,		insn);
 	RISCV_INSN_REJECTED(c_beqz,		insn);
 	RISCV_INSN_REJECTED(c_bnez,		insn);
 	RISCV_INSN_REJECTED(c_ebreak,		insn);
+
+	RISCV_INSN_SET_SIMULATE(c_j,		insn);
 #endif
 
 	RISCV_INSN_SET_SIMULATE(jal,		insn);
arch/riscv/kernel/probes/simulate-insn.c +11 -0
index 7441ac8a6843..3ba45c612cd8 100644
@@ -188,3 +188,27 @@ bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r
 
 	return true;
 }
+
+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;
+

arch/riscv/kernel/probes/simulate-insn.c bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *r · line 211

Quoted 10 lines
arch/riscv/kernel/probes/simulate-insn.c +10 -0
+	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;
+
+	instruction_pointer_set(regs, addr + sign_extend32(offset, 11));

Can you use riscv_insn_insert_jtype_imm() from insn.h since it is already created? It will also sign extend for you. Don't worry about creating a similar function for the branches, I am in the process of refactoring the insn.h file.

Quoted 18 lines
arch/riscv/kernel/probes/simulate-insn.c +3 -0
+
+	return true;
+}
arch/riscv/kernel/probes/simulate-insn.h +1 -0
index 61e35db31001..4bd6c266e7d3 100644
@@ -24,5 +24,6 @@ bool simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs);
 
 #endif /* _RISCV_KERNEL_PROBES_SIMULATE_INSN_H */
Signature · 1 line
2.34.1
Quoted 6 lines
Quoted 4 lines

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

Thanks,
Charlie

Oh sorry, I forgot this was compressed. You can ignore the last message. The changes look good. You can add:

author
#4 lore raw

PATCH 2/3 riscv: kprobes: simulate c.jr and c.jalr instructions

In reply to Nam Cao

kprobes currently rejects c.jr and c.jalr instructions. Implement them.

---
 arch/riscv/kernel/probes/decode-insn.c   |  4 +--
 arch/riscv/kernel/probes/simulate-insn.c | 37 ++++++++++++++++++++++++
 arch/riscv/kernel/probes/simulate-insn.h |  2 ++
 3 files changed, 41 insertions(+), 2 deletions(-)
arch/riscv/kernel/probes/decode-insn.c +2 -2
index 39adb07a342d..6dba23a55ac7 100644
@@ -29,14 +29,14 @@ riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)
 	 * TODO: the REJECTED ones below need to be implemented
 	 */
 #ifdef CONFIG_RISCV_ISA_C
-	RISCV_INSN_REJECTED(c_jr,		insn);
 	RISCV_INSN_REJECTED(c_jal,		insn);
-	RISCV_INSN_REJECTED(c_jalr,		insn);
 	RISCV_INSN_REJECTED(c_beqz,		insn);
 	RISCV_INSN_REJECTED(c_bnez,		insn);
 	RISCV_INSN_REJECTED(c_ebreak,		insn);
 
 	RISCV_INSN_SET_SIMULATE(c_j,		insn);
+	RISCV_INSN_SET_SIMULATE(c_jr,		insn);
+	RISCV_INSN_SET_SIMULATE(c_jalr,		insn);
 #endif
 
 	RISCV_INSN_SET_SIMULATE(jal,		insn);
arch/riscv/kernel/probes/simulate-insn.c +37 -0
index 3ba45c612cd8..1ead6f4951f9 100644
@@ -212,3 +212,40 @@ bool __kprobes simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs
 
 	return true;
 }
+
+static bool __kprobes simulate_c_jr_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs,
+					 bool is_jalr)
+{
+	/*
+	 *  15    12 11  7 6   2 1  0
+	 * | funct4 | rs1 | rs2 | op |
+	 *     4       5     5    2
+	 */
+
+	unsigned long jump_addr;
+
+	u32 rs1 = (opcode >> 7) & 0x1f;
+
+	if (rs1 == 0) /* C.JR is only valid when rs1 != x0 */
+		return false;
+
+	if (!rv_insn_reg_get_val(regs, rs1, &jump_addr))
+		return false;
+
+	if (is_jalr && !rv_insn_reg_set_val(regs, 1, addr + 2))
+		return false;
+
+	instruction_pointer_set(regs, jump_addr);
+
+	return true;
+}
+
+bool __kprobes simulate_c_jr(u32 opcode, unsigned long addr, struct pt_regs *regs)
+{
+	return simulate_c_jr_jalr(opcode, addr, regs, false);
+}
+
+bool __kprobes simulate_c_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs)
+{
+	return simulate_c_jr_jalr(opcode, addr, regs, true);
+}
arch/riscv/kernel/probes/simulate-insn.h +2 -0
index 4bd6c266e7d3..472a1948ec4f 100644
@@ -25,5 +25,7 @@ bool simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_jr(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
 
 #endif /* _RISCV_KERNEL_PROBES_SIMULATE_INSN_H */
Signature · 1 line
2.34.1
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] riscv: kprobes: simulate c.jr and c.jalr instructions

Quoted 95 lines · Nam Cao

kprobes currently rejects c.jr and c.jalr instructions. Implement them.

---
 arch/riscv/kernel/probes/decode-insn.c   |  4 +--
 arch/riscv/kernel/probes/simulate-insn.c | 37 ++++++++++++++++++++++++
 arch/riscv/kernel/probes/simulate-insn.h |  2 ++
 3 files changed, 41 insertions(+), 2 deletions(-)
arch/riscv/kernel/probes/decode-insn.c +2 -2
index 39adb07a342d..6dba23a55ac7 100644
@@ -29,14 +29,14 @@ riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)
 	 * TODO: the REJECTED ones below need to be implemented
 	 */
 #ifdef CONFIG_RISCV_ISA_C
-	RISCV_INSN_REJECTED(c_jr,		insn);
 	RISCV_INSN_REJECTED(c_jal,		insn);
-	RISCV_INSN_REJECTED(c_jalr,		insn);
 	RISCV_INSN_REJECTED(c_beqz,		insn);
 	RISCV_INSN_REJECTED(c_bnez,		insn);
 	RISCV_INSN_REJECTED(c_ebreak,		insn);
 
 	RISCV_INSN_SET_SIMULATE(c_j,		insn);
+	RISCV_INSN_SET_SIMULATE(c_jr,		insn);
+	RISCV_INSN_SET_SIMULATE(c_jalr,		insn);
 #endif
 
 	RISCV_INSN_SET_SIMULATE(jal,		insn);
arch/riscv/kernel/probes/simulate-insn.c +37 -0
index 3ba45c612cd8..1ead6f4951f9 100644
@@ -212,3 +212,40 @@ bool __kprobes simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs
 
 	return true;
 }
+
+static bool __kprobes simulate_c_jr_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs,
+					 bool is_jalr)
+{
+	/*
+	 *  15    12 11  7 6   2 1  0
+	 * | funct4 | rs1 | rs2 | op |
+	 *     4       5     5    2
+	 */
+
+	unsigned long jump_addr;
+
+	u32 rs1 = (opcode >> 7) & 0x1f;
+
+	if (rs1 == 0) /* C.JR is only valid when rs1 != x0 */
+		return false;
+
+	if (!rv_insn_reg_get_val(regs, rs1, &jump_addr))
+		return false;
+
+	if (is_jalr && !rv_insn_reg_set_val(regs, 1, addr + 2))
+		return false;
+
+	instruction_pointer_set(regs, jump_addr);
+
+	return true;
+}
+
+bool __kprobes simulate_c_jr(u32 opcode, unsigned long addr, struct pt_regs *regs)
+{
+	return simulate_c_jr_jalr(opcode, addr, regs, false);
+}
+
+bool __kprobes simulate_c_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs)
+{
+	return simulate_c_jr_jalr(opcode, addr, regs, true);
+}
arch/riscv/kernel/probes/simulate-insn.h +2 -0
index 4bd6c266e7d3..472a1948ec4f 100644
@@ -25,5 +25,7 @@ bool simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_jr(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
 
 #endif /* _RISCV_KERNEL_PROBES_SIMULATE_INSN_H */
Signature · 7 lines
2.34.1


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

You can add:

author
#6 lore raw

PATCH 3/3 riscv: kprobes: simulate c.beqz and c.bnez

In reply to Nam Cao

kprobes currently rejects instruction c.beqz and c.bnez. Implement them.

---
 arch/riscv/kernel/probes/decode-insn.c   |  4 +--
 arch/riscv/kernel/probes/simulate-insn.c | 44 ++++++++++++++++++++++++
 arch/riscv/kernel/probes/simulate-insn.h |  2 ++
 3 files changed, 48 insertions(+), 2 deletions(-)
arch/riscv/kernel/probes/decode-insn.c +2 -2
index 6dba23a55ac7..65d9590bfb9f 100644
@@ -30,13 +30,13 @@ riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)
 	 */
 #ifdef CONFIG_RISCV_ISA_C
 	RISCV_INSN_REJECTED(c_jal,		insn);
-	RISCV_INSN_REJECTED(c_beqz,		insn);
-	RISCV_INSN_REJECTED(c_bnez,		insn);
 	RISCV_INSN_REJECTED(c_ebreak,		insn);
 
 	RISCV_INSN_SET_SIMULATE(c_j,		insn);
 	RISCV_INSN_SET_SIMULATE(c_jr,		insn);
 	RISCV_INSN_SET_SIMULATE(c_jalr,		insn);
+	RISCV_INSN_SET_SIMULATE(c_beqz,		insn);
+	RISCV_INSN_SET_SIMULATE(c_bnez,		insn);
 #endif
 
 	RISCV_INSN_SET_SIMULATE(jal,		insn);
arch/riscv/kernel/probes/simulate-insn.c +44 -0

47 lines not on this page · raw message

arch/riscv/kernel/probes/simulate-insn.h +2 -0
index 472a1948ec4f..44ebbc444db9 100644
@@ -27,5 +27,7 @@ bool simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_c_jr(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_c_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_bnez(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_beqz(u32 opcode, unsigned long addr, struct pt_regs *regs);
 
 #endif /* _RISCV_KERNEL_PROBES_SIMULATE_INSN_H */
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] riscv: kprobes: simulate c.beqz and c.bnez

Quoted 101 lines · Nam Cao

kprobes currently rejects instruction c.beqz and c.bnez. Implement them.

---
 arch/riscv/kernel/probes/decode-insn.c   |  4 +--
 arch/riscv/kernel/probes/simulate-insn.c | 44 ++++++++++++++++++++++++
 arch/riscv/kernel/probes/simulate-insn.h |  2 ++
 3 files changed, 48 insertions(+), 2 deletions(-)
arch/riscv/kernel/probes/decode-insn.c +2 -2
index 6dba23a55ac7..65d9590bfb9f 100644
@@ -30,13 +30,13 @@ riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)
 	 */
 #ifdef CONFIG_RISCV_ISA_C
 	RISCV_INSN_REJECTED(c_jal,		insn);
-	RISCV_INSN_REJECTED(c_beqz,		insn);
-	RISCV_INSN_REJECTED(c_bnez,		insn);
 	RISCV_INSN_REJECTED(c_ebreak,		insn);
 
 	RISCV_INSN_SET_SIMULATE(c_j,		insn);
 	RISCV_INSN_SET_SIMULATE(c_jr,		insn);
 	RISCV_INSN_SET_SIMULATE(c_jalr,		insn);
+	RISCV_INSN_SET_SIMULATE(c_beqz,		insn);
+	RISCV_INSN_SET_SIMULATE(c_bnez,		insn);
 #endif
 
 	RISCV_INSN_SET_SIMULATE(jal,		insn);
arch/riscv/kernel/probes/simulate-insn.c +44 -0
index 1ead6f4951f9..d3099d67816d 100644
@@ -249,3 +249,47 @@ bool __kprobes simulate_c_jalr(u32 opcode, unsigned long addr, struct pt_regs *r
 {
 	return simulate_c_jr_jalr(opcode, addr, regs, true);
 }
+
+static bool __kprobes simulate_c_bnez_beqz(u32 opcode, unsigned long addr, struct pt_regs *regs,
+					   bool is_bnez)
+{
+	/*
+	 *  15    13 12           10 9    7 6                 2 1  0
+	 * | funct3 | offset[8|4:3] | rs1' | offset[7:6|2:1|5] | op |
+	 *     3            3          3             5           2
+	 */
+
+	s32 offset;
+	u32 rs1;
+	unsigned long rs1_val;
+
+	rs1 = 0x8 | ((opcode >> 7) & 0x7);
+
+	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 {
+		offset = 2;
+	}
+
+	instruction_pointer_set(regs, addr + offset);
+
+	return true;
+}
+
+bool __kprobes simulate_c_bnez(u32 opcode, unsigned long addr, struct pt_regs *regs)
+{
+	return simulate_c_bnez_beqz(opcode, addr, regs, true);
+}
+
+bool __kprobes simulate_c_beqz(u32 opcode, unsigned long addr, struct pt_regs *regs)
+{
+	return simulate_c_bnez_beqz(opcode, addr, regs, false);
+}
arch/riscv/kernel/probes/simulate-insn.h +2 -0
index 472a1948ec4f..44ebbc444db9 100644
@@ -27,5 +27,7 @@ bool simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_c_jr(u32 opcode, unsigned long addr, struct pt_regs *regs);
 bool simulate_c_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_bnez(u32 opcode, unsigned long addr, struct pt_regs *regs);
+bool simulate_c_beqz(u32 opcode, unsigned long addr, struct pt_regs *regs);
 
 #endif /* _RISCV_KERNEL_PROBES_SIMULATE_INSN_H */
Signature · 7 lines
2.34.1


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

You can add:

author
#8 lore raw

test code for kprobe

In reply to Nam Cao
---
 drivers/test_kprobe/Makefile      |   3 +
 drivers/test_kprobe/test_kprobe.c | 265 ++++++++++++++++++++++++++++++
 2 files changed, 268 insertions(+)
 create mode 100644 drivers/test_kprobe/Makefile
 create mode 100644 drivers/test_kprobe/test_kprobe.c
drivers/test_kprobe/Makefile new file +3 -0
new file mode 100644
index 000000000000..c3c39bd0f8b5
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+obj-m += test_kprobe.o
drivers/test_kprobe/test_kprobe.c new file +265 -0

265 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw

Re: test code for kprobe

Nam Cao <[email protected]> writes:

A RISC-V specific kprobes test -- much welcome!

Please add a proper commit message here.

drivers/test_kprobe/Makefile

Quoted 8 lines
---
 drivers/test_kprobe/Makefile      |   3 +
 drivers/test_kprobe/test_kprobe.c | 265 ++++++++++++++++++++++++++++++
 2 files changed, 268 insertions(+)
 create mode 100644 drivers/test_kprobe/Makefile
 create mode 100644 drivers/test_kprobe/test_kprobe.c
drivers/test_kprobe/Makefile +0 -0

Architecture specific test code usually reside in "arch/$ARCH" (arch/riscv), and is part of Kconfig.debug.

Have a look at:
* grep for ARM_KPROBES_TEST in arch/arm
* grep for KPROBES_SANITY_TEST, and in arch/powerpc grep test_emulate_step
* grep S390_KPROBES_SANITY_TEST

Björn

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

author
#10 lore raw

Re: test code for kprobe

Quoted 18 lines · Björn Töpel

Nam Cao <[email protected]> writes:

A RISC-V specific kprobes test -- much welcome!

Please add a proper commit message here.

drivers/test_kprobe/Makefile

Quoted 8 lines
---
 drivers/test_kprobe/Makefile      |   3 +
 drivers/test_kprobe/test_kprobe.c | 265 ++++++++++++++++++++++++++++++
 2 files changed, 268 insertions(+)
 create mode 100644 drivers/test_kprobe/Makefile
 create mode 100644 drivers/test_kprobe/test_kprobe.c
drivers/test_kprobe/Makefile +0 -0

Architecture specific test code usually reside in "arch/$ARCH" (arch/riscv), and is part of Kconfig.debug.

Quoted 5 lines

Have a look at:
* grep for ARM_KPROBES_TEST in arch/arm
* grep for KPROBES_SANITY_TEST, and in arch/powerpc grep test_emulate_step
* grep S390_KPROBES_SANITY_TEST

Sorry that I wasn't clear with this: I just wanted to show how testing was done. This is not meant to be merged.

I do have plans to clean this up and send upstream in the future, but not with this patch series.

Best regards,
Nam

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

#11 lore raw

Re: test code for kprobe

Quoted 25 lines · Nam Cao
Quoted 18 lines · Björn Töpel

Nam Cao <[email protected]> writes:

A RISC-V specific kprobes test -- much welcome!

Please add a proper commit message here.

drivers/test_kprobe/Makefile

Quoted 8 lines
---
 drivers/test_kprobe/Makefile      |   3 +
 drivers/test_kprobe/test_kprobe.c | 265 ++++++++++++++++++++++++++++++
 2 files changed, 268 insertions(+)
 create mode 100644 drivers/test_kprobe/Makefile
 create mode 100644 drivers/test_kprobe/test_kprobe.c
drivers/test_kprobe/Makefile +0 -0

Architecture specific test code usually reside in "arch/$ARCH" (arch/riscv), and is part of Kconfig.debug.

Quoted 5 lines

Have a look at:
* grep for ARM_KPROBES_TEST in arch/arm
* grep for KPROBES_SANITY_TEST, and in arch/powerpc grep test_emulate_step
* grep S390_KPROBES_SANITY_TEST

Quoted 5 lines

Sorry that I wasn't clear with this: I just wanted to show how testing was done. This is not meant to be merged.

I do have plans to clean this up and send upstream in the future, but not with this patch series.

Please do! A RISC-V specific kprobes test would be nice.

Björn

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

#12 lore raw
In reply to Nam Cao
This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This series was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 10 lines · On Sun, 30 Jul 2023 10:27:06 +0200 you wrote:

Simulate some currently rejected instructions. Still to be simulated are:
- c.jal
- c.ebreak

Nam Cao (3):
riscv: kprobes: simulate c.j instruction
riscv: kprobes: simulate c.jr and c.jalr instructions
riscv: kprobes: simulate c.beqz and c.bnez

[...]

Here is the summary with links:
- [1/3] riscv: kprobes: simulate c.j instruction

    https://git.kernel.org/riscv/c/a93892974f2e
  - [2/3] riscv: kprobes: simulate c.jr and c.jalr instructions
    https://git.kernel.org/riscv/c/b18256d9b744
  - [3/3] riscv: kprobes: simulate c.beqz and c.bnez
    https://git.kernel.org/riscv/c/d943705fba3a

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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