cs-etm.c 37 KB

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