backtrace.c 3.0 KB

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