unwind.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_S390_UNWIND_H
  3. #define _ASM_S390_UNWIND_H
  4. #include <linux/sched.h>
  5. #include <linux/ftrace.h>
  6. #include <linux/kprobes.h>
  7. #include <linux/llist.h>
  8. #include <asm/ptrace.h>
  9. #include <asm/stacktrace.h>
  10. /*
  11. * To use the stack unwinder it has to be initialized with unwind_start.
  12. * There four combinations for task and regs:
  13. * 1) task==NULL, regs==NULL: the unwind starts for the task that is currently
  14. * running, sp/ip picked up from the CPU registers
  15. * 2) task==NULL, regs!=NULL: the unwind starts from the sp/ip found in
  16. * the struct pt_regs of an interrupt frame for the current task
  17. * 3) task!=NULL, regs==NULL: the unwind starts for an inactive task with
  18. * the sp picked up from task->thread.ksp and the ip picked up from the
  19. * return address stored by __switch_to
  20. * 4) task!=NULL, regs!=NULL: the sp/ip are picked up from the interrupt
  21. * frame 'regs' of a inactive task
  22. * If 'first_frame' is not zero unwind_start skips unwind frames until it
  23. * reaches the specified stack pointer.
  24. * The end of the unwinding is indicated with unwind_done, this can be true
  25. * right after unwind_start, e.g. with first_frame!=0 that can not be found.
  26. * unwind_next_frame skips to the next frame.
  27. * Once the unwind is completed unwind_error() can be used to check if there
  28. * has been a situation where the unwinder could not correctly understand
  29. * the tasks call chain.
  30. */
  31. struct unwind_state {
  32. struct stack_info stack_info;
  33. unsigned long stack_mask;
  34. struct task_struct *task;
  35. struct pt_regs *regs;
  36. unsigned long sp, ip;
  37. int graph_idx;
  38. struct llist_node *kr_cur;
  39. bool reliable;
  40. bool error;
  41. };
  42. /* Recover the return address modified by kretprobe and ftrace_graph. */
  43. static inline unsigned long unwind_recover_ret_addr(struct unwind_state *state,
  44. unsigned long ip)
  45. {
  46. ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, (void *)state->sp);
  47. if (is_kretprobe_trampoline(ip))
  48. ip = kretprobe_find_ret_addr(state->task, (void *)state->sp, &state->kr_cur);
  49. return ip;
  50. }
  51. void __unwind_start(struct unwind_state *state, struct task_struct *task,
  52. struct pt_regs *regs, unsigned long first_frame);
  53. bool unwind_next_frame(struct unwind_state *state);
  54. unsigned long unwind_get_return_address(struct unwind_state *state);
  55. static inline bool unwind_done(struct unwind_state *state)
  56. {
  57. return state->stack_info.type == STACK_TYPE_UNKNOWN;
  58. }
  59. static inline bool unwind_error(struct unwind_state *state)
  60. {
  61. return state->error;
  62. }
  63. static __always_inline void unwind_start(struct unwind_state *state,
  64. struct task_struct *task,
  65. struct pt_regs *regs,
  66. unsigned long first_frame)
  67. {
  68. task = task ?: current;
  69. first_frame = first_frame ?: get_stack_pointer(task, regs);
  70. __unwind_start(state, task, regs, first_frame);
  71. }
  72. static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state)
  73. {
  74. return unwind_done(state) ? NULL : state->regs;
  75. }
  76. #define unwind_for_each_frame(state, task, regs, first_frame) \
  77. for (unwind_start(state, task, regs, first_frame); \
  78. !unwind_done(state); \
  79. unwind_next_frame(state))
  80. static inline void unwind_init(void) {}
  81. static inline void unwind_module_init(struct module *mod, void *orc_ip,
  82. size_t orc_ip_size, void *orc,
  83. size_t orc_size) {}
  84. #endif /* _ASM_S390_UNWIND_H */