simulate-insn.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * arch/arm64/kernel/probes/simulate-insn.c
  3. *
  4. * Copyright (C) 2013 Linaro Limited.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kprobes.h>
  18. #include "simulate-insn.h"
  19. #define bbl_displacement(insn) \
  20. sign_extend32(((insn) & 0x3ffffff) << 2, 27)
  21. #define bcond_displacement(insn) \
  22. sign_extend32(((insn >> 5) & 0x7ffff) << 2, 20)
  23. #define cbz_displacement(insn) \
  24. sign_extend32(((insn >> 5) & 0x7ffff) << 2, 20)
  25. #define tbz_displacement(insn) \
  26. sign_extend32(((insn >> 5) & 0x3fff) << 2, 15)
  27. #define ldr_displacement(insn) \
  28. sign_extend32(((insn >> 5) & 0x7ffff) << 2, 20)
  29. static inline void set_x_reg(struct pt_regs *regs, int reg, u64 val)
  30. {
  31. if (reg < 31)
  32. regs->regs[reg] = val;
  33. }
  34. static inline void set_w_reg(struct pt_regs *regs, int reg, u64 val)
  35. {
  36. if (reg < 31)
  37. regs->regs[reg] = lower_32_bits(val);
  38. }
  39. static inline u64 get_x_reg(struct pt_regs *regs, int reg)
  40. {
  41. if (reg < 31)
  42. return regs->regs[reg];
  43. else
  44. return 0;
  45. }
  46. static inline u32 get_w_reg(struct pt_regs *regs, int reg)
  47. {
  48. if (reg < 31)
  49. return lower_32_bits(regs->regs[reg]);
  50. else
  51. return 0;
  52. }
  53. static bool __kprobes check_cbz(u32 opcode, struct pt_regs *regs)
  54. {
  55. int xn = opcode & 0x1f;
  56. return (opcode & (1 << 31)) ?
  57. (get_x_reg(regs, xn) == 0) : (get_w_reg(regs, xn) == 0);
  58. }
  59. static bool __kprobes check_cbnz(u32 opcode, struct pt_regs *regs)
  60. {
  61. int xn = opcode & 0x1f;
  62. return (opcode & (1 << 31)) ?
  63. (get_x_reg(regs, xn) != 0) : (get_w_reg(regs, xn) != 0);
  64. }
  65. static bool __kprobes check_tbz(u32 opcode, struct pt_regs *regs)
  66. {
  67. int xn = opcode & 0x1f;
  68. int bit_pos = ((opcode & (1 << 31)) >> 26) | ((opcode >> 19) & 0x1f);
  69. return ((get_x_reg(regs, xn) >> bit_pos) & 0x1) == 0;
  70. }
  71. static bool __kprobes check_tbnz(u32 opcode, struct pt_regs *regs)
  72. {
  73. int xn = opcode & 0x1f;
  74. int bit_pos = ((opcode & (1 << 31)) >> 26) | ((opcode >> 19) & 0x1f);
  75. return ((get_x_reg(regs, xn) >> bit_pos) & 0x1) != 0;
  76. }
  77. /*
  78. * instruction simulation functions
  79. */
  80. void __kprobes
  81. simulate_adr_adrp(u32 opcode, long addr, struct pt_regs *regs)
  82. {
  83. long imm, xn, val;
  84. xn = opcode & 0x1f;
  85. imm = ((opcode >> 3) & 0x1ffffc) | ((opcode >> 29) & 0x3);
  86. imm = sign_extend64(imm, 20);
  87. if (opcode & 0x80000000)
  88. val = (imm<<12) + (addr & 0xfffffffffffff000);
  89. else
  90. val = imm + addr;
  91. set_x_reg(regs, xn, val);
  92. instruction_pointer_set(regs, instruction_pointer(regs) + 4);
  93. }
  94. void __kprobes
  95. simulate_b_bl(u32 opcode, long addr, struct pt_regs *regs)
  96. {
  97. int disp = bbl_displacement(opcode);
  98. /* Link register is x30 */
  99. if (opcode & (1 << 31))
  100. set_x_reg(regs, 30, addr + 4);
  101. instruction_pointer_set(regs, addr + disp);
  102. }
  103. void __kprobes
  104. simulate_b_cond(u32 opcode, long addr, struct pt_regs *regs)
  105. {
  106. int disp = 4;
  107. if (aarch32_opcode_cond_checks[opcode & 0xf](regs->pstate & 0xffffffff))
  108. disp = bcond_displacement(opcode);
  109. instruction_pointer_set(regs, addr + disp);
  110. }
  111. void __kprobes
  112. simulate_br_blr_ret(u32 opcode, long addr, struct pt_regs *regs)
  113. {
  114. int xn = (opcode >> 5) & 0x1f;
  115. /* update pc first in case we're doing a "blr lr" */
  116. instruction_pointer_set(regs, get_x_reg(regs, xn));
  117. /* Link register is x30 */
  118. if (((opcode >> 21) & 0x3) == 1)
  119. set_x_reg(regs, 30, addr + 4);
  120. }
  121. void __kprobes
  122. simulate_cbz_cbnz(u32 opcode, long addr, struct pt_regs *regs)
  123. {
  124. int disp = 4;
  125. if (opcode & (1 << 24)) {
  126. if (check_cbnz(opcode, regs))
  127. disp = cbz_displacement(opcode);
  128. } else {
  129. if (check_cbz(opcode, regs))
  130. disp = cbz_displacement(opcode);
  131. }
  132. instruction_pointer_set(regs, addr + disp);
  133. }
  134. void __kprobes
  135. simulate_tbz_tbnz(u32 opcode, long addr, struct pt_regs *regs)
  136. {
  137. int disp = 4;
  138. if (opcode & (1 << 24)) {
  139. if (check_tbnz(opcode, regs))
  140. disp = tbz_displacement(opcode);
  141. } else {
  142. if (check_tbz(opcode, regs))
  143. disp = tbz_displacement(opcode);
  144. }
  145. instruction_pointer_set(regs, addr + disp);
  146. }
  147. void __kprobes
  148. simulate_ldr_literal(u32 opcode, long addr, struct pt_regs *regs)
  149. {
  150. u64 *load_addr;
  151. int xn = opcode & 0x1f;
  152. int disp;
  153. disp = ldr_displacement(opcode);
  154. load_addr = (u64 *) (addr + disp);
  155. if (opcode & (1 << 30)) /* x0-x30 */
  156. set_x_reg(regs, xn, *load_addr);
  157. else /* w0-w30 */
  158. set_w_reg(regs, xn, *load_addr);
  159. instruction_pointer_set(regs, instruction_pointer(regs) + 4);
  160. }
  161. void __kprobes
  162. simulate_ldrsw_literal(u32 opcode, long addr, struct pt_regs *regs)
  163. {
  164. s32 *load_addr;
  165. int xn = opcode & 0x1f;
  166. int disp;
  167. disp = ldr_displacement(opcode);
  168. load_addr = (s32 *) (addr + disp);
  169. set_x_reg(regs, xn, *load_addr);
  170. instruction_pointer_set(regs, instruction_pointer(regs) + 4);
  171. }