backtrace.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Arm specific backtracing code for oprofile
  3. *
  4. * Copyright 2005 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <rpurdie@openedhand.com>
  7. *
  8. * Based on i386 oprofile backtrace code by John Levon, David Smith
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/oprofile.h>
  16. #include <linux/sched.h>
  17. #include <linux/mm.h>
  18. #include <asm/ptrace.h>
  19. #include <asm/uaccess.h>
  20. /*
  21. * The registers we're interested in are at the end of the variable
  22. * length saved register structure. The fp points at the end of this
  23. * structure so the address of this struct is:
  24. * (struct frame_tail *)(xxx->fp)-1
  25. */
  26. struct frame_tail {
  27. struct frame_tail *fp;
  28. unsigned long sp;
  29. unsigned long lr;
  30. } __attribute__((packed));
  31. #ifdef CONFIG_FRAME_POINTER
  32. static struct frame_tail* kernel_backtrace(struct frame_tail *tail)
  33. {
  34. oprofile_add_trace(tail->lr);
  35. /* frame pointers should strictly progress back up the stack
  36. * (towards higher addresses) */
  37. if (tail >= tail->fp)
  38. return NULL;
  39. return tail->fp-1;
  40. }
  41. #endif
  42. static struct frame_tail* user_backtrace(struct frame_tail *tail)
  43. {
  44. struct frame_tail buftail;
  45. /* hardware pte might not be valid due to dirty/accessed bit emulation
  46. * so we use copy_from_user and benefit from exception fixups */
  47. if (copy_from_user(&buftail, tail, sizeof(struct frame_tail)))
  48. return NULL;
  49. oprofile_add_trace(buftail.lr);
  50. /* frame pointers should strictly progress back up the stack
  51. * (towards higher addresses) */
  52. if (tail >= buftail.fp)
  53. return NULL;
  54. return buftail.fp-1;
  55. }
  56. /* Compare two addresses and see if they're on the same page */
  57. #define CMP_ADDR_EQUAL(x,y,offset) ((((unsigned long) x) >> PAGE_SHIFT) \
  58. == ((((unsigned long) y) + offset) >> PAGE_SHIFT))
  59. /* check that the page(s) containing the frame tail are present */
  60. static int pages_present(struct frame_tail *tail)
  61. {
  62. struct mm_struct * mm = current->mm;
  63. if (!check_user_page_readable(mm, (unsigned long)tail))
  64. return 0;
  65. if (CMP_ADDR_EQUAL(tail, tail, 8))
  66. return 1;
  67. if (!check_user_page_readable(mm, ((unsigned long)tail) + 8))
  68. return 0;
  69. return 1;
  70. }
  71. /*
  72. * | | /\ Higher addresses
  73. * | |
  74. * --------------- stack base (address of current_thread_info)
  75. * | thread info |
  76. * . .
  77. * | stack |
  78. * --------------- saved regs->ARM_fp value if valid (frame_tail address)
  79. * . .
  80. * --------------- struct pt_regs stored on stack (struct pt_regs *)
  81. * | |
  82. * . .
  83. * | |
  84. * --------------- %esp
  85. * | |
  86. * | | \/ Lower addresses
  87. *
  88. * Thus, &pt_regs <-> stack base restricts the valid(ish) fp values
  89. */
  90. static int valid_kernel_stack(struct frame_tail *tail, struct pt_regs *regs)
  91. {
  92. unsigned long tailaddr = (unsigned long)tail;
  93. unsigned long stack = (unsigned long)regs;
  94. unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;
  95. return (tailaddr > stack) && (tailaddr < stack_base);
  96. }
  97. void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
  98. {
  99. struct frame_tail *tail;
  100. unsigned long last_address = 0;
  101. tail = ((struct frame_tail *) regs->ARM_fp) - 1;
  102. if (!user_mode(regs)) {
  103. #ifdef CONFIG_FRAME_POINTER
  104. while (depth-- && tail && valid_kernel_stack(tail, regs)) {
  105. tail = kernel_backtrace(tail);
  106. }
  107. #endif
  108. return;
  109. }
  110. while (depth-- && tail && !((unsigned long) tail & 3)) {
  111. if ((!CMP_ADDR_EQUAL(last_address, tail, 0)
  112. || !CMP_ADDR_EQUAL(last_address, tail, 8))
  113. && !pages_present(tail))
  114. return;
  115. last_address = (unsigned long) tail;
  116. tail = user_backtrace(tail);
  117. }
  118. }