| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdint.h> |
| 10 |
|
| 11 |
#include <arch_helpers.h> |
| 12 |
#include <common/debug.h> |
| 13 |
#include <drivers/console.h> |
| 14 |
|
| 15 |
|
| 16 |
#define UNWIND_LIMIT 20U |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
struct frame_record { |
| 34 |
|
| 35 |
struct frame_record *parent; |
| 36 |
|
| 37 |
uintptr_t return_addr; |
| 38 |
}; |
| 39 |
|
| 40 |
static inline uintptr_t extract_address(uintptr_t address) |
| 41 |
{ |
| 42 |
uintptr_t ret = address; |
| 43 |
|
| 44 |
#if ENABLE_PAUTH |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
xpaci(ret); |
| 52 |
#endif |
| 53 |
|
| 54 |
return ret; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#ifdef __aarch64__ |
| 62 |
static bool is_address_readable(uintptr_t address) |
| 63 |
{ |
| 64 |
unsigned int el = get_current_el(); |
| 65 |
uintptr_t addr = extract_address(address); |
| 66 |
|
| 67 |
if (el == 3U) { |
| 68 |
ats1e3r(addr); |
| 69 |
} else if (el == 2U) { |
| 70 |
ats1e2r(addr); |
| 71 |
} else { |
| 72 |
AT(ats1e1r, addr); |
| 73 |
} |
| 74 |
|
| 75 |
isb(); |
| 76 |
|
| 77 |
|
| 78 |
if ((read_par_el1() & PAR_F_MASK) != 0U) |
| 79 |
return false; |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
#else |
| 84 |
static bool is_address_readable(uintptr_t addr) |
| 85 |
{ |
| 86 |
unsigned int el = get_current_el(); |
| 87 |
|
| 88 |
if (el == 3U) { |
| 89 |
write_ats1cpr(addr); |
| 90 |
} else if (el == 2U) { |
| 91 |
write_ats1hr(addr); |
| 92 |
} else { |
| 93 |
write_ats1cpr(addr); |
| 94 |
} |
| 95 |
|
| 96 |
isb(); |
| 97 |
|
| 98 |
|
| 99 |
if ((read64_par() & PAR_F_MASK) != 0U) |
| 100 |
return false; |
| 101 |
|
| 102 |
return true; |
| 103 |
} |
| 104 |
#endif |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static bool is_valid_object(uintptr_t addr, size_t size) |
| 111 |
{ |
| 112 |
assert(size > 0U); |
| 113 |
|
| 114 |
if (addr == 0U) |
| 115 |
return false; |
| 116 |
|
| 117 |
|
| 118 |
if ((addr + size) < addr) |
| 119 |
return false; |
| 120 |
|
| 121 |
|
| 122 |
if ((addr & (sizeof(uintptr_t) - 1U)) != 0U) |
| 123 |
return false; |
| 124 |
|
| 125 |
|
| 126 |
for (size_t i = 0; i < size; i++) { |
| 127 |
if (!is_address_readable(addr + i)) |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static bool is_valid_jump_address(uintptr_t addr) |
| 139 |
{ |
| 140 |
if (addr == 0U) |
| 141 |
return false; |
| 142 |
|
| 143 |
|
| 144 |
if ((addr & (sizeof(uint32_t) - 1U)) != 0U) |
| 145 |
return false; |
| 146 |
|
| 147 |
if (!is_address_readable(addr)) |
| 148 |
return false; |
| 149 |
|
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static bool is_valid_frame_record(struct frame_record *fr) |
| 157 |
{ |
| 158 |
return is_valid_object((uintptr_t)fr, sizeof(struct frame_record)); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
static struct frame_record *adjust_frame_record(struct frame_record *fr) |
| 166 |
{ |
| 167 |
#ifdef __aarch64__ |
| 168 |
return fr; |
| 169 |
#else |
| 170 |
return (struct frame_record *)((uintptr_t)fr - 4U); |
| 171 |
#endif |
| 172 |
} |
| 173 |
|
| 174 |
static void unwind_stack(struct frame_record *fr, uintptr_t current_pc, |
| 175 |
uintptr_t link_register) |
| 176 |
{ |
| 177 |
uintptr_t call_site; |
| 178 |
static const char *backtrace_str = "%u: %s: 0x%lx\n"; |
| 179 |
const char *el_str = get_el_str(get_current_el()); |
| 180 |
|
| 181 |
if (!is_valid_frame_record(fr)) { |
| 182 |
printf("ERROR: Corrupted frame pointer (frame record address = %p)\n", |
| 183 |
fr); |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
call_site = extract_address(fr->return_addr); |
| 188 |
if (call_site != link_register) { |
| 189 |
printf("ERROR: Corrupted stack (frame record address = %p)\n", |
| 190 |
fr); |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
printf(backtrace_str, 0U, el_str, current_pc); |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
for (unsigned int i = 1U; i < UNWIND_LIMIT; i++) { |
| 202 |
|
| 203 |
if (!is_valid_frame_record(fr)) |
| 204 |
return; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
call_site = extract_address(fr->return_addr) - 4U; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if (!is_valid_jump_address(call_site)) |
| 218 |
return; |
| 219 |
|
| 220 |
printf(backtrace_str, i, el_str, call_site); |
| 221 |
|
| 222 |
fr = adjust_frame_record(fr->parent); |
| 223 |
} |
| 224 |
|
| 225 |
printf("ERROR: Max backtrace depth reached\n"); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
void backtrace(const char *cookie) |
| 252 |
{ |
| 253 |
uintptr_t return_address = (uintptr_t)__builtin_return_address(0U); |
| 254 |
struct frame_record *fr = __builtin_frame_address(0U); |
| 255 |
|
| 256 |
|
| 257 |
console_flush(); |
| 258 |
|
| 259 |
fr = adjust_frame_record(fr); |
| 260 |
|
| 261 |
printf("BACKTRACE: START: %s\n", cookie); |
| 262 |
|
| 263 |
unwind_stack(fr, (uintptr_t)&backtrace, return_address); |
| 264 |
|
| 265 |
printf("BACKTRACE: END: %s\n", cookie); |
| 266 |
} |