decode-insn.c 5.0 KB

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