decode-insn.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * arch/arm64/kernel/probes/decode-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/kernel.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/module.h>
  18. #include <linux/kallsyms.h>
  19. #include <asm/kprobes.h>
  20. #include <asm/insn.h>
  21. #include <asm/sections.h>
  22. #include "decode-insn.h"
  23. #include "simulate-insn.h"
  24. static bool __kprobes aarch64_insn_is_steppable(u32 insn)
  25. {
  26. /*
  27. * Branch instructions will write a new value into the PC which is
  28. * likely to be relative to the XOL address and therefore invalid.
  29. * Deliberate generation of an exception during stepping is also not
  30. * currently safe. Lastly, MSR instructions can do any number of nasty
  31. * things we can't handle during single-stepping.
  32. */
  33. if (aarch64_get_insn_class(insn) == AARCH64_INSN_CLS_BR_SYS) {
  34. if (aarch64_insn_is_branch(insn) ||
  35. aarch64_insn_is_msr_imm(insn) ||
  36. aarch64_insn_is_msr_reg(insn) ||
  37. aarch64_insn_is_exception(insn) ||
  38. aarch64_insn_is_eret(insn))
  39. return false;
  40. /*
  41. * The MRS instruction may not return a correct value when
  42. * executing in the single-stepping environment. We do make one
  43. * exception, for reading the DAIF bits.
  44. */
  45. if (aarch64_insn_is_mrs(insn))
  46. return aarch64_insn_extract_system_reg(insn)
  47. != AARCH64_INSN_SPCLREG_DAIF;
  48. /*
  49. * The HINT instruction is is problematic when single-stepping,
  50. * except for the NOP case.
  51. */
  52. if (aarch64_insn_is_hint(insn))
  53. return aarch64_insn_is_nop(insn);
  54. return true;
  55. }
  56. /*
  57. * Instructions which load PC relative literals are not going to work
  58. * when executed from an XOL slot. Instructions doing an exclusive
  59. * load/store are not going to complete successfully when single-step
  60. * exception handling happens in the middle of the sequence.
  61. */
  62. if (aarch64_insn_uses_literal(insn) ||
  63. aarch64_insn_is_exclusive(insn))
  64. return false;
  65. return true;
  66. }
  67. /* Return:
  68. * INSN_REJECTED If instruction is one not allowed to kprobe,
  69. * INSN_GOOD If instruction is supported and uses instruction slot,
  70. * INSN_GOOD_NO_SLOT If instruction is supported but doesn't use its slot.
  71. */
  72. static enum kprobe_insn __kprobes
  73. arm_probe_decode_insn(kprobe_opcode_t insn, struct arch_specific_insn *asi)
  74. {
  75. /*
  76. * Instructions reading or modifying the PC won't work from the XOL
  77. * slot.
  78. */
  79. if (aarch64_insn_is_steppable(insn))
  80. return INSN_GOOD;
  81. if (aarch64_insn_is_bcond(insn)) {
  82. asi->handler = simulate_b_cond;
  83. } else if (aarch64_insn_is_cbz(insn) ||
  84. aarch64_insn_is_cbnz(insn)) {
  85. asi->handler = simulate_cbz_cbnz;
  86. } else if (aarch64_insn_is_tbz(insn) ||
  87. aarch64_insn_is_tbnz(insn)) {
  88. asi->handler = simulate_tbz_tbnz;
  89. } else if (aarch64_insn_is_adr_adrp(insn)) {
  90. asi->handler = simulate_adr_adrp;
  91. } else if (aarch64_insn_is_b(insn) ||
  92. aarch64_insn_is_bl(insn)) {
  93. asi->handler = simulate_b_bl;
  94. } else if (aarch64_insn_is_br(insn) ||
  95. aarch64_insn_is_blr(insn) ||
  96. aarch64_insn_is_ret(insn)) {
  97. asi->handler = simulate_br_blr_ret;
  98. } else if (aarch64_insn_is_ldr_lit(insn)) {
  99. asi->handler = simulate_ldr_literal;
  100. } else if (aarch64_insn_is_ldrsw_lit(insn)) {
  101. asi->handler = simulate_ldrsw_literal;
  102. } else {
  103. /*
  104. * Instruction cannot be stepped out-of-line and we don't
  105. * (yet) simulate it.
  106. */
  107. return INSN_REJECTED;
  108. }
  109. return INSN_GOOD_NO_SLOT;
  110. }
  111. static bool __kprobes
  112. is_probed_address_atomic(kprobe_opcode_t *scan_start, kprobe_opcode_t *scan_end)
  113. {
  114. while (scan_start >= scan_end) {
  115. /*
  116. * atomic region starts from exclusive load and ends with
  117. * exclusive store.
  118. */
  119. if (aarch64_insn_is_store_ex(le32_to_cpu(*scan_start)))
  120. return false;
  121. else if (aarch64_insn_is_load_ex(le32_to_cpu(*scan_start)))
  122. return true;
  123. scan_start--;
  124. }
  125. return false;
  126. }
  127. enum kprobe_insn __kprobes
  128. arm_kprobe_decode_insn(kprobe_opcode_t *addr, struct arch_specific_insn *asi)
  129. {
  130. enum kprobe_insn decoded;
  131. kprobe_opcode_t insn = le32_to_cpu(*addr);
  132. kprobe_opcode_t *scan_end = NULL;
  133. unsigned long size = 0, offset = 0;
  134. /*
  135. * If there's a symbol defined in front of and near enough to
  136. * the probe address assume it is the entry point to this
  137. * code and use it to further limit how far back we search
  138. * when determining if we're in an atomic sequence. If we could
  139. * not find any symbol skip the atomic test altogether as we
  140. * could otherwise end up searching irrelevant text/literals.
  141. * KPROBES depends on KALLSYMS so this last case should never
  142. * happen.
  143. */
  144. if (kallsyms_lookup_size_offset((unsigned long) addr, &size, &offset)) {
  145. if (offset < (MAX_ATOMIC_CONTEXT_SIZE*sizeof(kprobe_opcode_t)))
  146. scan_end = addr - (offset / sizeof(kprobe_opcode_t));
  147. else
  148. scan_end = addr - MAX_ATOMIC_CONTEXT_SIZE;
  149. }
  150. decoded = arm_probe_decode_insn(insn, asi);
  151. if (decoded != INSN_REJECTED && scan_end)
  152. if (is_probed_address_atomic(addr - 1, scan_end))
  153. return INSN_REJECTED;
  154. return decoded;
  155. }