checkers-arm.c 6.7 KB

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