cs-etm.c 36 KB

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