2 * Copyright (C) ARM Limited 2010-2013. 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 arm_backtrace_eabi(int cpu, struct pt_regs *const regs, unsigned int depth)
35 #if defined(__arm__) || defined(__aarch64__)
36 struct stack_frame_eabi *curr;
37 struct stack_frame_eabi bufcurr;
39 const bool is_compat = false;
40 unsigned long fp = regs->ARM_fp;
41 unsigned long sp = regs->ARM_sp;
42 unsigned long lr = regs->ARM_lr;
43 const int gcc_frame_offset = sizeof(unsigned long);
45 // Is userspace aarch32 (32 bit)
46 const bool is_compat = compat_user_mode(regs);
47 unsigned long fp = (is_compat ? regs->regs[11] : regs->regs[29]);
48 unsigned long sp = (is_compat ? regs->compat_sp : regs->sp);
49 unsigned long lr = (is_compat ? regs->compat_lr : regs->regs[30]);
50 const int gcc_frame_offset = (is_compat ? sizeof(u32) : 0);
52 // clang frame offset is always zero
53 int is_user_mode = user_mode(regs);
55 // pc (current function) has already been added
61 // Add the lr (parent function)
62 // entry preamble may not have executed
63 gator_add_trace(cpu, lr);
66 if (fp == 0 || fp < sp) {
70 // Get the current stack frame
71 curr = (struct stack_frame_eabi *)(fp - gcc_frame_offset);
72 if ((unsigned long)curr & 3) {
76 while (depth-- && curr) {
77 if (!access_ok(VERIFY_READ, curr, sizeof(struct stack_frame_eabi)) ||
78 __copy_from_user_inatomic(&bufcurr, curr, sizeof(struct stack_frame_eabi))) {
82 fp = (is_compat ? bufcurr.fp_32 : bufcurr.fp);
83 lr = (is_compat ? bufcurr.lr_32 : bufcurr.lr);
85 #define calc_next(reg) ((reg) - gcc_frame_offset)
86 // Returns true if reg is a valid fp
87 #define validate_next(reg, curr) \
88 ((reg) != 0 && (calc_next(reg) & 3) == 0 && (unsigned long)(curr) < calc_next(reg))
90 // Try lr from the stack as the fp because gcc leaf functions do not push lr
91 // If gcc_frame_offset is non-zero, the lr will also be the clang fp
92 // This assumes code is at a lower address than the stack
93 if (validate_next(lr, curr)) {
95 lr = (is_compat ? bufcurr.lr2_32 : bufcurr.lr2);
98 gator_add_trace(cpu, lr);
100 if (!validate_next(fp, curr)) {
104 // Move to the next stack frame
105 curr = (struct stack_frame_eabi *)calc_next(fp);
110 #if defined(__arm__) || defined(__aarch64__)
111 static int report_trace(struct stackframe *frame, void *d)
113 unsigned int *depth = d, cookie = NO_COOKIE;
114 unsigned long addr = frame->pc;
118 unsigned int cpu = get_physical_cpu();
119 struct module *mod = __module_address(addr);
121 cookie = get_cookie(cpu, current, mod->name, false);
122 addr = addr - (unsigned long)mod->module_core;
125 marshal_backtrace(addr & ~1, cookie);
133 // Uncomment the following line to enable kernel stack unwinding within gator, note it can also be defined from the Makefile
134 // #define GATOR_KERNEL_STACK_UNWINDING
135 static void kernel_backtrace(int cpu, struct pt_regs *const regs)
137 #if defined(__arm__) || defined(__aarch64__)
138 #ifdef GATOR_KERNEL_STACK_UNWINDING
139 int depth = gator_backtrace_depth;
143 struct stackframe frame;
147 frame.fp = regs->ARM_fp;
148 frame.sp = regs->ARM_sp;
149 frame.lr = regs->ARM_lr;
150 frame.pc = regs->ARM_pc;
152 frame.fp = regs->regs[29];
156 walk_stackframe(&frame, report_trace, &depth);
158 marshal_backtrace(PC_REG & ~1, NO_COOKIE);