backtrace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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[2];
  45. /* Also check accessibility of one struct frame_tail beyond */
  46. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  47. return NULL;
  48. if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
  49. return NULL;
  50. oprofile_add_trace(buftail[0].lr);
  51. /* frame pointers should strictly progress back up the stack
  52. * (towards higher addresses) */
  53. if (tail >= buftail[0].fp)
  54. return NULL;
  55. return buftail[0].fp-1;
  56. }
  57. /*
  58. * | | /\ Higher addresses
  59. * | |
  60. * --------------- stack base (address of current_thread_info)
  61. * | thread info |
  62. * . .
  63. * | stack |
  64. * --------------- saved regs->ARM_fp value if valid (frame_tail address)
  65. * . .
  66. * --------------- struct pt_regs stored on stack (struct pt_regs *)
  67. * | |
  68. * . .
  69. * | |
  70. * --------------- %esp
  71. * | |
  72. * | | \/ Lower addresses
  73. *
  74. * Thus, &pt_regs <-> stack base restricts the valid(ish) fp values
  75. */
  76. static int valid_kernel_stack(struct frame_tail *tail, struct pt_regs *regs)
  77. {
  78. unsigned long tailaddr = (unsigned long)tail;
  79. unsigned long stack = (unsigned long)regs;
  80. unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;
  81. return (tailaddr > stack) && (tailaddr < stack_base);
  82. }
  83. void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
  84. {
  85. struct frame_tail *tail;
  86. tail = ((struct frame_tail *) regs->ARM_fp) - 1;
  87. if (!user_mode(regs)) {
  88. #ifdef CONFIG_FRAME_POINTER
  89. while (depth-- && tail && valid_kernel_stack(tail, regs)) {
  90. tail = kernel_backtrace(tail);
  91. }
  92. #endif
  93. return;
  94. }
  95. while (depth-- && tail && !((unsigned long) tail & 3))
  96. tail = user_backtrace(tail);
  97. }