cs-etm-decoder.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 <asm/bug.h>
  9. #include <linux/coresight-pmu.h>
  10. #include <linux/err.h>
  11. #include <linux/list.h>
  12. #include <linux/zalloc.h>
  13. #include <stdlib.h>
  14. #include <opencsd/c_api/opencsd_c_api.h>
  15. #include "cs-etm.h"
  16. #include "cs-etm-decoder.h"
  17. #include "debug.h"
  18. #include "intlist.h"
  19. /* use raw logging */
  20. #ifdef CS_DEBUG_RAW
  21. #define CS_LOG_RAW_FRAMES
  22. #ifdef CS_RAW_PACKED
  23. #define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT | \
  24. OCSD_DFRMTR_PACKED_RAW_OUT)
  25. #else
  26. #define CS_RAW_DEBUG_FLAGS (OCSD_DFRMTR_UNPACKED_RAW_OUT)
  27. #endif
  28. #endif
  29. /*
  30. * Assume a maximum of 0.1ns elapsed per instruction. This would be the
  31. * case with a theoretical 10GHz core executing 1 instruction per cycle.
  32. * Used to estimate the sample time for synthesized instructions because
  33. * Coresight only emits a timestamp for a range of instructions rather
  34. * than per instruction.
  35. */
  36. const u32 INSTR_PER_NS = 10;
  37. struct cs_etm_decoder {
  38. void *data;
  39. void (*packet_printer)(const char *msg, void *data);
  40. bool suppress_printing;
  41. dcd_tree_handle_t dcd_tree;
  42. cs_etm_mem_cb_type mem_access;
  43. ocsd_datapath_resp_t prev_return;
  44. const char *decoder_name;
  45. };
  46. static u32
  47. cs_etm_decoder__mem_access(const void *context,
  48. const ocsd_vaddr_t address,
  49. const ocsd_mem_space_acc_t mem_space,
  50. const u8 trace_chan_id,
  51. const u32 req_size,
  52. u8 *buffer)
  53. {
  54. struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context;
  55. return decoder->mem_access(decoder->data, trace_chan_id, address,
  56. req_size, buffer, mem_space);
  57. }
  58. int cs_etm_decoder__add_mem_access_cb(struct cs_etm_decoder *decoder,
  59. u64 start, u64 end,
  60. cs_etm_mem_cb_type cb_func)
  61. {
  62. decoder->mem_access = cb_func;
  63. if (ocsd_dt_add_callback_trcid_mem_acc(decoder->dcd_tree, start, end,
  64. OCSD_MEM_SPACE_ANY,
  65. cs_etm_decoder__mem_access,
  66. decoder))
  67. return -1;
  68. return 0;
  69. }
  70. int cs_etm_decoder__reset(struct cs_etm_decoder *decoder)
  71. {
  72. ocsd_datapath_resp_t dp_ret;
  73. decoder->prev_return = OCSD_RESP_CONT;
  74. decoder->suppress_printing = true;
  75. dp_ret = ocsd_dt_process_data(decoder->dcd_tree, OCSD_OP_RESET,
  76. 0, 0, NULL, NULL);
  77. decoder->suppress_printing = false;
  78. if (OCSD_DATA_RESP_IS_FATAL(dp_ret))
  79. return -1;
  80. return 0;
  81. }
  82. int cs_etm_decoder__get_packet(struct cs_etm_packet_queue *packet_queue,
  83. struct cs_etm_packet *packet)
  84. {
  85. if (!packet_queue || !packet)
  86. return -EINVAL;
  87. /* Nothing to do, might as well just return */
  88. if (packet_queue->packet_count == 0)
  89. return 0;
  90. /*
  91. * The queueing process in function cs_etm_decoder__buffer_packet()
  92. * increments the tail *before* using it. This is somewhat counter
  93. * intuitive but it has the advantage of centralizing tail management
  94. * at a single location. Because of that we need to follow the same
  95. * heuristic with the head, i.e we increment it before using its
  96. * value. Otherwise the first element of the packet queue is not
  97. * used.
  98. */
  99. packet_queue->head = (packet_queue->head + 1) &
  100. (CS_ETM_PACKET_MAX_BUFFER - 1);
  101. *packet = packet_queue->packet_buffer[packet_queue->head];
  102. packet_queue->packet_count--;
  103. return 1;
  104. }
  105. /*
  106. * Calculate the number of nanoseconds elapsed.
  107. *
  108. * instr_count is updated in place with the remainder of the instructions
  109. * which didn't make up a whole nanosecond.
  110. */
  111. static u32 cs_etm_decoder__dec_instr_count_to_ns(u32 *instr_count)
  112. {
  113. const u32 instr_copy = *instr_count;
  114. *instr_count %= INSTR_PER_NS;
  115. return instr_copy / INSTR_PER_NS;
  116. }
  117. static int cs_etm_decoder__gen_etmv3_config(struct cs_etm_trace_params *params,
  118. ocsd_etmv3_cfg *config)
  119. {
  120. config->reg_idr = params->etmv3.reg_idr;
  121. config->reg_ctrl = params->etmv3.reg_ctrl;
  122. config->reg_ccer = params->etmv3.reg_ccer;
  123. config->reg_trc_id = params->etmv3.reg_trc_id;
  124. config->arch_ver = ARCH_V7;
  125. config->core_prof = profile_CortexA;
  126. return 0;
  127. }
  128. #define TRCIDR1_TRCARCHMIN_SHIFT 4
  129. #define TRCIDR1_TRCARCHMIN_MASK GENMASK(7, 4)
  130. #define TRCIDR1_TRCARCHMIN(x) (((x) & TRCIDR1_TRCARCHMIN_MASK) >> TRCIDR1_TRCARCHMIN_SHIFT)
  131. static enum _ocsd_arch_version cs_etm_decoder__get_etmv4_arch_ver(u32 reg_idr1)
  132. {
  133. /*
  134. * For ETMv4 if the trace minor version is 4 or more then we can assume
  135. * the architecture is ARCH_AA64 rather than just V8.
  136. * ARCH_V8 = V8 architecture
  137. * ARCH_AA64 = Min v8r3 plus additional AA64 PE features
  138. */
  139. return TRCIDR1_TRCARCHMIN(reg_idr1) >= 4 ? ARCH_AA64 : ARCH_V8;
  140. }
  141. static void cs_etm_decoder__gen_etmv4_config(struct cs_etm_trace_params *params,
  142. ocsd_etmv4_cfg *config)
  143. {
  144. config->reg_configr = params->etmv4.reg_configr;
  145. config->reg_traceidr = params->etmv4.reg_traceidr;
  146. config->reg_idr0 = params->etmv4.reg_idr0;
  147. config->reg_idr1 = params->etmv4.reg_idr1;
  148. config->reg_idr2 = params->etmv4.reg_idr2;
  149. config->reg_idr8 = params->etmv4.reg_idr8;
  150. config->reg_idr9 = 0;
  151. config->reg_idr10 = 0;
  152. config->reg_idr11 = 0;
  153. config->reg_idr12 = 0;
  154. config->reg_idr13 = 0;
  155. config->arch_ver = cs_etm_decoder__get_etmv4_arch_ver(params->etmv4.reg_idr1);
  156. config->core_prof = profile_CortexA;
  157. }
  158. static void cs_etm_decoder__gen_ete_config(struct cs_etm_trace_params *params,
  159. ocsd_ete_cfg *config)
  160. {
  161. config->reg_configr = params->ete.reg_configr;
  162. config->reg_traceidr = params->ete.reg_traceidr;
  163. config->reg_idr0 = params->ete.reg_idr0;
  164. config->reg_idr1 = params->ete.reg_idr1;
  165. config->reg_idr2 = params->ete.reg_idr2;
  166. config->reg_idr8 = params->ete.reg_idr8;
  167. config->reg_devarch = params->ete.reg_devarch;
  168. config->arch_ver = ARCH_AA64;
  169. config->core_prof = profile_CortexA;
  170. }
  171. static void cs_etm_decoder__print_str_cb(const void *p_context,
  172. const char *msg,
  173. const int str_len)
  174. {
  175. const struct cs_etm_decoder *decoder = p_context;
  176. if (p_context && str_len && !decoder->suppress_printing)
  177. decoder->packet_printer(msg, decoder->data);
  178. }
  179. static int
  180. cs_etm_decoder__init_def_logger_printing(struct cs_etm_decoder_params *d_params,
  181. struct cs_etm_decoder *decoder)
  182. {
  183. int ret = 0;
  184. if (d_params->packet_printer == NULL)
  185. return -1;
  186. decoder->packet_printer = d_params->packet_printer;
  187. /*
  188. * Set up a library default logger to process any printers
  189. * (packet/raw frame) we add later.
  190. */
  191. ret = ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
  192. if (ret != 0)
  193. return -1;
  194. /* no stdout / err / file output */
  195. ret = ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
  196. if (ret != 0)
  197. return -1;
  198. /*
  199. * Set the string CB for the default logger, passes strings to
  200. * perf print logger.
  201. */
  202. ret = ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
  203. (void *)decoder,
  204. cs_etm_decoder__print_str_cb);
  205. if (ret != 0)
  206. ret = -1;
  207. return 0;
  208. }
  209. #ifdef CS_LOG_RAW_FRAMES
  210. static void
  211. cs_etm_decoder__init_raw_frame_logging(struct cs_etm_decoder_params *d_params,
  212. struct cs_etm_decoder *decoder)
  213. {
  214. /* Only log these during a --dump operation */
  215. if (d_params->operation == CS_ETM_OPERATION_PRINT) {
  216. /* set up a library default logger to process the
  217. * raw frame printer we add later
  218. */
  219. ocsd_def_errlog_init(OCSD_ERR_SEV_ERROR, 1);
  220. /* no stdout / err / file output */
  221. ocsd_def_errlog_config_output(C_API_MSGLOGOUT_FLG_NONE, NULL);
  222. /* set the string CB for the default logger,
  223. * passes strings to perf print logger.
  224. */
  225. ocsd_def_errlog_set_strprint_cb(decoder->dcd_tree,
  226. (void *)decoder,
  227. cs_etm_decoder__print_str_cb);
  228. /* use the built in library printer for the raw frames */
  229. ocsd_dt_set_raw_frame_printer(decoder->dcd_tree,
  230. CS_RAW_DEBUG_FLAGS);
  231. }
  232. }
  233. #else
  234. static void
  235. cs_etm_decoder__init_raw_frame_logging(
  236. struct cs_etm_decoder_params *d_params __maybe_unused,
  237. struct cs_etm_decoder *decoder __maybe_unused)
  238. {
  239. }
  240. #endif
  241. static ocsd_datapath_resp_t
  242. cs_etm_decoder__do_soft_timestamp(struct cs_etm_queue *etmq,
  243. struct cs_etm_packet_queue *packet_queue,
  244. const uint8_t trace_chan_id)
  245. {
  246. u64 estimated_ts;
  247. /* No timestamp packet has been received, nothing to do */
  248. if (!packet_queue->next_cs_timestamp)
  249. return OCSD_RESP_CONT;
  250. estimated_ts = packet_queue->cs_timestamp +
  251. cs_etm_decoder__dec_instr_count_to_ns(&packet_queue->instr_count);
  252. /* Estimated TS can never be higher than the next real one in the trace */
  253. packet_queue->cs_timestamp = min(packet_queue->next_cs_timestamp, estimated_ts);
  254. /* Tell the front end which traceid_queue needs attention */
  255. cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id);
  256. return OCSD_RESP_WAIT;
  257. }
  258. static ocsd_datapath_resp_t
  259. cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq,
  260. const ocsd_generic_trace_elem *elem,
  261. const uint8_t trace_chan_id,
  262. const ocsd_trc_index_t indx)
  263. {
  264. struct cs_etm_packet_queue *packet_queue;
  265. u64 converted_timestamp;
  266. u64 estimated_first_ts;
  267. /* First get the packet queue for this traceID */
  268. packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id);
  269. if (!packet_queue)
  270. return OCSD_RESP_FATAL_SYS_ERR;
  271. /*
  272. * Coresight timestamps are raw timer values which need to be scaled to ns. Assume
  273. * 0 is a bad value so don't try to convert it.
  274. */
  275. converted_timestamp = elem->timestamp ?
  276. cs_etm__convert_sample_time(etmq, elem->timestamp) : 0;
  277. /*
  278. * We've seen a timestamp packet before - simply record the new value.
  279. * Function do_soft_timestamp() will report the value to the front end,
  280. * hence asking the decoder to keep decoding rather than stopping.
  281. */
  282. if (packet_queue->next_cs_timestamp) {
  283. /*
  284. * What was next is now where new ranges start from, overwriting
  285. * any previous estimate in cs_timestamp
  286. */
  287. packet_queue->cs_timestamp = packet_queue->next_cs_timestamp;
  288. packet_queue->next_cs_timestamp = converted_timestamp;
  289. return OCSD_RESP_CONT;
  290. }
  291. if (!converted_timestamp) {
  292. /*
  293. * Zero timestamps can be seen due to misconfiguration or hardware bugs.
  294. * Warn once, and don't try to subtract instr_count as it would result in an
  295. * underflow.
  296. */
  297. packet_queue->cs_timestamp = 0;
  298. if (!cs_etm__etmq_is_timeless(etmq))
  299. pr_warning_once("Zero Coresight timestamp found at Idx:%" OCSD_TRC_IDX_STR
  300. ". Decoding may be improved by prepending 'Z' to your current --itrace arguments.\n",
  301. indx);
  302. } else if (packet_queue->instr_count / INSTR_PER_NS > converted_timestamp) {
  303. /*
  304. * Sanity check that the elem->timestamp - packet_queue->instr_count would not
  305. * result in an underflow. Warn and clamp at 0 if it would.
  306. */
  307. packet_queue->cs_timestamp = 0;
  308. pr_err("Timestamp calculation underflow at Idx:%" OCSD_TRC_IDX_STR "\n", indx);
  309. } else {
  310. /*
  311. * This is the first timestamp we've seen since the beginning of traces
  312. * or a discontinuity. Since timestamps packets are generated *after*
  313. * range packets have been generated, we need to estimate the time at
  314. * which instructions started by subtracting the number of instructions
  315. * executed to the timestamp. Don't estimate earlier than the last used
  316. * timestamp though.
  317. */
  318. estimated_first_ts = converted_timestamp -
  319. (packet_queue->instr_count / INSTR_PER_NS);
  320. packet_queue->cs_timestamp = max(packet_queue->cs_timestamp, estimated_first_ts);
  321. }
  322. packet_queue->next_cs_timestamp = converted_timestamp;
  323. packet_queue->instr_count = 0;
  324. /* Tell the front end which traceid_queue needs attention */
  325. cs_etm__etmq_set_traceid_queue_timestamp(etmq, trace_chan_id);
  326. /* Halt processing until we are being told to proceed */
  327. return OCSD_RESP_WAIT;
  328. }
  329. static void
  330. cs_etm_decoder__reset_timestamp(struct cs_etm_packet_queue *packet_queue)
  331. {
  332. packet_queue->next_cs_timestamp = 0;
  333. packet_queue->instr_count = 0;
  334. }
  335. static ocsd_datapath_resp_t
  336. cs_etm_decoder__buffer_packet(struct cs_etm_queue *etmq,
  337. struct cs_etm_packet_queue *packet_queue,
  338. const u8 trace_chan_id,
  339. enum cs_etm_sample_type sample_type)
  340. {
  341. u32 et = 0;
  342. int cpu;
  343. if (packet_queue->packet_count >= CS_ETM_PACKET_MAX_BUFFER - 1)
  344. return OCSD_RESP_FATAL_SYS_ERR;
  345. if (cs_etm__get_cpu(etmq, trace_chan_id, &cpu) < 0)
  346. return OCSD_RESP_FATAL_SYS_ERR;
  347. et = packet_queue->tail;
  348. et = (et + 1) & (CS_ETM_PACKET_MAX_BUFFER - 1);
  349. packet_queue->tail = et;
  350. packet_queue->packet_count++;
  351. packet_queue->packet_buffer[et].sample_type = sample_type;
  352. packet_queue->packet_buffer[et].isa = CS_ETM_ISA_UNKNOWN;
  353. packet_queue->packet_buffer[et].cpu = cpu;
  354. packet_queue->packet_buffer[et].start_addr = CS_ETM_INVAL_ADDR;
  355. packet_queue->packet_buffer[et].end_addr = CS_ETM_INVAL_ADDR;
  356. packet_queue->packet_buffer[et].instr_count = 0;
  357. packet_queue->packet_buffer[et].last_instr_taken_branch = false;
  358. packet_queue->packet_buffer[et].last_instr_size = 0;
  359. packet_queue->packet_buffer[et].last_instr_type = 0;
  360. packet_queue->packet_buffer[et].last_instr_subtype = 0;
  361. packet_queue->packet_buffer[et].last_instr_cond = 0;
  362. packet_queue->packet_buffer[et].flags = 0;
  363. packet_queue->packet_buffer[et].exception_number = UINT32_MAX;
  364. packet_queue->packet_buffer[et].trace_chan_id = trace_chan_id;
  365. if (packet_queue->packet_count == CS_ETM_PACKET_MAX_BUFFER - 1)
  366. return OCSD_RESP_WAIT;
  367. return OCSD_RESP_CONT;
  368. }
  369. static ocsd_datapath_resp_t
  370. cs_etm_decoder__buffer_range(struct cs_etm_queue *etmq,
  371. struct cs_etm_packet_queue *packet_queue,
  372. const ocsd_generic_trace_elem *elem,
  373. const uint8_t trace_chan_id)
  374. {
  375. int ret = 0;
  376. struct cs_etm_packet *packet;
  377. ret = cs_etm_decoder__buffer_packet(etmq, packet_queue, trace_chan_id,
  378. CS_ETM_RANGE);
  379. if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
  380. return ret;
  381. packet = &packet_queue->packet_buffer[packet_queue->tail];
  382. switch (elem->isa) {
  383. case ocsd_isa_aarch64:
  384. packet->isa = CS_ETM_ISA_A64;
  385. break;
  386. case ocsd_isa_arm:
  387. packet->isa = CS_ETM_ISA_A32;
  388. break;
  389. case ocsd_isa_thumb2:
  390. packet->isa = CS_ETM_ISA_T32;
  391. break;
  392. case ocsd_isa_tee:
  393. case ocsd_isa_jazelle:
  394. case ocsd_isa_custom:
  395. case ocsd_isa_unknown:
  396. default:
  397. packet->isa = CS_ETM_ISA_UNKNOWN;
  398. }
  399. packet->start_addr = elem->st_addr;
  400. packet->end_addr = elem->en_addr;
  401. packet->instr_count = elem->num_instr_range;
  402. packet->last_instr_type = elem->last_i_type;
  403. packet->last_instr_subtype = elem->last_i_subtype;
  404. packet->last_instr_cond = elem->last_instr_cond;
  405. if (elem->last_i_type == OCSD_INSTR_BR || elem->last_i_type == OCSD_INSTR_BR_INDIRECT)
  406. packet->last_instr_taken_branch = elem->last_instr_exec;
  407. else
  408. packet->last_instr_taken_branch = false;
  409. packet->last_instr_size = elem->last_instr_sz;
  410. /* per-thread scenario, no need to generate a timestamp */
  411. if (cs_etm__etmq_is_timeless(etmq))
  412. goto out;
  413. /*
  414. * The packet queue is full and we haven't seen a timestamp (had we
  415. * seen one the packet queue wouldn't be full). Let the front end
  416. * deal with it.
  417. */
  418. if (ret == OCSD_RESP_WAIT)
  419. goto out;
  420. packet_queue->instr_count += elem->num_instr_range;
  421. /* Tell the front end we have a new timestamp to process */
  422. ret = cs_etm_decoder__do_soft_timestamp(etmq, packet_queue,
  423. trace_chan_id);
  424. out:
  425. return ret;
  426. }
  427. static ocsd_datapath_resp_t
  428. cs_etm_decoder__buffer_discontinuity(struct cs_etm_queue *etmq,
  429. struct cs_etm_packet_queue *queue,
  430. const uint8_t trace_chan_id)
  431. {
  432. /*
  433. * Something happened and who knows when we'll get new traces so
  434. * reset time statistics.
  435. */
  436. cs_etm_decoder__reset_timestamp(queue);
  437. return cs_etm_decoder__buffer_packet(etmq, queue, trace_chan_id,
  438. CS_ETM_DISCONTINUITY);
  439. }
  440. static ocsd_datapath_resp_t
  441. cs_etm_decoder__buffer_exception(struct cs_etm_queue *etmq,
  442. struct cs_etm_packet_queue *queue,
  443. const ocsd_generic_trace_elem *elem,
  444. const uint8_t trace_chan_id)
  445. { int ret = 0;
  446. struct cs_etm_packet *packet;
  447. ret = cs_etm_decoder__buffer_packet(etmq, queue, trace_chan_id,
  448. CS_ETM_EXCEPTION);
  449. if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
  450. return ret;
  451. packet = &queue->packet_buffer[queue->tail];
  452. packet->exception_number = elem->exception_number;
  453. return ret;
  454. }
  455. static ocsd_datapath_resp_t
  456. cs_etm_decoder__buffer_exception_ret(struct cs_etm_queue *etmq,
  457. struct cs_etm_packet_queue *queue,
  458. const uint8_t trace_chan_id)
  459. {
  460. return cs_etm_decoder__buffer_packet(etmq, queue, trace_chan_id,
  461. CS_ETM_EXCEPTION_RET);
  462. }
  463. static ocsd_datapath_resp_t
  464. cs_etm_decoder__set_tid(struct cs_etm_queue *etmq,
  465. struct cs_etm_packet_queue *packet_queue,
  466. const ocsd_generic_trace_elem *elem,
  467. const uint8_t trace_chan_id)
  468. {
  469. pid_t tid = -1;
  470. /*
  471. * Process the PE_CONTEXT packets if we have a valid contextID or VMID.
  472. * If the kernel is running at EL2, the PID is traced in CONTEXTIDR_EL2
  473. * as VMID, Format attribute 'contextid2' is set in this case.
  474. */
  475. switch (cs_etm__get_pid_fmt(etmq)) {
  476. case CS_ETM_PIDFMT_CTXTID:
  477. if (elem->context.ctxt_id_valid)
  478. tid = elem->context.context_id;
  479. break;
  480. case CS_ETM_PIDFMT_CTXTID2:
  481. if (elem->context.vmid_valid)
  482. tid = elem->context.vmid;
  483. break;
  484. case CS_ETM_PIDFMT_NONE:
  485. default:
  486. break;
  487. }
  488. if (cs_etm__etmq_set_tid_el(etmq, tid, trace_chan_id,
  489. elem->context.exception_level))
  490. return OCSD_RESP_FATAL_SYS_ERR;
  491. if (tid == -1)
  492. return OCSD_RESP_CONT;
  493. /*
  494. * A timestamp is generated after a PE_CONTEXT element so make sure
  495. * to rely on that coming one.
  496. */
  497. cs_etm_decoder__reset_timestamp(packet_queue);
  498. return OCSD_RESP_CONT;
  499. }
  500. static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer(
  501. const void *context,
  502. const ocsd_trc_index_t indx,
  503. const u8 trace_chan_id __maybe_unused,
  504. const ocsd_generic_trace_elem *elem)
  505. {
  506. ocsd_datapath_resp_t resp = OCSD_RESP_CONT;
  507. ocsd_gen_trc_elem_t type;
  508. struct cs_etm_decoder *decoder = (struct cs_etm_decoder *) context;
  509. struct cs_etm_queue *etmq = decoder->data;
  510. struct cs_etm_packet_queue *packet_queue;
  511. /* First get the packet queue for this traceID */
  512. packet_queue = cs_etm__etmq_get_packet_queue(etmq, trace_chan_id);
  513. if (!packet_queue)
  514. return OCSD_RESP_FATAL_SYS_ERR;
  515. type = elem->elem_type;
  516. if (type == OCSD_GEN_TRC_ELEM_EO_TRACE ||
  517. type == OCSD_GEN_TRC_ELEM_NO_SYNC ||
  518. type == OCSD_GEN_TRC_ELEM_TRACE_ON)
  519. resp = cs_etm_decoder__buffer_discontinuity(etmq, packet_queue,
  520. trace_chan_id);
  521. else if (type == OCSD_GEN_TRC_ELEM_INSTR_RANGE)
  522. resp = cs_etm_decoder__buffer_range(etmq, packet_queue, elem,
  523. trace_chan_id);
  524. else if (type == OCSD_GEN_TRC_ELEM_EXCEPTION)
  525. resp = cs_etm_decoder__buffer_exception(etmq, packet_queue, elem,
  526. trace_chan_id);
  527. else if (type == OCSD_GEN_TRC_ELEM_EXCEPTION_RET)
  528. resp = cs_etm_decoder__buffer_exception_ret(etmq, packet_queue,
  529. trace_chan_id);
  530. else if (type == OCSD_GEN_TRC_ELEM_TIMESTAMP)
  531. resp = cs_etm_decoder__do_hard_timestamp(etmq, elem,
  532. trace_chan_id,
  533. indx);
  534. else if (type == OCSD_GEN_TRC_ELEM_PE_CONTEXT)
  535. resp = cs_etm_decoder__set_tid(etmq, packet_queue,
  536. elem, trace_chan_id);
  537. return resp;
  538. }
  539. static int
  540. cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params,
  541. struct cs_etm_trace_params *t_params,
  542. struct cs_etm_decoder *decoder)
  543. {
  544. ocsd_etmv3_cfg config_etmv3;
  545. ocsd_etmv4_cfg trace_config_etmv4;
  546. ocsd_ete_cfg trace_config_ete;
  547. void *trace_config;
  548. u8 csid;
  549. switch (t_params->protocol) {
  550. case CS_ETM_PROTO_ETMV3:
  551. case CS_ETM_PROTO_PTM:
  552. csid = (t_params->etmv3.reg_idr & CORESIGHT_TRACE_ID_VAL_MASK);
  553. cs_etm_decoder__gen_etmv3_config(t_params, &config_etmv3);
  554. decoder->decoder_name = (t_params->protocol == CS_ETM_PROTO_ETMV3) ?
  555. OCSD_BUILTIN_DCD_ETMV3 :
  556. OCSD_BUILTIN_DCD_PTM;
  557. trace_config = &config_etmv3;
  558. break;
  559. case CS_ETM_PROTO_ETMV4i:
  560. csid = (t_params->etmv4.reg_traceidr & CORESIGHT_TRACE_ID_VAL_MASK);
  561. cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4);
  562. decoder->decoder_name = OCSD_BUILTIN_DCD_ETMV4I;
  563. trace_config = &trace_config_etmv4;
  564. break;
  565. case CS_ETM_PROTO_ETE:
  566. csid = (t_params->ete.reg_traceidr & CORESIGHT_TRACE_ID_VAL_MASK);
  567. cs_etm_decoder__gen_ete_config(t_params, &trace_config_ete);
  568. decoder->decoder_name = OCSD_BUILTIN_DCD_ETE;
  569. trace_config = &trace_config_ete;
  570. break;
  571. default:
  572. return -1;
  573. }
  574. if (d_params->operation == CS_ETM_OPERATION_DECODE) {
  575. int decode_flags = OCSD_CREATE_FLG_FULL_DECODER;
  576. #ifdef OCSD_OPFLG_N_UNCOND_DIR_BR_CHK
  577. decode_flags |= OCSD_OPFLG_N_UNCOND_DIR_BR_CHK | OCSD_OPFLG_CHK_RANGE_CONTINUE |
  578. ETM4_OPFLG_PKTDEC_AA64_OPCODE_CHK;
  579. #endif
  580. if (ocsd_dt_create_decoder(decoder->dcd_tree,
  581. decoder->decoder_name,
  582. decode_flags,
  583. trace_config, &csid))
  584. return -1;
  585. if (ocsd_dt_set_gen_elem_outfn(decoder->dcd_tree,
  586. cs_etm_decoder__gen_trace_elem_printer,
  587. decoder))
  588. return -1;
  589. return 0;
  590. } else if (d_params->operation == CS_ETM_OPERATION_PRINT) {
  591. if (ocsd_dt_create_decoder(decoder->dcd_tree, decoder->decoder_name,
  592. OCSD_CREATE_FLG_PACKET_PROC,
  593. trace_config, &csid))
  594. return -1;
  595. if (ocsd_dt_set_pkt_protocol_printer(decoder->dcd_tree, csid, 0))
  596. return -1;
  597. return 0;
  598. }
  599. return -1;
  600. }
  601. struct cs_etm_decoder *
  602. cs_etm_decoder__new(int decoders, struct cs_etm_decoder_params *d_params,
  603. struct cs_etm_trace_params t_params[])
  604. {
  605. struct cs_etm_decoder *decoder;
  606. ocsd_dcd_tree_src_t format;
  607. u32 flags;
  608. int i, ret;
  609. if ((!t_params) || (!d_params))
  610. return NULL;
  611. decoder = zalloc(sizeof(*decoder));
  612. if (!decoder)
  613. return NULL;
  614. decoder->data = d_params->data;
  615. decoder->prev_return = OCSD_RESP_CONT;
  616. format = (d_params->formatted ? OCSD_TRC_SRC_FRAME_FORMATTED :
  617. OCSD_TRC_SRC_SINGLE);
  618. flags = 0;
  619. flags |= (d_params->fsyncs ? OCSD_DFRMTR_HAS_FSYNCS : 0);
  620. flags |= (d_params->hsyncs ? OCSD_DFRMTR_HAS_HSYNCS : 0);
  621. flags |= (d_params->frame_aligned ? OCSD_DFRMTR_FRAME_MEM_ALIGN : 0);
  622. /*
  623. * Drivers may add barrier frames when used with perf, set up to
  624. * handle this. Barriers const of FSYNC packet repeated 4 times.
  625. */
  626. flags |= OCSD_DFRMTR_RESET_ON_4X_FSYNC;
  627. /* Create decode tree for the data source */
  628. decoder->dcd_tree = ocsd_create_dcd_tree(format, flags);
  629. if (decoder->dcd_tree == 0)
  630. goto err_free_decoder;
  631. /* init library print logging support */
  632. ret = cs_etm_decoder__init_def_logger_printing(d_params, decoder);
  633. if (ret != 0)
  634. goto err_free_decoder;
  635. /* init raw frame logging if required */
  636. cs_etm_decoder__init_raw_frame_logging(d_params, decoder);
  637. for (i = 0; i < decoders; i++) {
  638. ret = cs_etm_decoder__create_etm_decoder(d_params,
  639. &t_params[i],
  640. decoder);
  641. if (ret != 0)
  642. goto err_free_decoder;
  643. }
  644. return decoder;
  645. err_free_decoder:
  646. cs_etm_decoder__free(decoder);
  647. return NULL;
  648. }
  649. int cs_etm_decoder__process_data_block(struct cs_etm_decoder *decoder,
  650. u64 indx, const u8 *buf,
  651. size_t len, size_t *consumed)
  652. {
  653. int ret = 0;
  654. ocsd_datapath_resp_t cur = OCSD_RESP_CONT;
  655. ocsd_datapath_resp_t prev_return = decoder->prev_return;
  656. size_t processed = 0;
  657. u32 count;
  658. while (processed < len) {
  659. if (OCSD_DATA_RESP_IS_WAIT(prev_return)) {
  660. cur = ocsd_dt_process_data(decoder->dcd_tree,
  661. OCSD_OP_FLUSH,
  662. 0,
  663. 0,
  664. NULL,
  665. NULL);
  666. } else if (OCSD_DATA_RESP_IS_CONT(prev_return)) {
  667. cur = ocsd_dt_process_data(decoder->dcd_tree,
  668. OCSD_OP_DATA,
  669. indx + processed,
  670. len - processed,
  671. &buf[processed],
  672. &count);
  673. processed += count;
  674. } else {
  675. ret = -EINVAL;
  676. break;
  677. }
  678. /*
  679. * Return to the input code if the packet buffer is full.
  680. * Flushing will get done once the packet buffer has been
  681. * processed.
  682. */
  683. if (OCSD_DATA_RESP_IS_WAIT(cur))
  684. break;
  685. prev_return = cur;
  686. }
  687. decoder->prev_return = cur;
  688. *consumed = processed;
  689. return ret;
  690. }
  691. void cs_etm_decoder__free(struct cs_etm_decoder *decoder)
  692. {
  693. if (!decoder)
  694. return;
  695. ocsd_destroy_dcd_tree(decoder->dcd_tree);
  696. decoder->dcd_tree = NULL;
  697. free(decoder);
  698. }
  699. const char *cs_etm_decoder__get_name(struct cs_etm_decoder *decoder)
  700. {
  701. return decoder->decoder_name;
  702. }