simulate-insn.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/bitops.h>
  3. #include <linux/kernel.h>
  4. #include <linux/kprobes.h>
  5. #include "decode-insn.h"
  6. #include "simulate-insn.h"
  7. static inline bool rv_insn_reg_get_val(struct pt_regs *regs, u32 index,
  8. unsigned long *ptr)
  9. {
  10. if (index == 0)
  11. *ptr = 0;
  12. else if (index <= 31)
  13. *ptr = *((unsigned long *)regs + index);
  14. else
  15. return false;
  16. return true;
  17. }
  18. static inline bool rv_insn_reg_set_val(struct pt_regs *regs, u32 index,
  19. unsigned long val)
  20. {
  21. if (index == 0)
  22. return true;
  23. else if (index <= 31)
  24. *((unsigned long *)regs + index) = val;
  25. else
  26. return false;
  27. return true;
  28. }
  29. bool __kprobes simulate_jal(u32 opcode, unsigned long addr, struct pt_regs *regs)
  30. {
  31. /*
  32. * 31 30 21 20 19 12 11 7 6 0
  33. * imm [20] | imm[10:1] | imm[11] | imm[19:12] | rd | opcode
  34. * 1 10 1 8 5 JAL/J
  35. */
  36. bool ret;
  37. u32 imm;
  38. u32 index = (opcode >> 7) & 0x1f;
  39. ret = rv_insn_reg_set_val(regs, index, addr + 4);
  40. if (!ret)
  41. return ret;
  42. imm = ((opcode >> 21) & 0x3ff) << 1;
  43. imm |= ((opcode >> 20) & 0x1) << 11;
  44. imm |= ((opcode >> 12) & 0xff) << 12;
  45. imm |= ((opcode >> 31) & 0x1) << 20;
  46. instruction_pointer_set(regs, addr + sign_extend32((imm), 20));
  47. return ret;
  48. }
  49. bool __kprobes simulate_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs)
  50. {
  51. /*
  52. * 31 20 19 15 14 12 11 7 6 0
  53. * offset[11:0] | rs1 | 010 | rd | opcode
  54. * 12 5 3 5 JALR/JR
  55. */
  56. bool ret;
  57. unsigned long base_addr;
  58. u32 imm = (opcode >> 20) & 0xfff;
  59. u32 rd_index = (opcode >> 7) & 0x1f;
  60. u32 rs1_index = (opcode >> 15) & 0x1f;
  61. ret = rv_insn_reg_get_val(regs, rs1_index, &base_addr);
  62. if (!ret)
  63. return ret;
  64. ret = rv_insn_reg_set_val(regs, rd_index, addr + 4);
  65. if (!ret)
  66. return ret;
  67. instruction_pointer_set(regs, (base_addr + sign_extend32((imm), 11))&~1);
  68. return ret;
  69. }
  70. #define auipc_rd_idx(opcode) \
  71. ((opcode >> 7) & 0x1f)
  72. #define auipc_imm(opcode) \
  73. ((((opcode) >> 12) & 0xfffff) << 12)
  74. #if __riscv_xlen == 64
  75. #define auipc_offset(opcode) sign_extend64(auipc_imm(opcode), 31)
  76. #elif __riscv_xlen == 32
  77. #define auipc_offset(opcode) auipc_imm(opcode)
  78. #else
  79. #error "Unexpected __riscv_xlen"
  80. #endif
  81. bool __kprobes simulate_auipc(u32 opcode, unsigned long addr, struct pt_regs *regs)
  82. {
  83. /*
  84. * auipc instruction:
  85. * 31 12 11 7 6 0
  86. * | imm[31:12] | rd | opcode |
  87. * 20 5 7
  88. */
  89. u32 rd_idx = auipc_rd_idx(opcode);
  90. unsigned long rd_val = addr + auipc_offset(opcode);
  91. if (!rv_insn_reg_set_val(regs, rd_idx, rd_val))
  92. return false;
  93. instruction_pointer_set(regs, addr + 4);
  94. return true;
  95. }
  96. #define branch_rs1_idx(opcode) \
  97. (((opcode) >> 15) & 0x1f)
  98. #define branch_rs2_idx(opcode) \
  99. (((opcode) >> 20) & 0x1f)
  100. #define branch_funct3(opcode) \
  101. (((opcode) >> 12) & 0x7)
  102. #define branch_imm(opcode) \
  103. (((((opcode) >> 8) & 0xf ) << 1) | \
  104. ((((opcode) >> 25) & 0x3f) << 5) | \
  105. ((((opcode) >> 7) & 0x1 ) << 11) | \
  106. ((((opcode) >> 31) & 0x1 ) << 12))
  107. #define branch_offset(opcode) \
  108. sign_extend32((branch_imm(opcode)), 12)
  109. bool __kprobes simulate_branch(u32 opcode, unsigned long addr, struct pt_regs *regs)
  110. {
  111. /*
  112. * branch instructions:
  113. * 31 30 25 24 20 19 15 14 12 11 8 7 6 0
  114. * | imm[12] | imm[10:5] | rs2 | rs1 | funct3 | imm[4:1] | imm[11] | opcode |
  115. * 1 6 5 5 3 4 1 7
  116. * imm[12|10:5] rs2 rs1 000 imm[4:1|11] 1100011 BEQ
  117. * imm[12|10:5] rs2 rs1 001 imm[4:1|11] 1100011 BNE
  118. * imm[12|10:5] rs2 rs1 100 imm[4:1|11] 1100011 BLT
  119. * imm[12|10:5] rs2 rs1 101 imm[4:1|11] 1100011 BGE
  120. * imm[12|10:5] rs2 rs1 110 imm[4:1|11] 1100011 BLTU
  121. * imm[12|10:5] rs2 rs1 111 imm[4:1|11] 1100011 BGEU
  122. */
  123. s32 offset;
  124. s32 offset_tmp;
  125. unsigned long rs1_val;
  126. unsigned long rs2_val;
  127. if (!rv_insn_reg_get_val(regs, branch_rs1_idx(opcode), &rs1_val) ||
  128. !rv_insn_reg_get_val(regs, branch_rs2_idx(opcode), &rs2_val))
  129. return false;
  130. offset_tmp = branch_offset(opcode);
  131. switch (branch_funct3(opcode)) {
  132. case RVG_FUNCT3_BEQ:
  133. offset = (rs1_val == rs2_val) ? offset_tmp : 4;
  134. break;
  135. case RVG_FUNCT3_BNE:
  136. offset = (rs1_val != rs2_val) ? offset_tmp : 4;
  137. break;
  138. case RVG_FUNCT3_BLT:
  139. offset = ((long)rs1_val < (long)rs2_val) ? offset_tmp : 4;
  140. break;
  141. case RVG_FUNCT3_BGE:
  142. offset = ((long)rs1_val >= (long)rs2_val) ? offset_tmp : 4;
  143. break;
  144. case RVG_FUNCT3_BLTU:
  145. offset = (rs1_val < rs2_val) ? offset_tmp : 4;
  146. break;
  147. case RVG_FUNCT3_BGEU:
  148. offset = (rs1_val >= rs2_val) ? offset_tmp : 4;
  149. break;
  150. default:
  151. return false;
  152. }
  153. instruction_pointer_set(regs, addr + offset);
  154. return true;
  155. }
  156. bool __kprobes simulate_c_j(u32 opcode, unsigned long addr, struct pt_regs *regs)
  157. {
  158. /*
  159. * 15 13 12 2 1 0
  160. * | funct3 | offset[11|4|9:8|10|6|7|3:1|5] | opcode |
  161. * 3 11 2
  162. */
  163. s32 offset;
  164. offset = ((opcode >> 3) & 0x7) << 1;
  165. offset |= ((opcode >> 11) & 0x1) << 4;
  166. offset |= ((opcode >> 2) & 0x1) << 5;
  167. offset |= ((opcode >> 7) & 0x1) << 6;
  168. offset |= ((opcode >> 6) & 0x1) << 7;
  169. offset |= ((opcode >> 9) & 0x3) << 8;
  170. offset |= ((opcode >> 8) & 0x1) << 10;
  171. offset |= ((opcode >> 12) & 0x1) << 11;
  172. instruction_pointer_set(regs, addr + sign_extend32(offset, 11));
  173. return true;
  174. }
  175. static bool __kprobes simulate_c_jr_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs,
  176. bool is_jalr)
  177. {
  178. /*
  179. * 15 12 11 7 6 2 1 0
  180. * | funct4 | rs1 | rs2 | op |
  181. * 4 5 5 2
  182. */
  183. unsigned long jump_addr;
  184. u32 rs1 = (opcode >> 7) & 0x1f;
  185. if (rs1 == 0) /* C.JR is only valid when rs1 != x0 */
  186. return false;
  187. if (!rv_insn_reg_get_val(regs, rs1, &jump_addr))
  188. return false;
  189. if (is_jalr && !rv_insn_reg_set_val(regs, 1, addr + 2))
  190. return false;
  191. instruction_pointer_set(regs, jump_addr);
  192. return true;
  193. }
  194. bool __kprobes simulate_c_jr(u32 opcode, unsigned long addr, struct pt_regs *regs)
  195. {
  196. return simulate_c_jr_jalr(opcode, addr, regs, false);
  197. }
  198. bool __kprobes simulate_c_jalr(u32 opcode, unsigned long addr, struct pt_regs *regs)
  199. {
  200. return simulate_c_jr_jalr(opcode, addr, regs, true);
  201. }
  202. static bool __kprobes simulate_c_bnez_beqz(u32 opcode, unsigned long addr, struct pt_regs *regs,
  203. bool is_bnez)
  204. {
  205. /*
  206. * 15 13 12 10 9 7 6 2 1 0
  207. * | funct3 | offset[8|4:3] | rs1' | offset[7:6|2:1|5] | op |
  208. * 3 3 3 5 2
  209. */
  210. s32 offset;
  211. u32 rs1;
  212. unsigned long rs1_val;
  213. rs1 = 0x8 | ((opcode >> 7) & 0x7);
  214. if (!rv_insn_reg_get_val(regs, rs1, &rs1_val))
  215. return false;
  216. if ((rs1_val != 0 && is_bnez) || (rs1_val == 0 && !is_bnez)) {
  217. offset = ((opcode >> 3) & 0x3) << 1;
  218. offset |= ((opcode >> 10) & 0x3) << 3;
  219. offset |= ((opcode >> 2) & 0x1) << 5;
  220. offset |= ((opcode >> 5) & 0x3) << 6;
  221. offset |= ((opcode >> 12) & 0x1) << 8;
  222. offset = sign_extend32(offset, 8);
  223. } else {
  224. offset = 2;
  225. }
  226. instruction_pointer_set(regs, addr + offset);
  227. return true;
  228. }
  229. bool __kprobes simulate_c_bnez(u32 opcode, unsigned long addr, struct pt_regs *regs)
  230. {
  231. return simulate_c_bnez_beqz(opcode, addr, regs, true);
  232. }
  233. bool __kprobes simulate_c_beqz(u32 opcode, unsigned long addr, struct pt_regs *regs)
  234. {
  235. return simulate_c_bnez_beqz(opcode, addr, regs, false);
  236. }