Skip to contentSkip to conversation

riscv: add c.jal instruction simulation for kprobes

PATCH In review Xiaofeng Yuan 4 participants 8 messages Jul 1 – Jul 10 RISC-V

lore
Share & tools
lore
#0 lore raw

v6 cover riscv: add c.jal instruction simulation for kprobes

The c.jal instruction is part of the RISC-V compressed instruction set and is defined only on RV32. Currently kprobes rejects it, causing probes placed on c.jal to fail.

This series adds simulation support for c.jal and includes a test case by Nam Cao.

Changes since v5:
- Removed the #if __riscv_xlen == 32 guards around simulate_c_jal() as both Charlie Jenkins and Nam Cao agreed they are unnecessary. On RV64, riscv_insn_is_c_jal() always returns 0 so the simulation code is never invoked, and the small overhead in kernel size is acceptable.
- Included Nam Cao's test case for c.jal instruction simulation.

Changes since v4:
- Reuse simulate_c_j() for the jump logic (Nam Cao)
- Use direct register assignment instead of rv_insn_reg_set_val()
- Move RISCV_INSN_SET_SIMULATE(c_jal) to group with other compressed jump instructions

Nam Cao (1):
riscv: kprobes: add test case for c.jal instruction simulation

Xiaofeng Yuan (1):
riscv: probes: simulate c.jal instruction

Nam Cao (1):
riscv: kprobes: add test case for c.jal instruction simulation

Xiaofeng Yuan (1):
riscv: probes: simulate c.jal instruction

 arch/riscv/kernel/probes/decode-insn.c        |  2 +-
 arch/riscv/kernel/probes/simulate-insn.c      |  7 ++++++
 arch/riscv/kernel/probes/simulate-insn.h      |  1 +
 .../kernel/tests/kprobes/test-kprobes-asm.S   | 24 +++++++++++++++++++
 4 files changed, 33 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v6 1/2 riscv: probes: simulate c.jal instruction

The c.jal instruction is currently marked REJECTED in kprobes instruction decoding, but it should be SIMULATED like other compressed jump instructions.

Add simulate_c_jal() which saves the return address to RA and sets the program counter to the target offset, reusing simulate_c_j for the common jump logic.

Although c.jal is RV32-only, the function compiles unconditionally. On RV64, riscv_insn_is_c_jal() always returns 0, so the simulation code is never invoked and the small overhead in kernel size is acceptable.

---
 arch/riscv/kernel/probes/decode-insn.c   | 2 +-
 arch/riscv/kernel/probes/simulate-insn.c | 7 +++++++
 arch/riscv/kernel/probes/simulate-insn.h | 1 +
 3 files changed, 9 insertions(+), 1 deletion(-)
arch/riscv/kernel/probes/decode-insn.c +1 -1
index 65d9590bf..433d9035b 100644
@@ -29,12 +29,12 @@ 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_jal,		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_jal,		insn);
 	RISCV_INSN_SET_SIMULATE(c_beqz,		insn);
 	RISCV_INSN_SET_SIMULATE(c_bnez,		insn);
 #endif
arch/riscv/kernel/probes/simulate-insn.c +7 -0
index fa581590c..f8a2f6857 100644
@@ -163,6 +163,13 @@ bool __kprobes simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs
 	return true;
 }
 
+bool __kprobes simulate_c_jal(u32 opcode, unsigned long addr, struct pt_regs *regs)
+{
+	regs->ra = addr + 2;
+
+	return simulate_c_j(opcode, addr, regs);
+}
+
 static bool __kprobes simulate_c_jr_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs,
 					 bool is_jalr)
 {
arch/riscv/kernel/probes/simulate-insn.h +1 -0
index 44ebbc444..b89e1bb01 100644
@@ -25,6 +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_jal(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);
Signature · 1 line
2.43.0
Reviewed-by Tested-by on v6 1/2
#2 lore raw

Re: [PATCH v6 1/2] riscv: probes: simulate c.jal instruction

Quoted 8 lines · Xiaofeng Yuan

The c.jal instruction is currently marked REJECTED in kprobes instruction decoding, but it should be SIMULATED like other compressed jump instructions.

Add simulate_c_jal() which saves the return address to RA and sets the program counter to the target offset, reusing simulate_c_j for the common jump logic.

Quoted 6 lines

Although c.jal is RV32-only, the function compiles unconditionally. On RV64, riscv_insn_is_c_jal() always returns 0, so the simulation code is never invoked and the small overhead in kernel size is acceptable.

Reviewed-by Tested-by on v6 1/2
#3 lore raw

Re: [PATCH v6 1/2] riscv: probes: simulate c.jal instruction

In reply to Xiaofeng Yuan

# Add your code comments below. There is no need to trim or delete

# any existing content -- just insert your comments under the relevant
# lines of code. Lines starting with "> " are quoted diff context and
# lines starting with "| " are comments from other reviewers.
# The final email will be reformatted automatically to include only
# the sections that have your comments.
#
Quoted 58 lines

The c.jal instruction is currently marked REJECTED in kprobes instruction decoding, but it should be SIMULATED like other compressed jump instructions.

Add simulate_c_jal() which saves the return address to RA and sets the program counter to the target offset, reusing simulate_c_j for the common jump logic.

Although c.jal is RV32-only, the function compiles unconditionally. On RV64, riscv_insn_is_c_jal() always returns 0, so the simulation code is never invoked and the small overhead in kernel size is acceptable.

arch/riscv/kernel/probes/decode-insn.c +1 -1
index 65d9590bfb9f..433d9035b888 100644
@@ -29,12 +29,12 @@ 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_jal,		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_jal,		insn);
 	RISCV_INSN_SET_SIMULATE(c_beqz,		insn);
 	RISCV_INSN_SET_SIMULATE(c_bnez,		insn);
 #endif
arch/riscv/kernel/probes/simulate-insn.c +7 -0
index fa581590c1f8..f8a2f6857877 100644
@@ -163,6 +163,13 @@ bool __kprobes simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs
 	return true;
 }
 
+bool __kprobes simulate_c_jal(u32 opcode, unsigned long addr, struct pt_regs *regs)
+{
+	regs->ra = addr + 2;
+
+	return simulate_c_j(opcode, addr, regs);
+}
+
 static bool __kprobes simulate_c_jr_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs,
 					 bool is_jalr)
 {
arch/riscv/kernel/probes/simulate-insn.h +0 -0
index 44ebbc444db9..b89e1bb01842 100644

arch/riscv/kernel/probes/simulate-insn.h bool simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *regs); · line 31

Quoted 7 lines
arch/riscv/kernel/probes/simulate-insn.h +1 -0
@@ -25,6 +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_jal(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);
Signature · 1 line
- Charlie
maintainer Tested-by J. Kernel Developer Tested-by J. Kernel Developer
#4 lore raw

Re: [PATCH v6 1/2] riscv: probes: simulate c.jal instruction

Hi Nam, Charlie,

Quoted 2 lines · Charlie Jenkins

Thank you both for posting Reviewed-by:s and Tested-by:s for these patches. One request: at the end of your Tested-by:s, it's very helpful for myself and others if we know what platform they were tested on, and whether the testing was simply boot testing, or whether the tests were direct functional tests of the code being patched. So for example, something along the lines of the following would be really great:

or

Thanks again for reviewing and testing!

- Paul

#5 lore raw

PATCH v6 2/2 riscv: kprobes: add test case for c.jal instruction simulation

In reply to Xiaofeng Yuan

From: Nam Cao <[email protected]>

Add a test case validating that kprobes correctly simulates the c.jal instruction on RV32.

The test uses two probe points: a forward c.jal and a backward c.jal, and verifies that the containing function returns the expected magic value KPROBE_TEST_MAGIC after kprobe interception.

---
 .../kernel/tests/kprobes/test-kprobes-asm.S   | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S +24 -0
index f16deee9e..15e50934b 100644
@@ -179,6 +179,23 @@ test_kprobes_c_bnez_addr3:
 	ret
 SYM_FUNC_END(test_kprobes_c_bnez)
 
+#ifdef CONFIG_32BIT
+SYM_FUNC_START(test_kprobes_c_jal)
+	li a0, 0
+	mv a1, ra
+test_kprobes_c_jal_addr1:
+	c.jal 2f
+	ret
+1:	li a0, KPROBE_TEST_MAGIC_UPPER
+	ret
+test_kprobes_c_jal_addr2:
+2:	c.jal 1b
+	li a2, KPROBE_TEST_MAGIC_LOWER
+	add a0, a0, a2
+	jr a1
+SYM_FUNC_END(test_kprobes_c_jal)
+#endif
+
 #endif /* CONFIG_RISCV_ISA_C */
 
 .section .rodata
@@ -209,6 +226,10 @@ SYM_DATA_START(test_kprobes_addresses)
 	RISCV_PTR test_kprobes_c_bnez_addr1
 	RISCV_PTR test_kprobes_c_bnez_addr2
 	RISCV_PTR test_kprobes_c_bnez_addr3
+#ifdef CONFIG_32BIT
+	RISCV_PTR test_kprobes_c_jal_addr1
+	RISCV_PTR test_kprobes_c_jal_addr2
+#endif
 #endif /* CONFIG_RISCV_ISA_C */
 	RISCV_PTR 0
 SYM_DATA_END(test_kprobes_addresses)
@@ -226,6 +247,9 @@ SYM_DATA_START(test_kprobes_functions)
 	RISCV_PTR test_kprobes_c_jalr
 	RISCV_PTR test_kprobes_c_beqz
 	RISCV_PTR test_kprobes_c_bnez
+#ifdef CONFIG_32BIT
+	RISCV_PTR test_kprobes_c_jal
+#endif
 #endif /* CONFIG_RISCV_ISA_C */
 	RISCV_PTR 0
 SYM_DATA_END(test_kprobes_functions)
Signature · 1 line
2.43.0
Reviewed-by Tested-by on v6 2/2
#6 lore raw

Re: [PATCH v6 2/2] riscv: kprobes: add test case for c.jal instruction simulation

# Add your code comments below. There is no need to trim or delete

# any existing content -- just insert your comments under the relevant
# lines of code. Lines starting with "> " are quoted diff context and
# lines starting with "| " are comments from other reviewers.
# The final email will be reformatted automatically to include only
# the sections that have your comments.
#
Quoted 53 lines

Add a test case validating that kprobes correctly simulates the c.jal instruction on RV32.

The test uses two probe points: a forward c.jal and a backward c.jal, and verifies that the containing function returns the expected magic value KPROBE_TEST_MAGIC after kprobe interception.

arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S +21 -0
index f16deee9e091..15e50934bbd7 100644
@@ -179,6 +179,23 @@ test_kprobes_c_bnez_addr3:
 	ret
 SYM_FUNC_END(test_kprobes_c_bnez)
 
+#ifdef CONFIG_32BIT
+SYM_FUNC_START(test_kprobes_c_jal)
+	li a0, 0
+	mv a1, ra
+test_kprobes_c_jal_addr1:
+	c.jal 2f
+	ret
+1:	li a0, KPROBE_TEST_MAGIC_UPPER
+	ret
+test_kprobes_c_jal_addr2:
+2:	c.jal 1b
+	li a2, KPROBE_TEST_MAGIC_LOWER
+	add a0, a0, a2
+	jr a1
+SYM_FUNC_END(test_kprobes_c_jal)
+#endif
+
 #endif /* CONFIG_RISCV_ISA_C */
 
 .section .rodata
@@ -209,6 +226,10 @@ SYM_DATA_START(test_kprobes_addresses)
 	RISCV_PTR test_kprobes_c_bnez_addr1
 	RISCV_PTR test_kprobes_c_bnez_addr2
 	RISCV_PTR test_kprobes_c_bnez_addr3
+#ifdef CONFIG_32BIT
+	RISCV_PTR test_kprobes_c_jal_addr1
+	RISCV_PTR test_kprobes_c_jal_addr2
+#endif
 #endif /* CONFIG_RISCV_ISA_C */
 	RISCV_PTR 0
 SYM_DATA_END(test_kprobes_addresses)

arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S SYM_DATA_START(test_kprobes_functions) · line 255

Quoted 9 lines
arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S +3 -0
@@ -226,6 +247,9 @@ SYM_DATA_START(test_kprobes_functions)
 	RISCV_PTR test_kprobes_c_jalr
 	RISCV_PTR test_kprobes_c_beqz
 	RISCV_PTR test_kprobes_c_bnez
+#ifdef CONFIG_32BIT
+	RISCV_PTR test_kprobes_c_jal
+#endif
 #endif /* CONFIG_RISCV_ISA_C */
 	RISCV_PTR 0
 SYM_DATA_END(test_kprobes_functions)
Signature · 1 line
- Charlie
maintainer
#7 lore raw
In reply to Xiaofeng Yuan
Quoted 6 lines · Xiaofeng Yuan

The c.jal instruction is part of the RISC-V compressed instruction set and is defined only on RV32. Currently kprobes rejects it, causing probes placed on c.jal to fail.

This series adds simulation support for c.jal and includes a test case by Nam Cao.

Thanks, queued for v7.3.

- Paul