checkers-arm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/arm/probes/kprobes/checkers-arm.c
  4. *
  5. * Copyright (C) 2014 Huawei Inc.
  6. */
  7. #include <linux/kernel.h>
  8. #include "../decode.h"
  9. #include "../decode-arm.h"
  10. #include "checkers.h"
  11. static enum probes_insn __kprobes arm_check_stack(probes_opcode_t insn,
  12. struct arch_probes_insn *asi,
  13. const struct decode_header *h)
  14. {
  15. /*
  16. * PROBES_LDRSTRD, PROBES_LDMSTM, PROBES_STORE,
  17. * PROBES_STORE_EXTRA may get here. Simply mark all normal
  18. * insns as STACK_USE_NONE.
  19. */
  20. static const union decode_item table[] = {
  21. /*
  22. * 'STR{,D,B,H}, Rt, [Rn, Rm]' should be marked as UNKNOWN
  23. * if Rn or Rm is SP.
  24. * x
  25. * STR (register) cccc 011x x0x0 xxxx xxxx xxxx xxxx xxxx
  26. * STRB (register) cccc 011x x1x0 xxxx xxxx xxxx xxxx xxxx
  27. */
  28. DECODE_OR (0x0e10000f, 0x0600000d),
  29. DECODE_OR (0x0e1f0000, 0x060d0000),
  30. /*
  31. * x
  32. * STRD (register) cccc 000x x0x0 xxxx xxxx xxxx 1111 xxxx
  33. * STRH (register) cccc 000x x0x0 xxxx xxxx xxxx 1011 xxxx
  34. */
  35. DECODE_OR (0x0e5000bf, 0x000000bd),
  36. DECODE_CUSTOM (0x0e5f00b0, 0x000d00b0, STACK_USE_UNKNOWN),
  37. /*
  38. * For PROBES_LDMSTM, only stmdx sp, [...] need to examine
  39. *
  40. * Bit B/A (bit 24) encodes arithmetic operation order. 1 means
  41. * before, 0 means after.
  42. * Bit I/D (bit 23) encodes arithmetic operation. 1 means
  43. * increment, 0 means decrement.
  44. *
  45. * So:
  46. * B I
  47. * / /
  48. * A D | Rn |
  49. * STMDX SP, [...] cccc 100x 00x0 xxxx xxxx xxxx xxxx xxxx
  50. */
  51. DECODE_CUSTOM (0x0edf0000, 0x080d0000, STACK_USE_STMDX),
  52. /* P U W | Rn | Rt | imm12 |*/
  53. /* STR (immediate) cccc 010x x0x0 1101 xxxx xxxx xxxx xxxx */
  54. /* STRB (immediate) cccc 010x x1x0 1101 xxxx xxxx xxxx xxxx */
  55. /* P U W | Rn | Rt |imm4| |imm4|*/
  56. /* STRD (immediate) cccc 000x x1x0 1101 xxxx xxxx 1111 xxxx */
  57. /* STRH (immediate) cccc 000x x1x0 1101 xxxx xxxx 1011 xxxx */
  58. /*
  59. * index = (P == '1'); add = (U == '1').
  60. * Above insns with:
  61. * index == 0 (str{,d,h} rx, [sp], #+/-imm) or
  62. * add == 1 (str{,d,h} rx, [sp, #+<imm>])
  63. * should be STACK_USE_NONE.
  64. * Only str{,b,d,h} rx,[sp,#-n] (P == 1 and U == 0) are
  65. * required to be examined.
  66. */
  67. /* STR{,B} Rt,[SP,#-n] cccc 0101 0xx0 1101 xxxx xxxx xxxx xxxx */
  68. DECODE_CUSTOM (0x0f9f0000, 0x050d0000, STACK_USE_FIXED_XXX),
  69. /* STR{D,H} Rt,[SP,#-n] cccc 0001 01x0 1101 xxxx xxxx 1x11 xxxx */
  70. DECODE_CUSTOM (0x0fdf00b0, 0x014d00b0, STACK_USE_FIXED_X0X),
  71. /* fall through */
  72. DECODE_CUSTOM (0, 0, STACK_USE_NONE),
  73. DECODE_END
  74. };
  75. return probes_decode_insn(insn, asi, table, false, false, stack_check_actions, NULL);
  76. }
  77. const struct decode_checker arm_stack_checker[NUM_PROBES_ARM_ACTIONS] = {
  78. [PROBES_LDRSTRD] = {.checker = arm_check_stack},
  79. [PROBES_STORE_EXTRA] = {.checker = arm_check_stack},
  80. [PROBES_STORE] = {.checker = arm_check_stack},
  81. [PROBES_LDMSTM] = {.checker = arm_check_stack},
  82. };
  83. static enum probes_insn __kprobes arm_check_regs_nouse(probes_opcode_t insn,
  84. struct arch_probes_insn *asi,
  85. const struct decode_header *h)
  86. {
  87. asi->register_usage_flags = 0;
  88. return INSN_GOOD;
  89. }
  90. static enum probes_insn arm_check_regs_normal(probes_opcode_t insn,
  91. struct arch_probes_insn *asi,
  92. const struct decode_header *h)
  93. {
  94. u32 regs = h->type_regs.bits >> DECODE_TYPE_BITS;
  95. int i;
  96. asi->register_usage_flags = 0;
  97. for (i = 0; i < 5; regs >>= 4, insn >>= 4, i++)
  98. if (regs & 0xf)
  99. asi->register_usage_flags |= 1 << (insn & 0xf);
  100. return INSN_GOOD;
  101. }
  102. static enum probes_insn arm_check_regs_ldmstm(probes_opcode_t insn,
  103. struct arch_probes_insn *asi,
  104. const struct decode_header *h)
  105. {
  106. unsigned int reglist = insn & 0xffff;
  107. unsigned int rn = (insn >> 16) & 0xf;
  108. asi->register_usage_flags = reglist | (1 << rn);
  109. return INSN_GOOD;
  110. }
  111. static enum probes_insn arm_check_regs_mov_ip_sp(probes_opcode_t insn,
  112. struct arch_probes_insn *asi,
  113. const struct decode_header *h)
  114. {
  115. /* Instruction is 'mov ip, sp' i.e. 'mov r12, r13' */
  116. asi->register_usage_flags = (1 << 12) | (1<< 13);
  117. return INSN_GOOD;
  118. }
  119. /*
  120. * | Rn |Rt/d| | Rm |
  121. * LDRD (register) cccc 000x x0x0 xxxx xxxx xxxx 1101 xxxx
  122. * STRD (register) cccc 000x x0x0 xxxx xxxx xxxx 1111 xxxx
  123. * | Rn |Rt/d| |imm4L|
  124. * LDRD (immediate) cccc 000x x1x0 xxxx xxxx xxxx 1101 xxxx
  125. * STRD (immediate) cccc 000x x1x0 xxxx xxxx xxxx 1111 xxxx
  126. *
  127. * Such instructions access Rt/d and its next register, so different
  128. * from others, a specific checker is required to handle this extra
  129. * implicit register usage.
  130. */
  131. static enum probes_insn arm_check_regs_ldrdstrd(probes_opcode_t insn,
  132. struct arch_probes_insn *asi,
  133. const struct decode_header *h)
  134. {
  135. int rdt = (insn >> 12) & 0xf;
  136. arm_check_regs_normal(insn, asi, h);
  137. asi->register_usage_flags |= 1 << (rdt + 1);
  138. return INSN_GOOD;
  139. }
  140. const struct decode_checker arm_regs_checker[NUM_PROBES_ARM_ACTIONS] = {
  141. [PROBES_MRS] = {.checker = arm_check_regs_normal},
  142. [PROBES_SATURATING_ARITHMETIC] = {.checker = arm_check_regs_normal},
  143. [PROBES_MUL1] = {.checker = arm_check_regs_normal},
  144. [PROBES_MUL2] = {.checker = arm_check_regs_normal},
  145. [PROBES_MUL_ADD_LONG] = {.checker = arm_check_regs_normal},
  146. [PROBES_MUL_ADD] = {.checker = arm_check_regs_normal},
  147. [PROBES_LOAD] = {.checker = arm_check_regs_normal},
  148. [PROBES_LOAD_EXTRA] = {.checker = arm_check_regs_normal},
  149. [PROBES_STORE] = {.checker = arm_check_regs_normal},
  150. [PROBES_STORE_EXTRA] = {.checker = arm_check_regs_normal},
  151. [PROBES_DATA_PROCESSING_REG] = {.checker = arm_check_regs_normal},
  152. [PROBES_DATA_PROCESSING_IMM] = {.checker = arm_check_regs_normal},
  153. [PROBES_SEV] = {.checker = arm_check_regs_nouse},
  154. [PROBES_WFE] = {.checker = arm_check_regs_nouse},
  155. [PROBES_SATURATE] = {.checker = arm_check_regs_normal},
  156. [PROBES_REV] = {.checker = arm_check_regs_normal},
  157. [PROBES_MMI] = {.checker = arm_check_regs_normal},
  158. [PROBES_PACK] = {.checker = arm_check_regs_normal},
  159. [PROBES_EXTEND] = {.checker = arm_check_regs_normal},
  160. [PROBES_EXTEND_ADD] = {.checker = arm_check_regs_normal},
  161. [PROBES_BITFIELD] = {.checker = arm_check_regs_normal},
  162. [PROBES_LDMSTM] = {.checker = arm_check_regs_ldmstm},
  163. [PROBES_MOV_IP_SP] = {.checker = arm_check_regs_mov_ip_sp},
  164. [PROBES_LDRSTRD] = {.checker = arm_check_regs_ldrdstrd},
  165. };