Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 1 | /** |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame^] | 2 | * Copyright (C) ARM Limited 2010-2014. All rights reserved. |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 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 | |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame^] | 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 | |
| 42 | marshal_backtrace(offset & ~1, cookie, 0); |
| 43 | } |
| 44 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 45 | static void arm_backtrace_eabi(int cpu, struct pt_regs *const regs, unsigned int depth) |
| 46 | { |
| 47 | #if defined(__arm__) || defined(__aarch64__) |
| 48 | struct stack_frame_eabi *curr; |
| 49 | struct stack_frame_eabi bufcurr; |
| 50 | #if defined(__arm__) |
| 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); |
| 56 | #else |
| 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); |
| 63 | #endif |
| 64 | // clang frame offset is always zero |
| 65 | int is_user_mode = user_mode(regs); |
| 66 | |
| 67 | // pc (current function) has already been added |
| 68 | |
| 69 | if (!is_user_mode) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // Add the lr (parent function) |
| 74 | // entry preamble may not have executed |
| 75 | gator_add_trace(cpu, lr); |
| 76 | |
| 77 | // check fp is valid |
| 78 | if (fp == 0 || fp < sp) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | // Get the current stack frame |
| 83 | curr = (struct stack_frame_eabi *)(fp - gcc_frame_offset); |
| 84 | if ((unsigned long)curr & 3) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 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))) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | fp = (is_compat ? bufcurr.fp_32 : bufcurr.fp); |
| 95 | lr = (is_compat ? bufcurr.lr_32 : bufcurr.lr); |
| 96 | |
| 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)) |
| 101 | |
| 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)) { |
| 106 | fp = lr; |
| 107 | lr = (is_compat ? bufcurr.lr2_32 : bufcurr.lr2); |
| 108 | } |
| 109 | |
| 110 | gator_add_trace(cpu, lr); |
| 111 | |
| 112 | if (!validate_next(fp, curr)) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | // Move to the next stack frame |
| 117 | curr = (struct stack_frame_eabi *)calc_next(fp); |
| 118 | } |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | #if defined(__arm__) || defined(__aarch64__) |
| 123 | static int report_trace(struct stackframe *frame, void *d) |
| 124 | { |
| 125 | unsigned int *depth = d, cookie = NO_COOKIE; |
| 126 | unsigned long addr = frame->pc; |
| 127 | |
| 128 | if (*depth) { |
| 129 | #if defined(MODULE) |
| 130 | unsigned int cpu = get_physical_cpu(); |
| 131 | struct module *mod = __module_address(addr); |
| 132 | if (mod) { |
| 133 | cookie = get_cookie(cpu, current, mod->name, false); |
| 134 | addr = addr - (unsigned long)mod->module_core; |
| 135 | } |
| 136 | #endif |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame^] | 137 | marshal_backtrace(addr & ~1, cookie, 1); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 138 | (*depth)--; |
| 139 | } |
| 140 | |
| 141 | return *depth == 0; |
| 142 | } |
| 143 | #endif |
| 144 | |
| 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 |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 147 | |
| 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."); |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame^] | 151 | static bool kernel_stack_unwinding = 0; |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 152 | module_param(kernel_stack_unwinding, bool, 0644); |
| 153 | #endif |
| 154 | |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 155 | static void kernel_backtrace(int cpu, struct pt_regs *const regs) |
| 156 | { |
| 157 | #if defined(__arm__) || defined(__aarch64__) |
| 158 | #ifdef GATOR_KERNEL_STACK_UNWINDING |
| 159 | int depth = gator_backtrace_depth; |
| 160 | #else |
Jon Medhurst | 34d9769 | 2013-12-19 09:23:06 +0000 | [diff] [blame] | 161 | int depth = (kernel_stack_unwinding ? gator_backtrace_depth : 1); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 162 | #endif |
| 163 | struct stackframe frame; |
| 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 |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame^] | 178 | marshal_backtrace(PC_REG & ~1, NO_COOKIE, 1); |
Jon Medhurst | aaf37a3 | 2013-06-11 12:10:56 +0100 | [diff] [blame] | 179 | #endif |
| 180 | } |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame^] | 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 | } |