uprobes.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014-2016 Pratyush Anand <[email protected]>
  4. */
  5. #include <linux/highmem.h>
  6. #include <linux/ptrace.h>
  7. #include <linux/uprobes.h>
  8. #include <asm/cacheflush.h>
  9. #include "decode-insn.h"
  10. #define UPROBE_INV_FAULT_CODE UINT_MAX
  11. void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
  12. void *src, unsigned long len)
  13. {
  14. void *xol_page_kaddr = kmap_atomic(page);
  15. void *dst = xol_page_kaddr + (vaddr & ~PAGE_MASK);
  16. /* Initialize the slot */
  17. memcpy(dst, src, len);
  18. /* flush caches (dcache/icache) */
  19. sync_icache_aliases((unsigned long)dst, (unsigned long)dst + len);
  20. kunmap_atomic(xol_page_kaddr);
  21. }
  22. unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
  23. {
  24. return instruction_pointer(regs);
  25. }
  26. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
  27. unsigned long addr)
  28. {
  29. probe_opcode_t insn;
  30. /* TODO: Currently we do not support AARCH32 instruction probing */
  31. if (mm->context.flags & MMCF_AARCH32)
  32. return -EOPNOTSUPP;
  33. else if (!IS_ALIGNED(addr, AARCH64_INSN_SIZE))
  34. return -EINVAL;
  35. insn = *(probe_opcode_t *)(&auprobe->insn[0]);
  36. switch (arm_probe_decode_insn(insn, &auprobe->api)) {
  37. case INSN_REJECTED:
  38. return -EINVAL;
  39. case INSN_GOOD_NO_SLOT:
  40. auprobe->simulate = true;
  41. break;
  42. default:
  43. break;
  44. }
  45. return 0;
  46. }
  47. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  48. {
  49. struct uprobe_task *utask = current->utask;
  50. /* Initialize with an invalid fault code to detect if ol insn trapped */
  51. current->thread.fault_code = UPROBE_INV_FAULT_CODE;
  52. /* Instruction points to execute ol */
  53. instruction_pointer_set(regs, utask->xol_vaddr);
  54. user_enable_single_step(current);
  55. return 0;
  56. }
  57. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  58. {
  59. struct uprobe_task *utask = current->utask;
  60. WARN_ON_ONCE(current->thread.fault_code != UPROBE_INV_FAULT_CODE);
  61. /* Instruction points to execute next to breakpoint address */
  62. instruction_pointer_set(regs, utask->vaddr + 4);
  63. user_disable_single_step(current);
  64. return 0;
  65. }
  66. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  67. {
  68. /*
  69. * Between arch_uprobe_pre_xol and arch_uprobe_post_xol, if an xol
  70. * insn itself is trapped, then detect the case with the help of
  71. * invalid fault code which is being set in arch_uprobe_pre_xol
  72. */
  73. if (t->thread.fault_code != UPROBE_INV_FAULT_CODE)
  74. return true;
  75. return false;
  76. }
  77. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  78. {
  79. probe_opcode_t insn;
  80. unsigned long addr;
  81. if (!auprobe->simulate)
  82. return false;
  83. insn = *(probe_opcode_t *)(&auprobe->insn[0]);
  84. addr = instruction_pointer(regs);
  85. if (auprobe->api.handler)
  86. auprobe->api.handler(insn, addr, regs);
  87. return true;
  88. }
  89. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  90. {
  91. struct uprobe_task *utask = current->utask;
  92. /*
  93. * Task has received a fatal signal, so reset back to probbed
  94. * address.
  95. */
  96. instruction_pointer_set(regs, utask->vaddr);
  97. user_disable_single_step(current);
  98. }
  99. bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  100. struct pt_regs *regs)
  101. {
  102. /*
  103. * If a simple branch instruction (B) was called for retprobed
  104. * assembly label then return true even when regs->sp and ret->stack
  105. * are same. It will ensure that cleanup and reporting of return
  106. * instances corresponding to callee label is done when
  107. * handle_trampoline for called function is executed.
  108. */
  109. if (ctx == RP_CHECK_CHAIN_CALL)
  110. return regs->sp <= ret->stack;
  111. else
  112. return regs->sp < ret->stack;
  113. }
  114. unsigned long
  115. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  116. struct pt_regs *regs)
  117. {
  118. unsigned long orig_ret_vaddr;
  119. orig_ret_vaddr = procedure_link_pointer(regs);
  120. /* Replace the return addr with trampoline addr */
  121. procedure_link_pointer_set(regs, trampoline_vaddr);
  122. return orig_ret_vaddr;
  123. }
  124. int arch_uprobe_exception_notify(struct notifier_block *self,
  125. unsigned long val, void *data)
  126. {
  127. return NOTIFY_DONE;
  128. }
  129. static int uprobe_breakpoint_handler(struct pt_regs *regs,
  130. unsigned long esr)
  131. {
  132. if (uprobe_pre_sstep_notifier(regs))
  133. return DBG_HOOK_HANDLED;
  134. return DBG_HOOK_ERROR;
  135. }
  136. static int uprobe_single_step_handler(struct pt_regs *regs,
  137. unsigned long esr)
  138. {
  139. struct uprobe_task *utask = current->utask;
  140. WARN_ON(utask && (instruction_pointer(regs) != utask->xol_vaddr + 4));
  141. if (uprobe_post_sstep_notifier(regs))
  142. return DBG_HOOK_HANDLED;
  143. return DBG_HOOK_ERROR;
  144. }
  145. /* uprobe breakpoint handler hook */
  146. static struct break_hook uprobes_break_hook = {
  147. .imm = UPROBES_BRK_IMM,
  148. .fn = uprobe_breakpoint_handler,
  149. };
  150. /* uprobe single step handler hook */
  151. static struct step_hook uprobes_step_hook = {
  152. .fn = uprobe_single_step_handler,
  153. };
  154. static int __init arch_init_uprobes(void)
  155. {
  156. register_user_break_hook(&uprobes_break_hook);
  157. register_user_step_hook(&uprobes_step_hook);
  158. return 0;
  159. }
  160. device_initcall(arch_init_uprobes);