backtrace.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. }