1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/bitops.h>
4 #include <linux/kernel.h>
5 #include <linux/kprobes.h>
7 #include "decode-insn.h"
8 #include "simulate-insn.h"
10 static inline bool rv_insn_reg_get_val(struct pt_regs *regs, u32 index,
16 *ptr = *((unsigned long *)regs + index);
23 static inline bool rv_insn_reg_set_val(struct pt_regs *regs, u32 index,
29 *((unsigned long *)regs + index) = val;
36 bool __kprobes simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs)
39 * 31 30 21 20 19 12 11 7 6 0
40 * imm [20] | imm[10:1] | imm[11] | imm[19:12] | rd | opcode
45 u32 index = (opcode >> 7) & 0x1f;
47 ret = rv_insn_reg_set_val(regs, index, addr + 4);
51 imm = ((opcode >> 21) & 0x3ff) << 1;
52 imm |= ((opcode >> 20) & 0x1) << 11;
53 imm |= ((opcode >> 12) & 0xff) << 12;
54 imm |= ((opcode >> 31) & 0x1) << 20;
56 instruction_pointer_set(regs, addr + sign_extend32((imm), 20));
61 bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs)
64 * 31 20 19 15 14 12 11 7 6 0
65 * offset[11:0] | rs1 | 010 | rd | opcode
69 unsigned long base_addr;
70 u32 imm = (opcode >> 20) & 0xfff;
71 u32 rd_index = (opcode >> 7) & 0x1f;
72 u32 rs1_index = (opcode >> 15) & 0x1f;
74 ret = rv_insn_reg_set_val(regs, rd_index, addr + 4);
78 ret = rv_insn_reg_get_val(regs, rs1_index, &base_addr);
82 instruction_pointer_set(regs, (base_addr + sign_extend32((imm), 11))&~1);
87 #define auipc_rd_idx(opcode) \
88 ((opcode >> 7) & 0x1f)
90 #define auipc_imm(opcode) \
91 ((((opcode) >> 12) & 0xfffff) << 12)
93 #if __riscv_xlen == 64
94 #define auipc_offset(opcode) sign_extend64(auipc_imm(opcode), 31)
95 #elif __riscv_xlen == 32
96 #define auipc_offset(opcode) auipc_imm(opcode)
98 #error "Unexpected __riscv_xlen"
101 bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *regs)
106 * | imm[31:12] | rd | opcode |
110 u32 rd_idx = auipc_rd_idx(opcode);
111 unsigned long rd_val = addr + auipc_offset(opcode);
113 if (!rv_insn_reg_set_val(regs, rd_idx, rd_val))
116 instruction_pointer_set(regs, addr + 4);