riscv: add c.jal instruction simulation for kprobes
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
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
@@ -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
@@ -163,6 +163,13 @@ bool __kprobes simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regsreturn 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
@@ -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
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, reusingsimulate_c_jfor 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.Signed-off-by Xiaofeng Yuan <[email protected]>
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, reusingsimulate_c_jfor 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.Signed-off-by Xiaofeng Yuan <[email protected]>arch/riscv/kernel/probes/decode-insn.c +1
-1@@ -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@@ -163,6 +163,13 @@ bool __kprobes simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regsreturn 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
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
Re: [PATCH v6 1/2] riscv: probes: simulate c.jal instruction
Hi Nam, Charlie,
Quoted 2 lines · Charlie Jenkins
Reviewed-by Charlie Jenkins <[email protected]>Tested-by Charlie Jenkins <[email protected]>
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
PATCH v6 2/2 riscv: kprobes: add test case for c.jal instruction simulation
In reply to Xiaofeng YuanFrom: 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
@@ -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
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_MAGICafter kprobe interception.Co-developed-by Xiaofeng Yuan <[email protected]>Signed-off-by Nam Cao <[email protected]>Signed-off-by Xiaofeng Yuan <[email protected]>arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S +21
-0@@ -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
Paul Walmsley
maintainerQuoted 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