GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / arm / kernel / return_address.c
1 /*
2  * arch/arm/kernel/return_address.c
3  *
4  * Copyright (C) 2009 Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
5  * for Pengutronix
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  */
11 #include <linux/export.h>
12 #include <linux/ftrace.h>
13 #include <linux/sched.h>
14
15 #include <asm/stacktrace.h>
16
17 struct return_address_data {
18         unsigned int level;
19         void *addr;
20 };
21
22 static int save_return_addr(struct stackframe *frame, void *d)
23 {
24         struct return_address_data *data = d;
25
26         if (!data->level) {
27                 data->addr = (void *)frame->pc;
28
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;
40
41         data.level = level + 2;
42         data.addr = NULL;
43
44         frame.fp = (unsigned long)__builtin_frame_address(0);
45         frame.sp = current_stack_pointer;
46         frame.lr = (unsigned long)__builtin_return_address(0);
47         frame.pc = (unsigned long)return_address;
48
49         walk_stackframe(&frame, save_return_addr, &data);
50
51         if (!data.level)
52                 return data.addr;
53         else
54                 return NULL;
55 }
56
57 EXPORT_SYMBOL_GPL(return_address);