blob: 12dae2089a48843b505ce8f9fdacdfb11be3d471 [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
38 int depth;
39 int depth_limit;
40 int max_depth;
41};
42
Simon Glassbebc1412022-12-21 16:08:22 -070043/* Pointer to start of trace buffer */
44static struct trace_hdr *hdr __section(".data");
Simon Glassb2e16a82013-06-11 11:14:39 -070045
46static inline uintptr_t __attribute__((no_instrument_function))
47 func_ptr_to_num(void *func_ptr)
48{
49 uintptr_t offset = (uintptr_t)func_ptr;
50
51#ifdef CONFIG_SANDBOX
52 offset -= (uintptr_t)&_init;
53#else
54 if (gd->flags & GD_FLG_RELOC)
55 offset -= gd->relocaddr;
56 else
Simon Glass98463902022-10-20 18:22:39 -060057 offset -= CONFIG_TEXT_BASE;
Simon Glassb2e16a82013-06-11 11:14:39 -070058#endif
59 return offset / FUNC_SITE_SIZE;
60}
61
Heinrich Schuchardtd3d6afa2020-10-15 12:30:09 +020062#if defined(CONFIG_EFI_LOADER) && (defined(CONFIG_ARM) || defined(CONFIG_RISCV))
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020063
64/**
65 * trace_gd - the value of the gd register
66 */
Heinrich Schuchardt7f642cb2020-05-27 20:04:22 +020067static volatile gd_t *trace_gd;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020068
69/**
70 * trace_save_gd() - save the value of the gd register
71 */
Simon Glass33c60a32022-12-21 16:08:15 -070072static void notrace trace_save_gd(void)
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020073{
74 trace_gd = gd;
75}
76
77/**
78 * trace_swap_gd() - swap between U-Boot and application gd register value
79 *
80 * An UEFI application may change the value of the register that gd lives in.
81 * But some of our functions like get_ticks() access this register. So we
82 * have to set the gd register to the U-Boot value when entering a trace
83 * point and set it back to the application value when exiting the trace point.
84 */
Simon Glass33c60a32022-12-21 16:08:15 -070085static void notrace trace_swap_gd(void)
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020086{
Heinrich Schuchardt7f642cb2020-05-27 20:04:22 +020087 volatile gd_t *temp_gd = trace_gd;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020088
89 trace_gd = gd;
Heinrich Schuchardt7f642cb2020-05-27 20:04:22 +020090 set_gd(temp_gd);
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020091}
92
93#else
94
Simon Glass33c60a32022-12-21 16:08:15 -070095static void notrace trace_save_gd(void)
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +020096{
97}
98
Simon Glass33c60a32022-12-21 16:08:15 -070099static void notrace trace_swap_gd(void)
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200100{
101}
102
103#endif
104
Simon Glass33c60a32022-12-21 16:08:15 -0700105static void notrace add_ftrace(void *func_ptr, void *caller, ulong flags)
Simon Glassb2e16a82013-06-11 11:14:39 -0700106{
107 if (hdr->depth > hdr->depth_limit) {
108 hdr->ftrace_too_deep_count++;
109 return;
110 }
111 if (hdr->ftrace_count < hdr->ftrace_size) {
112 struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
113
114 rec->func = func_ptr_to_num(func_ptr);
115 rec->caller = func_ptr_to_num(caller);
116 rec->flags = flags | (timer_get_us() & FUNCF_TIMESTAMP_MASK);
117 }
118 hdr->ftrace_count++;
119}
120
Simon Glassb2e16a82013-06-11 11:14:39 -0700121/**
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100122 * __cyg_profile_func_enter() - record function entry
Simon Glassb2e16a82013-06-11 11:14:39 -0700123 *
124 * We add to our tally for this function and add to the list of called
125 * functions.
126 *
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100127 * @func_ptr: pointer to function being entered
128 * @caller: pointer to function which called this function
Simon Glassb2e16a82013-06-11 11:14:39 -0700129 */
Simon Glass33c60a32022-12-21 16:08:15 -0700130void notrace __cyg_profile_func_enter(void *func_ptr, void *caller)
Simon Glassb2e16a82013-06-11 11:14:39 -0700131{
132 if (trace_enabled) {
133 int func;
134
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200135 trace_swap_gd();
Simon Glassb2e16a82013-06-11 11:14:39 -0700136 add_ftrace(func_ptr, caller, FUNCF_ENTRY);
137 func = func_ptr_to_num(func_ptr);
138 if (func < hdr->func_count) {
139 hdr->call_accum[func]++;
140 hdr->call_count++;
141 } else {
142 hdr->untracked_count++;
143 }
144 hdr->depth++;
145 if (hdr->depth > hdr->depth_limit)
146 hdr->max_depth = hdr->depth;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200147 trace_swap_gd();
Simon Glassb2e16a82013-06-11 11:14:39 -0700148 }
149}
150
151/**
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100152 * __cyg_profile_func_exit() - record function exit
Simon Glassb2e16a82013-06-11 11:14:39 -0700153 *
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100154 * @func_ptr: pointer to function being entered
155 * @caller: pointer to function which called this function
Simon Glassb2e16a82013-06-11 11:14:39 -0700156 */
Simon Glass33c60a32022-12-21 16:08:15 -0700157void notrace __cyg_profile_func_exit(void *func_ptr, void *caller)
Simon Glassb2e16a82013-06-11 11:14:39 -0700158{
159 if (trace_enabled) {
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200160 trace_swap_gd();
Simon Glassb2e16a82013-06-11 11:14:39 -0700161 add_ftrace(func_ptr, caller, FUNCF_EXIT);
162 hdr->depth--;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200163 trace_swap_gd();
Simon Glassb2e16a82013-06-11 11:14:39 -0700164 }
165}
166
167/**
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100168 * trace_list_functions() - produce a list of called functions
Simon Glassb2e16a82013-06-11 11:14:39 -0700169 *
170 * The information is written into the supplied buffer - a header followed
171 * by a list of function records.
172 *
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100173 * @buff: buffer to place list into
174 * @buff_size: size of buffer
175 * @needed: returns size of buffer needed, which may be
176 * greater than buff_size if we ran out of space.
177 * Return: 0 if ok, -ENOSPC if space was exhausted
Simon Glassb2e16a82013-06-11 11:14:39 -0700178 */
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200179int trace_list_functions(void *buff, size_t buff_size, size_t *needed)
Simon Glassb2e16a82013-06-11 11:14:39 -0700180{
181 struct trace_output_hdr *output_hdr = NULL;
182 void *end, *ptr = buff;
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200183 size_t func;
184 size_t upto;
Simon Glassb2e16a82013-06-11 11:14:39 -0700185
186 end = buff ? buff + buff_size : NULL;
187
188 /* Place some header information */
189 if (ptr + sizeof(struct trace_output_hdr) < end)
190 output_hdr = ptr;
191 ptr += sizeof(struct trace_output_hdr);
192
193 /* Add information about each function */
194 for (func = upto = 0; func < hdr->func_count; func++) {
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200195 size_t calls = hdr->call_accum[func];
Simon Glassb2e16a82013-06-11 11:14:39 -0700196
197 if (!calls)
198 continue;
199
200 if (ptr + sizeof(struct trace_output_func) < end) {
201 struct trace_output_func *stats = ptr;
202
203 stats->offset = func * FUNC_SITE_SIZE;
204 stats->call_count = calls;
205 upto++;
206 }
207 ptr += sizeof(struct trace_output_func);
208 }
209
210 /* Update the header */
211 if (output_hdr) {
212 output_hdr->rec_count = upto;
213 output_hdr->type = TRACE_CHUNK_FUNCS;
214 }
215
216 /* Work out how must of the buffer we used */
217 *needed = ptr - buff;
218 if (ptr > end)
Simon Glassf564d092019-04-08 13:20:50 -0600219 return -ENOSPC;
220
Simon Glassb2e16a82013-06-11 11:14:39 -0700221 return 0;
222}
223
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100224/**
225 * trace_list_functions() - produce a list of function calls
226 *
227 * The information is written into the supplied buffer - a header followed
228 * by a list of function records.
229 *
230 * @buff: buffer to place list into
231 * @buff_size: size of buffer
232 * @needed: returns size of buffer needed, which may be
233 * greater than buff_size if we ran out of space.
234 * Return: 0 if ok, -ENOSPC if space was exhausted
235 */
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200236int trace_list_calls(void *buff, size_t buff_size, size_t *needed)
Simon Glassb2e16a82013-06-11 11:14:39 -0700237{
238 struct trace_output_hdr *output_hdr = NULL;
239 void *end, *ptr = buff;
Heinrich Schuchardt2b7a3882019-06-14 21:50:55 +0200240 size_t rec, upto;
241 size_t count;
Simon Glassb2e16a82013-06-11 11:14:39 -0700242
243 end = buff ? buff + buff_size : NULL;
244
245 /* Place some header information */
246 if (ptr + sizeof(struct trace_output_hdr) < end)
247 output_hdr = ptr;
248 ptr += sizeof(struct trace_output_hdr);
249
250 /* Add information about each call */
251 count = hdr->ftrace_count;
252 if (count > hdr->ftrace_size)
253 count = hdr->ftrace_size;
254 for (rec = upto = 0; rec < count; rec++) {
255 if (ptr + sizeof(struct trace_call) < end) {
256 struct trace_call *call = &hdr->ftrace[rec];
257 struct trace_call *out = ptr;
258
259 out->func = call->func * FUNC_SITE_SIZE;
260 out->caller = call->caller * FUNC_SITE_SIZE;
261 out->flags = call->flags;
262 upto++;
263 }
264 ptr += sizeof(struct trace_call);
265 }
266
267 /* Update the header */
268 if (output_hdr) {
Simon Glassd9044e52023-01-15 14:15:46 -0700269 memset(output_hdr, '\0', sizeof(*output_hdr));
Simon Glassb2e16a82013-06-11 11:14:39 -0700270 output_hdr->rec_count = upto;
271 output_hdr->type = TRACE_CHUNK_CALLS;
Simon Glassd9044e52023-01-15 14:15:46 -0700272 output_hdr->version = TRACE_VERSION;
273 output_hdr->text_base = CONFIG_TEXT_BASE;
Simon Glassb2e16a82013-06-11 11:14:39 -0700274 }
275
276 /* Work out how must of the buffer we used */
277 *needed = ptr - buff;
278 if (ptr > end)
Simon Glassf564d092019-04-08 13:20:50 -0600279 return -ENOSPC;
280
Simon Glassb2e16a82013-06-11 11:14:39 -0700281 return 0;
282}
283
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100284/**
285 * trace_print_stats() - print basic information about tracing
286 */
Simon Glassb2e16a82013-06-11 11:14:39 -0700287void trace_print_stats(void)
288{
289 ulong count;
290
291#ifndef FTRACE
292 puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
293 puts("You will likely get zeroed data here\n");
294#endif
295 if (!trace_inited) {
296 printf("Trace is disabled\n");
297 return;
298 }
299 print_grouped_ull(hdr->func_count, 10);
300 puts(" function sites\n");
301 print_grouped_ull(hdr->call_count, 10);
302 puts(" function calls\n");
303 print_grouped_ull(hdr->untracked_count, 10);
304 puts(" untracked function calls\n");
305 count = min(hdr->ftrace_count, hdr->ftrace_size);
306 print_grouped_ull(count, 10);
307 puts(" traced function calls");
308 if (hdr->ftrace_count > hdr->ftrace_size) {
309 printf(" (%lu dropped due to overflow)",
310 hdr->ftrace_count - hdr->ftrace_size);
311 }
312 puts("\n");
313 printf("%15d maximum observed call depth\n", hdr->max_depth);
314 printf("%15d call depth limit\n", hdr->depth_limit);
315 print_grouped_ull(hdr->ftrace_too_deep_count, 10);
316 puts(" calls not traced due to depth\n");
317}
318
Simon Glass33c60a32022-12-21 16:08:15 -0700319void notrace trace_set_enabled(int enabled)
Simon Glassb2e16a82013-06-11 11:14:39 -0700320{
321 trace_enabled = enabled != 0;
322}
323
Simon Glassc3d91812023-01-15 14:15:47 -0700324static int get_func_count(void)
325{
326 /* Detect no support for mon_len since this means tracing cannot work */
327 if (IS_ENABLED(CONFIG_SANDBOX) && !gd->mon_len) {
328 puts("Tracing is not supported on this board\n");
329 return -ENOTSUPP;
330 }
331
332 return gd->mon_len / FUNC_SITE_SIZE;
333}
334
Simon Glassb2e16a82013-06-11 11:14:39 -0700335/**
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100336 * trace_init() - initialize the tracing system and enable it
Simon Glassb2e16a82013-06-11 11:14:39 -0700337 *
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100338 * @buff: Pointer to trace buffer
339 * @buff_size: Size of trace buffer
340 * Return: 0 if ok
Simon Glassb2e16a82013-06-11 11:14:39 -0700341 */
Simon Glass33c60a32022-12-21 16:08:15 -0700342int notrace trace_init(void *buff, size_t buff_size)
Simon Glassb2e16a82013-06-11 11:14:39 -0700343{
Simon Glassc3d91812023-01-15 14:15:47 -0700344 int func_count = get_func_count();
Simon Glassb2e16a82013-06-11 11:14:39 -0700345 size_t needed;
346 int was_disabled = !trace_enabled;
347
Simon Glassc3d91812023-01-15 14:15:47 -0700348 if (func_count < 0)
349 return func_count;
Heinrich Schuchardta2fa38d2019-06-02 13:05:08 +0200350 trace_save_gd();
351
Simon Glassb2e16a82013-06-11 11:14:39 -0700352 if (!was_disabled) {
353#ifdef CONFIG_TRACE_EARLY
354 char *end;
355 ulong used;
356
357 /*
358 * Copy over the early trace data if we have it. Disable
359 * tracing while we are doing this.
360 */
361 trace_enabled = 0;
362 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
363 CONFIG_TRACE_EARLY_SIZE);
Simon Glass1c6eb072019-04-08 13:20:52 -0600364 end = (char *)&hdr->ftrace[min(hdr->ftrace_count,
365 hdr->ftrace_size)];
Simon Glassb2e16a82013-06-11 11:14:39 -0700366 used = end - (char *)hdr;
367 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
368 used, CONFIG_TRACE_EARLY_ADDR,
369 (ulong)map_to_sysmem(buff));
370 memcpy(buff, hdr, used);
371#else
372 puts("trace: already enabled\n");
Simon Glassf564d092019-04-08 13:20:50 -0600373 return -EALREADY;
Simon Glassb2e16a82013-06-11 11:14:39 -0700374#endif
375 }
376 hdr = (struct trace_hdr *)buff;
377 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
378 if (needed > buff_size) {
379 printf("trace: buffer size %zd bytes: at least %zd needed\n",
380 buff_size, needed);
Simon Glassf564d092019-04-08 13:20:50 -0600381 return -ENOSPC;
Simon Glassb2e16a82013-06-11 11:14:39 -0700382 }
383
384 if (was_disabled)
385 memset(hdr, '\0', needed);
386 hdr->func_count = func_count;
387 hdr->call_accum = (uintptr_t *)(hdr + 1);
388
389 /* Use any remaining space for the timed function trace */
390 hdr->ftrace = (struct trace_call *)(buff + needed);
391 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
Simon Glassd9044e52023-01-15 14:15:46 -0700392 hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
Simon Glassb2e16a82013-06-11 11:14:39 -0700393
394 puts("trace: enabled\n");
Simon Glassb2e16a82013-06-11 11:14:39 -0700395 trace_enabled = 1;
396 trace_inited = 1;
Simon Glassf564d092019-04-08 13:20:50 -0600397
Simon Glassb2e16a82013-06-11 11:14:39 -0700398 return 0;
399}
400
401#ifdef CONFIG_TRACE_EARLY
Heinrich Schuchardte605ab82020-01-01 15:52:31 +0100402/**
403 * trace_early_init() - initialize the tracing system for early tracing
404 *
405 * Return: 0 if ok, -ENOSPC if not enough memory is available
406 */
Simon Glass33c60a32022-12-21 16:08:15 -0700407int notrace trace_early_init(void)
Simon Glassb2e16a82013-06-11 11:14:39 -0700408{
Simon Glassc3d91812023-01-15 14:15:47 -0700409 int func_count = get_func_count();
Simon Glassb2e16a82013-06-11 11:14:39 -0700410 size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
411 size_t needed;
412
Simon Glassc3d91812023-01-15 14:15:47 -0700413 if (func_count < 0)
414 return func_count;
Simon Glassb2e16a82013-06-11 11:14:39 -0700415 /* We can ignore additional calls to this function */
416 if (trace_enabled)
417 return 0;
418
419 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
420 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
421 if (needed > buff_size) {
422 printf("trace: buffer size is %zd bytes, at least %zd needed\n",
423 buff_size, needed);
Simon Glassf564d092019-04-08 13:20:50 -0600424 return -ENOSPC;
Simon Glassb2e16a82013-06-11 11:14:39 -0700425 }
426
427 memset(hdr, '\0', needed);
428 hdr->call_accum = (uintptr_t *)(hdr + 1);
429 hdr->func_count = func_count;
430
431 /* Use any remaining space for the timed function trace */
432 hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
433 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
Heinrich Schuchardtda0fb5f2019-06-02 13:30:09 +0200434 hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT;
Simon Glassb2e16a82013-06-11 11:14:39 -0700435 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);
436
437 trace_enabled = 1;
Simon Glassf564d092019-04-08 13:20:50 -0600438
Simon Glassb2e16a82013-06-11 11:14:39 -0700439 return 0;
440}
441#endif