backtrace.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (C) 2010-2013 Imagination Technologies Ltd.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. */
  8. #include <linux/oprofile.h>
  9. #include <linux/uaccess.h>
  10. #include <asm/processor.h>
  11. #include <asm/stacktrace.h>
  12. #include "backtrace.h"
  13. static void user_backtrace_fp(unsigned long __user *fp, unsigned int depth)
  14. {
  15. while (depth-- && access_ok(VERIFY_READ, fp, 8)) {
  16. unsigned long addr;
  17. unsigned long __user *fpnew;
  18. if (__copy_from_user_inatomic(&addr, fp + 1, sizeof(addr)))
  19. break;
  20. addr -= 4;
  21. oprofile_add_trace(addr);
  22. /* stack grows up, so frame pointers must decrease */
  23. if (__copy_from_user_inatomic(&fpnew, fp + 0, sizeof(fpnew)))
  24. break;
  25. if (fpnew >= fp)
  26. break;
  27. fp = fpnew;
  28. }
  29. }
  30. static int kernel_backtrace_frame(struct stackframe *frame, void *data)
  31. {
  32. unsigned int *depth = data;
  33. oprofile_add_trace(frame->pc);
  34. /* decrement depth and stop if we reach 0 */
  35. if ((*depth)-- == 0)
  36. return 1;
  37. /* otherwise onto the next frame */
  38. return 0;
  39. }
  40. void metag_backtrace(struct pt_regs * const regs, unsigned int depth)
  41. {
  42. if (user_mode(regs)) {
  43. unsigned long *fp = (unsigned long *)regs->ctx.AX[1].U0;
  44. user_backtrace_fp((unsigned long __user __force *)fp, depth);
  45. } else {
  46. struct stackframe frame;
  47. frame.fp = regs->ctx.AX[1].U0; /* A0FrP */
  48. frame.sp = user_stack_pointer(regs); /* A0StP */
  49. frame.lr = 0; /* from stack */
  50. frame.pc = regs->ctx.CurrPC; /* PC */
  51. walk_stackframe(&frame, &kernel_backtrace_frame, &depth);
  52. }
  53. }