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/insn.h>
  20. #include <asm/sections.h>
  21. #include "decode-insn.h"
  22. #include "simulate-insn.h"
  23. static bool __kprobes aarch64_insn_is_steppable(u32 insn)
  24. {
  25. /*
  26. * Branch instructions will write a new value into the PC which is
  27. * likely to be relative to the XOL address and therefore invalid.
  28. * Deliberate generation of an exception during stepping is also not
  29. * currently safe. Lastly, MSR instructions can do any number of nasty
  30. * things we can't handle during single-stepping.
  31. */
  32. if (aarch64_get_insn_class(insn) == AARCH64_INSN_CLS_BR_SYS) {
  33. if (aarch64_insn_is_branch(insn) ||
  34. aarch64_insn_is_msr_imm(insn) ||
  35. aarch64_insn_is_msr_reg(insn) ||
  36. aarch64_insn_is_exception(insn) ||
  37. aarch64_insn_is_eret(insn))
  38. return false;
  39. /*
  40. * The MRS instruction may not return a correct value when
  41. * executing in the single-stepping environment. We do make one
  42. * exception, for reading the DAIF bits.
  43. */
  44. if (aarch64_insn_is_mrs(insn))
  45. return aarch64_insn_extract_system_reg(insn)
  46. != AARCH64_INSN_SPCLREG_DAIF;
  47. /*
  48. * The HINT instruction is is problematic when single-stepping,
  49. * except for the NOP case.
  50. */
  51. if (aarch64_insn_is_hint(insn))
  52. return aarch64_insn_is_nop(insn);
  53. return true;
  54. }
  55. /*
  56. * Instructions which load PC relative literals are not going to work
  57. * when executed from an XOL slot. Instructions doing an exclusive
  58. * load/store are not going to complete successfully when single-step
  59. * exception handling happens in the middle of the sequence.
  60. */
  61. if (aarch64_insn_uses_literal(insn) ||
  62. aarch64_insn_is_exclusive(insn))
  63. return false;
  64. return true;
  65. }
  66. /* Return:
  67. * INSN_REJECTED If instruction is one not allowed to kprobe,
  68. * INSN_GOOD If instruction is supported and uses instruction slot,
  69. * INSN_GOOD_NO_SLOT If instruction is supported but doesn't use its slot.
  70. */
  71. enum probe_insn __kprobes
  72. arm_probe_decode_insn(probe_opcode_t insn, struct arch_probe_insn *api)
  73. {
  74. /*
  75. * Instructions reading or modifying the PC won't work from the XOL
  76. * slot.
  77. */
  78. if (aarch64_insn_is_steppable(insn))
  79. return INSN_GOOD;
  80. if (aarch64_insn_is_bcond(insn)) {
  81. api->handler = simulate_b_cond;
  82. } else if (aarch64_insn_is_cbz(insn) ||
  83. aarch64_insn_is_cbnz(insn)) {
  84. api->handler = simulate_cbz_cbnz;
  85. } else if (aarch64_insn_is_tbz(insn) ||
  86. aarch64_insn_is_tbnz(insn)) {
  87. api->handler = simulate_tbz_tbnz;
  88. } else if (aarch64_insn_is_adr_adrp(insn)) {
  89. api->handler = simulate_adr_adrp;
  90. } else if (aarch64_insn_is_b(insn) ||
  91. aarch64_insn_is_bl(insn)) {
  92. api->handler = simulate_b_bl;
  93. } else if (aarch64_insn_is_br(insn) ||
  94. aarch64_insn_is_blr(insn) ||
  95. aarch64_insn_is_ret(insn)) {
  96. api->handler = simulate_br_blr_ret;
  97. } else if (aarch64_insn_is_ldr_lit(insn)) {
  98. api->handler = simulate_ldr_literal;
  99. } else if (aarch64_insn_is_ldrsw_lit(insn)) {
  100. api->handler = simulate_ldrsw_literal;
  101. } else {
  102. /*
  103. * Instruction cannot be stepped out-of-line and we don't
  104. * (yet) simulate it.
  105. */
  106. return INSN_REJECTED;
  107. }
  108. return INSN_GOOD_NO_SLOT;
  109. }
  110. #ifdef CONFIG_KPROBES
  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 probe_insn __kprobes
  128. arm_kprobe_decode_insn(kprobe_opcode_t *addr, struct arch_specific_insn *asi)
  129. {
  130. enum probe_insn decoded;
  131. probe_opcode_t insn = le32_to_cpu(*addr);
  132. probe_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->api);
  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. }
  156. #endif