cs-etm.c 37 KB

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