uprobes.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014-2016 Pratyush Anand <panand@redhat.com>
  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_TRAP_NR UINT_MAX
  11. bool is_swbp_insn(uprobe_opcode_t *insn)
  12. {
  13. return (*insn & 0xffff) == UPROBE_SWBP_INSN;
  14. }
  15. unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
  16. {
  17. return instruction_pointer(regs);
  18. }
  19. int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
  20. unsigned long addr)
  21. {
  22. probe_opcode_t insn;
  23. insn = *(probe_opcode_t *)(&auprobe->insn[0]);
  24. auprobe->insn_size = is_insn32(insn) ? 4 : 2;
  25. switch (csky_probe_decode_insn(&insn, &auprobe->api)) {
  26. case INSN_REJECTED:
  27. return -EINVAL;
  28. case INSN_GOOD_NO_SLOT:
  29. auprobe->simulate = true;
  30. break;
  31. default:
  32. break;
  33. }
  34. return 0;
  35. }
  36. int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  37. {
  38. struct uprobe_task *utask = current->utask;
  39. utask->autask.saved_trap_no = current->thread.trap_no;
  40. current->thread.trap_no = UPROBE_TRAP_NR;
  41. instruction_pointer_set(regs, utask->xol_vaddr);
  42. user_enable_single_step(current);
  43. return 0;
  44. }
  45. int arch_uprobe_post_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  46. {
  47. struct uprobe_task *utask = current->utask;
  48. WARN_ON_ONCE(current->thread.trap_no != UPROBE_TRAP_NR);
  49. current->thread.trap_no = utask->autask.saved_trap_no;
  50. instruction_pointer_set(regs, utask->vaddr + auprobe->insn_size);
  51. user_disable_single_step(current);
  52. return 0;
  53. }
  54. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  55. {
  56. if (t->thread.trap_no != UPROBE_TRAP_NR)
  57. return true;
  58. return false;
  59. }
  60. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  61. {
  62. probe_opcode_t insn;
  63. unsigned long addr;
  64. if (!auprobe->simulate)
  65. return false;
  66. insn = *(probe_opcode_t *)(&auprobe->insn[0]);
  67. addr = instruction_pointer(regs);
  68. if (auprobe->api.handler)
  69. auprobe->api.handler(insn, addr, regs);
  70. return true;
  71. }
  72. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  73. {
  74. struct uprobe_task *utask = current->utask;
  75. current->thread.trap_no = utask->autask.saved_trap_no;
  76. /*
  77. * Task has received a fatal signal, so reset back to probed
  78. * address.
  79. */
  80. instruction_pointer_set(regs, utask->vaddr);
  81. user_disable_single_step(current);
  82. }
  83. bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  84. struct pt_regs *regs)
  85. {
  86. if (ctx == RP_CHECK_CHAIN_CALL)
  87. return regs->usp <= ret->stack;
  88. else
  89. return regs->usp < ret->stack;
  90. }
  91. unsigned long
  92. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  93. struct pt_regs *regs)
  94. {
  95. unsigned long ra;
  96. ra = regs->lr;
  97. regs->lr = trampoline_vaddr;
  98. return ra;
  99. }
  100. int arch_uprobe_exception_notify(struct notifier_block *self,
  101. unsigned long val, void *data)
  102. {
  103. return NOTIFY_DONE;
  104. }
  105. int uprobe_breakpoint_handler(struct pt_regs *regs)
  106. {
  107. if (uprobe_pre_sstep_notifier(regs))
  108. return 1;
  109. return 0;
  110. }
  111. int uprobe_single_step_handler(struct pt_regs *regs)
  112. {
  113. if (uprobe_post_sstep_notifier(regs))
  114. return 1;
  115. return 0;
  116. }