From: Charlie Jenkins <charlie@rivosinc.com>
To: Nam Cao <namcaov@gmail.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 3/3] riscv: kprobes: simulate c.beqz and c.bnez
Date: Tue, 8 Aug 2023 18:14:39 -0700 [thread overview]
Message-ID: <ZNLof08CrlQ8eNAg@ghost> (raw)
In-Reply-To: <1d879dba4e4ee9a82e27625d6483b5c9cfed684f.1690704360.git.namcaov@gmail.com>
On Sun, Jul 30, 2023 at 10:27:09AM +0200, Nam Cao wrote:
> kprobes currently rejects instruction c.beqz and c.bnez. Implement them.
>
> Signed-off-by: Nam Cao <namcaov@gmail.com>
> ---
> 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(-)
>
> diff --git a/arch/riscv/kernel/probes/decode-insn.c b/arch/riscv/kernel/probes/decode-insn.c
> index 6dba23a55ac7..65d9590bfb9f 100644
> --- a/arch/riscv/kernel/probes/decode-insn.c
> +++ b/arch/riscv/kernel/probes/decode-insn.c
> @@ -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);
> diff --git a/arch/riscv/kernel/probes/simulate-insn.c b/arch/riscv/kernel/probes/simulate-insn.c
> index 1ead6f4951f9..d3099d67816d 100644
> --- a/arch/riscv/kernel/probes/simulate-insn.c
> +++ b/arch/riscv/kernel/probes/simulate-insn.c
> @@ -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);
> +}
> diff --git a/arch/riscv/kernel/probes/simulate-insn.h b/arch/riscv/kernel/probes/simulate-insn.h
> index 472a1948ec4f..44ebbc444db9 100644
> --- a/arch/riscv/kernel/probes/simulate-insn.h
> +++ b/arch/riscv/kernel/probes/simulate-insn.h
> @@ -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 */
> --
> 2.34.1
>
>
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv
You can add:
Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
next prev parent reply other threads:[~2023-08-09 1:14 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-30 8:27 [PATCH 0/3] riscv: kprobes: simulate some instructions Nam Cao
2023-07-30 8:27 ` [PATCH 1/3] riscv: kprobes: simulate c.j instruction Nam Cao
2023-08-09 0:11 ` Charlie Jenkins
2023-08-09 1:05 ` Charlie Jenkins
2023-07-30 8:27 ` [PATCH 2/3] riscv: kprobes: simulate c.jr and c.jalr instructions Nam Cao
2023-08-09 1:06 ` Charlie Jenkins
2023-07-30 8:27 ` [PATCH 3/3] riscv: kprobes: simulate c.beqz and c.bnez Nam Cao
2023-08-09 1:14 ` Charlie Jenkins [this message]
2023-07-30 8:27 ` test code for kprobe Nam Cao
2023-08-14 12:28 ` Björn Töpel
2023-08-14 13:50 ` Nam Cao
2023-08-14 14:10 ` Björn Töpel
2023-08-30 13:20 ` [PATCH 0/3] riscv: kprobes: simulate some instructions patchwork-bot+linux-riscv
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZNLof08CrlQ8eNAg@ghost \
--to=charlie@rivosinc.com \
--cc=aou@eecs.berkeley.edu \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=namcaov@gmail.com \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox