checkers-thumb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/arm/probes/kprobes/checkers-thumb.c
  4. *
  5. * Copyright (C) 2014 Huawei Inc.
  6. */
  7. #include <linux/kernel.h>
  8. #include "../decode.h"
  9. #include "../decode-thumb.h"
  10. #include "checkers.h"
  11. static enum probes_insn __kprobes t32_check_stack(probes_opcode_t insn,
  12. struct arch_probes_insn *asi,
  13. const struct decode_header *h)
  14. {
  15. /*
  16. * PROBES_T32_LDMSTM, PROBES_T32_LDRDSTRD and PROBES_T32_LDRSTR
  17. * may get here. Simply mark all normal insns as STACK_USE_NONE.
  18. */
  19. static const union decode_item table[] = {
  20. /*
  21. * First, filter out all ldr insns to make our life easier.
  22. * Following load insns may come here:
  23. * LDM, LDRD, LDR.
  24. * In T32 encoding, bit 20 is enough for distinguishing
  25. * load and store. All load insns have this bit set, when
  26. * all store insns have this bit clear.
  27. */
  28. DECODE_CUSTOM (0x00100000, 0x00100000, STACK_USE_NONE),
  29. /*
  30. * Mark all 'STR{,B,H}, Rt, [Rn, Rm]' as STACK_USE_UNKNOWN
  31. * if Rn or Rm is SP. T32 doesn't encode STRD.
  32. */
  33. /* xx | Rn | Rt | | Rm |*/
  34. /* STR (register) 1111 1000 0100 xxxx xxxx 0000 00xx xxxx */
  35. /* STRB (register) 1111 1000 0000 xxxx xxxx 0000 00xx xxxx */
  36. /* STRH (register) 1111 1000 0010 xxxx xxxx 0000 00xx xxxx */
  37. /* INVALID INSN 1111 1000 0110 xxxx xxxx 0000 00xx xxxx */
  38. /* By Introducing INVALID INSN, bit 21 and 22 can be ignored. */
  39. DECODE_OR (0xff9f0fc0, 0xf80d0000),
  40. DECODE_CUSTOM (0xff900fcf, 0xf800000d, STACK_USE_UNKNOWN),
  41. /* xx | Rn | Rt | PUW| imm8 |*/
  42. /* STR (imm 8) 1111 1000 0100 1101 xxxx 110x xxxx xxxx */
  43. /* STRB (imm 8) 1111 1000 0000 1101 xxxx 110x xxxx xxxx */
  44. /* STRH (imm 8) 1111 1000 0010 1101 xxxx 110x xxxx xxxx */
  45. /* INVALID INSN 1111 1000 0110 1101 xxxx 110x xxxx xxxx */
  46. /* Only consider U == 0 and P == 1: strx rx, [sp, #-<imm>] */
  47. DECODE_CUSTOM (0xff9f0e00, 0xf80d0c00, STACK_USE_FIXED_0XX),
  48. /* For STR{,B,H} (imm 12), offset is always positive, so ignore them. */
  49. /* P U W | Rn | Rt | Rt2| imm8 |*/
  50. /* STRD (immediate) 1110 1001 01x0 1101 xxxx xxxx xxxx xxxx */
  51. /*
  52. * Only consider U == 0 and P == 1.
  53. * Also note that STRD in T32 encoding is special:
  54. * imm = ZeroExtend(imm8:'00', 32)
  55. */
  56. DECODE_CUSTOM (0xffdf0000, 0xe94d0000, STACK_USE_T32STRD),
  57. /* | Rn | */
  58. /* STMDB 1110 1001 00x0 1101 xxxx xxxx xxxx xxxx */
  59. DECODE_CUSTOM (0xffdf0000, 0xe90d0000, STACK_USE_STMDX),
  60. /* fall through */
  61. DECODE_CUSTOM (0, 0, STACK_USE_NONE),
  62. DECODE_END
  63. };
  64. return probes_decode_insn(insn, asi, table, false, false, stack_check_actions, NULL);
  65. }
  66. const struct decode_checker t32_stack_checker[NUM_PROBES_T32_ACTIONS] = {
  67. [PROBES_T32_LDMSTM] = {.checker = t32_check_stack},
  68. [PROBES_T32_LDRDSTRD] = {.checker = t32_check_stack},
  69. [PROBES_T32_LDRSTR] = {.checker = t32_check_stack},
  70. };
  71. /*
  72. * See following comments. This insn must be 'push'.
  73. */
  74. static enum probes_insn __kprobes t16_check_stack(probes_opcode_t insn,
  75. struct arch_probes_insn *asi,
  76. const struct decode_header *h)
  77. {
  78. unsigned int reglist = insn & 0x1ff;
  79. asi->stack_space = hweight32(reglist) * 4;
  80. return INSN_GOOD;
  81. }
  82. /*
  83. * T16 encoding is simple: only the 'push' insn can need extra stack space.
  84. * Other insns, like str, can only use r0-r7 as Rn.
  85. */
  86. const struct decode_checker t16_stack_checker[NUM_PROBES_T16_ACTIONS] = {
  87. [PROBES_T16_PUSH] = {.checker = t16_check_stack},
  88. };