backtrace.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @file backtrace.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon
  8. * @author David Smith
  9. */
  10. #include <linux/oprofile.h>
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/compat.h>
  14. #include <linux/highmem.h>
  15. #include <asm/ptrace.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/stacktrace.h>
  18. static int backtrace_stack(void *data, char *name)
  19. {
  20. /* Yes, we want all stacks */
  21. return 0;
  22. }
  23. static void backtrace_address(void *data, unsigned long addr, int reliable)
  24. {
  25. unsigned int *depth = data;
  26. if ((*depth)--)
  27. oprofile_add_trace(addr);
  28. }
  29. static struct stacktrace_ops backtrace_ops = {
  30. .stack = backtrace_stack,
  31. .address = backtrace_address,
  32. .walk_stack = print_context_stack,
  33. };
  34. /* from arch/x86/kernel/cpu/perf_event.c: */
  35. /*
  36. * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  37. */
  38. static unsigned long
  39. copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
  40. {
  41. unsigned long offset, addr = (unsigned long)from;
  42. unsigned long size, len = 0;
  43. struct page *page;
  44. void *map;
  45. int ret;
  46. do {
  47. ret = __get_user_pages_fast(addr, 1, 0, &page);
  48. if (!ret)
  49. break;
  50. offset = addr & (PAGE_SIZE - 1);
  51. size = min(PAGE_SIZE - offset, n - len);
  52. map = kmap_atomic(page);
  53. memcpy(to, map+offset, size);
  54. kunmap_atomic(map);
  55. put_page(page);
  56. len += size;
  57. to += size;
  58. addr += size;
  59. } while (len < n);
  60. return len;
  61. }
  62. #ifdef CONFIG_COMPAT
  63. static struct stack_frame_ia32 *
  64. dump_user_backtrace_32(struct stack_frame_ia32 *head)
  65. {
  66. /* Also check accessibility of one struct frame_head beyond: */
  67. struct stack_frame_ia32 bufhead[2];
  68. struct stack_frame_ia32 *fp;
  69. unsigned long bytes;
  70. bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
  71. if (bytes != sizeof(bufhead))
  72. return NULL;
  73. fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
  74. oprofile_add_trace(bufhead[0].return_address);
  75. /* frame pointers should strictly progress back up the stack
  76. * (towards higher addresses) */
  77. if (head >= fp)
  78. return NULL;
  79. return fp;
  80. }
  81. static inline int
  82. x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
  83. {
  84. struct stack_frame_ia32 *head;
  85. /* User process is 32-bit */
  86. if (!current || !test_thread_flag(TIF_IA32))
  87. return 0;
  88. head = (struct stack_frame_ia32 *) regs->bp;
  89. while (depth-- && head)
  90. head = dump_user_backtrace_32(head);
  91. return 1;
  92. }
  93. #else
  94. static inline int
  95. x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
  96. {
  97. return 0;
  98. }
  99. #endif /* CONFIG_COMPAT */
  100. static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
  101. {
  102. /* Also check accessibility of one struct frame_head beyond: */
  103. struct stack_frame bufhead[2];
  104. unsigned long bytes;
  105. bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
  106. if (bytes != sizeof(bufhead))
  107. return NULL;
  108. oprofile_add_trace(bufhead[0].return_address);
  109. /* frame pointers should strictly progress back up the stack
  110. * (towards higher addresses) */
  111. if (head >= bufhead[0].next_frame)
  112. return NULL;
  113. return bufhead[0].next_frame;
  114. }
  115. void
  116. x86_backtrace(struct pt_regs * const regs, unsigned int depth)
  117. {
  118. struct stack_frame *head = (struct stack_frame *)frame_pointer(regs);
  119. if (!user_mode_vm(regs)) {
  120. unsigned long stack = kernel_stack_pointer(regs);
  121. if (depth)
  122. dump_trace(NULL, regs, (unsigned long *)stack, 0,
  123. &backtrace_ops, &depth);
  124. return;
  125. }
  126. if (x86_backtrace_32(regs, depth))
  127. return;
  128. while (depth-- && head)
  129. head = dump_user_backtrace(head);
  130. }