AKASHI Takahiro | 3711784 | 2014-04-30 10:54:35 +0100 | [diff] [blame] | 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 | |
| 12 | #include <linux/export.h> |
| 13 | #include <linux/ftrace.h> |
| 14 | |
Mark Rutland | ab96e21 | 2016-11-03 20:23:05 +0000 | [diff] [blame] | 15 | #include <asm/stack_pointer.h> |
AKASHI Takahiro | 3711784 | 2014-04-30 10:54:35 +0100 | [diff] [blame] | 16 | #include <asm/stacktrace.h> |
| 17 | |
| 18 | struct return_address_data { |
| 19 | unsigned int level; |
| 20 | void *addr; |
| 21 | }; |
| 22 | |
| 23 | static int save_return_addr(struct stackframe *frame, void *d) |
| 24 | { |
| 25 | struct return_address_data *data = d; |
| 26 | |
| 27 | if (!data->level) { |
| 28 | data->addr = (void *)frame->pc; |
| 29 | return 1; |
| 30 | } else { |
| 31 | --data->level; |
| 32 | return 0; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void *return_address(unsigned int level) |
| 37 | { |
| 38 | struct return_address_data data; |
| 39 | struct stackframe frame; |
AKASHI Takahiro | 3711784 | 2014-04-30 10:54:35 +0100 | [diff] [blame] | 40 | |
| 41 | data.level = level + 2; |
| 42 | data.addr = NULL; |
| 43 | |
| 44 | frame.fp = (unsigned long)__builtin_frame_address(0); |
Behan Webster | a4ceab1 | 2014-08-27 05:29:34 +0100 | [diff] [blame] | 45 | frame.sp = current_stack_pointer; |
AKASHI Takahiro | 3711784 | 2014-04-30 10:54:35 +0100 | [diff] [blame] | 46 | frame.pc = (unsigned long)return_address; /* dummy */ |
AKASHI Takahiro | 20380bb | 2015-12-15 17:33:41 +0900 | [diff] [blame] | 47 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 48 | frame.graph = current->curr_ret_stack; |
| 49 | #endif |
AKASHI Takahiro | 3711784 | 2014-04-30 10:54:35 +0100 | [diff] [blame] | 50 | |
AKASHI Takahiro | fe13f95 | 2015-12-15 17:33:40 +0900 | [diff] [blame] | 51 | walk_stackframe(current, &frame, save_return_addr, &data); |
AKASHI Takahiro | 3711784 | 2014-04-30 10:54:35 +0100 | [diff] [blame] | 52 | |
| 53 | if (!data.level) |
| 54 | return data.addr; |
| 55 | else |
| 56 | return NULL; |
| 57 | } |
| 58 | EXPORT_SYMBOL_GPL(return_address); |