Merge branch 'linux-3.10.y' of git://git.kernel.org/pub/scm/linux/kernel/git/stable...
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_backtrace.c
1 /**
2  * Copyright (C) ARM Limited 2010-2015. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  */
9
10 /*
11  * EABI backtrace stores {fp,lr} on the stack.
12  */
13 struct stack_frame_eabi {
14         union {
15                 struct {
16                         unsigned long fp;
17                         /* May be the fp in the case of a leaf function or clang */
18                         unsigned long lr;
19                         /* If lr is really the fp, lr2 is the corresponding lr */
20                         unsigned long lr2;
21                 };
22                 /* Used to read 32 bit fp/lr from a 64 bit kernel */
23                 struct {
24                         u32 fp_32;
25                         /* same as lr above */
26                         u32 lr_32;
27                         /* same as lr2 above */
28                         u32 lr2_32;
29                 };
30         };
31 };
32
33 static void gator_add_trace(int cpu, unsigned long address)
34 {
35         off_t offset = 0;
36         unsigned long cookie = get_address_cookie(cpu, current, address & ~1, &offset);
37
38         if (cookie == NO_COOKIE || cookie == UNRESOLVED_COOKIE)
39                 offset = address;
40
41         marshal_backtrace(offset & ~1, cookie, 0);
42 }
43
44 static void arm_backtrace_eabi(int cpu, struct pt_regs *const regs, unsigned int depth)
45 {
46 #if defined(__arm__) || defined(__aarch64__)
47         struct stack_frame_eabi *curr;
48         struct stack_frame_eabi bufcurr;
49 #if defined(__arm__)
50         const bool is_compat = false;
51         unsigned long fp = regs->ARM_fp;
52         unsigned long sp = regs->ARM_sp;
53         unsigned long lr = regs->ARM_lr;
54         const int gcc_frame_offset = sizeof(unsigned long);
55 #else
56         /* Is userspace aarch32 (32 bit) */
57         const bool is_compat = compat_user_mode(regs);
58         unsigned long fp = (is_compat ? regs->regs[11] : regs->regs[29]);
59         unsigned long sp = (is_compat ? regs->compat_sp : regs->sp);
60         unsigned long lr = (is_compat ? regs->compat_lr : regs->regs[30]);
61         const int gcc_frame_offset = (is_compat ? sizeof(u32) : 0);
62 #endif
63         /* clang frame offset is always zero */
64         int is_user_mode = user_mode(regs);
65
66         /* pc (current function) has already been added */
67
68         if (!is_user_mode)
69                 return;
70
71         /* Add the lr (parent function), entry preamble may not have
72          * executed
73          */
74         gator_add_trace(cpu, lr);
75
76         /* check fp is valid */
77         if (fp == 0 || fp < sp)
78                 return;
79
80         /* Get the current stack frame */
81         curr = (struct stack_frame_eabi *)(fp - gcc_frame_offset);
82         if ((unsigned long)curr & 3)
83                 return;
84
85         while (depth-- && curr) {
86                 if (!access_ok(VERIFY_READ, curr, sizeof(struct stack_frame_eabi)) ||
87                                 __copy_from_user_inatomic(&bufcurr, curr, sizeof(struct stack_frame_eabi))) {
88                         return;
89                 }
90
91                 fp = (is_compat ? bufcurr.fp_32 : bufcurr.fp);
92                 lr = (is_compat ? bufcurr.lr_32 : bufcurr.lr);
93
94 #define calc_next(reg) ((reg) - gcc_frame_offset)
95                 /* Returns true if reg is a valid fp */
96 #define validate_next(reg, curr) \
97                 ((reg) != 0 && (calc_next(reg) & 3) == 0 && (unsigned long)(curr) < calc_next(reg))
98
99                 /* Try lr from the stack as the fp because gcc leaf functions do
100                  * not push lr. If gcc_frame_offset is non-zero, the lr will also
101                  * be the clang fp. This assumes code is at a lower address than
102                  * the stack
103                  */
104                 if (validate_next(lr, curr)) {
105                         fp = lr;
106                         lr = (is_compat ? bufcurr.lr2_32 : bufcurr.lr2);
107                 }
108
109                 gator_add_trace(cpu, lr);
110
111                 if (!validate_next(fp, curr))
112                         return;
113
114                 /* Move to the next stack frame */
115                 curr = (struct stack_frame_eabi *)calc_next(fp);
116         }
117 #endif
118 }
119
120 #if defined(__arm__) || defined(__aarch64__)
121 static int report_trace(struct stackframe *frame, void *d)
122 {
123         unsigned int *depth = d, cookie = NO_COOKIE;
124         unsigned long addr = frame->pc;
125
126         if (*depth) {
127 #if defined(MODULE)
128                 unsigned int cpu = get_physical_cpu();
129                 struct module *mod = __module_address(addr);
130
131                 if (mod) {
132                         cookie = get_cookie(cpu, current, mod->name, false);
133                         addr = addr - (unsigned long)mod->module_core;
134                 }
135 #endif
136                 marshal_backtrace(addr & ~1, cookie, 1);
137                 (*depth)--;
138         }
139
140         return *depth == 0;
141 }
142 #endif
143
144 /* Uncomment the following line to enable kernel stack unwinding within gator, note it can also be defined from the Makefile */
145 /* #define GATOR_KERNEL_STACK_UNWINDING */
146
147 #if (defined(__arm__) || defined(__aarch64__)) && !defined(GATOR_KERNEL_STACK_UNWINDING)
148 /* Disabled by default */
149 MODULE_PARM_DESC(kernel_stack_unwinding, "Allow kernel stack unwinding.");
150 static bool kernel_stack_unwinding;
151 module_param(kernel_stack_unwinding, bool, 0644);
152 #endif
153
154 static void kernel_backtrace(int cpu, struct pt_regs *const regs)
155 {
156 #if defined(__arm__) || defined(__aarch64__)
157 #ifdef GATOR_KERNEL_STACK_UNWINDING
158         int depth = gator_backtrace_depth;
159 #else
160         int depth = (kernel_stack_unwinding ? gator_backtrace_depth : 1);
161 #endif
162         struct stackframe frame;
163
164         if (depth == 0)
165                 depth = 1;
166 #if defined(__arm__)
167         frame.fp = regs->ARM_fp;
168         frame.sp = regs->ARM_sp;
169         frame.lr = regs->ARM_lr;
170         frame.pc = regs->ARM_pc;
171 #else
172         frame.fp = regs->regs[29];
173         frame.sp = regs->sp;
174         frame.pc = regs->pc;
175 #endif
176         walk_stackframe(&frame, report_trace, &depth);
177 #else
178         marshal_backtrace(PC_REG & ~1, NO_COOKIE, 1);
179 #endif
180 }
181
182 static void gator_add_sample(int cpu, struct pt_regs *const regs, u64 time)
183 {
184         bool in_kernel;
185         unsigned long exec_cookie;
186
187         if (!regs)
188                 return;
189
190         in_kernel = !user_mode(regs);
191         exec_cookie = get_exec_cookie(cpu, current);
192
193         if (!marshal_backtrace_header(exec_cookie, current->tgid, current->pid, time))
194                 return;
195
196         if (in_kernel) {
197                 kernel_backtrace(cpu, regs);
198         } else {
199                 /* Cookie+PC */
200                 gator_add_trace(cpu, PC_REG);
201
202                 /* Backtrace */
203                 if (gator_backtrace_depth)
204                         arm_backtrace_eabi(cpu, regs, gator_backtrace_depth);
205         }
206
207         marshal_backtrace_footer(time);
208 }