trace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2012 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <mapmem.h>
  7. #include <time.h>
  8. #include <trace.h>
  9. #include <asm/global_data.h>
  10. #include <asm/io.h>
  11. #include <asm/sections.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. static char trace_enabled __section(".data");
  14. static char trace_inited __section(".data");
  15. /* The header block at the start of the trace memory area */
  16. struct trace_hdr {
  17. int func_count; /* Total number of function call sites */
  18. u64 call_count; /* Total number of tracked function calls */
  19. u64 untracked_count; /* Total number of untracked function calls */
  20. int funcs_used; /* Total number of functions used */
  21. /*
  22. * Call count for each function. This is indexed by the word offset
  23. * of the function from gd->relocaddr
  24. */
  25. uintptr_t *call_accum;
  26. /* Function trace list */
  27. struct trace_call *ftrace; /* The function call records */
  28. ulong ftrace_size; /* Num. of ftrace records we have space for */
  29. ulong ftrace_count; /* Num. of ftrace records written */
  30. ulong ftrace_too_deep_count; /* Functions that were too deep */
  31. int depth;
  32. int depth_limit;
  33. int max_depth;
  34. };
  35. static struct trace_hdr *hdr; /* Pointer to start of trace buffer */
  36. static inline uintptr_t __attribute__((no_instrument_function))
  37. func_ptr_to_num(void *func_ptr)
  38. {
  39. uintptr_t offset = (uintptr_t)func_ptr;
  40. #ifdef CONFIG_SANDBOX
  41. offset -= (uintptr_t)&_init;
  42. #else
  43. if (gd->flags & GD_FLG_RELOC)
  44. offset -= gd->relocaddr;
  45. else
  46. offset -= CONFIG_SYS_TEXT_BASE;
  47. #endif
  48. return offset / FUNC_SITE_SIZE;
  49. }
  50. #if defined(CONFIG_EFI_LOADER) && (defined(CONFIG_ARM) || defined(CONFIG_RISCV))
  51. /**
  52. * trace_gd - the value of the gd register
  53. */
  54. static volatile gd_t *trace_gd;
  55. /**
  56. * trace_save_gd() - save the value of the gd register
  57. */
  58. static void __attribute__((no_instrument_function)) trace_save_gd(void)
  59. {
  60. trace_gd = gd;
  61. }
  62. /**
  63. * trace_swap_gd() - swap between U-Boot and application gd register value
  64. *
  65. * An UEFI application may change the value of the register that gd lives in.
  66. * But some of our functions like get_ticks() access this register. So we
  67. * have to set the gd register to the U-Boot value when entering a trace
  68. * point and set it back to the application value when exiting the trace point.
  69. */
  70. static void __attribute__((no_instrument_function)) trace_swap_gd(void)
  71. {
  72. volatile gd_t *temp_gd = trace_gd;
  73. trace_gd = gd;
  74. set_gd(temp_gd);
  75. }
  76. #else
  77. static void __attribute__((no_instrument_function)) trace_save_gd(void)
  78. {
  79. }
  80. static void __attribute__((no_instrument_function)) trace_swap_gd(void)
  81. {
  82. }
  83. #endif
  84. static void __attribute__((no_instrument_function)) add_ftrace(void *func_ptr,
  85. void *caller, ulong flags)
  86. {
  87. if (hdr->depth > hdr->depth_limit) {
  88. hdr->ftrace_too_deep_count++;
  89. return;
  90. }
  91. if (hdr->ftrace_count < hdr->ftrace_size) {
  92. struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
  93. rec->func = func_ptr_to_num(func_ptr);
  94. rec->caller = func_ptr_to_num(caller);
  95. rec->flags = flags | (timer_get_us() & FUNCF_TIMESTAMP_MASK);
  96. }
  97. hdr->ftrace_count++;
  98. }
  99. static void __attribute__((no_instrument_function)) add_textbase(void)
  100. {
  101. if (hdr->ftrace_count < hdr->ftrace_size) {
  102. struct trace_call *rec = &hdr->ftrace[hdr->ftrace_count];
  103. rec->func = CONFIG_SYS_TEXT_BASE;
  104. rec->caller = 0;
  105. rec->flags = FUNCF_TEXTBASE;
  106. }
  107. hdr->ftrace_count++;
  108. }
  109. /**
  110. * __cyg_profile_func_enter() - record function entry
  111. *
  112. * We add to our tally for this function and add to the list of called
  113. * functions.
  114. *
  115. * @func_ptr: pointer to function being entered
  116. * @caller: pointer to function which called this function
  117. */
  118. void __attribute__((no_instrument_function)) __cyg_profile_func_enter(
  119. void *func_ptr, void *caller)
  120. {
  121. if (trace_enabled) {
  122. int func;
  123. trace_swap_gd();
  124. add_ftrace(func_ptr, caller, FUNCF_ENTRY);
  125. func = func_ptr_to_num(func_ptr);
  126. if (func < hdr->func_count) {
  127. hdr->call_accum[func]++;
  128. hdr->call_count++;
  129. } else {
  130. hdr->untracked_count++;
  131. }
  132. hdr->depth++;
  133. if (hdr->depth > hdr->depth_limit)
  134. hdr->max_depth = hdr->depth;
  135. trace_swap_gd();
  136. }
  137. }
  138. /**
  139. * __cyg_profile_func_exit() - record function exit
  140. *
  141. * @func_ptr: pointer to function being entered
  142. * @caller: pointer to function which called this function
  143. */
  144. void __attribute__((no_instrument_function)) __cyg_profile_func_exit(
  145. void *func_ptr, void *caller)
  146. {
  147. if (trace_enabled) {
  148. trace_swap_gd();
  149. add_ftrace(func_ptr, caller, FUNCF_EXIT);
  150. hdr->depth--;
  151. trace_swap_gd();
  152. }
  153. }
  154. /**
  155. * trace_list_functions() - produce a list of called functions
  156. *
  157. * The information is written into the supplied buffer - a header followed
  158. * by a list of function records.
  159. *
  160. * @buff: buffer to place list into
  161. * @buff_size: size of buffer
  162. * @needed: returns size of buffer needed, which may be
  163. * greater than buff_size if we ran out of space.
  164. * Return: 0 if ok, -ENOSPC if space was exhausted
  165. */
  166. int trace_list_functions(void *buff, size_t buff_size, size_t *needed)
  167. {
  168. struct trace_output_hdr *output_hdr = NULL;
  169. void *end, *ptr = buff;
  170. size_t func;
  171. size_t upto;
  172. end = buff ? buff + buff_size : NULL;
  173. /* Place some header information */
  174. if (ptr + sizeof(struct trace_output_hdr) < end)
  175. output_hdr = ptr;
  176. ptr += sizeof(struct trace_output_hdr);
  177. /* Add information about each function */
  178. for (func = upto = 0; func < hdr->func_count; func++) {
  179. size_t calls = hdr->call_accum[func];
  180. if (!calls)
  181. continue;
  182. if (ptr + sizeof(struct trace_output_func) < end) {
  183. struct trace_output_func *stats = ptr;
  184. stats->offset = func * FUNC_SITE_SIZE;
  185. stats->call_count = calls;
  186. upto++;
  187. }
  188. ptr += sizeof(struct trace_output_func);
  189. }
  190. /* Update the header */
  191. if (output_hdr) {
  192. output_hdr->rec_count = upto;
  193. output_hdr->type = TRACE_CHUNK_FUNCS;
  194. }
  195. /* Work out how must of the buffer we used */
  196. *needed = ptr - buff;
  197. if (ptr > end)
  198. return -ENOSPC;
  199. return 0;
  200. }
  201. /**
  202. * trace_list_functions() - produce a list of function calls
  203. *
  204. * The information is written into the supplied buffer - a header followed
  205. * by a list of function records.
  206. *
  207. * @buff: buffer to place list into
  208. * @buff_size: size of buffer
  209. * @needed: returns size of buffer needed, which may be
  210. * greater than buff_size if we ran out of space.
  211. * Return: 0 if ok, -ENOSPC if space was exhausted
  212. */
  213. int trace_list_calls(void *buff, size_t buff_size, size_t *needed)
  214. {
  215. struct trace_output_hdr *output_hdr = NULL;
  216. void *end, *ptr = buff;
  217. size_t rec, upto;
  218. size_t count;
  219. end = buff ? buff + buff_size : NULL;
  220. /* Place some header information */
  221. if (ptr + sizeof(struct trace_output_hdr) < end)
  222. output_hdr = ptr;
  223. ptr += sizeof(struct trace_output_hdr);
  224. /* Add information about each call */
  225. count = hdr->ftrace_count;
  226. if (count > hdr->ftrace_size)
  227. count = hdr->ftrace_size;
  228. for (rec = upto = 0; rec < count; rec++) {
  229. if (ptr + sizeof(struct trace_call) < end) {
  230. struct trace_call *call = &hdr->ftrace[rec];
  231. struct trace_call *out = ptr;
  232. out->func = call->func * FUNC_SITE_SIZE;
  233. out->caller = call->caller * FUNC_SITE_SIZE;
  234. out->flags = call->flags;
  235. upto++;
  236. }
  237. ptr += sizeof(struct trace_call);
  238. }
  239. /* Update the header */
  240. if (output_hdr) {
  241. output_hdr->rec_count = upto;
  242. output_hdr->type = TRACE_CHUNK_CALLS;
  243. }
  244. /* Work out how must of the buffer we used */
  245. *needed = ptr - buff;
  246. if (ptr > end)
  247. return -ENOSPC;
  248. return 0;
  249. }
  250. /**
  251. * trace_print_stats() - print basic information about tracing
  252. */
  253. void trace_print_stats(void)
  254. {
  255. ulong count;
  256. #ifndef FTRACE
  257. puts("Warning: make U-Boot with FTRACE to enable function instrumenting.\n");
  258. puts("You will likely get zeroed data here\n");
  259. #endif
  260. if (!trace_inited) {
  261. printf("Trace is disabled\n");
  262. return;
  263. }
  264. print_grouped_ull(hdr->func_count, 10);
  265. puts(" function sites\n");
  266. print_grouped_ull(hdr->call_count, 10);
  267. puts(" function calls\n");
  268. print_grouped_ull(hdr->untracked_count, 10);
  269. puts(" untracked function calls\n");
  270. count = min(hdr->ftrace_count, hdr->ftrace_size);
  271. print_grouped_ull(count, 10);
  272. puts(" traced function calls");
  273. if (hdr->ftrace_count > hdr->ftrace_size) {
  274. printf(" (%lu dropped due to overflow)",
  275. hdr->ftrace_count - hdr->ftrace_size);
  276. }
  277. puts("\n");
  278. printf("%15d maximum observed call depth\n", hdr->max_depth);
  279. printf("%15d call depth limit\n", hdr->depth_limit);
  280. print_grouped_ull(hdr->ftrace_too_deep_count, 10);
  281. puts(" calls not traced due to depth\n");
  282. }
  283. void __attribute__((no_instrument_function)) trace_set_enabled(int enabled)
  284. {
  285. trace_enabled = enabled != 0;
  286. }
  287. /**
  288. * trace_init() - initialize the tracing system and enable it
  289. *
  290. * @buff: Pointer to trace buffer
  291. * @buff_size: Size of trace buffer
  292. * Return: 0 if ok
  293. */
  294. int __attribute__((no_instrument_function)) trace_init(void *buff,
  295. size_t buff_size)
  296. {
  297. ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
  298. size_t needed;
  299. int was_disabled = !trace_enabled;
  300. trace_save_gd();
  301. if (!was_disabled) {
  302. #ifdef CONFIG_TRACE_EARLY
  303. char *end;
  304. ulong used;
  305. /*
  306. * Copy over the early trace data if we have it. Disable
  307. * tracing while we are doing this.
  308. */
  309. trace_enabled = 0;
  310. hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR,
  311. CONFIG_TRACE_EARLY_SIZE);
  312. end = (char *)&hdr->ftrace[min(hdr->ftrace_count,
  313. hdr->ftrace_size)];
  314. used = end - (char *)hdr;
  315. printf("trace: copying %08lx bytes of early data from %x to %08lx\n",
  316. used, CONFIG_TRACE_EARLY_ADDR,
  317. (ulong)map_to_sysmem(buff));
  318. memcpy(buff, hdr, used);
  319. #else
  320. puts("trace: already enabled\n");
  321. return -EALREADY;
  322. #endif
  323. }
  324. hdr = (struct trace_hdr *)buff;
  325. needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
  326. if (needed > buff_size) {
  327. printf("trace: buffer size %zd bytes: at least %zd needed\n",
  328. buff_size, needed);
  329. return -ENOSPC;
  330. }
  331. if (was_disabled)
  332. memset(hdr, '\0', needed);
  333. hdr->func_count = func_count;
  334. hdr->call_accum = (uintptr_t *)(hdr + 1);
  335. /* Use any remaining space for the timed function trace */
  336. hdr->ftrace = (struct trace_call *)(buff + needed);
  337. hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
  338. add_textbase();
  339. puts("trace: enabled\n");
  340. hdr->depth_limit = CONFIG_TRACE_CALL_DEPTH_LIMIT;
  341. trace_enabled = 1;
  342. trace_inited = 1;
  343. return 0;
  344. }
  345. #ifdef CONFIG_TRACE_EARLY
  346. /**
  347. * trace_early_init() - initialize the tracing system for early tracing
  348. *
  349. * Return: 0 if ok, -ENOSPC if not enough memory is available
  350. */
  351. int __attribute__((no_instrument_function)) trace_early_init(void)
  352. {
  353. ulong func_count = gd->mon_len / FUNC_SITE_SIZE;
  354. size_t buff_size = CONFIG_TRACE_EARLY_SIZE;
  355. size_t needed;
  356. /* We can ignore additional calls to this function */
  357. if (trace_enabled)
  358. return 0;
  359. hdr = map_sysmem(CONFIG_TRACE_EARLY_ADDR, CONFIG_TRACE_EARLY_SIZE);
  360. needed = sizeof(*hdr) + func_count * sizeof(uintptr_t);
  361. if (needed > buff_size) {
  362. printf("trace: buffer size is %zd bytes, at least %zd needed\n",
  363. buff_size, needed);
  364. return -ENOSPC;
  365. }
  366. memset(hdr, '\0', needed);
  367. hdr->call_accum = (uintptr_t *)(hdr + 1);
  368. hdr->func_count = func_count;
  369. /* Use any remaining space for the timed function trace */
  370. hdr->ftrace = (struct trace_call *)((char *)hdr + needed);
  371. hdr->ftrace_size = (buff_size - needed) / sizeof(*hdr->ftrace);
  372. add_textbase();
  373. hdr->depth_limit = CONFIG_TRACE_EARLY_CALL_DEPTH_LIMIT;
  374. printf("trace: early enable at %08x\n", CONFIG_TRACE_EARLY_ADDR);
  375. trace_enabled = 1;
  376. return 0;
  377. }
  378. #endif