backtrace.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * AVR32 specific backtracing code for oprofile
  3. *
  4. * Copyright 2008 Weinmann GmbH
  5. *
  6. * Author: Nikolaus Voss <n.voss@weinmann.de>
  7. *
  8. * Based on i386 oprofile backtrace code by John Levon and 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/uaccess.h>
  18. /* The first two words of each frame on the stack look like this if we have
  19. * frame pointers */
  20. struct frame_head {
  21. unsigned long lr;
  22. struct frame_head *fp;
  23. };
  24. /* copied from arch/avr32/kernel/process.c */
  25. static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p)
  26. {
  27. return (p > (unsigned long)tinfo)
  28. && (p < (unsigned long)tinfo + THREAD_SIZE - 3);
  29. }
  30. /* copied from arch/x86/oprofile/backtrace.c */
  31. static struct frame_head *dump_user_backtrace(struct frame_head *head)
  32. {
  33. struct frame_head bufhead[2];
  34. /* Also check accessibility of one struct frame_head beyond */
  35. if (!access_ok(VERIFY_READ, head, sizeof(bufhead)))
  36. return NULL;
  37. if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
  38. return NULL;
  39. oprofile_add_trace(bufhead[0].lr);
  40. /* frame pointers should strictly progress back up the stack
  41. * (towards higher addresses) */
  42. if (bufhead[0].fp <= head)
  43. return NULL;
  44. return bufhead[0].fp;
  45. }
  46. void avr32_backtrace(struct pt_regs * const regs, unsigned int depth)
  47. {
  48. /* Get first frame pointer */
  49. struct frame_head *head = (struct frame_head *)(regs->r7);
  50. if (!user_mode(regs)) {
  51. #ifdef CONFIG_FRAME_POINTER
  52. /*
  53. * Traverse the kernel stack from frame to frame up to
  54. * "depth" steps.
  55. */
  56. while (depth-- && valid_stack_ptr(task_thread_info(current),
  57. (unsigned long)head)) {
  58. oprofile_add_trace(head->lr);
  59. if (head->fp <= head)
  60. break;
  61. head = head->fp;
  62. }
  63. #endif
  64. } else {
  65. /* Assume we have frame pointers in user mode process */
  66. while (depth-- && head)
  67. head = dump_user_backtrace(head);
  68. }
  69. }