Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 7 | #include <mapmem.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 8 | #include <time.h> |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 9 | #include <trace.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm/sections.h> |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Marek BehĂșn | 236f2ec | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 16 | static char trace_enabled __section(".data"); |
| 17 | static char trace_inited __section(".data"); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 18 | |
| 19 | /* The header block at the start of the trace memory area */ |
| 20 | struct trace_hdr { |
| 21 | int func_count; /* Total number of function call sites */ |
| 22 | u64 call_count; /* Total number of tracked function calls */ |
| 23 | u64 untracked_count; /* Total number of untracked function calls */ |
| 24 | int funcs_used; /* Total number of functions used */ |
| 25 | |
| 26 | /* |
| 27 | * Call count for each function. This is indexed by the word offset |
| 28 | * of the function from gd->relocaddr |
| 29 | */ |
| 30 | uintptr_t *call_accum; |
| 31 | |
| 32 | /* Function trace list */ |
| 33 | struct trace_call *ftrace; /* The function call records */ |
| 34 | ulong ftrace_size; /* Num. of ftrace records we have space for */ |
| 35 | ulong ftrace_count; /* Num. of ftrace records written */ |
| 36 | ulong ftrace_too_deep_count; /* Functions that were too deep */ |
| 37 | |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame] | 38 | int depth; /* Depth of function calls */ |
| 39 | int depth_limit; /* Depth limit to trace to */ |
| 40 | int max_depth; /* Maximum depth seen so far */ |
| 41 | int min_depth; /* Minimum depth seen so far */ |
Simon Glass | 852d4db | 2023-01-15 14:15:51 -0700 | [diff] [blame] | 42 | bool trace_locked; /* Used to detect recursive tracing */ |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Simon Glass | bebc141 | 2022-12-21 16:08:22 -0700 | [diff] [blame] | 45 | /* Pointer to start of trace buffer */ |
| 46 | static struct trace_hdr *hdr __section(".data"); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 47 | |
| 48 | static inline uintptr_t __attribute__((no_instrument_function)) |
| 49 | func_ptr_to_num(void *func_ptr) |
| 50 | { |
| 51 | uintptr_t offset = (uintptr_t)func_ptr; |
| 52 | |
| 53 | #ifdef CONFIG_SANDBOX |
Shiji Yang | ccea96f | 2023-08-03 09:47:17 +0800 | [diff] [blame] | 54 | offset -= (uintptr_t)_init; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 55 | #else |
| 56 | if (gd->flags & GD_FLG_RELOC) |
| 57 | offset -= gd->relocaddr; |
| 58 | else |
Simon Glass | 9846390 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 59 | offset -= CONFIG_TEXT_BASE; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 60 | #endif |
| 61 | return offset / FUNC_SITE_SIZE; |
| 62 | } |
| 63 | |
Heinrich Schuchardt | d3d6afa | 2020-10-15 12:30:09 +0200 | [diff] [blame] | 64 | #if defined(CONFIG_EFI_LOADER) && (defined(CONFIG_ARM) || defined(CONFIG_RISCV)) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * trace_gd - the value of the gd register |
| 68 | */ |
Heinrich Schuchardt | 7f642cb | 2020-05-27 20:04:22 +0200 | [diff] [blame] | 69 | static volatile gd_t *trace_gd; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * trace_save_gd() - save the value of the gd register |
| 73 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 74 | static void notrace trace_save_gd(void) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 75 | { |
| 76 | trace_gd = gd; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * trace_swap_gd() - swap between U-Boot and application gd register value |
| 81 | * |
| 82 | * An UEFI application may change the value of the register that gd lives in. |
| 83 | * But some of our functions like get_ticks() access this register. So we |
| 84 | * have to set the gd register to the U-Boot value when entering a trace |
| 85 | * point and set it back to the application value when exiting the trace point. |
| 86 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 87 | static void notrace trace_swap_gd(void) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 88 | { |
Heinrich Schuchardt | 7f642cb | 2020-05-27 20:04:22 +0200 | [diff] [blame] | 89 | volatile gd_t *temp_gd = trace_gd; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 90 | |
| 91 | trace_gd = gd; |
Heinrich Schuchardt | 7f642cb | 2020-05-27 20:04:22 +0200 | [diff] [blame] | 92 | set_gd(temp_gd); |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | #else |
| 96 | |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 97 | static void notrace trace_save_gd(void) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 98 | { |
| 99 | } |
| 100 | |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 101 | static void notrace trace_swap_gd(void) |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 102 | { |
| 103 | } |
| 104 | |
| 105 | #endif |
| 106 | |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 107 | static void notrace add_ftrace(void *func_ptr, void *caller, ulong flags) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 108 | { |
| 109 | if (hdr->depth > hdr->depth_limit) { |
| 110 | hdr->ftrace_too_deep_count++; |
| 111 | return; |
| 112 | } |
| 113 | if (hdr->ftrace_count < hdr->ftrace_size) { |
| 114 | struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count]; |
| 115 | |
| 116 | rec->func = func_ptr_to_num(func_ptr); |
| 117 | rec->caller = func_ptr_to_num(caller); |
| 118 | rec->flags = flags | (timer_get_us() & FUNCF_TIMESTAMP_MASK); |
| 119 | } |
| 120 | hdr->ftrace_count++; |
| 121 | } |
| 122 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 123 | /** |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 124 | * __cyg_profile_func_enter() - record function entry |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 125 | * |
| 126 | * We add to our tally for this function and add to the list of called |
| 127 | * functions. |
| 128 | * |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 129 | * @func_ptr: pointer to function being entered |
| 130 | * @caller: pointer to function which called this function |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 131 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 132 | void notrace __cyg_profile_func_enter(void *func_ptr, void *caller) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 133 | { |
| 134 | if (trace_enabled) { |
| 135 | int func; |
| 136 | |
Simon Glass | 852d4db | 2023-01-15 14:15:51 -0700 | [diff] [blame] | 137 | if (hdr->trace_locked) { |
| 138 | trace_enabled = 0; |
| 139 | puts("trace: recursion detected, disabling\n"); |
| 140 | hdr->trace_locked = false; |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | hdr->trace_locked = true; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 145 | trace_swap_gd(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 146 | add_ftrace(func_ptr, caller, FUNCF_ENTRY); |
| 147 | func = func_ptr_to_num(func_ptr); |
| 148 | if (func < hdr->func_count) { |
| 149 | hdr->call_accum[func]++; |
| 150 | hdr->call_count++; |
| 151 | } else { |
| 152 | hdr->untracked_count++; |
| 153 | } |
| 154 | hdr->depth++; |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame] | 155 | if (hdr->depth > hdr->max_depth) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 156 | hdr->max_depth = hdr->depth; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 157 | trace_swap_gd(); |
Simon Glass | 852d4db | 2023-01-15 14:15:51 -0700 | [diff] [blame] | 158 | hdr->trace_locked = false; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 163 | * __cyg_profile_func_exit() - record function exit |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 164 | * |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 165 | * @func_ptr: pointer to function being entered |
| 166 | * @caller: pointer to function which called this function |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 167 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 168 | void notrace __cyg_profile_func_exit(void *func_ptr, void *caller) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 169 | { |
| 170 | if (trace_enabled) { |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 171 | trace_swap_gd(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 172 | hdr->depth--; |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame] | 173 | add_ftrace(func_ptr, caller, FUNCF_EXIT); |
| 174 | if (hdr->depth < hdr->min_depth) |
| 175 | hdr->min_depth = hdr->depth; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 176 | trace_swap_gd(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 181 | * trace_list_functions() - produce a list of called functions |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 182 | * |
| 183 | * The information is written into the supplied buffer - a header followed |
| 184 | * by a list of function records. |
| 185 | * |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 186 | * @buff: buffer to place list into |
| 187 | * @buff_size: size of buffer |
| 188 | * @needed: returns size of buffer needed, which may be |
| 189 | * greater than buff_size if we ran out of space. |
| 190 | * Return: 0 if ok, -ENOSPC if space was exhausted |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 191 | */ |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 192 | int trace_list_functions(void *buff, size_t buff_size, size_t *needed) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 193 | { |
| 194 | struct trace_output_hdr *output_hdr = NULL; |
| 195 | void *end, *ptr = buff; |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 196 | size_t func; |
| 197 | size_t upto; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 198 | |
| 199 | end = buff ? buff + buff_size : NULL; |
| 200 | |
| 201 | /* Place some header information */ |
| 202 | if (ptr + sizeof(struct trace_output_hdr) < end) |
| 203 | output_hdr = ptr; |
| 204 | ptr += sizeof(struct trace_output_hdr); |
| 205 | |
| 206 | /* Add information about each function */ |
| 207 | for (func = upto = 0; func < hdr->func_count; func++) { |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 208 | size_t calls = hdr->call_accum[func]; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 209 | |
| 210 | if (!calls) |
| 211 | continue; |
| 212 | |
| 213 | if (ptr + sizeof(struct trace_output_func) < end) { |
| 214 | struct trace_output_func *stats = ptr; |
| 215 | |
| 216 | stats->offset = func * FUNC_SITE_SIZE; |
| 217 | stats->call_count = calls; |
| 218 | upto++; |
| 219 | } |
| 220 | ptr += sizeof(struct trace_output_func); |
| 221 | } |
| 222 | |
| 223 | /* Update the header */ |
| 224 | if (output_hdr) { |
| 225 | output_hdr->rec_count = upto; |
| 226 | output_hdr->type = TRACE_CHUNK_FUNCS; |
| 227 | } |
| 228 | |
| 229 | /* Work out how must of the buffer we used */ |
| 230 | *needed = ptr - buff; |
| 231 | if (ptr > end) |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 232 | return -ENOSPC; |
| 233 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 237 | /** |
| 238 | * trace_list_functions() - produce a list of function calls |
| 239 | * |
| 240 | * The information is written into the supplied buffer - a header followed |
| 241 | * by a list of function records. |
| 242 | * |
| 243 | * @buff: buffer to place list into |
| 244 | * @buff_size: size of buffer |
| 245 | * @needed: returns size of buffer needed, which may be |
| 246 | * greater than buff_size if we ran out of space. |
| 247 | * Return: 0 if ok, -ENOSPC if space was exhausted |
| 248 | */ |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 249 | int trace_list_calls(void *buff, size_t buff_size, size_t *needed) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 250 | { |
| 251 | struct trace_output_hdr *output_hdr = NULL; |
| 252 | void *end, *ptr = buff; |
Heinrich Schuchardt | 2b7a388 | 2019-06-14 21:50:55 +0200 | [diff] [blame] | 253 | size_t rec, upto; |
| 254 | size_t count; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 255 | |
| 256 | end = buff ? buff + buff_size : NULL; |
| 257 | |
| 258 | /* Place some header information */ |
| 259 | if (ptr + sizeof(struct trace_output_hdr) < end) |
| 260 | output_hdr = ptr; |
| 261 | ptr += sizeof(struct trace_output_hdr); |
| 262 | |
| 263 | /* Add information about each call */ |
| 264 | count = hdr->ftrace_count; |
| 265 | if (count > hdr->ftrace_size) |
| 266 | count = hdr->ftrace_size; |
| 267 | for (rec = upto = 0; rec < count; rec++) { |
| 268 | if (ptr + sizeof(struct trace_call) < end) { |
| 269 | struct trace_call *call = &hdr->ftrace[rec]; |
| 270 | struct trace_call *out = ptr; |
| 271 | |
| 272 | out->func = call->func * FUNC_SITE_SIZE; |
| 273 | out->caller = call->caller * FUNC_SITE_SIZE; |
| 274 | out->flags = call->flags; |
| 275 | upto++; |
| 276 | } |
| 277 | ptr += sizeof(struct trace_call); |
| 278 | } |
| 279 | |
| 280 | /* Update the header */ |
| 281 | if (output_hdr) { |
Simon Glass | d9044e5 | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 282 | memset(output_hdr, '\0', sizeof(*output_hdr)); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 283 | output_hdr->rec_count = upto; |
| 284 | output_hdr->type = TRACE_CHUNK_CALLS; |
Simon Glass | d9044e5 | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 285 | output_hdr->version = TRACE_VERSION; |
| 286 | output_hdr->text_base = CONFIG_TEXT_BASE; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* Work out how must of the buffer we used */ |
| 290 | *needed = ptr - buff; |
| 291 | if (ptr > end) |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 292 | return -ENOSPC; |
| 293 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 297 | /** |
| 298 | * trace_print_stats() - print basic information about tracing |
| 299 | */ |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 300 | void trace_print_stats(void) |
| 301 | { |
| 302 | ulong count; |
| 303 | |
| 304 | #ifndef FTRACE |
| 305 | puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n"); |
| 306 | puts("You will likely get zeroed data here\n"); |
| 307 | #endif |
| 308 | if (!trace_inited) { |
| 309 | printf("Trace is disabled\n"); |
| 310 | return; |
| 311 | } |
| 312 | print_grouped_ull(hdr->func_count, 10); |
| 313 | puts(" function sites\n"); |
| 314 | print_grouped_ull(hdr->call_count, 10); |
| 315 | puts(" function calls\n"); |
| 316 | print_grouped_ull(hdr->untracked_count, 10); |
| 317 | puts(" untracked function calls\n"); |
| 318 | count = min(hdr->ftrace_count, hdr->ftrace_size); |
| 319 | print_grouped_ull(count, 10); |
| 320 | puts(" traced function calls"); |
| 321 | if (hdr->ftrace_count > hdr->ftrace_size) { |
| 322 | printf(" (%lu dropped due to overflow)", |
| 323 | hdr->ftrace_count - hdr->ftrace_size); |
| 324 | } |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame] | 325 | |
| 326 | /* Add in minimum depth since the trace did not start at top level */ |
| 327 | printf("\n%15d maximum observed call depth\n", |
| 328 | hdr->max_depth - hdr->min_depth); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 329 | printf("%15d call depth limit\n", hdr->depth_limit); |
| 330 | print_grouped_ull(hdr->ftrace_too_deep_count, 10); |
| 331 | puts(" calls not traced due to depth\n"); |
Simon Glass | 9dd665a | 2023-01-15 14:15:49 -0700 | [diff] [blame] | 332 | print_grouped_ull(hdr->ftrace_size, 10); |
| 333 | puts(" max function calls\n"); |
| 334 | printf("\ntrace buffer %lx call records %lx\n", |
| 335 | (ulong)map_to_sysmem(hdr), (ulong)map_to_sysmem(hdr->ftrace)); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 338 | void notrace trace_set_enabled(int enabled) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 339 | { |
| 340 | trace_enabled = enabled != 0; |
| 341 | } |
| 342 | |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 343 | static int get_func_count(void) |
| 344 | { |
| 345 | /* Detect no support for mon_len since this means tracing cannot work */ |
| 346 | if (IS_ENABLED(CONFIG_SANDBOX) && !gd->mon_len) { |
| 347 | puts("Tracing is not supported on this board\n"); |
| 348 | return -ENOTSUPP; |
| 349 | } |
| 350 | |
| 351 | return gd->mon_len / FUNC_SITE_SIZE; |
| 352 | } |
| 353 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 354 | /** |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 355 | * trace_init() - initialize the tracing system and enable it |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 356 | * |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 357 | * @buff: Pointer to trace buffer |
| 358 | * @buff_size: Size of trace buffer |
| 359 | * Return: 0 if ok |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 360 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 361 | int notrace trace_init(void *buff, size_t buff_size) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 362 | { |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 363 | int func_count = get_func_count(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 364 | size_t needed; |
| 365 | int was_disabled = !trace_enabled; |
| 366 | |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 367 | if (func_count < 0) |
| 368 | return func_count; |
Heinrich Schuchardt | a2fa38d | 2019-06-02 13:05:08 +0200 | [diff] [blame] | 369 | trace_save_gd(); |
| 370 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 371 | if (!was_disabled) { |
| 372 | #ifdef CONFIG_TRACE_EARLY |
Simon Glass | 80f9155 | 2023-01-15 14:15:50 -0700 | [diff] [blame] | 373 | ulong used, count; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 374 | char *end; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * Copy over the early trace data if we have it. Disable |
| 378 | * tracing while we are doing this. |
| 379 | */ |
| 380 | trace_enabled = 0; |
| 381 | hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, |
| 382 | CONFIG_TRACE_EARLY_SIZE); |
Simon Glass | 80f9155 | 2023-01-15 14:15:50 -0700 | [diff] [blame] | 383 | count = min(hdr->ftrace_count, hdr->ftrace_size); |
| 384 | end = (char *)&hdr->ftrace[count]; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 385 | used = end - (char *)hdr; |
| 386 | printf("trace: copying %08lx bytes of early data from %x to %08lx\n", |
| 387 | used, CONFIG_TRACE_EARLY_ADDR, |
| 388 | (ulong)map_to_sysmem(buff)); |
Simon Glass | 80f9155 | 2023-01-15 14:15:50 -0700 | [diff] [blame] | 389 | printf("%lu traced function calls", count); |
| 390 | if (hdr->ftrace_count > hdr->ftrace_size) { |
| 391 | printf(" (%lu dropped due to overflow)", |
| 392 | hdr->ftrace_count - hdr->ftrace_size); |
| 393 | hdr->ftrace_count = hdr->ftrace_size; |
| 394 | } |
| 395 | puts("\n"); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 396 | memcpy(buff, hdr, used); |
| 397 | #else |
| 398 | puts("trace: already enabled\n"); |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 399 | return -EALREADY; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 400 | #endif |
| 401 | } |
| 402 | hdr = (struct trace_hdr *)buff; |
| 403 | needed = sizeof(*hdr) + func_count * sizeof(uintptr_t); |
| 404 | if (needed > buff_size) { |
Simon Glass | 9dd665a | 2023-01-15 14:15:49 -0700 | [diff] [blame] | 405 | printf("trace: buffer size %zx bytes: at least %zx needed\n", |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 406 | buff_size, needed); |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 407 | return -ENOSPC; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame] | 410 | if (was_disabled) { |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 411 | memset(hdr, '\0', needed); |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame] | 412 | hdr->min_depth = INT_MAX; |
| 413 | } |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 414 | hdr->func_count = func_count; |
| 415 | hdr->call_accum = (uintptr_t *)(hdr + 1); |
| 416 | |
| 417 | /* Use any remaining space for the timed function trace */ |
| 418 | hdr->ftrace = (struct trace_call *)(buff + needed); |
| 419 | hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace); |
Simon Glass | d9044e5 | 2023-01-15 14:15:46 -0700 | [diff] [blame] | 420 | hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 421 | |
| 422 | puts("trace: enabled\n"); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 423 | trace_enabled = 1; |
| 424 | trace_inited = 1; |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 425 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | #ifdef CONFIG_TRACE_EARLY |
Heinrich Schuchardt | e605ab8 | 2020-01-01 15:52:31 +0100 | [diff] [blame] | 430 | /** |
| 431 | * trace_early_init() - initialize the tracing system for early tracing |
| 432 | * |
| 433 | * Return: 0 if ok, -ENOSPC if not enough memory is available |
| 434 | */ |
Simon Glass | 33c60a3 | 2022-12-21 16:08:15 -0700 | [diff] [blame] | 435 | int notrace trace_early_init(void) |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 436 | { |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 437 | int func_count = get_func_count(); |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 438 | size_t buff_size = CONFIG_TRACE_EARLY_SIZE; |
| 439 | size_t needed; |
| 440 | |
Simon Glass | c3d9181 | 2023-01-15 14:15:47 -0700 | [diff] [blame] | 441 | if (func_count < 0) |
| 442 | return func_count; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 443 | /* We can ignore additional calls to this function */ |
| 444 | if (trace_enabled) |
| 445 | return 0; |
| 446 | |
| 447 | hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE); |
| 448 | needed = sizeof(*hdr) + func_count * sizeof(uintptr_t); |
| 449 | if (needed > buff_size) { |
Simon Glass | 9dd665a | 2023-01-15 14:15:49 -0700 | [diff] [blame] | 450 | printf("trace: buffer size is %zx bytes, at least %zx needed\n", |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 451 | buff_size, needed); |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 452 | return -ENOSPC; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | memset(hdr, '\0', needed); |
| 456 | hdr->call_accum = (uintptr_t *)(hdr + 1); |
| 457 | hdr->func_count = func_count; |
Simon Glass | daca66d | 2023-01-15 14:15:48 -0700 | [diff] [blame] | 458 | hdr->min_depth = INT_MAX; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 459 | |
| 460 | /* Use any remaining space for the timed function trace */ |
| 461 | hdr->ftrace = (struct trace_call *)((char *)hdr + needed); |
| 462 | hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace); |
Heinrich Schuchardt | da0fb5f | 2019-06-02 13:30:09 +0200 | [diff] [blame] | 463 | hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT; |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 464 | printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR); |
| 465 | |
| 466 | trace_enabled = 1; |
Simon Glass | f564d09 | 2019-04-08 13:20:50 -0600 | [diff] [blame] | 467 | |
Simon Glass | b2e16a8 | 2013-06-11 11:14:39 -0700 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | #endif |