simulate-insn.c 4.5 KB

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