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
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) {
42 marshal_backtrace(offset & ~1, cookie, 0);
45 static void arm_backtrace_eabi(int cpu, struct pt_regs *const regs, unsigned int depth)
47 #if defined(__arm__) || defined(__aarch64__)
48 struct stack_frame_eabi *curr;
49 struct stack_frame_eabi bufcurr;
51 const bool is_compat = false;
52 unsigned long fp = regs->ARM_fp;
53 unsigned long sp = regs->ARM_sp;
54 unsigned long lr = regs->ARM_lr;
55 const int gcc_frame_offset = sizeof(unsigned long);
57 // Is userspace aarch32 (32 bit)
58 const bool is_compat = compat_user_mode(regs);
59 unsigned long fp = (is_compat ? regs->regs[11] : regs->regs[29]);
60 unsigned long sp = (is_compat ? regs->compat_sp : regs->sp);
61 unsigned long lr = (is_compat ? regs->compat_lr : regs->regs[30]);
62 const int gcc_frame_offset = (is_compat ? sizeof(u32) : 0);
64 // clang frame offset is always zero
65 int is_user_mode = user_mode(regs);
67 // pc (current function) has already been added
73 // Add the lr (parent function)
74 // entry preamble may not have executed
75 gator_add_trace(cpu, lr);
78 if (fp == 0 || fp < sp) {
82 // Get the current stack frame
83 curr = (struct stack_frame_eabi *)(fp - gcc_frame_offset);
84 if ((unsigned long)curr & 3) {
88 while (depth-- && curr) {
89 if (!access_ok(VERIFY_READ, curr, sizeof(struct stack_frame_eabi)) ||
90 __copy_from_user_inatomic(&bufcurr, curr, sizeof(struct stack_frame_eabi))) {
94 fp = (is_compat ? bufcurr.fp_32 : bufcurr.fp);
95 lr = (is_compat ? bufcurr.lr_32 : bufcurr.lr);
97 #define calc_next(reg) ((reg) - gcc_frame_offset)
98 // Returns true if reg is a valid fp
99 #define validate_next(reg, curr) \
100 ((reg) != 0 && (calc_next(reg) & 3) == 0 && (unsigned long)(curr) < calc_next(reg))
102 // Try lr from the stack as the fp because gcc leaf functions do not push lr
103 // If gcc_frame_offset is non-zero, the lr will also be the clang fp
104 // This assumes code is at a lower address than the stack
105 if (validate_next(lr, curr)) {
107 lr = (is_compat ? bufcurr.lr2_32 : bufcurr.lr2);
110 gator_add_trace(cpu, lr);
112 if (!validate_next(fp, curr)) {
116 // Move to the next stack frame
117 curr = (struct stack_frame_eabi *)calc_next(fp);
122 #if defined(__arm__) || defined(__aarch64__)
123 static int report_trace(struct stackframe *frame, void *d)
125 unsigned int *depth = d, cookie = NO_COOKIE;
126 unsigned long addr = frame->pc;
130 unsigned int cpu = get_physical_cpu();
131 struct module *mod = __module_address(addr);
133 cookie = get_cookie(cpu, current, mod->name, false);
134 addr = addr - (unsigned long)mod->module_core;
137 marshal_backtrace(addr & ~1, cookie, 1);
145 // Uncomment the following line to enable kernel stack unwinding within gator, note it can also be defined from the Makefile
146 // #define GATOR_KERNEL_STACK_UNWINDING
148 #if (defined(__arm__) || defined(__aarch64__)) && !defined(GATOR_KERNEL_STACK_UNWINDING)
149 // Disabled by default
150 MODULE_PARM_DESC(kernel_stack_unwinding, "Allow kernel stack unwinding.");
151 static bool kernel_stack_unwinding = 0;
152 module_param(kernel_stack_unwinding, bool, 0644);
155 static void kernel_backtrace(int cpu, struct pt_regs *const regs)
157 #if defined(__arm__) || defined(__aarch64__)
158 #ifdef GATOR_KERNEL_STACK_UNWINDING
159 int depth = gator_backtrace_depth;
161 int depth = (kernel_stack_unwinding ? gator_backtrace_depth : 1);
163 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);