cs-etm.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright(C) 2015-2018 Linaro Limited.
  4. *
  5. * Author: Tor Jeremiassen <tor@ti.com>
  6. * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/err.h>
  10. #include <linux/kernel.h>
  11. #include <linux/log2.h>
  12. #include <linux/types.h>
  13. #include <stdlib.h>
  14. #include "auxtrace.h"
  15. #include "color.h"
  16. #include "cs-etm.h"
  17. #include "cs-etm-decoder/cs-etm-decoder.h"
  18. #include "debug.h"
  19. #include "evlist.h"
  20. #include "intlist.h"
  21. #include "machine.h"
  22. #include "map.h"
  23. #include "perf.h"
  24. #include "thread.h"
  25. #include "thread_map.h"
  26. #include "thread-stack.h"
  27. #include "util.h"
  28. #define MAX_TIMESTAMP (~0ULL)
  29. /*
  30. * A64 instructions are always 4 bytes
  31. *
  32. * Only A64 is supported, so can use this constant for converting between
  33. * addresses and instruction counts, calculting offsets etc
  34. */
  35. #define A64_INSTR_SIZE 4
  36. struct cs_etm_auxtrace {
  37. struct auxtrace auxtrace;
  38. struct auxtrace_queues queues;
  39. struct auxtrace_heap heap;
  40. struct itrace_synth_opts synth_opts;
  41. struct perf_session *session;
  42. struct machine *machine;
  43. struct thread *unknown_thread;
  44. u8 timeless_decoding;
  45. u8 snapshot_mode;
  46. u8 data_queued;
  47. u8 sample_branches;
  48. u8 sample_instructions;
  49. int num_cpu;
  50. u32 auxtrace_type;
  51. u64 branches_sample_type;
  52. u64 branches_id;
  53. u64 instructions_sample_type;
  54. u64 instructions_sample_period;
  55. u64 instructions_id;
  56. u64 **metadata;
  57. u64 kernel_start;
  58. unsigned int pmu_type;
  59. };
  60. struct cs_etm_queue {
  61. struct cs_etm_auxtrace *etm;
  62. struct thread *thread;
  63. struct cs_etm_decoder *decoder;
  64. struct auxtrace_buffer *buffer;
  65. const struct cs_etm_state *state;
  66. union perf_event *event_buf;
  67. unsigned int queue_nr;
  68. pid_t pid, tid;
  69. int cpu;
  70. u64 time;
  71. u64 timestamp;
  72. u64 offset;
  73. u64 period_instructions;
  74. struct branch_stack *last_branch;
  75. struct branch_stack *last_branch_rb;
  76. size_t last_branch_pos;
  77. struct cs_etm_packet *prev_packet;
  78. struct cs_etm_packet *packet;
  79. };
  80. static int cs_etm__update_queues(struct cs_etm_auxtrace *etm);
  81. static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
  82. pid_t tid, u64 time_);
  83. static void cs_etm__packet_dump(const char *pkt_string)
  84. {
  85. const char *color = PERF_COLOR_BLUE;
  86. int len = strlen(pkt_string);
  87. if (len && (pkt_string[len-1] == '\n'))
  88. color_fprintf(stdout, color, " %s", pkt_string);
  89. else
  90. color_fprintf(stdout, color, " %s\n", pkt_string);
  91. fflush(stdout);
  92. }
  93. static void cs_etm__dump_event(struct cs_etm_auxtrace *etm,
  94. struct auxtrace_buffer *buffer)
  95. {
  96. int i, ret;
  97. const char *color = PERF_COLOR_BLUE;
  98. struct cs_etm_decoder_params d_params;
  99. struct cs_etm_trace_params *t_params;
  100. struct cs_etm_decoder *decoder;
  101. size_t buffer_used = 0;
  102. fprintf(stdout, "\n");
  103. color_fprintf(stdout, color,
  104. ". ... CoreSight ETM Trace data: size %zu bytes\n",
  105. buffer->size);
  106. /* Use metadata to fill in trace parameters for trace decoder */
  107. t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
  108. for (i = 0; i < etm->num_cpu; i++) {
  109. t_params[i].protocol = CS_ETM_PROTO_ETMV4i;
  110. t_params[i].etmv4.reg_idr0 = etm->metadata[i][CS_ETMV4_TRCIDR0];
  111. t_params[i].etmv4.reg_idr1 = etm->metadata[i][CS_ETMV4_TRCIDR1];
  112. t_params[i].etmv4.reg_idr2 = etm->metadata[i][CS_ETMV4_TRCIDR2];
  113. t_params[i].etmv4.reg_idr8 = etm->metadata[i][CS_ETMV4_TRCIDR8];
  114. t_params[i].etmv4.reg_configr =
  115. etm->metadata[i][CS_ETMV4_TRCCONFIGR];
  116. t_params[i].etmv4.reg_traceidr =
  117. etm->metadata[i][CS_ETMV4_TRCTRACEIDR];
  118. }
  119. /* Set decoder parameters to simply print the trace packets */
  120. d_params.packet_printer = cs_etm__packet_dump;
  121. d_params.operation = CS_ETM_OPERATION_PRINT;
  122. d_params.formatted = true;
  123. d_params.fsyncs = false;
  124. d_params.hsyncs = false;
  125. d_params.frame_aligned = true;
  126. decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
  127. zfree(&t_params);
  128. if (!decoder)
  129. return;
  130. do {
  131. size_t consumed;
  132. ret = cs_etm_decoder__process_data_block(
  133. decoder, buffer->offset,
  134. &((u8 *)buffer->data)[buffer_used],
  135. buffer->size - buffer_used, &consumed);
  136. if (ret)
  137. break;
  138. buffer_used += consumed;
  139. } while (buffer_used < buffer->size);
  140. cs_etm_decoder__free(decoder);
  141. }
  142. static int cs_etm__flush_events(struct perf_session *session,
  143. struct perf_tool *tool)
  144. {
  145. int ret;
  146. struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
  147. struct cs_etm_auxtrace,
  148. auxtrace);
  149. if (dump_trace)
  150. return 0;
  151. if (!tool->ordered_events)
  152. return -EINVAL;
  153. if (!etm->timeless_decoding)
  154. return -EINVAL;
  155. ret = cs_etm__update_queues(etm);
  156. if (ret < 0)
  157. return ret;
  158. return cs_etm__process_timeless_queues(etm, -1, MAX_TIMESTAMP - 1);
  159. }
  160. static void cs_etm__free_queue(void *priv)
  161. {
  162. struct cs_etm_queue *etmq = priv;
  163. if (!etmq)
  164. return;
  165. thread__zput(etmq->thread);
  166. cs_etm_decoder__free(etmq->decoder);
  167. zfree(&etmq->event_buf);
  168. zfree(&etmq->last_branch);
  169. zfree(&etmq->last_branch_rb);
  170. zfree(&etmq->prev_packet);
  171. zfree(&etmq->packet);
  172. free(etmq);
  173. }
  174. static void cs_etm__free_events(struct perf_session *session)
  175. {
  176. unsigned int i;
  177. struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
  178. struct cs_etm_auxtrace,
  179. auxtrace);
  180. struct auxtrace_queues *queues = &aux->queues;
  181. for (i = 0; i < queues->nr_queues; i++) {
  182. cs_etm__free_queue(queues->queue_array[i].priv);
  183. queues->queue_array[i].priv = NULL;
  184. }
  185. auxtrace_queues__free(queues);
  186. }
  187. static void cs_etm__free(struct perf_session *session)
  188. {
  189. int i;
  190. struct int_node *inode, *tmp;
  191. struct cs_etm_auxtrace *aux = container_of(session->auxtrace,
  192. struct cs_etm_auxtrace,
  193. auxtrace);
  194. cs_etm__free_events(session);
  195. session->auxtrace = NULL;
  196. /* First remove all traceID/CPU# nodes for the RB tree */
  197. intlist__for_each_entry_safe(inode, tmp, traceid_list)
  198. intlist__remove(traceid_list, inode);
  199. /* Then the RB tree itself */
  200. intlist__delete(traceid_list);
  201. for (i = 0; i < aux->num_cpu; i++)
  202. zfree(&aux->metadata[i]);
  203. thread__zput(aux->unknown_thread);
  204. zfree(&aux->metadata);
  205. zfree(&aux);
  206. }
  207. static u32 cs_etm__mem_access(struct cs_etm_queue *etmq, u64 address,
  208. size_t size, u8 *buffer)
  209. {
  210. u8 cpumode;
  211. u64 offset;
  212. int len;
  213. struct thread *thread;
  214. struct machine *machine;
  215. struct addr_location al;
  216. if (!etmq)
  217. return -1;
  218. machine = etmq->etm->machine;
  219. if (address >= etmq->etm->kernel_start)
  220. cpumode = PERF_RECORD_MISC_KERNEL;
  221. else
  222. cpumode = PERF_RECORD_MISC_USER;
  223. thread = etmq->thread;
  224. if (!thread) {
  225. if (cpumode != PERF_RECORD_MISC_KERNEL)
  226. return -EINVAL;
  227. thread = etmq->etm->unknown_thread;
  228. }
  229. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, address, &al);
  230. if (!al.map || !al.map->dso)
  231. return 0;
  232. if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
  233. dso__data_status_seen(al.map->dso, DSO_DATA_STATUS_SEEN_ITRACE))
  234. return 0;
  235. offset = al.map->map_ip(al.map, address);
  236. map__load(al.map);
  237. len = dso__data_read_offset(al.map->dso, machine, offset, buffer, size);
  238. if (len <= 0)
  239. return 0;
  240. return len;
  241. }
  242. static struct cs_etm_queue *cs_etm__alloc_queue(struct cs_etm_auxtrace *etm,
  243. unsigned int queue_nr)
  244. {
  245. int i;
  246. struct cs_etm_decoder_params d_params;
  247. struct cs_etm_trace_params *t_params;
  248. struct cs_etm_queue *etmq;
  249. size_t szp = sizeof(struct cs_etm_packet);
  250. etmq = zalloc(sizeof(*etmq));
  251. if (!etmq)
  252. return NULL;
  253. etmq->packet = zalloc(szp);
  254. if (!etmq->packet)
  255. goto out_free;
  256. if (etm->synth_opts.last_branch || etm->sample_branches) {
  257. etmq->prev_packet = zalloc(szp);
  258. if (!etmq->prev_packet)
  259. goto out_free;
  260. }
  261. if (etm->synth_opts.last_branch) {
  262. size_t sz = sizeof(struct branch_stack);
  263. sz += etm->synth_opts.last_branch_sz *
  264. sizeof(struct branch_entry);
  265. etmq->last_branch = zalloc(sz);
  266. if (!etmq->last_branch)
  267. goto out_free;
  268. etmq->last_branch_rb = zalloc(sz);
  269. if (!etmq->last_branch_rb)
  270. goto out_free;
  271. }
  272. etmq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
  273. if (!etmq->event_buf)
  274. goto out_free;
  275. etmq->etm = etm;
  276. etmq->queue_nr = queue_nr;
  277. etmq->pid = -1;
  278. etmq->tid = -1;
  279. etmq->cpu = -1;
  280. /* Use metadata to fill in trace parameters for trace decoder */
  281. t_params = zalloc(sizeof(*t_params) * etm->num_cpu);
  282. if (!t_params)
  283. goto out_free;
  284. for (i = 0; i < etm->num_cpu; i++) {
  285. t_params[i].protocol = CS_ETM_PROTO_ETMV4i;
  286. t_params[i].etmv4.reg_idr0 = etm->metadata[i][CS_ETMV4_TRCIDR0];
  287. t_params[i].etmv4.reg_idr1 = etm->metadata[i][CS_ETMV4_TRCIDR1];
  288. t_params[i].etmv4.reg_idr2 = etm->metadata[i][CS_ETMV4_TRCIDR2];
  289. t_params[i].etmv4.reg_idr8 = etm->metadata[i][CS_ETMV4_TRCIDR8];
  290. t_params[i].etmv4.reg_configr =
  291. etm->metadata[i][CS_ETMV4_TRCCONFIGR];
  292. t_params[i].etmv4.reg_traceidr =
  293. etm->metadata[i][CS_ETMV4_TRCTRACEIDR];
  294. }
  295. /* Set decoder parameters to simply print the trace packets */
  296. d_params.packet_printer = cs_etm__packet_dump;
  297. d_params.operation = CS_ETM_OPERATION_DECODE;
  298. d_params.formatted = true;
  299. d_params.fsyncs = false;
  300. d_params.hsyncs = false;
  301. d_params.frame_aligned = true;
  302. d_params.data = etmq;
  303. etmq->decoder = cs_etm_decoder__new(etm->num_cpu, &d_params, t_params);
  304. zfree(&t_params);
  305. if (!etmq->decoder)
  306. goto out_free;
  307. /*
  308. * Register a function to handle all memory accesses required by
  309. * the trace decoder library.
  310. */
  311. if (cs_etm_decoder__add_mem_access_cb(etmq->decoder,
  312. 0x0L, ((u64) -1L),
  313. cs_etm__mem_access))
  314. goto out_free_decoder;
  315. etmq->offset = 0;
  316. etmq->period_instructions = 0;
  317. return etmq;
  318. out_free_decoder:
  319. cs_etm_decoder__free(etmq->decoder);
  320. out_free:
  321. zfree(&etmq->event_buf);
  322. zfree(&etmq->last_branch);
  323. zfree(&etmq->last_branch_rb);
  324. zfree(&etmq->prev_packet);
  325. zfree(&etmq->packet);
  326. free(etmq);
  327. return NULL;
  328. }
  329. static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
  330. struct auxtrace_queue *queue,
  331. unsigned int queue_nr)
  332. {
  333. struct cs_etm_queue *etmq = queue->priv;
  334. if (list_empty(&queue->head) || etmq)
  335. return 0;
  336. etmq = cs_etm__alloc_queue(etm, queue_nr);
  337. if (!etmq)
  338. return -ENOMEM;
  339. queue->priv = etmq;
  340. if (queue->cpu != -1)
  341. etmq->cpu = queue->cpu;
  342. etmq->tid = queue->tid;
  343. return 0;
  344. }
  345. static int cs_etm__setup_queues(struct cs_etm_auxtrace *etm)
  346. {
  347. unsigned int i;
  348. int ret;
  349. for (i = 0; i < etm->queues.nr_queues; i++) {
  350. ret = cs_etm__setup_queue(etm, &etm->queues.queue_array[i], i);
  351. if (ret)
  352. return ret;
  353. }
  354. return 0;
  355. }
  356. static int cs_etm__update_queues(struct cs_etm_auxtrace *etm)
  357. {
  358. if (etm->queues.new_data) {
  359. etm->queues.new_data = false;
  360. return cs_etm__setup_queues(etm);
  361. }
  362. return 0;
  363. }
  364. static inline void cs_etm__copy_last_branch_rb(struct cs_etm_queue *etmq)
  365. {
  366. struct branch_stack *bs_src = etmq->last_branch_rb;
  367. struct branch_stack *bs_dst = etmq->last_branch;
  368. size_t nr = 0;
  369. /*
  370. * Set the number of records before early exit: ->nr is used to
  371. * determine how many branches to copy from ->entries.
  372. */
  373. bs_dst->nr = bs_src->nr;
  374. /*
  375. * Early exit when there is nothing to copy.
  376. */
  377. if (!bs_src->nr)
  378. return;
  379. /*
  380. * As bs_src->entries is a circular buffer, we need to copy from it in
  381. * two steps. First, copy the branches from the most recently inserted
  382. * branch ->last_branch_pos until the end of bs_src->entries buffer.
  383. */
  384. nr = etmq->etm->synth_opts.last_branch_sz - etmq->last_branch_pos;
  385. memcpy(&bs_dst->entries[0],
  386. &bs_src->entries[etmq->last_branch_pos],
  387. sizeof(struct branch_entry) * nr);
  388. /*
  389. * If we wrapped around at least once, the branches from the beginning
  390. * of the bs_src->entries buffer and until the ->last_branch_pos element
  391. * are older valid branches: copy them over. The total number of
  392. * branches copied over will be equal to the number of branches asked by
  393. * the user in last_branch_sz.
  394. */
  395. if (bs_src->nr >= etmq->etm->synth_opts.last_branch_sz) {
  396. memcpy(&bs_dst->entries[nr],
  397. &bs_src->entries[0],
  398. sizeof(struct branch_entry) * etmq->last_branch_pos);
  399. }
  400. }
  401. static inline void cs_etm__reset_last_branch_rb(struct cs_etm_queue *etmq)
  402. {
  403. etmq->last_branch_pos = 0;
  404. etmq->last_branch_rb->nr = 0;
  405. }
  406. static inline u64 cs_etm__last_executed_instr(struct cs_etm_packet *packet)
  407. {
  408. /*
  409. * The packet records the execution range with an exclusive end address
  410. *
  411. * A64 instructions are constant size, so the last executed
  412. * instruction is A64_INSTR_SIZE before the end address
  413. * Will need to do instruction level decode for T32 instructions as
  414. * they can be variable size (not yet supported).
  415. */
  416. return packet->end_addr - A64_INSTR_SIZE;
  417. }
  418. static inline u64 cs_etm__instr_count(const struct cs_etm_packet *packet)
  419. {
  420. /*
  421. * Only A64 instructions are currently supported, so can get
  422. * instruction count by dividing.
  423. * Will need to do instruction level decode for T32 instructions as
  424. * they can be variable size (not yet supported).
  425. */
  426. return (packet->end_addr - packet->start_addr) / A64_INSTR_SIZE;
  427. }
  428. static inline u64 cs_etm__instr_addr(const struct cs_etm_packet *packet,
  429. u64 offset)
  430. {
  431. /*
  432. * Only A64 instructions are currently supported, so can get
  433. * instruction address by muliplying.
  434. * Will need to do instruction level decode for T32 instructions as
  435. * they can be variable size (not yet supported).
  436. */
  437. return packet->start_addr + offset * A64_INSTR_SIZE;
  438. }
  439. static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq)
  440. {
  441. struct branch_stack *bs = etmq->last_branch_rb;
  442. struct branch_entry *be;
  443. /*
  444. * The branches are recorded in a circular buffer in reverse
  445. * chronological order: we start recording from the last element of the
  446. * buffer down. After writing the first element of the stack, move the
  447. * insert position back to the end of the buffer.
  448. */
  449. if (!etmq->last_branch_pos)
  450. etmq->last_branch_pos = etmq->etm->synth_opts.last_branch_sz;
  451. etmq->last_branch_pos -= 1;
  452. be = &bs->entries[etmq->last_branch_pos];
  453. be->from = cs_etm__last_executed_instr(etmq->prev_packet);
  454. be->to = etmq->packet->start_addr;
  455. /* No support for mispredict */
  456. be->flags.mispred = 0;
  457. be->flags.predicted = 1;
  458. /*
  459. * Increment bs->nr until reaching the number of last branches asked by
  460. * the user on the command line.
  461. */
  462. if (bs->nr < etmq->etm->synth_opts.last_branch_sz)
  463. bs->nr += 1;
  464. }
  465. static int cs_etm__inject_event(union perf_event *event,
  466. struct perf_sample *sample, u64 type)
  467. {
  468. event->header.size = perf_event__sample_event_size(sample, type, 0);
  469. return perf_event__synthesize_sample(event, type, 0, sample);
  470. }
  471. static int
  472. cs_etm__get_trace(struct cs_etm_buffer *buff, struct cs_etm_queue *etmq)
  473. {
  474. struct auxtrace_buffer *aux_buffer = etmq->buffer;
  475. struct auxtrace_buffer *old_buffer = aux_buffer;
  476. struct auxtrace_queue *queue;
  477. queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
  478. aux_buffer = auxtrace_buffer__next(queue, aux_buffer);
  479. /* If no more data, drop the previous auxtrace_buffer and return */
  480. if (!aux_buffer) {
  481. if (old_buffer)
  482. auxtrace_buffer__drop_data(old_buffer);
  483. buff->len = 0;
  484. return 0;
  485. }
  486. etmq->buffer = aux_buffer;
  487. /* If the aux_buffer doesn't have data associated, try to load it */
  488. if (!aux_buffer->data) {
  489. /* get the file desc associated with the perf data file */
  490. int fd = perf_data__fd(etmq->etm->session->data);
  491. aux_buffer->data = auxtrace_buffer__get_data(aux_buffer, fd);
  492. if (!aux_buffer->data)
  493. return -ENOMEM;
  494. }
  495. /* If valid, drop the previous buffer */
  496. if (old_buffer)
  497. auxtrace_buffer__drop_data(old_buffer);
  498. buff->offset = aux_buffer->offset;
  499. buff->len = aux_buffer->size;
  500. buff->buf = aux_buffer->data;
  501. buff->ref_timestamp = aux_buffer->reference;
  502. return buff->len;
  503. }
  504. static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm,
  505. struct auxtrace_queue *queue)
  506. {
  507. struct cs_etm_queue *etmq = queue->priv;
  508. /* CPU-wide tracing isn't supported yet */
  509. if (queue->tid == -1)
  510. return;
  511. if ((!etmq->thread) && (etmq->tid != -1))
  512. etmq->thread = machine__find_thread(etm->machine, -1,
  513. etmq->tid);
  514. if (etmq->thread) {
  515. etmq->pid = etmq->thread->pid_;
  516. if (queue->cpu == -1)
  517. etmq->cpu = etmq->thread->cpu;
  518. }
  519. }
  520. static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
  521. u64 addr, u64 period)
  522. {
  523. int ret = 0;
  524. struct cs_etm_auxtrace *etm = etmq->etm;
  525. union perf_event *event = etmq->event_buf;
  526. struct perf_sample sample = {.ip = 0,};
  527. event->sample.header.type = PERF_RECORD_SAMPLE;
  528. event->sample.header.misc = PERF_RECORD_MISC_USER;
  529. event->sample.header.size = sizeof(struct perf_event_header);
  530. sample.ip = addr;
  531. sample.pid = etmq->pid;
  532. sample.tid = etmq->tid;
  533. sample.id = etmq->etm->instructions_id;
  534. sample.stream_id = etmq->etm->instructions_id;
  535. sample.period = period;
  536. sample.cpu = etmq->packet->cpu;
  537. sample.flags = 0;
  538. sample.insn_len = 1;
  539. sample.cpumode = event->header.misc;
  540. if (etm->synth_opts.last_branch) {
  541. cs_etm__copy_last_branch_rb(etmq);
  542. sample.branch_stack = etmq->last_branch;
  543. }
  544. if (etm->synth_opts.inject) {
  545. ret = cs_etm__inject_event(event, &sample,
  546. etm->instructions_sample_type);
  547. if (ret)
  548. return ret;
  549. }
  550. ret = perf_session__deliver_synth_event(etm->session, event, &sample);
  551. if (ret)
  552. pr_err(
  553. "CS ETM Trace: failed to deliver instruction event, error %d\n",
  554. ret);
  555. if (etm->synth_opts.last_branch)
  556. cs_etm__reset_last_branch_rb(etmq);
  557. return ret;
  558. }
  559. /*
  560. * The cs etm packet encodes an instruction range between a branch target
  561. * and the next taken branch. Generate sample accordingly.
  562. */
  563. static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq)
  564. {
  565. int ret = 0;
  566. struct cs_etm_auxtrace *etm = etmq->etm;
  567. struct perf_sample sample = {.ip = 0,};
  568. union perf_event *event = etmq->event_buf;
  569. struct dummy_branch_stack {
  570. u64 nr;
  571. struct branch_entry entries;
  572. } dummy_bs;
  573. event->sample.header.type = PERF_RECORD_SAMPLE;
  574. event->sample.header.misc = PERF_RECORD_MISC_USER;
  575. event->sample.header.size = sizeof(struct perf_event_header);
  576. sample.ip = cs_etm__last_executed_instr(etmq->prev_packet);
  577. sample.pid = etmq->pid;
  578. sample.tid = etmq->tid;
  579. sample.addr = etmq->packet->start_addr;
  580. sample.id = etmq->etm->branches_id;
  581. sample.stream_id = etmq->etm->branches_id;
  582. sample.period = 1;
  583. sample.cpu = etmq->packet->cpu;
  584. sample.flags = 0;
  585. sample.cpumode = PERF_RECORD_MISC_USER;
  586. /*
  587. * perf report cannot handle events without a branch stack
  588. */
  589. if (etm->synth_opts.last_branch) {
  590. dummy_bs = (struct dummy_branch_stack){
  591. .nr = 1,
  592. .entries = {
  593. .from = sample.ip,
  594. .to = sample.addr,
  595. },
  596. };
  597. sample.branch_stack = (struct branch_stack *)&dummy_bs;
  598. }
  599. if (etm->synth_opts.inject) {
  600. ret = cs_etm__inject_event(event, &sample,
  601. etm->branches_sample_type);
  602. if (ret)
  603. return ret;
  604. }
  605. ret = perf_session__deliver_synth_event(etm->session, event, &sample);
  606. if (ret)
  607. pr_err(
  608. "CS ETM Trace: failed to deliver instruction event, error %d\n",
  609. ret);
  610. return ret;
  611. }
  612. struct cs_etm_synth {
  613. struct perf_tool dummy_tool;
  614. struct perf_session *session;
  615. };
  616. static int cs_etm__event_synth(struct perf_tool *tool,
  617. union perf_event *event,
  618. struct perf_sample *sample __maybe_unused,
  619. struct machine *machine __maybe_unused)
  620. {
  621. struct cs_etm_synth *cs_etm_synth =
  622. container_of(tool, struct cs_etm_synth, dummy_tool);
  623. return perf_session__deliver_synth_event(cs_etm_synth->session,
  624. event, NULL);
  625. }
  626. static int cs_etm__synth_event(struct perf_session *session,
  627. struct perf_event_attr *attr, u64 id)
  628. {
  629. struct cs_etm_synth cs_etm_synth;
  630. memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth));
  631. cs_etm_synth.session = session;
  632. return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1,
  633. &id, cs_etm__event_synth);
  634. }
  635. static int cs_etm__synth_events(struct cs_etm_auxtrace *etm,
  636. struct perf_session *session)
  637. {
  638. struct perf_evlist *evlist = session->evlist;
  639. struct perf_evsel *evsel;
  640. struct perf_event_attr attr;
  641. bool found = false;
  642. u64 id;
  643. int err;
  644. evlist__for_each_entry(evlist, evsel) {
  645. if (evsel->attr.type == etm->pmu_type) {
  646. found = true;
  647. break;
  648. }
  649. }
  650. if (!found) {
  651. pr_debug("No selected events with CoreSight Trace data\n");
  652. return 0;
  653. }
  654. memset(&attr, 0, sizeof(struct perf_event_attr));
  655. attr.size = sizeof(struct perf_event_attr);
  656. attr.type = PERF_TYPE_HARDWARE;
  657. attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
  658. attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
  659. PERF_SAMPLE_PERIOD;
  660. if (etm->timeless_decoding)
  661. attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
  662. else
  663. attr.sample_type |= PERF_SAMPLE_TIME;
  664. attr.exclude_user = evsel->attr.exclude_user;
  665. attr.exclude_kernel = evsel->attr.exclude_kernel;
  666. attr.exclude_hv = evsel->attr.exclude_hv;
  667. attr.exclude_host = evsel->attr.exclude_host;
  668. attr.exclude_guest = evsel->attr.exclude_guest;
  669. attr.sample_id_all = evsel->attr.sample_id_all;
  670. attr.read_format = evsel->attr.read_format;
  671. /* create new id val to be a fixed offset from evsel id */
  672. id = evsel->id[0] + 1000000000;
  673. if (!id)
  674. id = 1;
  675. if (etm->synth_opts.branches) {
  676. attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
  677. attr.sample_period = 1;
  678. attr.sample_type |= PERF_SAMPLE_ADDR;
  679. err = cs_etm__synth_event(session, &attr, id);
  680. if (err)
  681. return err;
  682. etm->sample_branches = true;
  683. etm->branches_sample_type = attr.sample_type;
  684. etm->branches_id = id;
  685. id += 1;
  686. attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
  687. }
  688. if (etm->synth_opts.last_branch)
  689. attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
  690. if (etm->synth_opts.instructions) {
  691. attr.config = PERF_COUNT_HW_INSTRUCTIONS;
  692. attr.sample_period = etm->synth_opts.period;
  693. etm->instructions_sample_period = attr.sample_period;
  694. err = cs_etm__synth_event(session, &attr, id);
  695. if (err)
  696. return err;
  697. etm->sample_instructions = true;
  698. etm->instructions_sample_type = attr.sample_type;
  699. etm->instructions_id = id;
  700. id += 1;
  701. }
  702. return 0;
  703. }
  704. static int cs_etm__sample(struct cs_etm_queue *etmq)
  705. {
  706. struct cs_etm_auxtrace *etm = etmq->etm;
  707. struct cs_etm_packet *tmp;
  708. int ret;
  709. u64 instrs_executed;
  710. instrs_executed = cs_etm__instr_count(etmq->packet);
  711. etmq->period_instructions += instrs_executed;
  712. /*
  713. * Record a branch when the last instruction in
  714. * PREV_PACKET is a branch.
  715. */
  716. if (etm->synth_opts.last_branch &&
  717. etmq->prev_packet &&
  718. etmq->prev_packet->sample_type == CS_ETM_RANGE &&
  719. etmq->prev_packet->last_instr_taken_branch)
  720. cs_etm__update_last_branch_rb(etmq);
  721. if (etm->sample_instructions &&
  722. etmq->period_instructions >= etm->instructions_sample_period) {
  723. /*
  724. * Emit instruction sample periodically
  725. * TODO: allow period to be defined in cycles and clock time
  726. */
  727. /* Get number of instructions executed after the sample point */
  728. u64 instrs_over = etmq->period_instructions -
  729. etm->instructions_sample_period;
  730. /*
  731. * Calculate the address of the sampled instruction (-1 as
  732. * sample is reported as though instruction has just been
  733. * executed, but PC has not advanced to next instruction)
  734. */
  735. u64 offset = (instrs_executed - instrs_over - 1);
  736. u64 addr = cs_etm__instr_addr(etmq->packet, offset);
  737. ret = cs_etm__synth_instruction_sample(
  738. etmq, addr, etm->instructions_sample_period);
  739. if (ret)
  740. return ret;
  741. /* Carry remaining instructions into next sample period */
  742. etmq->period_instructions = instrs_over;
  743. }
  744. if (etm->sample_branches &&
  745. etmq->prev_packet &&
  746. etmq->prev_packet->sample_type == CS_ETM_RANGE &&
  747. etmq->prev_packet->last_instr_taken_branch) {
  748. ret = cs_etm__synth_branch_sample(etmq);
  749. if (ret)
  750. return ret;
  751. }
  752. if (etm->sample_branches || etm->synth_opts.last_branch) {
  753. /*
  754. * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
  755. * the next incoming packet.
  756. */
  757. tmp = etmq->packet;
  758. etmq->packet = etmq->prev_packet;
  759. etmq->prev_packet = tmp;
  760. }
  761. return 0;
  762. }
  763. static int cs_etm__flush(struct cs_etm_queue *etmq)
  764. {
  765. int err = 0;
  766. struct cs_etm_packet *tmp;
  767. if (etmq->etm->synth_opts.last_branch &&
  768. etmq->prev_packet &&
  769. etmq->prev_packet->sample_type == CS_ETM_RANGE) {
  770. /*
  771. * Generate a last branch event for the branches left in the
  772. * circular buffer at the end of the trace.
  773. *
  774. * Use the address of the end of the last reported execution
  775. * range
  776. */
  777. u64 addr = cs_etm__last_executed_instr(etmq->prev_packet);
  778. err = cs_etm__synth_instruction_sample(
  779. etmq, addr,
  780. etmq->period_instructions);
  781. etmq->period_instructions = 0;
  782. /*
  783. * Swap PACKET with PREV_PACKET: PACKET becomes PREV_PACKET for
  784. * the next incoming packet.
  785. */
  786. tmp = etmq->packet;
  787. etmq->packet = etmq->prev_packet;
  788. etmq->prev_packet = tmp;
  789. }
  790. return err;
  791. }
  792. static int cs_etm__run_decoder(struct cs_etm_queue *etmq)
  793. {
  794. struct cs_etm_auxtrace *etm = etmq->etm;
  795. struct cs_etm_buffer buffer;
  796. size_t buffer_used, processed;
  797. int err = 0;
  798. if (!etm->kernel_start)
  799. etm->kernel_start = machine__kernel_start(etm->machine);
  800. /* Go through each buffer in the queue and decode them one by one */
  801. while (1) {
  802. buffer_used = 0;
  803. memset(&buffer, 0, sizeof(buffer));
  804. err = cs_etm__get_trace(&buffer, etmq);
  805. if (err <= 0)
  806. return err;
  807. /*
  808. * We cannot assume consecutive blocks in the data file are
  809. * contiguous, reset the decoder to force re-sync.
  810. */
  811. err = cs_etm_decoder__reset(etmq->decoder);
  812. if (err != 0)
  813. return err;
  814. /* Run trace decoder until buffer consumed or end of trace */
  815. do {
  816. processed = 0;
  817. err = cs_etm_decoder__process_data_block(
  818. etmq->decoder,
  819. etmq->offset,
  820. &buffer.buf[buffer_used],
  821. buffer.len - buffer_used,
  822. &processed);
  823. if (err)
  824. return err;
  825. etmq->offset += processed;
  826. buffer_used += processed;
  827. /* Process each packet in this chunk */
  828. while (1) {
  829. err = cs_etm_decoder__get_packet(etmq->decoder,
  830. etmq->packet);
  831. if (err <= 0)
  832. /*
  833. * Stop processing this chunk on
  834. * end of data or error
  835. */
  836. break;
  837. switch (etmq->packet->sample_type) {
  838. case CS_ETM_RANGE:
  839. /*
  840. * If the packet contains an instruction
  841. * range, generate instruction sequence
  842. * events.
  843. */
  844. cs_etm__sample(etmq);
  845. break;
  846. case CS_ETM_TRACE_ON:
  847. /*
  848. * Discontinuity in trace, flush
  849. * previous branch stack
  850. */
  851. cs_etm__flush(etmq);
  852. break;
  853. default:
  854. break;
  855. }
  856. }
  857. } while (buffer.len > buffer_used);
  858. if (err == 0)
  859. /* Flush any remaining branch stack entries */
  860. err = cs_etm__flush(etmq);
  861. }
  862. return err;
  863. }
  864. static int cs_etm__process_timeless_queues(struct cs_etm_auxtrace *etm,
  865. pid_t tid, u64 time_)
  866. {
  867. unsigned int i;
  868. struct auxtrace_queues *queues = &etm->queues;
  869. for (i = 0; i < queues->nr_queues; i++) {
  870. struct auxtrace_queue *queue = &etm->queues.queue_array[i];
  871. struct cs_etm_queue *etmq = queue->priv;
  872. if (etmq && ((tid == -1) || (etmq->tid == tid))) {
  873. etmq->time = time_;
  874. cs_etm__set_pid_tid_cpu(etm, queue);
  875. cs_etm__run_decoder(etmq);
  876. }
  877. }
  878. return 0;
  879. }
  880. static int cs_etm__process_event(struct perf_session *session,
  881. union perf_event *event,
  882. struct perf_sample *sample,
  883. struct perf_tool *tool)
  884. {
  885. int err = 0;
  886. u64 timestamp;
  887. struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
  888. struct cs_etm_auxtrace,
  889. auxtrace);
  890. if (dump_trace)
  891. return 0;
  892. if (!tool->ordered_events) {
  893. pr_err("CoreSight ETM Trace requires ordered events\n");
  894. return -EINVAL;
  895. }
  896. if (!etm->timeless_decoding)
  897. return -EINVAL;
  898. if (sample->time && (sample->time != (u64) -1))
  899. timestamp = sample->time;
  900. else
  901. timestamp = 0;
  902. if (timestamp || etm->timeless_decoding) {
  903. err = cs_etm__update_queues(etm);
  904. if (err)
  905. return err;
  906. }
  907. if (event->header.type == PERF_RECORD_EXIT)
  908. return cs_etm__process_timeless_queues(etm,
  909. event->fork.tid,
  910. sample->time);
  911. return 0;
  912. }
  913. static int cs_etm__process_auxtrace_event(struct perf_session *session,
  914. union perf_event *event,
  915. struct perf_tool *tool __maybe_unused)
  916. {
  917. struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
  918. struct cs_etm_auxtrace,
  919. auxtrace);
  920. if (!etm->data_queued) {
  921. struct auxtrace_buffer *buffer;
  922. off_t data_offset;
  923. int fd = perf_data__fd(session->data);
  924. bool is_pipe = perf_data__is_pipe(session->data);
  925. int err;
  926. if (is_pipe)
  927. data_offset = 0;
  928. else {
  929. data_offset = lseek(fd, 0, SEEK_CUR);
  930. if (data_offset == -1)
  931. return -errno;
  932. }
  933. err = auxtrace_queues__add_event(&etm->queues, session,
  934. event, data_offset, &buffer);
  935. if (err)
  936. return err;
  937. if (dump_trace)
  938. if (auxtrace_buffer__get_data(buffer, fd)) {
  939. cs_etm__dump_event(etm, buffer);
  940. auxtrace_buffer__put_data(buffer);
  941. }
  942. }
  943. return 0;
  944. }
  945. static bool cs_etm__is_timeless_decoding(struct cs_etm_auxtrace *etm)
  946. {
  947. struct perf_evsel *evsel;
  948. struct perf_evlist *evlist = etm->session->evlist;
  949. bool timeless_decoding = true;
  950. /*
  951. * Circle through the list of event and complain if we find one
  952. * with the time bit set.
  953. */
  954. evlist__for_each_entry(evlist, evsel) {
  955. if ((evsel->attr.sample_type & PERF_SAMPLE_TIME))
  956. timeless_decoding = false;
  957. }
  958. return timeless_decoding;
  959. }
  960. static const char * const cs_etm_global_header_fmts[] = {
  961. [CS_HEADER_VERSION_0] = " Header version %llx\n",
  962. [CS_PMU_TYPE_CPUS] = " PMU type/num cpus %llx\n",
  963. [CS_ETM_SNAPSHOT] = " Snapshot %llx\n",
  964. };
  965. static const char * const cs_etm_priv_fmts[] = {
  966. [CS_ETM_MAGIC] = " Magic number %llx\n",
  967. [CS_ETM_CPU] = " CPU %lld\n",
  968. [CS_ETM_ETMCR] = " ETMCR %llx\n",
  969. [CS_ETM_ETMTRACEIDR] = " ETMTRACEIDR %llx\n",
  970. [CS_ETM_ETMCCER] = " ETMCCER %llx\n",
  971. [CS_ETM_ETMIDR] = " ETMIDR %llx\n",
  972. };
  973. static const char * const cs_etmv4_priv_fmts[] = {
  974. [CS_ETM_MAGIC] = " Magic number %llx\n",
  975. [CS_ETM_CPU] = " CPU %lld\n",
  976. [CS_ETMV4_TRCCONFIGR] = " TRCCONFIGR %llx\n",
  977. [CS_ETMV4_TRCTRACEIDR] = " TRCTRACEIDR %llx\n",
  978. [CS_ETMV4_TRCIDR0] = " TRCIDR0 %llx\n",
  979. [CS_ETMV4_TRCIDR1] = " TRCIDR1 %llx\n",
  980. [CS_ETMV4_TRCIDR2] = " TRCIDR2 %llx\n",
  981. [CS_ETMV4_TRCIDR8] = " TRCIDR8 %llx\n",
  982. [CS_ETMV4_TRCAUTHSTATUS] = " TRCAUTHSTATUS %llx\n",
  983. };
  984. static void cs_etm__print_auxtrace_info(u64 *val, int num)
  985. {
  986. int i, j, cpu = 0;
  987. for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
  988. fprintf(stdout, cs_etm_global_header_fmts[i], val[i]);
  989. for (i = CS_HEADER_VERSION_0_MAX; cpu < num; cpu++) {
  990. if (val[i] == __perf_cs_etmv3_magic)
  991. for (j = 0; j < CS_ETM_PRIV_MAX; j++, i++)
  992. fprintf(stdout, cs_etm_priv_fmts[j], val[i]);
  993. else if (val[i] == __perf_cs_etmv4_magic)
  994. for (j = 0; j < CS_ETMV4_PRIV_MAX; j++, i++)
  995. fprintf(stdout, cs_etmv4_priv_fmts[j], val[i]);
  996. else
  997. /* failure.. return */
  998. return;
  999. }
  1000. }
  1001. int cs_etm__process_auxtrace_info(union perf_event *event,
  1002. struct perf_session *session)
  1003. {
  1004. struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
  1005. struct cs_etm_auxtrace *etm = NULL;
  1006. struct int_node *inode;
  1007. unsigned int pmu_type;
  1008. int event_header_size = sizeof(struct perf_event_header);
  1009. int info_header_size;
  1010. int total_size = auxtrace_info->header.size;
  1011. int priv_size = 0;
  1012. int num_cpu;
  1013. int err = 0, idx = -1;
  1014. int i, j, k;
  1015. u64 *ptr, *hdr = NULL;
  1016. u64 **metadata = NULL;
  1017. /*
  1018. * sizeof(auxtrace_info_event::type) +
  1019. * sizeof(auxtrace_info_event::reserved) == 8
  1020. */
  1021. info_header_size = 8;
  1022. if (total_size < (event_header_size + info_header_size))
  1023. return -EINVAL;
  1024. priv_size = total_size - event_header_size - info_header_size;
  1025. /* First the global part */
  1026. ptr = (u64 *) auxtrace_info->priv;
  1027. /* Look for version '0' of the header */
  1028. if (ptr[0] != 0)
  1029. return -EINVAL;
  1030. hdr = zalloc(sizeof(*hdr) * CS_HEADER_VERSION_0_MAX);
  1031. if (!hdr)
  1032. return -ENOMEM;
  1033. /* Extract header information - see cs-etm.h for format */
  1034. for (i = 0; i < CS_HEADER_VERSION_0_MAX; i++)
  1035. hdr[i] = ptr[i];
  1036. num_cpu = hdr[CS_PMU_TYPE_CPUS] & 0xffffffff;
  1037. pmu_type = (unsigned int) ((hdr[CS_PMU_TYPE_CPUS] >> 32) &
  1038. 0xffffffff);
  1039. /*
  1040. * Create an RB tree for traceID-CPU# tuple. Since the conversion has
  1041. * to be made for each packet that gets decoded, optimizing access in
  1042. * anything other than a sequential array is worth doing.
  1043. */
  1044. traceid_list = intlist__new(NULL);
  1045. if (!traceid_list) {
  1046. err = -ENOMEM;
  1047. goto err_free_hdr;
  1048. }
  1049. metadata = zalloc(sizeof(*metadata) * num_cpu);
  1050. if (!metadata) {
  1051. err = -ENOMEM;
  1052. goto err_free_traceid_list;
  1053. }
  1054. /*
  1055. * The metadata is stored in the auxtrace_info section and encodes
  1056. * the configuration of the ARM embedded trace macrocell which is
  1057. * required by the trace decoder to properly decode the trace due
  1058. * to its highly compressed nature.
  1059. */
  1060. for (j = 0; j < num_cpu; j++) {
  1061. if (ptr[i] == __perf_cs_etmv3_magic) {
  1062. metadata[j] = zalloc(sizeof(*metadata[j]) *
  1063. CS_ETM_PRIV_MAX);
  1064. if (!metadata[j]) {
  1065. err = -ENOMEM;
  1066. goto err_free_metadata;
  1067. }
  1068. for (k = 0; k < CS_ETM_PRIV_MAX; k++)
  1069. metadata[j][k] = ptr[i + k];
  1070. /* The traceID is our handle */
  1071. idx = metadata[j][CS_ETM_ETMTRACEIDR];
  1072. i += CS_ETM_PRIV_MAX;
  1073. } else if (ptr[i] == __perf_cs_etmv4_magic) {
  1074. metadata[j] = zalloc(sizeof(*metadata[j]) *
  1075. CS_ETMV4_PRIV_MAX);
  1076. if (!metadata[j]) {
  1077. err = -ENOMEM;
  1078. goto err_free_metadata;
  1079. }
  1080. for (k = 0; k < CS_ETMV4_PRIV_MAX; k++)
  1081. metadata[j][k] = ptr[i + k];
  1082. /* The traceID is our handle */
  1083. idx = metadata[j][CS_ETMV4_TRCTRACEIDR];
  1084. i += CS_ETMV4_PRIV_MAX;
  1085. }
  1086. /* Get an RB node for this CPU */
  1087. inode = intlist__findnew(traceid_list, idx);
  1088. /* Something went wrong, no need to continue */
  1089. if (!inode) {
  1090. err = PTR_ERR(inode);
  1091. goto err_free_metadata;
  1092. }
  1093. /*
  1094. * The node for that CPU should not be taken.
  1095. * Back out if that's the case.
  1096. */
  1097. if (inode->priv) {
  1098. err = -EINVAL;
  1099. goto err_free_metadata;
  1100. }
  1101. /* All good, associate the traceID with the CPU# */
  1102. inode->priv = &metadata[j][CS_ETM_CPU];
  1103. }
  1104. /*
  1105. * Each of CS_HEADER_VERSION_0_MAX, CS_ETM_PRIV_MAX and
  1106. * CS_ETMV4_PRIV_MAX mark how many double words are in the
  1107. * global metadata, and each cpu's metadata respectively.
  1108. * The following tests if the correct number of double words was
  1109. * present in the auxtrace info section.
  1110. */
  1111. if (i * 8 != priv_size) {
  1112. err = -EINVAL;
  1113. goto err_free_metadata;
  1114. }
  1115. etm = zalloc(sizeof(*etm));
  1116. if (!etm) {
  1117. err = -ENOMEM;
  1118. goto err_free_metadata;
  1119. }
  1120. err = auxtrace_queues__init(&etm->queues);
  1121. if (err)
  1122. goto err_free_etm;
  1123. etm->session = session;
  1124. etm->machine = &session->machines.host;
  1125. etm->num_cpu = num_cpu;
  1126. etm->pmu_type = pmu_type;
  1127. etm->snapshot_mode = (hdr[CS_ETM_SNAPSHOT] != 0);
  1128. etm->metadata = metadata;
  1129. etm->auxtrace_type = auxtrace_info->type;
  1130. etm->timeless_decoding = cs_etm__is_timeless_decoding(etm);
  1131. etm->auxtrace.process_event = cs_etm__process_event;
  1132. etm->auxtrace.process_auxtrace_event = cs_etm__process_auxtrace_event;
  1133. etm->auxtrace.flush_events = cs_etm__flush_events;
  1134. etm->auxtrace.free_events = cs_etm__free_events;
  1135. etm->auxtrace.free = cs_etm__free;
  1136. session->auxtrace = &etm->auxtrace;
  1137. etm->unknown_thread = thread__new(999999999, 999999999);
  1138. if (!etm->unknown_thread)
  1139. goto err_free_queues;
  1140. /*
  1141. * Initialize list node so that at thread__zput() we can avoid
  1142. * segmentation fault at list_del_init().
  1143. */
  1144. INIT_LIST_HEAD(&etm->unknown_thread->node);
  1145. err = thread__set_comm(etm->unknown_thread, "unknown", 0);
  1146. if (err)
  1147. goto err_delete_thread;
  1148. if (thread__init_map_groups(etm->unknown_thread, etm->machine))
  1149. goto err_delete_thread;
  1150. if (dump_trace) {
  1151. cs_etm__print_auxtrace_info(auxtrace_info->priv, num_cpu);
  1152. return 0;
  1153. }
  1154. if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
  1155. etm->synth_opts = *session->itrace_synth_opts;
  1156. } else {
  1157. itrace_synth_opts__set_default(&etm->synth_opts);
  1158. etm->synth_opts.callchain = false;
  1159. }
  1160. err = cs_etm__synth_events(etm, session);
  1161. if (err)
  1162. goto err_delete_thread;
  1163. err = auxtrace_queues__process_index(&etm->queues, session);
  1164. if (err)
  1165. goto err_delete_thread;
  1166. etm->data_queued = etm->queues.populated;
  1167. return 0;
  1168. err_delete_thread:
  1169. thread__zput(etm->unknown_thread);
  1170. err_free_queues:
  1171. auxtrace_queues__free(&etm->queues);
  1172. session->auxtrace = NULL;
  1173. err_free_etm:
  1174. zfree(&etm);
  1175. err_free_metadata:
  1176. /* No need to check @metadata[j], free(NULL) is supported */
  1177. for (j = 0; j < num_cpu; j++)
  1178. free(metadata[j]);
  1179. zfree(&metadata);
  1180. err_free_traceid_list:
  1181. intlist__delete(traceid_list);
  1182. err_free_hdr:
  1183. zfree(&hdr);
  1184. return -EINVAL;
  1185. }