blob: 3551ef3a23c9080858b7995c493be08b3f86a53e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb2e16a82013-06-11 11:14:39 -07002/*
3 * Copyright (c) 2012 The Chromium OS Authors.
Simon Glassb2e16a82013-06-11 11:14:39 -07004 */
5
6#include <common.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -05007#include <mapmem.h>
Simon Glass10453152019-11-14 12:57:30 -07008#include <time.h>
Simon Glassb2e16a82013-06-11 11:14:39 -07009#include <trace.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Simon Glassb2e16a82013-06-11 11:14:39 -070011#include <asm/io.h>
12#include <asm/sections.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
Marek BehĂșn236f2ec2021-05-20 13:23:52 +020016static char trace_enabled __section(".data");
17static char trace_inited __section(".data");
Simon Glassb2e16a82013-06-11 11:14:39 -070018
19/* The header block at the start of the trace memory area */
20struct 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 Glassdaca66d2023-01-15 14:15:48 -070038 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 Glassb2e16a82013-06-11 11:14:39 -070042};
43
Simon Glassbebc1412022-12-21 16:08:22 -070044/* Pointer to start of trace buffer */
45static struct trace_hdr *hdr __section(".data");
Simon Glassb2e16a82013-06-11 11:14:39 -070046
47static inline uintptr_t __attribute__((no_instrument_function))
48 func_ptr_to_num(void *func_ptr)
49{
50 uintptr_t offset = (uintptr_t)func_ptr;
51
52#ifdef CONFIG_SANDBOX
53 offset -= (uintptr_t)&_init;
54#else
55 if (gd->flags & GD_FLG_RELOC)
56 offset -= gd->relocaddr;
57 else
Simon Glass98463902022-10-20 18:22:39 -060058 offset -= CONFIG_TEXT_BASE;
Simon Glassb2e16a82013-06-11 11:14:39 -070059#endif
60 return offset / FUNC_SITE_SIZE;
61}
62
Heinrich Schuchardtd3d6afa2020-10-15 12:30:09 +020063#if defined(CONFIG_EFI_LOADER) && (defined(CONFIG_ARM) || defined(CONFIG_RISCV))
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020064
65/**
66 * trace_gd - the value of the gd register
67 */
Heinrich Schuchardt7f642cb2020-05-27 20:04:22 +020068static volatile gd_t *trace_gd;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020069
70/**
71 * trace_save_gd() - save the value of the gd register
72 */
Simon Glass33c60a32022-12-21 16:08:15 -070073static void notrace trace_save_gd(void)
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020074{
75 trace_gd = gd;
76}
77
78/**
79 * trace_swap_gd() - swap between U-Boot and application gd register value
80 *
81 * An UEFI application may change the value of the register that gd lives in.
82 * But some of our functions like get_ticks() access this register. So we
83 * have to set the gd register to the U-Boot value when entering a trace
84 * point and set it back to the application value when exiting the trace point.
85 */
Simon Glass33c60a32022-12-21 16:08:15 -070086static void notrace trace_swap_gd(void)
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020087{
Heinrich Schuchardt7f642cb2020-05-27 20:04:22 +020088 volatile gd_t *temp_gd = trace_gd;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020089
90 trace_gd = gd;
Heinrich Schuchardt7f642cb2020-05-27 20:04:22 +020091 set_gd(temp_gd);
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020092}
93
94#else
95
Simon Glass33c60a32022-12-21 16:08:15 -070096static void notrace trace_save_gd(void)
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020097{
98}
99
Simon Glass33c60a32022-12-21 16:08:15 -0700100static void notrace trace_swap_gd(void)
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200101{
102}
103
104#endif
105
Simon Glass33c60a32022-12-21 16:08:15 -0700106static void notrace add_ftrace(void *func_ptr, void *caller, ulong flags)
Simon Glassb2e16a82013-06-11 11:14:39 -0700107{
108 if (hdr->depth > hdr->depth_limit) {
109 hdr->ftrace_too_deep_count++;
110 return;
111 }
112 if (hdr->ftrace_count < hdr->ftrace_size) {
113 struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
114
115 rec->func = func_ptr_to_num(func_ptr);
116 rec->caller = func_ptr_to_num(caller);
117 rec->flags = flags | (timer_get_us() & FUNCF_TIMESTAMP_MASK);
118 }
119 hdr->ftrace_count++;
120}
121
Simon Glassb2e16a82013-06-11 11:14:39 -0700122/**
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100123 * __cyg_profile_func_enter() - record function entry
Simon Glassb2e16a82013-06-11 11:14:39 -0700124 *
125 * We add to our tally for this function and add to the list of called
126 * functions.
127 *
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100128 * @func_ptr: pointer to function being entered
129 * @caller: pointer to function which called this function
Simon Glassb2e16a82013-06-11 11:14:39 -0700130 */
Simon Glass33c60a32022-12-21 16:08:15 -0700131void notrace __cyg_profile_func_enter(void *func_ptr, void *caller)
Simon Glassb2e16a82013-06-11 11:14:39 -0700132{
133 if (trace_enabled) {
134 int func;
135
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200136 trace_swap_gd();
Simon Glassb2e16a82013-06-11 11:14:39 -0700137 add_ftrace(func_ptr, caller, FUNCF_ENTRY);
138 func = func_ptr_to_num(func_ptr);
139 if (func < hdr->func_count) {
140 hdr->call_accum[func]++;
141 hdr->call_count++;
142 } else {
143 hdr->untracked_count++;
144 }
145 hdr->depth++;
Simon Glassdaca66d2023-01-15 14:15:48 -0700146 if (hdr->depth > hdr->max_depth)
Simon Glassb2e16a82013-06-11 11:14:39 -0700147 hdr->max_depth = hdr->depth;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200148 trace_swap_gd();
Simon Glassb2e16a82013-06-11 11:14:39 -0700149 }
150}
151
152/**
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100153 * __cyg_profile_func_exit() - record function exit
Simon Glassb2e16a82013-06-11 11:14:39 -0700154 *
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100155 * @func_ptr: pointer to function being entered
156 * @caller: pointer to function which called this function
Simon Glassb2e16a82013-06-11 11:14:39 -0700157 */
Simon Glass33c60a32022-12-21 16:08:15 -0700158void notrace __cyg_profile_func_exit(void *func_ptr, void *caller)
Simon Glassb2e16a82013-06-11 11:14:39 -0700159{
160 if (trace_enabled) {
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200161 trace_swap_gd();
Simon Glassb2e16a82013-06-11 11:14:39 -0700162 hdr->depth--;
Simon Glassdaca66d2023-01-15 14:15:48 -0700163 add_ftrace(func_ptr, caller, FUNCF_EXIT);
164 if (hdr->depth < hdr->min_depth)
165 hdr->min_depth = hdr->depth;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200166 trace_swap_gd();
Simon Glassb2e16a82013-06-11 11:14:39 -0700167 }
168}
169
170/**
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100171 * trace_list_functions() - produce a list of called functions
Simon Glassb2e16a82013-06-11 11:14:39 -0700172 *
173 * The information is written into the supplied buffer - a header followed
174 * by a list of function records.
175 *
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100176 * @buff: buffer to place list into
177 * @buff_size: size of buffer
178 * @needed: returns size of buffer needed, which may be
179 * greater than buff_size if we ran out of space.
180 * Return: 0 if ok, -ENOSPC if space was exhausted
Simon Glassb2e16a82013-06-11 11:14:39 -0700181 */
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200182int trace_list_functions(void *buff, size_t buff_size, size_t *needed)
Simon Glassb2e16a82013-06-11 11:14:39 -0700183{
184 struct trace_output_hdr *output_hdr = NULL;
185 void *end, *ptr = buff;
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200186 size_t func;
187 size_t upto;
Simon Glassb2e16a82013-06-11 11:14:39 -0700188
189 end = buff ? buff + buff_size : NULL;
190
191 /* Place some header information */
192 if (ptr + sizeof(struct trace_output_hdr) < end)
193 output_hdr = ptr;
194 ptr += sizeof(struct trace_output_hdr);
195
196 /* Add information about each function */
197 for (func = upto = 0; func < hdr->func_count; func++) {
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200198 size_t calls = hdr->call_accum[func];
Simon Glassb2e16a82013-06-11 11:14:39 -0700199
200 if (!calls)
201 continue;
202
203 if (ptr + sizeof(struct trace_output_func) < end) {
204 struct trace_output_func *stats = ptr;
205
206 stats->offset = func * FUNC_SITE_SIZE;
207 stats->call_count = calls;
208 upto++;
209 }
210 ptr += sizeof(struct trace_output_func);
211 }
212
213 /* Update the header */
214 if (output_hdr) {
215 output_hdr->rec_count = upto;
216 output_hdr->type = TRACE_CHUNK_FUNCS;
217 }
218
219 /* Work out how must of the buffer we used */
220 *needed = ptr - buff;
221 if (ptr > end)
Simon Glassf564d092019-04-08 13:20:50 -0600222 return -ENOSPC;
223
Simon Glassb2e16a82013-06-11 11:14:39 -0700224 return 0;
225}
226
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100227/**
228 * trace_list_functions() - produce a list of function calls
229 *
230 * The information is written into the supplied buffer - a header followed
231 * by a list of function records.
232 *
233 * @buff: buffer to place list into
234 * @buff_size: size of buffer
235 * @needed: returns size of buffer needed, which may be
236 * greater than buff_size if we ran out of space.
237 * Return: 0 if ok, -ENOSPC if space was exhausted
238 */
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200239int trace_list_calls(void *buff, size_t buff_size, size_t *needed)
Simon Glassb2e16a82013-06-11 11:14:39 -0700240{
241 struct trace_output_hdr *output_hdr = NULL;
242 void *end, *ptr = buff;
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200243 size_t rec, upto;
244 size_t count;
Simon Glassb2e16a82013-06-11 11:14:39 -0700245
246 end = buff ? buff + buff_size : NULL;
247
248 /* Place some header information */
249 if (ptr + sizeof(struct trace_output_hdr) < end)
250 output_hdr = ptr;
251 ptr += sizeof(struct trace_output_hdr);
252
253 /* Add information about each call */
254 count = hdr->ftrace_count;
255 if (count > hdr->ftrace_size)
256 count = hdr->ftrace_size;
257 for (rec = upto = 0; rec < count; rec++) {
258 if (ptr + sizeof(struct trace_call) < end) {
259 struct trace_call *call = &hdr->ftrace[rec];
260 struct trace_call *out = ptr;
261
262 out->func = call->func * FUNC_SITE_SIZE;
263 out->caller = call->caller * FUNC_SITE_SIZE;
264 out->flags = call->flags;
265 upto++;
266 }
267 ptr += sizeof(struct trace_call);
268 }
269
270 /* Update the header */
271 if (output_hdr) {
Simon Glassd9044e52023-01-15 14:15:46 -0700272 memset(output_hdr, '\0', sizeof(*output_hdr));
Simon Glassb2e16a82013-06-11 11:14:39 -0700273 output_hdr->rec_count = upto;
274 output_hdr->type = TRACE_CHUNK_CALLS;
Simon Glassd9044e52023-01-15 14:15:46 -0700275 output_hdr->version = TRACE_VERSION;
276 output_hdr->text_base = CONFIG_TEXT_BASE;
Simon Glassb2e16a82013-06-11 11:14:39 -0700277 }
278
279 /* Work out how must of the buffer we used */
280 *needed = ptr - buff;
281 if (ptr > end)
Simon Glassf564d092019-04-08 13:20:50 -0600282 return -ENOSPC;
283
Simon Glassb2e16a82013-06-11 11:14:39 -0700284 return 0;
285}
286
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100287/**
288 * trace_print_stats() - print basic information about tracing
289 */
Simon Glassb2e16a82013-06-11 11:14:39 -0700290void trace_print_stats(void)
291{
292 ulong count;
293
294#ifndef FTRACE
295 puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
296 puts("You will likely get zeroed data here\n");
297#endif
298 if (!trace_inited) {
299 printf("Trace is disabled\n");
300 return;
301 }
302 print_grouped_ull(hdr->func_count, 10);
303 puts(" function sites\n");
304 print_grouped_ull(hdr->call_count, 10);
305 puts(" function calls\n");
306 print_grouped_ull(hdr->untracked_count, 10);
307 puts(" untracked function calls\n");
308 count = min(hdr->ftrace_count, hdr->ftrace_size);
309 print_grouped_ull(count, 10);
310 puts(" traced function calls");
311 if (hdr->ftrace_count > hdr->ftrace_size) {
312 printf(" (%lu dropped due to overflow)",
313 hdr->ftrace_count - hdr->ftrace_size);
314 }
Simon Glassdaca66d2023-01-15 14:15:48 -0700315
316 /* Add in minimum depth since the trace did not start at top level */
317 printf("\n%15d maximum observed call depth\n",
318 hdr->max_depth - hdr->min_depth);
Simon Glassb2e16a82013-06-11 11:14:39 -0700319 printf("%15d call depth limit\n", hdr->depth_limit);
320 print_grouped_ull(hdr->ftrace_too_deep_count, 10);
321 puts(" calls not traced due to depth\n");
322}
323
Simon Glass33c60a32022-12-21 16:08:15 -0700324void notrace trace_set_enabled(int enabled)
Simon Glassb2e16a82013-06-11 11:14:39 -0700325{
326 trace_enabled = enabled != 0;
327}
328
Simon Glassc3d91812023-01-15 14:15:47 -0700329static int get_func_count(void)
330{
331 /* Detect no support for mon_len since this means tracing cannot work */
332 if (IS_ENABLED(CONFIG_SANDBOX) && !gd->mon_len) {
333 puts("Tracing is not supported on this board\n");
334 return -ENOTSUPP;
335 }
336
337 return gd->mon_len / FUNC_SITE_SIZE;
338}
339
Simon Glassb2e16a82013-06-11 11:14:39 -0700340/**
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100341 * trace_init() - initialize the tracing system and enable it
Simon Glassb2e16a82013-06-11 11:14:39 -0700342 *
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100343 * @buff: Pointer to trace buffer
344 * @buff_size: Size of trace buffer
345 * Return: 0 if ok
Simon Glassb2e16a82013-06-11 11:14:39 -0700346 */
Simon Glass33c60a32022-12-21 16:08:15 -0700347int notrace trace_init(void *buff, size_t buff_size)
Simon Glassb2e16a82013-06-11 11:14:39 -0700348{
Simon Glassc3d91812023-01-15 14:15:47 -0700349 int func_count = get_func_count();
Simon Glassb2e16a82013-06-11 11:14:39 -0700350 size_t needed;
351 int was_disabled = !trace_enabled;
352
Simon Glassc3d91812023-01-15 14:15:47 -0700353 if (func_count < 0)
354 return func_count;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200355 trace_save_gd();
356
Simon Glassb2e16a82013-06-11 11:14:39 -0700357 if (!was_disabled) {
358#ifdef CONFIG_TRACE_EARLY
359 char *end;
360 ulong used;
361
362 /*
363 * Copy over the early trace data if we have it. Disable
364 * tracing while we are doing this.
365 */
366 trace_enabled = 0;
367 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
368 CONFIG_TRACE_EARLY_SIZE);
Simon Glass1c6eb072019-04-08 13:20:52 -0600369 end = (char *)&hdr->ftrace[min(hdr->ftrace_count,
370 hdr->ftrace_size)];
Simon Glassb2e16a82013-06-11 11:14:39 -0700371 used = end - (char *)hdr;
372 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
373 used, CONFIG_TRACE_EARLY_ADDR,
374 (ulong)map_to_sysmem(buff));
375 memcpy(buff, hdr, used);
376#else
377 puts("trace: already enabled\n");
Simon Glassf564d092019-04-08 13:20:50 -0600378 return -EALREADY;
Simon Glassb2e16a82013-06-11 11:14:39 -0700379#endif
380 }
381 hdr = (struct trace_hdr *)buff;
382 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
383 if (needed > buff_size) {
384 printf("trace: buffer size %zd bytes: at least %zd needed\n",
385 buff_size, needed);
Simon Glassf564d092019-04-08 13:20:50 -0600386 return -ENOSPC;
Simon Glassb2e16a82013-06-11 11:14:39 -0700387 }
388
Simon Glassdaca66d2023-01-15 14:15:48 -0700389 if (was_disabled) {
Simon Glassb2e16a82013-06-11 11:14:39 -0700390 memset(hdr, '\0', needed);
Simon Glassdaca66d2023-01-15 14:15:48 -0700391 hdr->min_depth = INT_MAX;
392 }
Simon Glassb2e16a82013-06-11 11:14:39 -0700393 hdr->func_count = func_count;
394 hdr->call_accum = (uintptr_t *)(hdr + 1);
395
396 /* Use any remaining space for the timed function trace */
397 hdr->ftrace = (struct trace_call *)(buff + needed);
398 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
Simon Glassd9044e52023-01-15 14:15:46 -0700399 hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
Simon Glassb2e16a82013-06-11 11:14:39 -0700400
401 puts("trace: enabled\n");
Simon Glassb2e16a82013-06-11 11:14:39 -0700402 trace_enabled = 1;
403 trace_inited = 1;
Simon Glassf564d092019-04-08 13:20:50 -0600404
Simon Glassb2e16a82013-06-11 11:14:39 -0700405 return 0;
406}
407
408#ifdef CONFIG_TRACE_EARLY
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100409/**
410 * trace_early_init() - initialize the tracing system for early tracing
411 *
412 * Return: 0 if ok, -ENOSPC if not enough memory is available
413 */
Simon Glass33c60a32022-12-21 16:08:15 -0700414int notrace trace_early_init(void)
Simon Glassb2e16a82013-06-11 11:14:39 -0700415{
Simon Glassc3d91812023-01-15 14:15:47 -0700416 int func_count = get_func_count();
Simon Glassb2e16a82013-06-11 11:14:39 -0700417 size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
418 size_t needed;
419
Simon Glassc3d91812023-01-15 14:15:47 -0700420 if (func_count < 0)
421 return func_count;
Simon Glassb2e16a82013-06-11 11:14:39 -0700422 /* We can ignore additional calls to this function */
423 if (trace_enabled)
424 return 0;
425
426 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
427 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
428 if (needed > buff_size) {
429 printf("trace: buffer size is %zd bytes, at least %zd needed\n",
430 buff_size, needed);
Simon Glassf564d092019-04-08 13:20:50 -0600431 return -ENOSPC;
Simon Glassb2e16a82013-06-11 11:14:39 -0700432 }
433
434 memset(hdr, '\0', needed);
435 hdr->call_accum = (uintptr_t *)(hdr + 1);
436 hdr->func_count = func_count;
Simon Glassdaca66d2023-01-15 14:15:48 -0700437 hdr->min_depth = INT_MAX;
Simon Glassb2e16a82013-06-11 11:14:39 -0700438
439 /* Use any remaining space for the timed function trace */
440 hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
441 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
Heinrich Schuchardtda0fb5f2019-06-02 13:30:09 +0200442 hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT;
Simon Glassb2e16a82013-06-11 11:14:39 -0700443 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);
444
445 trace_enabled = 1;
Simon Glassf564d092019-04-08 13:20:50 -0600446
Simon Glassb2e16a82013-06-11 11:14:39 -0700447 return 0;
448}
449#endif