backtrace.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <linux/uaccess.h>
  13. #include <asm/compat.h>
  14. #include <asm/oprofile_impl.h>
  15. #define STACK_SP(STACK) *(STACK)
  16. #define STACK_LR64(STACK) *((unsigned long *)(STACK) + 2)
  17. #define STACK_LR32(STACK) *((unsigned int *)(STACK) + 1)
  18. #ifdef CONFIG_PPC64
  19. #define STACK_LR(STACK) STACK_LR64(STACK)
  20. #else
  21. #define STACK_LR(STACK) STACK_LR32(STACK)
  22. #endif
  23. static unsigned int user_getsp32(unsigned int sp, int is_first)
  24. {
  25. unsigned int stack_frame[2];
  26. void __user *p = compat_ptr(sp);
  27. if (!access_ok(VERIFY_READ, p, sizeof(stack_frame)))
  28. return 0;
  29. /*
  30. * The most likely reason for this is that we returned -EFAULT,
  31. * which means that we've done all that we can do from
  32. * interrupt context.
  33. */
  34. if (__copy_from_user_inatomic(stack_frame, p, sizeof(stack_frame)))
  35. return 0;
  36. if (!is_first)
  37. oprofile_add_trace(STACK_LR32(stack_frame));
  38. /*
  39. * We do not enforce increasing stack addresses here because
  40. * we may transition to a different stack, eg a signal handler.
  41. */
  42. return STACK_SP(stack_frame);
  43. }
  44. #ifdef CONFIG_PPC64
  45. static unsigned long user_getsp64(unsigned long sp, int is_first)
  46. {
  47. unsigned long stack_frame[3];
  48. if (!access_ok(VERIFY_READ, (void __user *)sp, sizeof(stack_frame)))
  49. return 0;
  50. if (__copy_from_user_inatomic(stack_frame, (void __user *)sp,
  51. sizeof(stack_frame)))
  52. return 0;
  53. if (!is_first)
  54. oprofile_add_trace(STACK_LR64(stack_frame));
  55. return STACK_SP(stack_frame);
  56. }
  57. #endif
  58. static unsigned long kernel_getsp(unsigned long sp, int is_first)
  59. {
  60. unsigned long *stack_frame = (unsigned long *)sp;
  61. if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
  62. return 0;
  63. if (!is_first)
  64. oprofile_add_trace(STACK_LR(stack_frame));
  65. /*
  66. * We do not enforce increasing stack addresses here because
  67. * we might be transitioning from an interrupt stack to a kernel
  68. * stack. validate_sp() is designed to understand this, so just
  69. * use it.
  70. */
  71. return STACK_SP(stack_frame);
  72. }
  73. void op_powerpc_backtrace(struct pt_regs * const regs, unsigned int depth)
  74. {
  75. unsigned long sp = regs->gpr[1];
  76. int first_frame = 1;
  77. /* We ditch the top stackframe so need to loop through an extra time */
  78. depth += 1;
  79. if (!user_mode(regs)) {
  80. while (depth--) {
  81. sp = kernel_getsp(sp, first_frame);
  82. if (!sp)
  83. break;
  84. first_frame = 0;
  85. }
  86. } else {
  87. pagefault_disable();
  88. #ifdef CONFIG_PPC64
  89. if (!is_32bit_task()) {
  90. while (depth--) {
  91. sp = user_getsp64(sp, first_frame);
  92. if (!sp)
  93. break;
  94. first_frame = 0;
  95. }
  96. pagefault_enable();
  97. return;
  98. }
  99. #endif
  100. while (depth--) {
  101. sp = user_getsp32(sp, first_frame);
  102. if (!sp)
  103. break;
  104. first_frame = 0;
  105. }
  106. pagefault_enable();
  107. }
  108. }