2 * Copyright (C) ARM Limited 2010-2014. All rights reserved.
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.
11 * EABI backtrace stores {fp,lr} on the stack.
13 struct stack_frame_eabi {
17 /* May be the fp in the case of a leaf function or clang */
19 /* If lr is really the fp, lr2 is the corresponding lr */
22 /* Used to read 32 bit fp/lr from a 64 bit kernel */
25 /* same as lr above */
27 /* same as lr2 above */
33 static void gator_add_trace(int cpu, unsigned long address)
36 unsigned long cookie = get_address_cookie(cpu, current, address & ~1, &offset);
38 if (cookie == NO_COOKIE || cookie == UNRESOLVED_COOKIE)
41 marshal_backtrace(offset & ~1, cookie, 0);
44 static void arm_backtrace_eabi(int cpu, struct pt_regs *const regs, unsigned int depth)
46 #if defined(__arm__) || defined(__aarch64__)
47 struct stack_frame_eabi *curr;
48 struct stack_frame_eabi bufcurr;
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);
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);
63 /* clang frame offset is always zero */
64 int is_user_mode = user_mode(regs);
66 /* pc (current function) has already been added */
71 /* Add the lr (parent function), entry preamble may not have
74 gator_add_trace(cpu, lr);
76 /* check fp is valid */
77 if (fp == 0 || fp < sp)
80 /* Get the current stack frame */
81 curr = (struct stack_frame_eabi *)(fp - gcc_frame_offset);
82 if ((unsigned long)curr & 3)
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))) {
91 fp = (is_compat ? bufcurr.fp_32 : bufcurr.fp);
92 lr = (is_compat ? bufcurr.lr_32 : bufcurr.lr);
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))
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
104 if (validate_next(lr, curr)) {
106 lr = (is_compat ? bufcurr.lr2_32 : bufcurr.lr2);
109 gator_add_trace(cpu, lr);
111 if (!validate_next(fp, curr))
114 /* Move to the next stack frame */
115 curr = (struct stack_frame_eabi *)calc_next(fp);
120 #if defined(__arm__) || defined(__aarch64__)
121 static int report_trace(struct stackframe *frame, void *d)
123 unsigned int *depth = d, cookie = NO_COOKIE;
124 unsigned long addr = frame->pc;
128 unsigned int cpu = get_physical_cpu();
129 struct module *mod = __module_address(addr);
132 cookie = get_cookie(cpu, current, mod->name, false);
133 addr = addr - (unsigned long)mod->module_core;
136 marshal_backtrace(addr & ~1, cookie, 1);
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 */
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);
154 static void kernel_backtrace(int cpu, struct pt_regs *const regs)
156 #if defined(__arm__) || defined(__aarch64__)
157 #ifdef GATOR_KERNEL_STACK_UNWINDING
158 int depth = gator_backtrace_depth;
160 int depth = (kernel_stack_unwinding ? gator_backtrace_depth : 1);
162 struct stackframe frame;
167 frame.fp = regs->ARM_fp;
168 frame.sp = regs->ARM_sp;
169 frame.lr = regs->ARM_lr;
170 frame.pc = regs->ARM_pc;
172 frame.fp = regs->regs[29];
176 walk_stackframe(&frame, report_trace, &depth);
178 marshal_backtrace(PC_REG & ~1, NO_COOKIE, 1);
182 static void gator_add_sample(int cpu, struct pt_regs *const regs, u64 time)
185 unsigned long exec_cookie;
190 in_kernel = !user_mode(regs);
191 exec_cookie = get_exec_cookie(cpu, current);
193 if (!marshal_backtrace_header(exec_cookie, current->tgid, current->pid, time))
197 kernel_backtrace(cpu, regs);
200 gator_add_trace(cpu, PC_REG);
203 if (gator_backtrace_depth)
204 arm_backtrace_eabi(cpu, regs, gator_backtrace_depth);
207 marshal_backtrace_footer(time);