blob: c3354a256fbf5fb9b94cb2b06df9a109127f129a [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");
Simon Glass9dd665a2023-01-15 14:15:49 -0700322 print_grouped_ull(hdr->ftrace_size, 10);
323 puts(" max function calls\n");
324 printf("\ntrace buffer %lx call records %lx\n",
325 (ulong)map_to_sysmem(hdr), (ulong)map_to_sysmem(hdr->ftrace));
Simon Glassb2e16a82013-06-11 11:14:39 -0700326}
327
Simon Glass33c60a32022-12-21 16:08:15 -0700328void notrace trace_set_enabled(int enabled)
Simon Glassb2e16a82013-06-11 11:14:39 -0700329{
330 trace_enabled = enabled != 0;
331}
332
Simon Glassc3d91812023-01-15 14:15:47 -0700333static int get_func_count(void)
334{
335 /* Detect no support for mon_len since this means tracing cannot work */
336 if (IS_ENABLED(CONFIG_SANDBOX) && !gd->mon_len) {
337 puts("Tracing is not supported on this board\n");
338 return -ENOTSUPP;
339 }
340
341 return gd->mon_len / FUNC_SITE_SIZE;
342}
343
Simon Glassb2e16a82013-06-11 11:14:39 -0700344/**
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100345 * trace_init() - initialize the tracing system and enable it
Simon Glassb2e16a82013-06-11 11:14:39 -0700346 *
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100347 * @buff: Pointer to trace buffer
348 * @buff_size: Size of trace buffer
349 * Return: 0 if ok
Simon Glassb2e16a82013-06-11 11:14:39 -0700350 */
Simon Glass33c60a32022-12-21 16:08:15 -0700351int notrace trace_init(void *buff, size_t buff_size)
Simon Glassb2e16a82013-06-11 11:14:39 -0700352{
Simon Glassc3d91812023-01-15 14:15:47 -0700353 int func_count = get_func_count();
Simon Glassb2e16a82013-06-11 11:14:39 -0700354 size_t needed;
355 int was_disabled = !trace_enabled;
356
Simon Glassc3d91812023-01-15 14:15:47 -0700357 if (func_count < 0)
358 return func_count;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200359 trace_save_gd();
360
Simon Glassb2e16a82013-06-11 11:14:39 -0700361 if (!was_disabled) {
362#ifdef CONFIG_TRACE_EARLY
363 char *end;
364 ulong used;
365
366 /*
367 * Copy over the early trace data if we have it. Disable
368 * tracing while we are doing this.
369 */
370 trace_enabled = 0;
371 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
372 CONFIG_TRACE_EARLY_SIZE);
Simon Glass1c6eb072019-04-08 13:20:52 -0600373 end = (char *)&hdr->ftrace[min(hdr->ftrace_count,
374 hdr->ftrace_size)];
Simon Glassb2e16a82013-06-11 11:14:39 -0700375 used = end - (char *)hdr;
376 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
377 used, CONFIG_TRACE_EARLY_ADDR,
378 (ulong)map_to_sysmem(buff));
379 memcpy(buff, hdr, used);
380#else
381 puts("trace: already enabled\n");
Simon Glassf564d092019-04-08 13:20:50 -0600382 return -EALREADY;
Simon Glassb2e16a82013-06-11 11:14:39 -0700383#endif
384 }
385 hdr = (struct trace_hdr *)buff;
386 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
387 if (needed > buff_size) {
Simon Glass9dd665a2023-01-15 14:15:49 -0700388 printf("trace: buffer size %zx bytes: at least %zx needed\n",
Simon Glassb2e16a82013-06-11 11:14:39 -0700389 buff_size, needed);
Simon Glassf564d092019-04-08 13:20:50 -0600390 return -ENOSPC;
Simon Glassb2e16a82013-06-11 11:14:39 -0700391 }
392
Simon Glassdaca66d2023-01-15 14:15:48 -0700393 if (was_disabled) {
Simon Glassb2e16a82013-06-11 11:14:39 -0700394 memset(hdr, '\0', needed);
Simon Glassdaca66d2023-01-15 14:15:48 -0700395 hdr->min_depth = INT_MAX;
396 }
Simon Glassb2e16a82013-06-11 11:14:39 -0700397 hdr->func_count = func_count;
398 hdr->call_accum = (uintptr_t *)(hdr + 1);
399
400 /* Use any remaining space for the timed function trace */
401 hdr->ftrace = (struct trace_call *)(buff + needed);
402 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
Simon Glassd9044e52023-01-15 14:15:46 -0700403 hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
Simon Glassb2e16a82013-06-11 11:14:39 -0700404
405 puts("trace: enabled\n");
Simon Glassb2e16a82013-06-11 11:14:39 -0700406 trace_enabled = 1;
407 trace_inited = 1;
Simon Glassf564d092019-04-08 13:20:50 -0600408
Simon Glassb2e16a82013-06-11 11:14:39 -0700409 return 0;
410}
411
412#ifdef CONFIG_TRACE_EARLY
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100413/**
414 * trace_early_init() - initialize the tracing system for early tracing
415 *
416 * Return: 0 if ok, -ENOSPC if not enough memory is available
417 */
Simon Glass33c60a32022-12-21 16:08:15 -0700418int notrace trace_early_init(void)
Simon Glassb2e16a82013-06-11 11:14:39 -0700419{
Simon Glassc3d91812023-01-15 14:15:47 -0700420 int func_count = get_func_count();
Simon Glassb2e16a82013-06-11 11:14:39 -0700421 size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
422 size_t needed;
423
Simon Glassc3d91812023-01-15 14:15:47 -0700424 if (func_count < 0)
425 return func_count;
Simon Glassb2e16a82013-06-11 11:14:39 -0700426 /* We can ignore additional calls to this function */
427 if (trace_enabled)
428 return 0;
429
430 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
431 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
432 if (needed > buff_size) {
Simon Glass9dd665a2023-01-15 14:15:49 -0700433 printf("trace: buffer size is %zx bytes, at least %zx needed\n",
Simon Glassb2e16a82013-06-11 11:14:39 -0700434 buff_size, needed);
Simon Glassf564d092019-04-08 13:20:50 -0600435 return -ENOSPC;
Simon Glassb2e16a82013-06-11 11:14:39 -0700436 }
437
438 memset(hdr, '\0', needed);
439 hdr->call_accum = (uintptr_t *)(hdr + 1);
440 hdr->func_count = func_count;
Simon Glassdaca66d2023-01-15 14:15:48 -0700441 hdr->min_depth = INT_MAX;
Simon Glassb2e16a82013-06-11 11:14:39 -0700442
443 /* Use any remaining space for the timed function trace */
444 hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
445 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
Heinrich Schuchardtda0fb5f2019-06-02 13:30:09 +0200446 hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT;
Simon Glassb2e16a82013-06-11 11:14:39 -0700447 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);
448
449 trace_enabled = 1;
Simon Glassf564d092019-04-08 13:20:50 -0600450
Simon Glassb2e16a82013-06-11 11:14:39 -0700451 return 0;
452}
453#endif