simulate-insn.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <asm/ptrace.h>
  19. #include "simulate-insn.h"
  20. #define bbl_displacement(insn) \
  21. sign_extend32(((insn) & 0x3ffffff) << 2, 27)
  22. #define bcond_displacement(insn) \
  23. sign_extend32(((insn >> 5) & 0x7ffff) << 2, 20)
  24. #define cbz_displacement(insn) \
  25. sign_extend32(((insn >> 5) & 0x7ffff) << 2, 20)
  26. #define tbz_displacement(insn) \
  27. sign_extend32(((insn >> 5) & 0x3fff) << 2, 15)
  28. #define ldr_displacement(insn) \
  29. sign_extend32(((insn >> 5) & 0x7ffff) << 2, 20)
  30. static inline void set_x_reg(struct pt_regs *regs, int reg, u64 val)
  31. {
  32. pt_regs_write_reg(regs, reg, val);
  33. }
  34. static inline void set_w_reg(struct pt_regs *regs, int reg, u64 val)
  35. {
  36. pt_regs_write_reg(regs, reg, lower_32_bits(val));
  37. }
  38. static inline u64 get_x_reg(struct pt_regs *regs, int reg)
  39. {
  40. return pt_regs_read_reg(regs, reg);
  41. }
  42. static inline u32 get_w_reg(struct pt_regs *regs, int reg)
  43. {
  44. return lower_32_bits(pt_regs_read_reg(regs, reg));
  45. }
  46. static bool __kprobes check_cbz(u32 opcode, struct pt_regs *regs)
  47. {
  48. int xn = opcode & 0x1f;
  49. return (opcode & (1 << 31)) ?
  50. (get_x_reg(regs, xn) == 0) : (get_w_reg(regs, xn) == 0);
  51. }
  52. static bool __kprobes check_cbnz(u32 opcode, struct pt_regs *regs)
  53. {
  54. int xn = opcode & 0x1f;
  55. return (opcode & (1 << 31)) ?
  56. (get_x_reg(regs, xn) != 0) : (get_w_reg(regs, xn) != 0);
  57. }
  58. static bool __kprobes check_tbz(u32 opcode, struct pt_regs *regs)
  59. {
  60. int xn = opcode & 0x1f;
  61. int bit_pos = ((opcode & (1 << 31)) >> 26) | ((opcode >> 19) & 0x1f);
  62. return ((get_x_reg(regs, xn) >> bit_pos) & 0x1) == 0;
  63. }
  64. static bool __kprobes check_tbnz(u32 opcode, struct pt_regs *regs)
  65. {
  66. int xn = opcode & 0x1f;
  67. int bit_pos = ((opcode & (1 << 31)) >> 26) | ((opcode >> 19) & 0x1f);
  68. return ((get_x_reg(regs, xn) >> bit_pos) & 0x1) != 0;
  69. }
  70. /*
  71. * instruction simulation functions
  72. */
  73. void __kprobes
  74. simulate_adr_adrp(u32 opcode, long addr, struct pt_regs *regs)
  75. {
  76. long imm, xn, val;
  77. xn = opcode & 0x1f;
  78. imm = ((opcode >> 3) & 0x1ffffc) | ((opcode >> 29) & 0x3);
  79. imm = sign_extend64(imm, 20);
  80. if (opcode & 0x80000000)
  81. val = (imm<<12) + (addr & 0xfffffffffffff000);
  82. else
  83. val = imm + addr;
  84. set_x_reg(regs, xn, val);
  85. instruction_pointer_set(regs, instruction_pointer(regs) + 4);
  86. }
  87. void __kprobes
  88. simulate_b_bl(u32 opcode, long addr, struct pt_regs *regs)
  89. {
  90. int disp = bbl_displacement(opcode);
  91. /* Link register is x30 */
  92. if (opcode & (1 << 31))
  93. set_x_reg(regs, 30, addr + 4);
  94. instruction_pointer_set(regs, addr + disp);
  95. }
  96. void __kprobes
  97. simulate_b_cond(u32 opcode, long addr, struct pt_regs *regs)
  98. {
  99. int disp = 4;
  100. if (aarch32_opcode_cond_checks[opcode & 0xf](regs->pstate & 0xffffffff))
  101. disp = bcond_displacement(opcode);
  102. instruction_pointer_set(regs, addr + disp);
  103. }
  104. void __kprobes
  105. simulate_br_blr_ret(u32 opcode, long addr, struct pt_regs *regs)
  106. {
  107. int xn = (opcode >> 5) & 0x1f;
  108. /* update pc first in case we're doing a "blr lr" */
  109. instruction_pointer_set(regs, get_x_reg(regs, xn));
  110. /* Link register is x30 */
  111. if (((opcode >> 21) & 0x3) == 1)
  112. set_x_reg(regs, 30, addr + 4);
  113. }
  114. void __kprobes
  115. simulate_cbz_cbnz(u32 opcode, long addr, struct pt_regs *regs)
  116. {
  117. int disp = 4;
  118. if (opcode & (1 << 24)) {
  119. if (check_cbnz(opcode, regs))
  120. disp = cbz_displacement(opcode);
  121. } else {
  122. if (check_cbz(opcode, regs))
  123. disp = cbz_displacement(opcode);
  124. }
  125. instruction_pointer_set(regs, addr + disp);
  126. }
  127. void __kprobes
  128. simulate_tbz_tbnz(u32 opcode, long addr, struct pt_regs *regs)
  129. {
  130. int disp = 4;
  131. if (opcode & (1 << 24)) {
  132. if (check_tbnz(opcode, regs))
  133. disp = tbz_displacement(opcode);
  134. } else {
  135. if (check_tbz(opcode, regs))
  136. disp = tbz_displacement(opcode);
  137. }
  138. instruction_pointer_set(regs, addr + disp);
  139. }
  140. void __kprobes
  141. simulate_ldr_literal(u32 opcode, long addr, struct pt_regs *regs)
  142. {
  143. u64 *load_addr;
  144. int xn = opcode & 0x1f;
  145. int disp;
  146. disp = ldr_displacement(opcode);
  147. load_addr = (u64 *) (addr + disp);
  148. if (opcode & (1 << 30)) /* x0-x30 */
  149. set_x_reg(regs, xn, *load_addr);
  150. else /* w0-w30 */
  151. set_w_reg(regs, xn, *load_addr);
  152. instruction_pointer_set(regs, instruction_pointer(regs) + 4);
  153. }
  154. void __kprobes
  155. simulate_ldrsw_literal(u32 opcode, long addr, struct pt_regs *regs)
  156. {
  157. s32 *load_addr;
  158. int xn = opcode & 0x1f;
  159. int disp;
  160. disp = ldr_displacement(opcode);
  161. load_addr = (s32 *) (addr + disp);
  162. set_x_reg(regs, xn, *load_addr);
  163. instruction_pointer_set(regs, instruction_pointer(regs) + 4);
  164. }