uprobes.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_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. instruction_pointer_set(regs, utask->vaddr + auprobe->insn_size);
  50. user_disable_single_step(current);
  51. return 0;
  52. }
  53. bool arch_uprobe_xol_was_trapped(struct task_struct *t)
  54. {
  55. if (t->thread.trap_no != UPROBE_TRAP_NR)
  56. return true;
  57. return false;
  58. }
  59. bool arch_uprobe_skip_sstep(struct arch_uprobe *auprobe, struct pt_regs *regs)
  60. {
  61. probe_opcode_t insn;
  62. unsigned long addr;
  63. if (!auprobe->simulate)
  64. return false;
  65. insn = *(probe_opcode_t *)(&auprobe->insn[0]);
  66. addr = instruction_pointer(regs);
  67. if (auprobe->api.handler)
  68. auprobe->api.handler(insn, addr, regs);
  69. return true;
  70. }
  71. void arch_uprobe_abort_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
  72. {
  73. struct uprobe_task *utask = current->utask;
  74. /*
  75. * Task has received a fatal signal, so reset back to probed
  76. * address.
  77. */
  78. instruction_pointer_set(regs, utask->vaddr);
  79. user_disable_single_step(current);
  80. }
  81. bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  82. struct pt_regs *regs)
  83. {
  84. if (ctx == RP_CHECK_CHAIN_CALL)
  85. return regs->usp <= ret->stack;
  86. else
  87. return regs->usp < ret->stack;
  88. }
  89. unsigned long
  90. arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr,
  91. struct pt_regs *regs)
  92. {
  93. unsigned long ra;
  94. ra = regs->lr;
  95. regs->lr = trampoline_vaddr;
  96. return ra;
  97. }
  98. int arch_uprobe_exception_notify(struct notifier_block *self,
  99. unsigned long val, void *data)
  100. {
  101. return NOTIFY_DONE;
  102. }
  103. int uprobe_breakpoint_handler(struct pt_regs *regs)
  104. {
  105. if (uprobe_pre_sstep_notifier(regs))
  106. return 1;
  107. return 0;
  108. }
  109. int uprobe_single_step_handler(struct pt_regs *regs)
  110. {
  111. if (uprobe_post_sstep_notifier(regs))
  112. return 1;
  113. return 0;
  114. }