return_address.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * arch/arm64/kernel/return_address.c
  3. *
  4. * Copyright (C) 2013 Linaro Limited
  5. * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/export.h>
  12. #include <linux/ftrace.h>
  13. #include <asm/stacktrace.h>
  14. struct return_address_data {
  15. unsigned int level;
  16. void *addr;
  17. };
  18. static int save_return_addr(struct stackframe *frame, void *d)
  19. {
  20. struct return_address_data *data = d;
  21. if (!data->level) {
  22. data->addr = (void *)frame->pc;
  23. return 1;
  24. } else {
  25. --data->level;
  26. return 0;
  27. }
  28. }
  29. void *return_address(unsigned int level)
  30. {
  31. struct return_address_data data;
  32. struct stackframe frame;
  33. data.level = level + 2;
  34. data.addr = NULL;
  35. frame.fp = (unsigned long)__builtin_frame_address(0);
  36. frame.sp = current_stack_pointer;
  37. frame.pc = (unsigned long)return_address; /* dummy */
  38. walk_stackframe(&frame, save_return_addr, &data);
  39. if (!data.level)
  40. return data.addr;
  41. else
  42. return NULL;
  43. }
  44. EXPORT_SYMBOL_GPL(return_address);