1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012 The Chromium OS Authors.
10 #include <asm/global_data.h>
12 #include <asm/sections.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 static char trace_enabled __attribute__((section(".data")));
17 static char trace_inited __attribute__((section(".data")));
19 /* The header block at the start of the trace memory area */
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 */
27 * Call count for each function. This is indexed by the word offset
28 * of the function from gd->relocaddr
30 uintptr_t *call_accum;
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 */
43 static struct trace_hdr *hdr; /* Pointer to start of trace buffer */
45 static inline uintptr_t __attribute__((no_instrument_function))
46 func_ptr_to_num(void *func_ptr)
48 uintptr_t offset = (uintptr_t)func_ptr;
51 offset -= (uintptr_t)&_init;
53 if (gd->flags & GD_FLG_RELOC)
54 offset -= gd->relocaddr;
56 offset -= CONFIG_SYS_TEXT_BASE;
58 return offset / FUNC_SITE_SIZE;
61 #if defined(CONFIG_EFI_LOADER) && (defined(CONFIG_ARM) || defined(CONFIG_RISCV))
64 * trace_gd - the value of the gd register
66 static volatile gd_t *trace_gd;
69 * trace_save_gd() - save the value of the gd register
71 static void __attribute__((no_instrument_function)) trace_save_gd(void)
77 * trace_swap_gd() - swap between U-Boot and application gd register value
79 * An UEFI application may change the value of the register that gd lives in.
80 * But some of our functions like get_ticks() access this register. So we
81 * have to set the gd register to the U-Boot value when entering a trace
82 * point and set it back to the application value when exiting the trace point.
84 static void __attribute__((no_instrument_function)) trace_swap_gd(void)
86 volatile gd_t *temp_gd = trace_gd;
94 static void __attribute__((no_instrument_function)) trace_save_gd(void)
98 static void __attribute__((no_instrument_function)) trace_swap_gd(void)
104 static void __attribute__((no_instrument_function)) add_ftrace(void *func_ptr,
105 void *caller, ulong flags)
107 if (hdr->depth > hdr->depth_limit) {
108 hdr->ftrace_too_deep_count++;
111 if (hdr->ftrace_count < hdr->ftrace_size) {
112 struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
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);
121 static void __attribute__((no_instrument_function)) add_textbase(void)
123 if (hdr->ftrace_count < hdr->ftrace_size) {
124 struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
126 rec->func = CONFIG_SYS_TEXT_BASE;
128 rec->flags = FUNCF_TEXTBASE;
134 * __cyg_profile_func_enter() - record function entry
136 * We add to our tally for this function and add to the list of called
139 * @func_ptr: pointer to function being entered
140 * @caller: pointer to function which called this function
142 void __attribute__((no_instrument_function)) __cyg_profile_func_enter(
143 void *func_ptr, void *caller)
149 add_ftrace(func_ptr, caller, FUNCF_ENTRY);
150 func = func_ptr_to_num(func_ptr);
151 if (func < hdr->func_count) {
152 hdr->call_accum[func]++;
155 hdr->untracked_count++;
158 if (hdr->depth > hdr->depth_limit)
159 hdr->max_depth = hdr->depth;
165 * __cyg_profile_func_exit() - record function exit
167 * @func_ptr: pointer to function being entered
168 * @caller: pointer to function which called this function
170 void __attribute__((no_instrument_function)) __cyg_profile_func_exit(
171 void *func_ptr, void *caller)
175 add_ftrace(func_ptr, caller, FUNCF_EXIT);
182 * trace_list_functions() - produce a list of called functions
184 * The information is written into the supplied buffer - a header followed
185 * by a list of function records.
187 * @buff: buffer to place list into
188 * @buff_size: size of buffer
189 * @needed: returns size of buffer needed, which may be
190 * greater than buff_size if we ran out of space.
191 * Return: 0 if ok, -ENOSPC if space was exhausted
193 int trace_list_functions(void *buff, size_t buff_size, size_t *needed)
195 struct trace_output_hdr *output_hdr = NULL;
196 void *end, *ptr = buff;
200 end = buff ? buff + buff_size : NULL;
202 /* Place some header information */
203 if (ptr + sizeof(struct trace_output_hdr) < end)
205 ptr += sizeof(struct trace_output_hdr);
207 /* Add information about each function */
208 for (func = upto = 0; func < hdr->func_count; func++) {
209 size_t calls = hdr->call_accum[func];
214 if (ptr + sizeof(struct trace_output_func) < end) {
215 struct trace_output_func *stats = ptr;
217 stats->offset = func * FUNC_SITE_SIZE;
218 stats->call_count = calls;
221 ptr += sizeof(struct trace_output_func);
224 /* Update the header */
226 output_hdr->rec_count = upto;
227 output_hdr->type = TRACE_CHUNK_FUNCS;
230 /* Work out how must of the buffer we used */
231 *needed = ptr - buff;
239 * trace_list_functions() - produce a list of function calls
241 * The information is written into the supplied buffer - a header followed
242 * by a list of function records.
244 * @buff: buffer to place list into
245 * @buff_size: size of buffer
246 * @needed: returns size of buffer needed, which may be
247 * greater than buff_size if we ran out of space.
248 * Return: 0 if ok, -ENOSPC if space was exhausted
250 int trace_list_calls(void *buff, size_t buff_size, size_t *needed)
252 struct trace_output_hdr *output_hdr = NULL;
253 void *end, *ptr = buff;
257 end = buff ? buff + buff_size : NULL;
259 /* Place some header information */
260 if (ptr + sizeof(struct trace_output_hdr) < end)
262 ptr += sizeof(struct trace_output_hdr);
264 /* Add information about each call */
265 count = hdr->ftrace_count;
266 if (count > hdr->ftrace_size)
267 count = hdr->ftrace_size;
268 for (rec = upto = 0; rec < count; rec++) {
269 if (ptr + sizeof(struct trace_call) < end) {
270 struct trace_call *call = &hdr->ftrace[rec];
271 struct trace_call *out = ptr;
273 out->func = call->func * FUNC_SITE_SIZE;
274 out->caller = call->caller * FUNC_SITE_SIZE;
275 out->flags = call->flags;
278 ptr += sizeof(struct trace_call);
281 /* Update the header */
283 output_hdr->rec_count = upto;
284 output_hdr->type = TRACE_CHUNK_CALLS;
287 /* Work out how must of the buffer we used */
288 *needed = ptr - buff;
296 * trace_print_stats() - print basic information about tracing
298 void trace_print_stats(void)
303 puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
304 puts("You will likely get zeroed data here\n");
307 printf("Trace is disabled\n");
310 print_grouped_ull(hdr->func_count, 10);
311 puts(" function sites\n");
312 print_grouped_ull(hdr->call_count, 10);
313 puts(" function calls\n");
314 print_grouped_ull(hdr->untracked_count, 10);
315 puts(" untracked function calls\n");
316 count = min(hdr->ftrace_count, hdr->ftrace_size);
317 print_grouped_ull(count, 10);
318 puts(" traced function calls");
319 if (hdr->ftrace_count > hdr->ftrace_size) {
320 printf(" (%lu dropped due to overflow)",
321 hdr->ftrace_count - hdr->ftrace_size);
324 printf("%15d maximum observed call depth\n", hdr->max_depth);
325 printf("%15d call depth limit\n", hdr->depth_limit);
326 print_grouped_ull(hdr->ftrace_too_deep_count, 10);
327 puts(" calls not traced due to depth\n");
330 void __attribute__((no_instrument_function)) trace_set_enabled(int enabled)
332 trace_enabled = enabled != 0;
336 * trace_init() - initialize the tracing system and enable it
338 * @buff: Pointer to trace buffer
339 * @buff_size: Size of trace buffer
342 int __attribute__((no_instrument_function)) trace_init(void *buff,
345 ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
347 int was_disabled = !trace_enabled;
352 #ifdef CONFIG_TRACE_EARLY
357 * Copy over the early trace data if we have it. Disable
358 * tracing while we are doing this.
361 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
362 CONFIG_TRACE_EARLY_SIZE);
363 end = (char *)&hdr->ftrace[min(hdr->ftrace_count,
365 used = end - (char *)hdr;
366 printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
367 used, CONFIG_TRACE_EARLY_ADDR,
368 (ulong)map_to_sysmem(buff));
369 memcpy(buff, hdr, used);
371 puts("trace: already enabled\n");
375 hdr = (struct trace_hdr *)buff;
376 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
377 if (needed > buff_size) {
378 printf("trace: buffer size %zd bytes: at least %zd needed\n",
384 memset(hdr, '\0', needed);
385 hdr->func_count = func_count;
386 hdr->call_accum = (uintptr_t *)(hdr + 1);
388 /* Use any remaining space for the timed function trace */
389 hdr->ftrace = (struct trace_call *)(buff + needed);
390 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
393 puts("trace: enabled\n");
394 hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
401 #ifdef CONFIG_TRACE_EARLY
403 * trace_early_init() - initialize the tracing system for early tracing
405 * Return: 0 if ok, -ENOSPC if not enough memory is available
407 int __attribute__((no_instrument_function)) trace_early_init(void)
409 ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
410 size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
413 /* We can ignore additional calls to this function */
417 hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
418 needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
419 if (needed > buff_size) {
420 printf("trace: buffer size is %zd bytes, at least %zd needed\n",
425 memset(hdr, '\0', needed);
426 hdr->call_accum = (uintptr_t *)(hdr + 1);
427 hdr->func_count = func_count;
429 /* Use any remaining space for the timed function trace */
430 hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
431 hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
433 hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT;
434 printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);