backtrace.c 2.8 KB

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