backtrace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright (C) 2005 Brian Rogan <bcr6@cornell.edu>, IBM
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. **/
  9. #include <linux/oprofile.h>
  10. #include <linux/sched.h>
  11. #include <asm/processor.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/compat.h>
  14. #define STACK_SP(STACK) *(STACK)
  15. #define STACK_LR64(STACK) *((unsigned long *)(STACK) + 2)
  16. #define STACK_LR32(STACK) *((unsigned int *)(STACK) + 1)
  17. #ifdef CONFIG_PPC64
  18. #define STACK_LR(STACK) STACK_LR64(STACK)
  19. #else
  20. #define STACK_LR(STACK) STACK_LR32(STACK)
  21. #endif
  22. static unsigned int user_getsp32(unsigned int sp, int is_first)
  23. {
  24. unsigned int stack_frame[2];
  25. void __user *p = compat_ptr(sp);
  26. if (!access_ok(VERIFY_READ, p, sizeof(stack_frame)))
  27. return 0;
  28. /*
  29. * The most likely reason for this is that we returned -EFAULT,
  30. * which means that we've done all that we can do from
  31. * interrupt context.
  32. */
  33. if (__copy_from_user_inatomic(stack_frame, p, sizeof(stack_frame)))
  34. return 0;
  35. if (!is_first)
  36. oprofile_add_trace(STACK_LR32(stack_frame));
  37. /*
  38. * We do not enforce increasing stack addresses here because
  39. * we may transition to a different stack, eg a signal handler.
  40. */
  41. return STACK_SP(stack_frame);
  42. }
  43. #ifdef CONFIG_PPC64
  44. static unsigned long user_getsp64(unsigned long sp, int is_first)
  45. {
  46. unsigned long stack_frame[3];
  47. if (!access_ok(VERIFY_READ, (void __user *)sp, sizeof(stack_frame)))
  48. return 0;
  49. if (__copy_from_user_inatomic(stack_frame, (void __user *)sp,
  50. sizeof(stack_frame)))
  51. return 0;
  52. if (!is_first)
  53. oprofile_add_trace(STACK_LR64(stack_frame));
  54. return STACK_SP(stack_frame);
  55. }
  56. #endif
  57. static unsigned long kernel_getsp(unsigned long sp, int is_first)
  58. {
  59. unsigned long *stack_frame = (unsigned long *)sp;
  60. if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
  61. return 0;
  62. if (!is_first)
  63. oprofile_add_trace(STACK_LR(stack_frame));
  64. /*
  65. * We do not enforce increasing stack addresses here because
  66. * we might be transitioning from an interrupt stack to a kernel
  67. * stack. validate_sp() is designed to understand this, so just
  68. * use it.
  69. */
  70. return STACK_SP(stack_frame);
  71. }
  72. void op_powerpc_backtrace(struct pt_regs * const regs, unsigned int depth)
  73. {
  74. unsigned long sp = regs->gpr[1];
  75. int first_frame = 1;
  76. /* We ditch the top stackframe so need to loop through an extra time */
  77. depth += 1;
  78. if (!user_mode(regs)) {
  79. while (depth--) {
  80. sp = kernel_getsp(sp, first_frame);
  81. if (!sp)
  82. break;
  83. first_frame = 0;
  84. }
  85. } else {
  86. #ifdef CONFIG_PPC64
  87. if (!is_32bit_task()) {
  88. while (depth--) {
  89. sp = user_getsp64(sp, first_frame);
  90. if (!sp)
  91. break;
  92. first_frame = 0;
  93. }
  94. return;
  95. }
  96. #endif
  97. while (depth--) {
  98. sp = user_getsp32(sp, first_frame);
  99. if (!sp)
  100. break;
  101. first_frame = 0;
  102. }
  103. }
  104. }