cs-etm.c 38 KB

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