intel-pt-decoder.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  1. /*
  2. * intel_pt_decoder.c: Intel Processor Trace support
  3. * Copyright (c) 2013-2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #ifndef _GNU_SOURCE
  16. #define _GNU_SOURCE
  17. #endif
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <stdint.h>
  23. #include <inttypes.h>
  24. #include <linux/compiler.h>
  25. #include "../cache.h"
  26. #include "../util.h"
  27. #include "intel-pt-insn-decoder.h"
  28. #include "intel-pt-pkt-decoder.h"
  29. #include "intel-pt-decoder.h"
  30. #include "intel-pt-log.h"
  31. #define INTEL_PT_BLK_SIZE 1024
  32. #define BIT63 (((uint64_t)1 << 63))
  33. #define INTEL_PT_RETURN 1
  34. /* Maximum number of loops with no packets consumed i.e. stuck in a loop */
  35. #define INTEL_PT_MAX_LOOPS 10000
  36. struct intel_pt_blk {
  37. struct intel_pt_blk *prev;
  38. uint64_t ip[INTEL_PT_BLK_SIZE];
  39. };
  40. struct intel_pt_stack {
  41. struct intel_pt_blk *blk;
  42. struct intel_pt_blk *spare;
  43. int pos;
  44. };
  45. enum intel_pt_pkt_state {
  46. INTEL_PT_STATE_NO_PSB,
  47. INTEL_PT_STATE_NO_IP,
  48. INTEL_PT_STATE_ERR_RESYNC,
  49. INTEL_PT_STATE_IN_SYNC,
  50. INTEL_PT_STATE_TNT,
  51. INTEL_PT_STATE_TIP,
  52. INTEL_PT_STATE_TIP_PGD,
  53. INTEL_PT_STATE_FUP,
  54. INTEL_PT_STATE_FUP_NO_TIP,
  55. };
  56. #ifdef INTEL_PT_STRICT
  57. #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
  58. #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
  59. #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
  60. #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
  61. #else
  62. #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
  63. #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
  64. #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
  65. #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
  66. #endif
  67. struct intel_pt_decoder {
  68. int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
  69. int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
  70. uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
  71. uint64_t max_insn_cnt, void *data);
  72. bool (*pgd_ip)(uint64_t ip, void *data);
  73. void *data;
  74. struct intel_pt_state state;
  75. const unsigned char *buf;
  76. size_t len;
  77. bool return_compression;
  78. bool mtc_insn;
  79. bool pge;
  80. bool have_tma;
  81. bool have_cyc;
  82. bool fixup_last_mtc;
  83. uint64_t pos;
  84. uint64_t last_ip;
  85. uint64_t ip;
  86. uint64_t cr3;
  87. uint64_t timestamp;
  88. uint64_t tsc_timestamp;
  89. uint64_t ref_timestamp;
  90. uint64_t ret_addr;
  91. uint64_t ctc_timestamp;
  92. uint64_t ctc_delta;
  93. uint64_t cycle_cnt;
  94. uint64_t cyc_ref_timestamp;
  95. uint32_t last_mtc;
  96. uint32_t tsc_ctc_ratio_n;
  97. uint32_t tsc_ctc_ratio_d;
  98. uint32_t tsc_ctc_mult;
  99. uint32_t tsc_slip;
  100. uint32_t ctc_rem_mask;
  101. int mtc_shift;
  102. struct intel_pt_stack stack;
  103. enum intel_pt_pkt_state pkt_state;
  104. struct intel_pt_pkt packet;
  105. struct intel_pt_pkt tnt;
  106. int pkt_step;
  107. int pkt_len;
  108. int last_packet_type;
  109. unsigned int cbr;
  110. unsigned int max_non_turbo_ratio;
  111. double max_non_turbo_ratio_fp;
  112. double cbr_cyc_to_tsc;
  113. double calc_cyc_to_tsc;
  114. bool have_calc_cyc_to_tsc;
  115. int exec_mode;
  116. unsigned int insn_bytes;
  117. uint64_t period;
  118. enum intel_pt_period_type period_type;
  119. uint64_t tot_insn_cnt;
  120. uint64_t period_insn_cnt;
  121. uint64_t period_mask;
  122. uint64_t period_ticks;
  123. uint64_t last_masked_timestamp;
  124. bool continuous_period;
  125. bool overflow;
  126. bool set_fup_tx_flags;
  127. unsigned int fup_tx_flags;
  128. unsigned int tx_flags;
  129. uint64_t timestamp_insn_cnt;
  130. uint64_t stuck_ip;
  131. int no_progress;
  132. int stuck_ip_prd;
  133. int stuck_ip_cnt;
  134. const unsigned char *next_buf;
  135. size_t next_len;
  136. unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
  137. };
  138. static uint64_t intel_pt_lower_power_of_2(uint64_t x)
  139. {
  140. int i;
  141. for (i = 0; x != 1; i++)
  142. x >>= 1;
  143. return x << i;
  144. }
  145. static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
  146. {
  147. if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
  148. uint64_t period;
  149. period = intel_pt_lower_power_of_2(decoder->period);
  150. decoder->period_mask = ~(period - 1);
  151. decoder->period_ticks = period;
  152. }
  153. }
  154. static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
  155. {
  156. if (!d)
  157. return 0;
  158. return (t / d) * n + ((t % d) * n) / d;
  159. }
  160. struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
  161. {
  162. struct intel_pt_decoder *decoder;
  163. if (!params->get_trace || !params->walk_insn)
  164. return NULL;
  165. decoder = zalloc(sizeof(struct intel_pt_decoder));
  166. if (!decoder)
  167. return NULL;
  168. decoder->get_trace = params->get_trace;
  169. decoder->walk_insn = params->walk_insn;
  170. decoder->pgd_ip = params->pgd_ip;
  171. decoder->data = params->data;
  172. decoder->return_compression = params->return_compression;
  173. decoder->period = params->period;
  174. decoder->period_type = params->period_type;
  175. decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
  176. decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
  177. intel_pt_setup_period(decoder);
  178. decoder->mtc_shift = params->mtc_period;
  179. decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
  180. decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
  181. decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
  182. if (!decoder->tsc_ctc_ratio_n)
  183. decoder->tsc_ctc_ratio_d = 0;
  184. if (decoder->tsc_ctc_ratio_d) {
  185. if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
  186. decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
  187. decoder->tsc_ctc_ratio_d;
  188. /*
  189. * Allow for timestamps appearing to backwards because a TSC
  190. * packet has slipped past a MTC packet, so allow 2 MTC ticks
  191. * or ...
  192. */
  193. decoder->tsc_slip = multdiv(2 << decoder->mtc_shift,
  194. decoder->tsc_ctc_ratio_n,
  195. decoder->tsc_ctc_ratio_d);
  196. }
  197. /* ... or 0x100 paranoia */
  198. if (decoder->tsc_slip < 0x100)
  199. decoder->tsc_slip = 0x100;
  200. intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
  201. intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
  202. intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
  203. intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
  204. intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
  205. return decoder;
  206. }
  207. static void intel_pt_pop_blk(struct intel_pt_stack *stack)
  208. {
  209. struct intel_pt_blk *blk = stack->blk;
  210. stack->blk = blk->prev;
  211. if (!stack->spare)
  212. stack->spare = blk;
  213. else
  214. free(blk);
  215. }
  216. static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
  217. {
  218. if (!stack->pos) {
  219. if (!stack->blk)
  220. return 0;
  221. intel_pt_pop_blk(stack);
  222. if (!stack->blk)
  223. return 0;
  224. stack->pos = INTEL_PT_BLK_SIZE;
  225. }
  226. return stack->blk->ip[--stack->pos];
  227. }
  228. static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
  229. {
  230. struct intel_pt_blk *blk;
  231. if (stack->spare) {
  232. blk = stack->spare;
  233. stack->spare = NULL;
  234. } else {
  235. blk = malloc(sizeof(struct intel_pt_blk));
  236. if (!blk)
  237. return -ENOMEM;
  238. }
  239. blk->prev = stack->blk;
  240. stack->blk = blk;
  241. stack->pos = 0;
  242. return 0;
  243. }
  244. static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
  245. {
  246. int err;
  247. if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
  248. err = intel_pt_alloc_blk(stack);
  249. if (err)
  250. return err;
  251. }
  252. stack->blk->ip[stack->pos++] = ip;
  253. return 0;
  254. }
  255. static void intel_pt_clear_stack(struct intel_pt_stack *stack)
  256. {
  257. while (stack->blk)
  258. intel_pt_pop_blk(stack);
  259. stack->pos = 0;
  260. }
  261. static void intel_pt_free_stack(struct intel_pt_stack *stack)
  262. {
  263. intel_pt_clear_stack(stack);
  264. zfree(&stack->blk);
  265. zfree(&stack->spare);
  266. }
  267. void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
  268. {
  269. intel_pt_free_stack(&decoder->stack);
  270. free(decoder);
  271. }
  272. static int intel_pt_ext_err(int code)
  273. {
  274. switch (code) {
  275. case -ENOMEM:
  276. return INTEL_PT_ERR_NOMEM;
  277. case -ENOSYS:
  278. return INTEL_PT_ERR_INTERN;
  279. case -EBADMSG:
  280. return INTEL_PT_ERR_BADPKT;
  281. case -ENODATA:
  282. return INTEL_PT_ERR_NODATA;
  283. case -EILSEQ:
  284. return INTEL_PT_ERR_NOINSN;
  285. case -ENOENT:
  286. return INTEL_PT_ERR_MISMAT;
  287. case -EOVERFLOW:
  288. return INTEL_PT_ERR_OVR;
  289. case -ENOSPC:
  290. return INTEL_PT_ERR_LOST;
  291. case -ELOOP:
  292. return INTEL_PT_ERR_NELOOP;
  293. default:
  294. return INTEL_PT_ERR_UNK;
  295. }
  296. }
  297. static const char *intel_pt_err_msgs[] = {
  298. [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
  299. [INTEL_PT_ERR_INTERN] = "Internal error",
  300. [INTEL_PT_ERR_BADPKT] = "Bad packet",
  301. [INTEL_PT_ERR_NODATA] = "No more data",
  302. [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
  303. [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
  304. [INTEL_PT_ERR_OVR] = "Overflow packet",
  305. [INTEL_PT_ERR_LOST] = "Lost trace data",
  306. [INTEL_PT_ERR_UNK] = "Unknown error!",
  307. [INTEL_PT_ERR_NELOOP] = "Never-ending loop",
  308. };
  309. int intel_pt__strerror(int code, char *buf, size_t buflen)
  310. {
  311. if (code < 1 || code >= INTEL_PT_ERR_MAX)
  312. code = INTEL_PT_ERR_UNK;
  313. strlcpy(buf, intel_pt_err_msgs[code], buflen);
  314. return 0;
  315. }
  316. static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
  317. uint64_t last_ip)
  318. {
  319. uint64_t ip;
  320. switch (packet->count) {
  321. case 1:
  322. ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
  323. packet->payload;
  324. break;
  325. case 2:
  326. ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
  327. packet->payload;
  328. break;
  329. case 3:
  330. ip = packet->payload;
  331. /* Sign-extend 6-byte ip */
  332. if (ip & (uint64_t)0x800000000000ULL)
  333. ip |= (uint64_t)0xffff000000000000ULL;
  334. break;
  335. case 4:
  336. ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
  337. packet->payload;
  338. break;
  339. case 6:
  340. ip = packet->payload;
  341. break;
  342. default:
  343. return 0;
  344. }
  345. return ip;
  346. }
  347. static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
  348. {
  349. decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
  350. }
  351. static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
  352. {
  353. intel_pt_set_last_ip(decoder);
  354. decoder->ip = decoder->last_ip;
  355. }
  356. static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
  357. {
  358. intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
  359. decoder->buf);
  360. }
  361. static int intel_pt_bug(struct intel_pt_decoder *decoder)
  362. {
  363. intel_pt_log("ERROR: Internal error\n");
  364. decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
  365. return -ENOSYS;
  366. }
  367. static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
  368. {
  369. decoder->tx_flags = 0;
  370. }
  371. static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
  372. {
  373. decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
  374. }
  375. static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
  376. {
  377. intel_pt_clear_tx_flags(decoder);
  378. decoder->have_tma = false;
  379. decoder->pkt_len = 1;
  380. decoder->pkt_step = 1;
  381. intel_pt_decoder_log_packet(decoder);
  382. if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
  383. intel_pt_log("ERROR: Bad packet\n");
  384. decoder->pkt_state = INTEL_PT_STATE_ERR1;
  385. }
  386. return -EBADMSG;
  387. }
  388. static int intel_pt_get_data(struct intel_pt_decoder *decoder)
  389. {
  390. struct intel_pt_buffer buffer = { .buf = 0, };
  391. int ret;
  392. decoder->pkt_step = 0;
  393. intel_pt_log("Getting more data\n");
  394. ret = decoder->get_trace(&buffer, decoder->data);
  395. if (ret)
  396. return ret;
  397. decoder->buf = buffer.buf;
  398. decoder->len = buffer.len;
  399. if (!decoder->len) {
  400. intel_pt_log("No more data\n");
  401. return -ENODATA;
  402. }
  403. if (!buffer.consecutive) {
  404. decoder->ip = 0;
  405. decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
  406. decoder->ref_timestamp = buffer.ref_timestamp;
  407. decoder->timestamp = 0;
  408. decoder->have_tma = false;
  409. decoder->state.trace_nr = buffer.trace_nr;
  410. intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
  411. decoder->ref_timestamp);
  412. return -ENOLINK;
  413. }
  414. return 0;
  415. }
  416. static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
  417. {
  418. if (!decoder->next_buf)
  419. return intel_pt_get_data(decoder);
  420. decoder->buf = decoder->next_buf;
  421. decoder->len = decoder->next_len;
  422. decoder->next_buf = 0;
  423. decoder->next_len = 0;
  424. return 0;
  425. }
  426. static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
  427. {
  428. unsigned char *buf = decoder->temp_buf;
  429. size_t old_len, len, n;
  430. int ret;
  431. old_len = decoder->len;
  432. len = decoder->len;
  433. memcpy(buf, decoder->buf, len);
  434. ret = intel_pt_get_data(decoder);
  435. if (ret) {
  436. decoder->pos += old_len;
  437. return ret < 0 ? ret : -EINVAL;
  438. }
  439. n = INTEL_PT_PKT_MAX_SZ - len;
  440. if (n > decoder->len)
  441. n = decoder->len;
  442. memcpy(buf + len, decoder->buf, n);
  443. len += n;
  444. ret = intel_pt_get_packet(buf, len, &decoder->packet);
  445. if (ret < (int)old_len) {
  446. decoder->next_buf = decoder->buf;
  447. decoder->next_len = decoder->len;
  448. decoder->buf = buf;
  449. decoder->len = old_len;
  450. return intel_pt_bad_packet(decoder);
  451. }
  452. decoder->next_buf = decoder->buf + (ret - old_len);
  453. decoder->next_len = decoder->len - (ret - old_len);
  454. decoder->buf = buf;
  455. decoder->len = ret;
  456. return ret;
  457. }
  458. struct intel_pt_pkt_info {
  459. struct intel_pt_decoder *decoder;
  460. struct intel_pt_pkt packet;
  461. uint64_t pos;
  462. int pkt_len;
  463. int last_packet_type;
  464. void *data;
  465. };
  466. typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
  467. /* Lookahead packets in current buffer */
  468. static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
  469. intel_pt_pkt_cb_t cb, void *data)
  470. {
  471. struct intel_pt_pkt_info pkt_info;
  472. const unsigned char *buf = decoder->buf;
  473. size_t len = decoder->len;
  474. int ret;
  475. pkt_info.decoder = decoder;
  476. pkt_info.pos = decoder->pos;
  477. pkt_info.pkt_len = decoder->pkt_step;
  478. pkt_info.last_packet_type = decoder->last_packet_type;
  479. pkt_info.data = data;
  480. while (1) {
  481. do {
  482. pkt_info.pos += pkt_info.pkt_len;
  483. buf += pkt_info.pkt_len;
  484. len -= pkt_info.pkt_len;
  485. if (!len)
  486. return INTEL_PT_NEED_MORE_BYTES;
  487. ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
  488. if (!ret)
  489. return INTEL_PT_NEED_MORE_BYTES;
  490. if (ret < 0)
  491. return ret;
  492. pkt_info.pkt_len = ret;
  493. } while (pkt_info.packet.type == INTEL_PT_PAD);
  494. ret = cb(&pkt_info);
  495. if (ret)
  496. return 0;
  497. pkt_info.last_packet_type = pkt_info.packet.type;
  498. }
  499. }
  500. struct intel_pt_calc_cyc_to_tsc_info {
  501. uint64_t cycle_cnt;
  502. unsigned int cbr;
  503. uint32_t last_mtc;
  504. uint64_t ctc_timestamp;
  505. uint64_t ctc_delta;
  506. uint64_t tsc_timestamp;
  507. uint64_t timestamp;
  508. bool have_tma;
  509. bool fixup_last_mtc;
  510. bool from_mtc;
  511. double cbr_cyc_to_tsc;
  512. };
  513. /*
  514. * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
  515. * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
  516. * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
  517. * packet by copying the missing bits from the current MTC assuming the least
  518. * difference between the two, and that the current MTC comes after last_mtc.
  519. */
  520. static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
  521. uint32_t *last_mtc)
  522. {
  523. uint32_t first_missing_bit = 1U << (16 - mtc_shift);
  524. uint32_t mask = ~(first_missing_bit - 1);
  525. *last_mtc |= mtc & mask;
  526. if (*last_mtc >= mtc) {
  527. *last_mtc -= first_missing_bit;
  528. *last_mtc &= 0xff;
  529. }
  530. }
  531. static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
  532. {
  533. struct intel_pt_decoder *decoder = pkt_info->decoder;
  534. struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
  535. uint64_t timestamp;
  536. double cyc_to_tsc;
  537. unsigned int cbr;
  538. uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
  539. switch (pkt_info->packet.type) {
  540. case INTEL_PT_TNT:
  541. case INTEL_PT_TIP_PGE:
  542. case INTEL_PT_TIP:
  543. case INTEL_PT_FUP:
  544. case INTEL_PT_PSB:
  545. case INTEL_PT_PIP:
  546. case INTEL_PT_MODE_EXEC:
  547. case INTEL_PT_MODE_TSX:
  548. case INTEL_PT_PSBEND:
  549. case INTEL_PT_PAD:
  550. case INTEL_PT_VMCS:
  551. case INTEL_PT_MNT:
  552. return 0;
  553. case INTEL_PT_MTC:
  554. if (!data->have_tma)
  555. return 0;
  556. mtc = pkt_info->packet.payload;
  557. if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
  558. data->fixup_last_mtc = false;
  559. intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
  560. &data->last_mtc);
  561. }
  562. if (mtc > data->last_mtc)
  563. mtc_delta = mtc - data->last_mtc;
  564. else
  565. mtc_delta = mtc + 256 - data->last_mtc;
  566. data->ctc_delta += mtc_delta << decoder->mtc_shift;
  567. data->last_mtc = mtc;
  568. if (decoder->tsc_ctc_mult) {
  569. timestamp = data->ctc_timestamp +
  570. data->ctc_delta * decoder->tsc_ctc_mult;
  571. } else {
  572. timestamp = data->ctc_timestamp +
  573. multdiv(data->ctc_delta,
  574. decoder->tsc_ctc_ratio_n,
  575. decoder->tsc_ctc_ratio_d);
  576. }
  577. if (timestamp < data->timestamp)
  578. return 1;
  579. if (pkt_info->last_packet_type != INTEL_PT_CYC) {
  580. data->timestamp = timestamp;
  581. return 0;
  582. }
  583. break;
  584. case INTEL_PT_TSC:
  585. timestamp = pkt_info->packet.payload |
  586. (data->timestamp & (0xffULL << 56));
  587. if (data->from_mtc && timestamp < data->timestamp &&
  588. data->timestamp - timestamp < decoder->tsc_slip)
  589. return 1;
  590. if (timestamp < data->timestamp)
  591. timestamp += (1ULL << 56);
  592. if (pkt_info->last_packet_type != INTEL_PT_CYC) {
  593. if (data->from_mtc)
  594. return 1;
  595. data->tsc_timestamp = timestamp;
  596. data->timestamp = timestamp;
  597. return 0;
  598. }
  599. break;
  600. case INTEL_PT_TMA:
  601. if (data->from_mtc)
  602. return 1;
  603. if (!decoder->tsc_ctc_ratio_d)
  604. return 0;
  605. ctc = pkt_info->packet.payload;
  606. fc = pkt_info->packet.count;
  607. ctc_rem = ctc & decoder->ctc_rem_mask;
  608. data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
  609. data->ctc_timestamp = data->tsc_timestamp - fc;
  610. if (decoder->tsc_ctc_mult) {
  611. data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
  612. } else {
  613. data->ctc_timestamp -=
  614. multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
  615. decoder->tsc_ctc_ratio_d);
  616. }
  617. data->ctc_delta = 0;
  618. data->have_tma = true;
  619. data->fixup_last_mtc = true;
  620. return 0;
  621. case INTEL_PT_CYC:
  622. data->cycle_cnt += pkt_info->packet.payload;
  623. return 0;
  624. case INTEL_PT_CBR:
  625. cbr = pkt_info->packet.payload;
  626. if (data->cbr && data->cbr != cbr)
  627. return 1;
  628. data->cbr = cbr;
  629. data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
  630. return 0;
  631. case INTEL_PT_TIP_PGD:
  632. case INTEL_PT_TRACESTOP:
  633. case INTEL_PT_OVF:
  634. case INTEL_PT_BAD: /* Does not happen */
  635. default:
  636. return 1;
  637. }
  638. if (!data->cbr && decoder->cbr) {
  639. data->cbr = decoder->cbr;
  640. data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
  641. }
  642. if (!data->cycle_cnt)
  643. return 1;
  644. cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
  645. if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
  646. cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
  647. intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
  648. cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
  649. return 1;
  650. }
  651. decoder->calc_cyc_to_tsc = cyc_to_tsc;
  652. decoder->have_calc_cyc_to_tsc = true;
  653. if (data->cbr) {
  654. intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
  655. cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
  656. } else {
  657. intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
  658. cyc_to_tsc, pkt_info->pos);
  659. }
  660. return 1;
  661. }
  662. static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
  663. bool from_mtc)
  664. {
  665. struct intel_pt_calc_cyc_to_tsc_info data = {
  666. .cycle_cnt = 0,
  667. .cbr = 0,
  668. .last_mtc = decoder->last_mtc,
  669. .ctc_timestamp = decoder->ctc_timestamp,
  670. .ctc_delta = decoder->ctc_delta,
  671. .tsc_timestamp = decoder->tsc_timestamp,
  672. .timestamp = decoder->timestamp,
  673. .have_tma = decoder->have_tma,
  674. .fixup_last_mtc = decoder->fixup_last_mtc,
  675. .from_mtc = from_mtc,
  676. .cbr_cyc_to_tsc = 0,
  677. };
  678. intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
  679. }
  680. static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
  681. {
  682. int ret;
  683. decoder->last_packet_type = decoder->packet.type;
  684. do {
  685. decoder->pos += decoder->pkt_step;
  686. decoder->buf += decoder->pkt_step;
  687. decoder->len -= decoder->pkt_step;
  688. if (!decoder->len) {
  689. ret = intel_pt_get_next_data(decoder);
  690. if (ret)
  691. return ret;
  692. }
  693. ret = intel_pt_get_packet(decoder->buf, decoder->len,
  694. &decoder->packet);
  695. if (ret == INTEL_PT_NEED_MORE_BYTES &&
  696. decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
  697. ret = intel_pt_get_split_packet(decoder);
  698. if (ret < 0)
  699. return ret;
  700. }
  701. if (ret <= 0)
  702. return intel_pt_bad_packet(decoder);
  703. decoder->pkt_len = ret;
  704. decoder->pkt_step = ret;
  705. intel_pt_decoder_log_packet(decoder);
  706. } while (decoder->packet.type == INTEL_PT_PAD);
  707. return 0;
  708. }
  709. static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
  710. {
  711. uint64_t timestamp, masked_timestamp;
  712. timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
  713. masked_timestamp = timestamp & decoder->period_mask;
  714. if (decoder->continuous_period) {
  715. if (masked_timestamp != decoder->last_masked_timestamp)
  716. return 1;
  717. } else {
  718. timestamp += 1;
  719. masked_timestamp = timestamp & decoder->period_mask;
  720. if (masked_timestamp != decoder->last_masked_timestamp) {
  721. decoder->last_masked_timestamp = masked_timestamp;
  722. decoder->continuous_period = true;
  723. }
  724. }
  725. return decoder->period_ticks - (timestamp - masked_timestamp);
  726. }
  727. static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
  728. {
  729. switch (decoder->period_type) {
  730. case INTEL_PT_PERIOD_INSTRUCTIONS:
  731. return decoder->period - decoder->period_insn_cnt;
  732. case INTEL_PT_PERIOD_TICKS:
  733. return intel_pt_next_period(decoder);
  734. case INTEL_PT_PERIOD_NONE:
  735. case INTEL_PT_PERIOD_MTC:
  736. default:
  737. return 0;
  738. }
  739. }
  740. static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
  741. {
  742. uint64_t timestamp, masked_timestamp;
  743. switch (decoder->period_type) {
  744. case INTEL_PT_PERIOD_INSTRUCTIONS:
  745. decoder->period_insn_cnt = 0;
  746. break;
  747. case INTEL_PT_PERIOD_TICKS:
  748. timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
  749. masked_timestamp = timestamp & decoder->period_mask;
  750. decoder->last_masked_timestamp = masked_timestamp;
  751. break;
  752. case INTEL_PT_PERIOD_NONE:
  753. case INTEL_PT_PERIOD_MTC:
  754. default:
  755. break;
  756. }
  757. decoder->state.type |= INTEL_PT_INSTRUCTION;
  758. }
  759. static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
  760. struct intel_pt_insn *intel_pt_insn, uint64_t ip)
  761. {
  762. uint64_t max_insn_cnt, insn_cnt = 0;
  763. int err;
  764. if (!decoder->mtc_insn)
  765. decoder->mtc_insn = true;
  766. max_insn_cnt = intel_pt_next_sample(decoder);
  767. err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
  768. max_insn_cnt, decoder->data);
  769. decoder->tot_insn_cnt += insn_cnt;
  770. decoder->timestamp_insn_cnt += insn_cnt;
  771. decoder->period_insn_cnt += insn_cnt;
  772. if (err) {
  773. decoder->no_progress = 0;
  774. decoder->pkt_state = INTEL_PT_STATE_ERR2;
  775. intel_pt_log_at("ERROR: Failed to get instruction",
  776. decoder->ip);
  777. if (err == -ENOENT)
  778. return -ENOLINK;
  779. return -EILSEQ;
  780. }
  781. if (ip && decoder->ip == ip) {
  782. err = -EAGAIN;
  783. goto out;
  784. }
  785. if (max_insn_cnt && insn_cnt >= max_insn_cnt)
  786. intel_pt_sample_insn(decoder);
  787. if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
  788. decoder->state.type = INTEL_PT_INSTRUCTION;
  789. decoder->state.from_ip = decoder->ip;
  790. decoder->state.to_ip = 0;
  791. decoder->ip += intel_pt_insn->length;
  792. err = INTEL_PT_RETURN;
  793. goto out;
  794. }
  795. if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
  796. /* Zero-length calls are excluded */
  797. if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
  798. intel_pt_insn->rel) {
  799. err = intel_pt_push(&decoder->stack, decoder->ip +
  800. intel_pt_insn->length);
  801. if (err)
  802. goto out;
  803. }
  804. } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
  805. decoder->ret_addr = intel_pt_pop(&decoder->stack);
  806. }
  807. if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
  808. int cnt = decoder->no_progress++;
  809. decoder->state.from_ip = decoder->ip;
  810. decoder->ip += intel_pt_insn->length +
  811. intel_pt_insn->rel;
  812. decoder->state.to_ip = decoder->ip;
  813. err = INTEL_PT_RETURN;
  814. /*
  815. * Check for being stuck in a loop. This can happen if a
  816. * decoder error results in the decoder erroneously setting the
  817. * ip to an address that is itself in an infinite loop that
  818. * consumes no packets. When that happens, there must be an
  819. * unconditional branch.
  820. */
  821. if (cnt) {
  822. if (cnt == 1) {
  823. decoder->stuck_ip = decoder->state.to_ip;
  824. decoder->stuck_ip_prd = 1;
  825. decoder->stuck_ip_cnt = 1;
  826. } else if (cnt > INTEL_PT_MAX_LOOPS ||
  827. decoder->state.to_ip == decoder->stuck_ip) {
  828. intel_pt_log_at("ERROR: Never-ending loop",
  829. decoder->state.to_ip);
  830. decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
  831. err = -ELOOP;
  832. goto out;
  833. } else if (!--decoder->stuck_ip_cnt) {
  834. decoder->stuck_ip_prd += 1;
  835. decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
  836. decoder->stuck_ip = decoder->state.to_ip;
  837. }
  838. }
  839. goto out_no_progress;
  840. }
  841. out:
  842. decoder->no_progress = 0;
  843. out_no_progress:
  844. decoder->state.insn_op = intel_pt_insn->op;
  845. decoder->state.insn_len = intel_pt_insn->length;
  846. memcpy(decoder->state.insn, intel_pt_insn->buf,
  847. INTEL_PT_INSN_BUF_SZ);
  848. if (decoder->tx_flags & INTEL_PT_IN_TX)
  849. decoder->state.flags |= INTEL_PT_IN_TX;
  850. return err;
  851. }
  852. static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
  853. {
  854. struct intel_pt_insn intel_pt_insn;
  855. uint64_t ip;
  856. int err;
  857. ip = decoder->last_ip;
  858. while (1) {
  859. err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
  860. if (err == INTEL_PT_RETURN)
  861. return 0;
  862. if (err == -EAGAIN) {
  863. if (decoder->set_fup_tx_flags) {
  864. decoder->set_fup_tx_flags = false;
  865. decoder->tx_flags = decoder->fup_tx_flags;
  866. decoder->state.type = INTEL_PT_TRANSACTION;
  867. decoder->state.from_ip = decoder->ip;
  868. decoder->state.to_ip = 0;
  869. decoder->state.flags = decoder->fup_tx_flags;
  870. return 0;
  871. }
  872. return err;
  873. }
  874. decoder->set_fup_tx_flags = false;
  875. if (err)
  876. return err;
  877. if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
  878. intel_pt_log_at("ERROR: Unexpected indirect branch",
  879. decoder->ip);
  880. decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
  881. return -ENOENT;
  882. }
  883. if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
  884. intel_pt_log_at("ERROR: Unexpected conditional branch",
  885. decoder->ip);
  886. decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
  887. return -ENOENT;
  888. }
  889. intel_pt_bug(decoder);
  890. }
  891. }
  892. static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
  893. {
  894. struct intel_pt_insn intel_pt_insn;
  895. int err;
  896. err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
  897. if (err == INTEL_PT_RETURN &&
  898. decoder->pgd_ip &&
  899. decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
  900. (decoder->state.type & INTEL_PT_BRANCH) &&
  901. decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
  902. /* Unconditional branch leaving filter region */
  903. decoder->no_progress = 0;
  904. decoder->pge = false;
  905. decoder->continuous_period = false;
  906. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  907. decoder->state.to_ip = 0;
  908. return 0;
  909. }
  910. if (err == INTEL_PT_RETURN)
  911. return 0;
  912. if (err)
  913. return err;
  914. if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
  915. if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
  916. decoder->pge = false;
  917. decoder->continuous_period = false;
  918. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  919. decoder->state.from_ip = decoder->ip;
  920. decoder->state.to_ip = 0;
  921. if (decoder->packet.count != 0)
  922. decoder->ip = decoder->last_ip;
  923. } else {
  924. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  925. decoder->state.from_ip = decoder->ip;
  926. if (decoder->packet.count == 0) {
  927. decoder->state.to_ip = 0;
  928. } else {
  929. decoder->state.to_ip = decoder->last_ip;
  930. decoder->ip = decoder->last_ip;
  931. }
  932. }
  933. return 0;
  934. }
  935. if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
  936. uint64_t to_ip = decoder->ip + intel_pt_insn.length +
  937. intel_pt_insn.rel;
  938. if (decoder->pgd_ip &&
  939. decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
  940. decoder->pgd_ip(to_ip, decoder->data)) {
  941. /* Conditional branch leaving filter region */
  942. decoder->pge = false;
  943. decoder->continuous_period = false;
  944. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  945. decoder->ip = to_ip;
  946. decoder->state.from_ip = decoder->ip;
  947. decoder->state.to_ip = 0;
  948. return 0;
  949. }
  950. intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
  951. decoder->ip);
  952. decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
  953. return -ENOENT;
  954. }
  955. return intel_pt_bug(decoder);
  956. }
  957. static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
  958. {
  959. struct intel_pt_insn intel_pt_insn;
  960. int err;
  961. while (1) {
  962. err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
  963. if (err == INTEL_PT_RETURN)
  964. return 0;
  965. if (err)
  966. return err;
  967. if (intel_pt_insn.op == INTEL_PT_OP_RET) {
  968. if (!decoder->return_compression) {
  969. intel_pt_log_at("ERROR: RET when expecting conditional branch",
  970. decoder->ip);
  971. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  972. return -ENOENT;
  973. }
  974. if (!decoder->ret_addr) {
  975. intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
  976. decoder->ip);
  977. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  978. return -ENOENT;
  979. }
  980. if (!(decoder->tnt.payload & BIT63)) {
  981. intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
  982. decoder->ip);
  983. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  984. return -ENOENT;
  985. }
  986. decoder->tnt.count -= 1;
  987. if (!decoder->tnt.count)
  988. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  989. decoder->tnt.payload <<= 1;
  990. decoder->state.from_ip = decoder->ip;
  991. decoder->ip = decoder->ret_addr;
  992. decoder->state.to_ip = decoder->ip;
  993. return 0;
  994. }
  995. if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
  996. /* Handle deferred TIPs */
  997. err = intel_pt_get_next_packet(decoder);
  998. if (err)
  999. return err;
  1000. if (decoder->packet.type != INTEL_PT_TIP ||
  1001. decoder->packet.count == 0) {
  1002. intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
  1003. decoder->ip);
  1004. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  1005. decoder->pkt_step = 0;
  1006. return -ENOENT;
  1007. }
  1008. intel_pt_set_last_ip(decoder);
  1009. decoder->state.from_ip = decoder->ip;
  1010. decoder->state.to_ip = decoder->last_ip;
  1011. decoder->ip = decoder->last_ip;
  1012. return 0;
  1013. }
  1014. if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
  1015. decoder->tnt.count -= 1;
  1016. if (!decoder->tnt.count)
  1017. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1018. if (decoder->tnt.payload & BIT63) {
  1019. decoder->tnt.payload <<= 1;
  1020. decoder->state.from_ip = decoder->ip;
  1021. decoder->ip += intel_pt_insn.length +
  1022. intel_pt_insn.rel;
  1023. decoder->state.to_ip = decoder->ip;
  1024. return 0;
  1025. }
  1026. /* Instruction sample for a non-taken branch */
  1027. if (decoder->state.type & INTEL_PT_INSTRUCTION) {
  1028. decoder->tnt.payload <<= 1;
  1029. decoder->state.type = INTEL_PT_INSTRUCTION;
  1030. decoder->state.from_ip = decoder->ip;
  1031. decoder->state.to_ip = 0;
  1032. decoder->ip += intel_pt_insn.length;
  1033. return 0;
  1034. }
  1035. decoder->ip += intel_pt_insn.length;
  1036. if (!decoder->tnt.count)
  1037. return -EAGAIN;
  1038. decoder->tnt.payload <<= 1;
  1039. continue;
  1040. }
  1041. return intel_pt_bug(decoder);
  1042. }
  1043. }
  1044. static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
  1045. {
  1046. unsigned int fup_tx_flags;
  1047. int err;
  1048. fup_tx_flags = decoder->packet.payload &
  1049. (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
  1050. err = intel_pt_get_next_packet(decoder);
  1051. if (err)
  1052. return err;
  1053. if (decoder->packet.type == INTEL_PT_FUP) {
  1054. decoder->fup_tx_flags = fup_tx_flags;
  1055. decoder->set_fup_tx_flags = true;
  1056. if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
  1057. *no_tip = true;
  1058. } else {
  1059. intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
  1060. decoder->pos);
  1061. intel_pt_update_in_tx(decoder);
  1062. }
  1063. return 0;
  1064. }
  1065. static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
  1066. {
  1067. uint64_t timestamp;
  1068. decoder->have_tma = false;
  1069. if (decoder->ref_timestamp) {
  1070. timestamp = decoder->packet.payload |
  1071. (decoder->ref_timestamp & (0xffULL << 56));
  1072. if (timestamp < decoder->ref_timestamp) {
  1073. if (decoder->ref_timestamp - timestamp > (1ULL << 55))
  1074. timestamp += (1ULL << 56);
  1075. } else {
  1076. if (timestamp - decoder->ref_timestamp > (1ULL << 55))
  1077. timestamp -= (1ULL << 56);
  1078. }
  1079. decoder->tsc_timestamp = timestamp;
  1080. decoder->timestamp = timestamp;
  1081. decoder->ref_timestamp = 0;
  1082. decoder->timestamp_insn_cnt = 0;
  1083. } else if (decoder->timestamp) {
  1084. timestamp = decoder->packet.payload |
  1085. (decoder->timestamp & (0xffULL << 56));
  1086. decoder->tsc_timestamp = timestamp;
  1087. if (timestamp < decoder->timestamp &&
  1088. decoder->timestamp - timestamp < decoder->tsc_slip) {
  1089. intel_pt_log_to("Suppressing backwards timestamp",
  1090. timestamp);
  1091. timestamp = decoder->timestamp;
  1092. }
  1093. if (timestamp < decoder->timestamp) {
  1094. intel_pt_log_to("Wraparound timestamp", timestamp);
  1095. timestamp += (1ULL << 56);
  1096. decoder->tsc_timestamp = timestamp;
  1097. }
  1098. decoder->timestamp = timestamp;
  1099. decoder->timestamp_insn_cnt = 0;
  1100. }
  1101. if (decoder->last_packet_type == INTEL_PT_CYC) {
  1102. decoder->cyc_ref_timestamp = decoder->timestamp;
  1103. decoder->cycle_cnt = 0;
  1104. decoder->have_calc_cyc_to_tsc = false;
  1105. intel_pt_calc_cyc_to_tsc(decoder, false);
  1106. }
  1107. intel_pt_log_to("Setting timestamp", decoder->timestamp);
  1108. }
  1109. static int intel_pt_overflow(struct intel_pt_decoder *decoder)
  1110. {
  1111. intel_pt_log("ERROR: Buffer overflow\n");
  1112. intel_pt_clear_tx_flags(decoder);
  1113. decoder->have_tma = false;
  1114. decoder->cbr = 0;
  1115. decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
  1116. decoder->overflow = true;
  1117. return -EOVERFLOW;
  1118. }
  1119. static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
  1120. {
  1121. uint32_t ctc = decoder->packet.payload;
  1122. uint32_t fc = decoder->packet.count;
  1123. uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
  1124. if (!decoder->tsc_ctc_ratio_d)
  1125. return;
  1126. decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
  1127. decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
  1128. if (decoder->tsc_ctc_mult) {
  1129. decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
  1130. } else {
  1131. decoder->ctc_timestamp -= multdiv(ctc_rem,
  1132. decoder->tsc_ctc_ratio_n,
  1133. decoder->tsc_ctc_ratio_d);
  1134. }
  1135. decoder->ctc_delta = 0;
  1136. decoder->have_tma = true;
  1137. decoder->fixup_last_mtc = true;
  1138. intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
  1139. decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
  1140. }
  1141. static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
  1142. {
  1143. uint64_t timestamp;
  1144. uint32_t mtc, mtc_delta;
  1145. if (!decoder->have_tma)
  1146. return;
  1147. mtc = decoder->packet.payload;
  1148. if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
  1149. decoder->fixup_last_mtc = false;
  1150. intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
  1151. &decoder->last_mtc);
  1152. }
  1153. if (mtc > decoder->last_mtc)
  1154. mtc_delta = mtc - decoder->last_mtc;
  1155. else
  1156. mtc_delta = mtc + 256 - decoder->last_mtc;
  1157. decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
  1158. if (decoder->tsc_ctc_mult) {
  1159. timestamp = decoder->ctc_timestamp +
  1160. decoder->ctc_delta * decoder->tsc_ctc_mult;
  1161. } else {
  1162. timestamp = decoder->ctc_timestamp +
  1163. multdiv(decoder->ctc_delta,
  1164. decoder->tsc_ctc_ratio_n,
  1165. decoder->tsc_ctc_ratio_d);
  1166. }
  1167. if (timestamp < decoder->timestamp)
  1168. intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
  1169. timestamp, decoder->timestamp);
  1170. else
  1171. decoder->timestamp = timestamp;
  1172. decoder->timestamp_insn_cnt = 0;
  1173. decoder->last_mtc = mtc;
  1174. if (decoder->last_packet_type == INTEL_PT_CYC) {
  1175. decoder->cyc_ref_timestamp = decoder->timestamp;
  1176. decoder->cycle_cnt = 0;
  1177. decoder->have_calc_cyc_to_tsc = false;
  1178. intel_pt_calc_cyc_to_tsc(decoder, true);
  1179. }
  1180. }
  1181. static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
  1182. {
  1183. unsigned int cbr = decoder->packet.payload;
  1184. if (decoder->cbr == cbr)
  1185. return;
  1186. decoder->cbr = cbr;
  1187. decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
  1188. }
  1189. static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
  1190. {
  1191. uint64_t timestamp = decoder->cyc_ref_timestamp;
  1192. decoder->have_cyc = true;
  1193. decoder->cycle_cnt += decoder->packet.payload;
  1194. if (!decoder->cyc_ref_timestamp)
  1195. return;
  1196. if (decoder->have_calc_cyc_to_tsc)
  1197. timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
  1198. else if (decoder->cbr)
  1199. timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
  1200. else
  1201. return;
  1202. if (timestamp < decoder->timestamp)
  1203. intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
  1204. timestamp, decoder->timestamp);
  1205. else
  1206. decoder->timestamp = timestamp;
  1207. decoder->timestamp_insn_cnt = 0;
  1208. }
  1209. /* Walk PSB+ packets when already in sync. */
  1210. static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
  1211. {
  1212. int err;
  1213. while (1) {
  1214. err = intel_pt_get_next_packet(decoder);
  1215. if (err)
  1216. return err;
  1217. switch (decoder->packet.type) {
  1218. case INTEL_PT_PSBEND:
  1219. return 0;
  1220. case INTEL_PT_TIP_PGD:
  1221. case INTEL_PT_TIP_PGE:
  1222. case INTEL_PT_TIP:
  1223. case INTEL_PT_TNT:
  1224. case INTEL_PT_TRACESTOP:
  1225. case INTEL_PT_BAD:
  1226. case INTEL_PT_PSB:
  1227. decoder->have_tma = false;
  1228. intel_pt_log("ERROR: Unexpected packet\n");
  1229. return -EAGAIN;
  1230. case INTEL_PT_OVF:
  1231. return intel_pt_overflow(decoder);
  1232. case INTEL_PT_TSC:
  1233. intel_pt_calc_tsc_timestamp(decoder);
  1234. break;
  1235. case INTEL_PT_TMA:
  1236. intel_pt_calc_tma(decoder);
  1237. break;
  1238. case INTEL_PT_CBR:
  1239. intel_pt_calc_cbr(decoder);
  1240. break;
  1241. case INTEL_PT_MODE_EXEC:
  1242. decoder->exec_mode = decoder->packet.payload;
  1243. break;
  1244. case INTEL_PT_PIP:
  1245. decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
  1246. break;
  1247. case INTEL_PT_FUP:
  1248. decoder->pge = true;
  1249. intel_pt_set_last_ip(decoder);
  1250. break;
  1251. case INTEL_PT_MODE_TSX:
  1252. intel_pt_update_in_tx(decoder);
  1253. break;
  1254. case INTEL_PT_MTC:
  1255. intel_pt_calc_mtc_timestamp(decoder);
  1256. if (decoder->period_type == INTEL_PT_PERIOD_MTC)
  1257. decoder->state.type |= INTEL_PT_INSTRUCTION;
  1258. break;
  1259. case INTEL_PT_CYC:
  1260. case INTEL_PT_VMCS:
  1261. case INTEL_PT_MNT:
  1262. case INTEL_PT_PAD:
  1263. default:
  1264. break;
  1265. }
  1266. }
  1267. }
  1268. static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
  1269. {
  1270. int err;
  1271. if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
  1272. decoder->tx_flags = 0;
  1273. decoder->state.flags &= ~INTEL_PT_IN_TX;
  1274. decoder->state.flags |= INTEL_PT_ABORT_TX;
  1275. } else {
  1276. decoder->state.flags |= INTEL_PT_ASYNC;
  1277. }
  1278. while (1) {
  1279. err = intel_pt_get_next_packet(decoder);
  1280. if (err)
  1281. return err;
  1282. switch (decoder->packet.type) {
  1283. case INTEL_PT_TNT:
  1284. case INTEL_PT_FUP:
  1285. case INTEL_PT_TRACESTOP:
  1286. case INTEL_PT_PSB:
  1287. case INTEL_PT_TSC:
  1288. case INTEL_PT_TMA:
  1289. case INTEL_PT_CBR:
  1290. case INTEL_PT_MODE_TSX:
  1291. case INTEL_PT_BAD:
  1292. case INTEL_PT_PSBEND:
  1293. intel_pt_log("ERROR: Missing TIP after FUP\n");
  1294. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  1295. return -ENOENT;
  1296. case INTEL_PT_OVF:
  1297. return intel_pt_overflow(decoder);
  1298. case INTEL_PT_TIP_PGD:
  1299. decoder->state.from_ip = decoder->ip;
  1300. decoder->state.to_ip = 0;
  1301. if (decoder->packet.count != 0) {
  1302. intel_pt_set_ip(decoder);
  1303. intel_pt_log("Omitting PGD ip " x64_fmt "\n",
  1304. decoder->ip);
  1305. }
  1306. decoder->pge = false;
  1307. decoder->continuous_period = false;
  1308. return 0;
  1309. case INTEL_PT_TIP_PGE:
  1310. decoder->pge = true;
  1311. intel_pt_log("Omitting PGE ip " x64_fmt "\n",
  1312. decoder->ip);
  1313. decoder->state.from_ip = 0;
  1314. if (decoder->packet.count == 0) {
  1315. decoder->state.to_ip = 0;
  1316. } else {
  1317. intel_pt_set_ip(decoder);
  1318. decoder->state.to_ip = decoder->ip;
  1319. }
  1320. return 0;
  1321. case INTEL_PT_TIP:
  1322. decoder->state.from_ip = decoder->ip;
  1323. if (decoder->packet.count == 0) {
  1324. decoder->state.to_ip = 0;
  1325. } else {
  1326. intel_pt_set_ip(decoder);
  1327. decoder->state.to_ip = decoder->ip;
  1328. }
  1329. return 0;
  1330. case INTEL_PT_PIP:
  1331. decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
  1332. break;
  1333. case INTEL_PT_MTC:
  1334. intel_pt_calc_mtc_timestamp(decoder);
  1335. if (decoder->period_type == INTEL_PT_PERIOD_MTC)
  1336. decoder->state.type |= INTEL_PT_INSTRUCTION;
  1337. break;
  1338. case INTEL_PT_CYC:
  1339. intel_pt_calc_cyc_timestamp(decoder);
  1340. break;
  1341. case INTEL_PT_MODE_EXEC:
  1342. decoder->exec_mode = decoder->packet.payload;
  1343. break;
  1344. case INTEL_PT_VMCS:
  1345. case INTEL_PT_MNT:
  1346. case INTEL_PT_PAD:
  1347. break;
  1348. default:
  1349. return intel_pt_bug(decoder);
  1350. }
  1351. }
  1352. }
  1353. static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
  1354. {
  1355. bool no_tip = false;
  1356. int err;
  1357. while (1) {
  1358. err = intel_pt_get_next_packet(decoder);
  1359. if (err)
  1360. return err;
  1361. next:
  1362. switch (decoder->packet.type) {
  1363. case INTEL_PT_TNT:
  1364. if (!decoder->packet.count)
  1365. break;
  1366. decoder->tnt = decoder->packet;
  1367. decoder->pkt_state = INTEL_PT_STATE_TNT;
  1368. err = intel_pt_walk_tnt(decoder);
  1369. if (err == -EAGAIN)
  1370. break;
  1371. return err;
  1372. case INTEL_PT_TIP_PGD:
  1373. if (decoder->packet.count != 0)
  1374. intel_pt_set_last_ip(decoder);
  1375. decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
  1376. return intel_pt_walk_tip(decoder);
  1377. case INTEL_PT_TIP_PGE: {
  1378. decoder->pge = true;
  1379. if (decoder->packet.count == 0) {
  1380. intel_pt_log_at("Skipping zero TIP.PGE",
  1381. decoder->pos);
  1382. break;
  1383. }
  1384. intel_pt_set_ip(decoder);
  1385. decoder->state.from_ip = 0;
  1386. decoder->state.to_ip = decoder->ip;
  1387. return 0;
  1388. }
  1389. case INTEL_PT_OVF:
  1390. return intel_pt_overflow(decoder);
  1391. case INTEL_PT_TIP:
  1392. if (decoder->packet.count != 0)
  1393. intel_pt_set_last_ip(decoder);
  1394. decoder->pkt_state = INTEL_PT_STATE_TIP;
  1395. return intel_pt_walk_tip(decoder);
  1396. case INTEL_PT_FUP:
  1397. if (decoder->packet.count == 0) {
  1398. intel_pt_log_at("Skipping zero FUP",
  1399. decoder->pos);
  1400. no_tip = false;
  1401. break;
  1402. }
  1403. intel_pt_set_last_ip(decoder);
  1404. err = intel_pt_walk_fup(decoder);
  1405. if (err != -EAGAIN) {
  1406. if (err)
  1407. return err;
  1408. if (no_tip)
  1409. decoder->pkt_state =
  1410. INTEL_PT_STATE_FUP_NO_TIP;
  1411. else
  1412. decoder->pkt_state = INTEL_PT_STATE_FUP;
  1413. return 0;
  1414. }
  1415. if (no_tip) {
  1416. no_tip = false;
  1417. break;
  1418. }
  1419. return intel_pt_walk_fup_tip(decoder);
  1420. case INTEL_PT_TRACESTOP:
  1421. decoder->pge = false;
  1422. decoder->continuous_period = false;
  1423. intel_pt_clear_tx_flags(decoder);
  1424. decoder->have_tma = false;
  1425. break;
  1426. case INTEL_PT_PSB:
  1427. intel_pt_clear_stack(&decoder->stack);
  1428. err = intel_pt_walk_psbend(decoder);
  1429. if (err == -EAGAIN)
  1430. goto next;
  1431. if (err)
  1432. return err;
  1433. break;
  1434. case INTEL_PT_PIP:
  1435. decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
  1436. break;
  1437. case INTEL_PT_MTC:
  1438. intel_pt_calc_mtc_timestamp(decoder);
  1439. if (decoder->period_type != INTEL_PT_PERIOD_MTC)
  1440. break;
  1441. /*
  1442. * Ensure that there has been an instruction since the
  1443. * last MTC.
  1444. */
  1445. if (!decoder->mtc_insn)
  1446. break;
  1447. decoder->mtc_insn = false;
  1448. /* Ensure that there is a timestamp */
  1449. if (!decoder->timestamp)
  1450. break;
  1451. decoder->state.type = INTEL_PT_INSTRUCTION;
  1452. decoder->state.from_ip = decoder->ip;
  1453. decoder->state.to_ip = 0;
  1454. decoder->mtc_insn = false;
  1455. return 0;
  1456. case INTEL_PT_TSC:
  1457. intel_pt_calc_tsc_timestamp(decoder);
  1458. break;
  1459. case INTEL_PT_TMA:
  1460. intel_pt_calc_tma(decoder);
  1461. break;
  1462. case INTEL_PT_CYC:
  1463. intel_pt_calc_cyc_timestamp(decoder);
  1464. break;
  1465. case INTEL_PT_CBR:
  1466. intel_pt_calc_cbr(decoder);
  1467. break;
  1468. case INTEL_PT_MODE_EXEC:
  1469. decoder->exec_mode = decoder->packet.payload;
  1470. break;
  1471. case INTEL_PT_MODE_TSX:
  1472. /* MODE_TSX need not be followed by FUP */
  1473. if (!decoder->pge) {
  1474. intel_pt_update_in_tx(decoder);
  1475. break;
  1476. }
  1477. err = intel_pt_mode_tsx(decoder, &no_tip);
  1478. if (err)
  1479. return err;
  1480. goto next;
  1481. case INTEL_PT_BAD: /* Does not happen */
  1482. return intel_pt_bug(decoder);
  1483. case INTEL_PT_PSBEND:
  1484. case INTEL_PT_VMCS:
  1485. case INTEL_PT_MNT:
  1486. case INTEL_PT_PAD:
  1487. break;
  1488. default:
  1489. return intel_pt_bug(decoder);
  1490. }
  1491. }
  1492. }
  1493. static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
  1494. {
  1495. return decoder->last_ip || decoder->packet.count == 0 ||
  1496. decoder->packet.count == 3 || decoder->packet.count == 6;
  1497. }
  1498. /* Walk PSB+ packets to get in sync. */
  1499. static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
  1500. {
  1501. int err;
  1502. while (1) {
  1503. err = intel_pt_get_next_packet(decoder);
  1504. if (err)
  1505. return err;
  1506. switch (decoder->packet.type) {
  1507. case INTEL_PT_TIP_PGD:
  1508. decoder->continuous_period = false;
  1509. __fallthrough;
  1510. case INTEL_PT_TIP_PGE:
  1511. case INTEL_PT_TIP:
  1512. intel_pt_log("ERROR: Unexpected packet\n");
  1513. return -ENOENT;
  1514. case INTEL_PT_FUP:
  1515. decoder->pge = true;
  1516. if (intel_pt_have_ip(decoder)) {
  1517. uint64_t current_ip = decoder->ip;
  1518. intel_pt_set_ip(decoder);
  1519. if (current_ip)
  1520. intel_pt_log_to("Setting IP",
  1521. decoder->ip);
  1522. }
  1523. break;
  1524. case INTEL_PT_MTC:
  1525. intel_pt_calc_mtc_timestamp(decoder);
  1526. break;
  1527. case INTEL_PT_TSC:
  1528. intel_pt_calc_tsc_timestamp(decoder);
  1529. break;
  1530. case INTEL_PT_TMA:
  1531. intel_pt_calc_tma(decoder);
  1532. break;
  1533. case INTEL_PT_CYC:
  1534. intel_pt_calc_cyc_timestamp(decoder);
  1535. break;
  1536. case INTEL_PT_CBR:
  1537. intel_pt_calc_cbr(decoder);
  1538. break;
  1539. case INTEL_PT_PIP:
  1540. decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
  1541. break;
  1542. case INTEL_PT_MODE_EXEC:
  1543. decoder->exec_mode = decoder->packet.payload;
  1544. break;
  1545. case INTEL_PT_MODE_TSX:
  1546. intel_pt_update_in_tx(decoder);
  1547. break;
  1548. case INTEL_PT_TRACESTOP:
  1549. decoder->pge = false;
  1550. decoder->continuous_period = false;
  1551. intel_pt_clear_tx_flags(decoder);
  1552. __fallthrough;
  1553. case INTEL_PT_TNT:
  1554. decoder->have_tma = false;
  1555. intel_pt_log("ERROR: Unexpected packet\n");
  1556. if (decoder->ip)
  1557. decoder->pkt_state = INTEL_PT_STATE_ERR4;
  1558. else
  1559. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  1560. return -ENOENT;
  1561. case INTEL_PT_BAD: /* Does not happen */
  1562. return intel_pt_bug(decoder);
  1563. case INTEL_PT_OVF:
  1564. return intel_pt_overflow(decoder);
  1565. case INTEL_PT_PSBEND:
  1566. return 0;
  1567. case INTEL_PT_PSB:
  1568. case INTEL_PT_VMCS:
  1569. case INTEL_PT_MNT:
  1570. case INTEL_PT_PAD:
  1571. default:
  1572. break;
  1573. }
  1574. }
  1575. }
  1576. static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
  1577. {
  1578. int err;
  1579. while (1) {
  1580. err = intel_pt_get_next_packet(decoder);
  1581. if (err)
  1582. return err;
  1583. switch (decoder->packet.type) {
  1584. case INTEL_PT_TIP_PGD:
  1585. decoder->continuous_period = false;
  1586. __fallthrough;
  1587. case INTEL_PT_TIP_PGE:
  1588. case INTEL_PT_TIP:
  1589. decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
  1590. if (intel_pt_have_ip(decoder))
  1591. intel_pt_set_ip(decoder);
  1592. if (decoder->ip)
  1593. return 0;
  1594. break;
  1595. case INTEL_PT_FUP:
  1596. if (decoder->overflow) {
  1597. if (intel_pt_have_ip(decoder))
  1598. intel_pt_set_ip(decoder);
  1599. if (decoder->ip)
  1600. return 0;
  1601. }
  1602. if (decoder->packet.count)
  1603. intel_pt_set_last_ip(decoder);
  1604. break;
  1605. case INTEL_PT_MTC:
  1606. intel_pt_calc_mtc_timestamp(decoder);
  1607. break;
  1608. case INTEL_PT_TSC:
  1609. intel_pt_calc_tsc_timestamp(decoder);
  1610. break;
  1611. case INTEL_PT_TMA:
  1612. intel_pt_calc_tma(decoder);
  1613. break;
  1614. case INTEL_PT_CYC:
  1615. intel_pt_calc_cyc_timestamp(decoder);
  1616. break;
  1617. case INTEL_PT_CBR:
  1618. intel_pt_calc_cbr(decoder);
  1619. break;
  1620. case INTEL_PT_PIP:
  1621. decoder->cr3 = decoder->packet.payload & (BIT63 - 1);
  1622. break;
  1623. case INTEL_PT_MODE_EXEC:
  1624. decoder->exec_mode = decoder->packet.payload;
  1625. break;
  1626. case INTEL_PT_MODE_TSX:
  1627. intel_pt_update_in_tx(decoder);
  1628. break;
  1629. case INTEL_PT_OVF:
  1630. return intel_pt_overflow(decoder);
  1631. case INTEL_PT_BAD: /* Does not happen */
  1632. return intel_pt_bug(decoder);
  1633. case INTEL_PT_TRACESTOP:
  1634. decoder->pge = false;
  1635. decoder->continuous_period = false;
  1636. intel_pt_clear_tx_flags(decoder);
  1637. decoder->have_tma = false;
  1638. break;
  1639. case INTEL_PT_PSB:
  1640. err = intel_pt_walk_psb(decoder);
  1641. if (err)
  1642. return err;
  1643. if (decoder->ip) {
  1644. /* Do not have a sample */
  1645. decoder->state.type = 0;
  1646. return 0;
  1647. }
  1648. break;
  1649. case INTEL_PT_TNT:
  1650. case INTEL_PT_PSBEND:
  1651. case INTEL_PT_VMCS:
  1652. case INTEL_PT_MNT:
  1653. case INTEL_PT_PAD:
  1654. default:
  1655. break;
  1656. }
  1657. }
  1658. }
  1659. static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
  1660. {
  1661. int err;
  1662. intel_pt_log("Scanning for full IP\n");
  1663. err = intel_pt_walk_to_ip(decoder);
  1664. if (err)
  1665. return err;
  1666. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1667. decoder->overflow = false;
  1668. decoder->state.from_ip = 0;
  1669. decoder->state.to_ip = decoder->ip;
  1670. intel_pt_log_to("Setting IP", decoder->ip);
  1671. return 0;
  1672. }
  1673. static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
  1674. {
  1675. const unsigned char *end = decoder->buf + decoder->len;
  1676. size_t i;
  1677. for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
  1678. if (i > decoder->len)
  1679. continue;
  1680. if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
  1681. return i;
  1682. }
  1683. return 0;
  1684. }
  1685. static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
  1686. {
  1687. size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
  1688. const char *psb = INTEL_PT_PSB_STR;
  1689. if (rest_psb > decoder->len ||
  1690. memcmp(decoder->buf, psb + part_psb, rest_psb))
  1691. return 0;
  1692. return rest_psb;
  1693. }
  1694. static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
  1695. int part_psb)
  1696. {
  1697. int rest_psb, ret;
  1698. decoder->pos += decoder->len;
  1699. decoder->len = 0;
  1700. ret = intel_pt_get_next_data(decoder);
  1701. if (ret)
  1702. return ret;
  1703. rest_psb = intel_pt_rest_psb(decoder, part_psb);
  1704. if (!rest_psb)
  1705. return 0;
  1706. decoder->pos -= part_psb;
  1707. decoder->next_buf = decoder->buf + rest_psb;
  1708. decoder->next_len = decoder->len - rest_psb;
  1709. memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
  1710. decoder->buf = decoder->temp_buf;
  1711. decoder->len = INTEL_PT_PSB_LEN;
  1712. return 0;
  1713. }
  1714. static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
  1715. {
  1716. unsigned char *next;
  1717. int ret;
  1718. intel_pt_log("Scanning for PSB\n");
  1719. while (1) {
  1720. if (!decoder->len) {
  1721. ret = intel_pt_get_next_data(decoder);
  1722. if (ret)
  1723. return ret;
  1724. }
  1725. next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
  1726. INTEL_PT_PSB_LEN);
  1727. if (!next) {
  1728. int part_psb;
  1729. part_psb = intel_pt_part_psb(decoder);
  1730. if (part_psb) {
  1731. ret = intel_pt_get_split_psb(decoder, part_psb);
  1732. if (ret)
  1733. return ret;
  1734. } else {
  1735. decoder->pos += decoder->len;
  1736. decoder->len = 0;
  1737. }
  1738. continue;
  1739. }
  1740. decoder->pkt_step = next - decoder->buf;
  1741. return intel_pt_get_next_packet(decoder);
  1742. }
  1743. }
  1744. static int intel_pt_sync(struct intel_pt_decoder *decoder)
  1745. {
  1746. int err;
  1747. decoder->pge = false;
  1748. decoder->continuous_period = false;
  1749. decoder->last_ip = 0;
  1750. decoder->ip = 0;
  1751. intel_pt_clear_stack(&decoder->stack);
  1752. err = intel_pt_scan_for_psb(decoder);
  1753. if (err)
  1754. return err;
  1755. decoder->pkt_state = INTEL_PT_STATE_NO_IP;
  1756. err = intel_pt_walk_psb(decoder);
  1757. if (err)
  1758. return err;
  1759. if (decoder->ip) {
  1760. decoder->state.type = 0; /* Do not have a sample */
  1761. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1762. } else {
  1763. return intel_pt_sync_ip(decoder);
  1764. }
  1765. return 0;
  1766. }
  1767. static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
  1768. {
  1769. uint64_t est = decoder->timestamp_insn_cnt << 1;
  1770. if (!decoder->cbr || !decoder->max_non_turbo_ratio)
  1771. goto out;
  1772. est *= decoder->max_non_turbo_ratio;
  1773. est /= decoder->cbr;
  1774. out:
  1775. return decoder->timestamp + est;
  1776. }
  1777. const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
  1778. {
  1779. int err;
  1780. do {
  1781. decoder->state.type = INTEL_PT_BRANCH;
  1782. decoder->state.flags = 0;
  1783. switch (decoder->pkt_state) {
  1784. case INTEL_PT_STATE_NO_PSB:
  1785. err = intel_pt_sync(decoder);
  1786. break;
  1787. case INTEL_PT_STATE_NO_IP:
  1788. decoder->last_ip = 0;
  1789. /* Fall through */
  1790. case INTEL_PT_STATE_ERR_RESYNC:
  1791. err = intel_pt_sync_ip(decoder);
  1792. break;
  1793. case INTEL_PT_STATE_IN_SYNC:
  1794. err = intel_pt_walk_trace(decoder);
  1795. break;
  1796. case INTEL_PT_STATE_TNT:
  1797. err = intel_pt_walk_tnt(decoder);
  1798. if (err == -EAGAIN)
  1799. err = intel_pt_walk_trace(decoder);
  1800. break;
  1801. case INTEL_PT_STATE_TIP:
  1802. case INTEL_PT_STATE_TIP_PGD:
  1803. err = intel_pt_walk_tip(decoder);
  1804. break;
  1805. case INTEL_PT_STATE_FUP:
  1806. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1807. err = intel_pt_walk_fup(decoder);
  1808. if (err == -EAGAIN)
  1809. err = intel_pt_walk_fup_tip(decoder);
  1810. else if (!err)
  1811. decoder->pkt_state = INTEL_PT_STATE_FUP;
  1812. break;
  1813. case INTEL_PT_STATE_FUP_NO_TIP:
  1814. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1815. err = intel_pt_walk_fup(decoder);
  1816. if (err == -EAGAIN)
  1817. err = intel_pt_walk_trace(decoder);
  1818. break;
  1819. default:
  1820. err = intel_pt_bug(decoder);
  1821. break;
  1822. }
  1823. } while (err == -ENOLINK);
  1824. if (err) {
  1825. decoder->state.err = intel_pt_ext_err(err);
  1826. decoder->state.from_ip = decoder->ip;
  1827. } else {
  1828. decoder->state.err = 0;
  1829. }
  1830. decoder->state.timestamp = decoder->timestamp;
  1831. decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
  1832. decoder->state.cr3 = decoder->cr3;
  1833. decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
  1834. return &decoder->state;
  1835. }
  1836. static bool intel_pt_at_psb(unsigned char *buf, size_t len)
  1837. {
  1838. if (len < INTEL_PT_PSB_LEN)
  1839. return false;
  1840. return memmem(buf, INTEL_PT_PSB_LEN, INTEL_PT_PSB_STR,
  1841. INTEL_PT_PSB_LEN);
  1842. }
  1843. /**
  1844. * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
  1845. * @buf: pointer to buffer pointer
  1846. * @len: size of buffer
  1847. *
  1848. * Updates the buffer pointer to point to the start of the next PSB packet if
  1849. * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
  1850. * @len is adjusted accordingly.
  1851. *
  1852. * Return: %true if a PSB packet is found, %false otherwise.
  1853. */
  1854. static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
  1855. {
  1856. unsigned char *next;
  1857. next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
  1858. if (next) {
  1859. *len -= next - *buf;
  1860. *buf = next;
  1861. return true;
  1862. }
  1863. return false;
  1864. }
  1865. /**
  1866. * intel_pt_step_psb - move buffer pointer to the start of the following PSB
  1867. * packet.
  1868. * @buf: pointer to buffer pointer
  1869. * @len: size of buffer
  1870. *
  1871. * Updates the buffer pointer to point to the start of the following PSB packet
  1872. * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
  1873. * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
  1874. *
  1875. * Return: %true if a PSB packet is found, %false otherwise.
  1876. */
  1877. static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
  1878. {
  1879. unsigned char *next;
  1880. if (!*len)
  1881. return false;
  1882. next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
  1883. if (next) {
  1884. *len -= next - *buf;
  1885. *buf = next;
  1886. return true;
  1887. }
  1888. return false;
  1889. }
  1890. /**
  1891. * intel_pt_last_psb - find the last PSB packet in a buffer.
  1892. * @buf: buffer
  1893. * @len: size of buffer
  1894. *
  1895. * This function finds the last PSB in a buffer.
  1896. *
  1897. * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
  1898. */
  1899. static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
  1900. {
  1901. const char *n = INTEL_PT_PSB_STR;
  1902. unsigned char *p;
  1903. size_t k;
  1904. if (len < INTEL_PT_PSB_LEN)
  1905. return NULL;
  1906. k = len - INTEL_PT_PSB_LEN + 1;
  1907. while (1) {
  1908. p = memrchr(buf, n[0], k);
  1909. if (!p)
  1910. return NULL;
  1911. if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
  1912. return p;
  1913. k = p - buf;
  1914. if (!k)
  1915. return NULL;
  1916. }
  1917. }
  1918. /**
  1919. * intel_pt_next_tsc - find and return next TSC.
  1920. * @buf: buffer
  1921. * @len: size of buffer
  1922. * @tsc: TSC value returned
  1923. *
  1924. * Find a TSC packet in @buf and return the TSC value. This function assumes
  1925. * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
  1926. * PSBEND packet is found.
  1927. *
  1928. * Return: %true if TSC is found, false otherwise.
  1929. */
  1930. static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc)
  1931. {
  1932. struct intel_pt_pkt packet;
  1933. int ret;
  1934. while (len) {
  1935. ret = intel_pt_get_packet(buf, len, &packet);
  1936. if (ret <= 0)
  1937. return false;
  1938. if (packet.type == INTEL_PT_TSC) {
  1939. *tsc = packet.payload;
  1940. return true;
  1941. }
  1942. if (packet.type == INTEL_PT_PSBEND)
  1943. return false;
  1944. buf += ret;
  1945. len -= ret;
  1946. }
  1947. return false;
  1948. }
  1949. /**
  1950. * intel_pt_tsc_cmp - compare 7-byte TSCs.
  1951. * @tsc1: first TSC to compare
  1952. * @tsc2: second TSC to compare
  1953. *
  1954. * This function compares 7-byte TSC values allowing for the possibility that
  1955. * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
  1956. * around so for that purpose this function assumes the absolute difference is
  1957. * less than half the maximum difference.
  1958. *
  1959. * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
  1960. * after @tsc2.
  1961. */
  1962. static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
  1963. {
  1964. const uint64_t halfway = (1ULL << 55);
  1965. if (tsc1 == tsc2)
  1966. return 0;
  1967. if (tsc1 < tsc2) {
  1968. if (tsc2 - tsc1 < halfway)
  1969. return -1;
  1970. else
  1971. return 1;
  1972. } else {
  1973. if (tsc1 - tsc2 < halfway)
  1974. return 1;
  1975. else
  1976. return -1;
  1977. }
  1978. }
  1979. /**
  1980. * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
  1981. * using TSC.
  1982. * @buf_a: first buffer
  1983. * @len_a: size of first buffer
  1984. * @buf_b: second buffer
  1985. * @len_b: size of second buffer
  1986. *
  1987. * If the trace contains TSC we can look at the last TSC of @buf_a and the
  1988. * first TSC of @buf_b in order to determine if the buffers overlap, and then
  1989. * walk forward in @buf_b until a later TSC is found. A precondition is that
  1990. * @buf_a and @buf_b are positioned at a PSB.
  1991. *
  1992. * Return: A pointer into @buf_b from where non-overlapped data starts, or
  1993. * @buf_b + @len_b if there is no non-overlapped data.
  1994. */
  1995. static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
  1996. size_t len_a,
  1997. unsigned char *buf_b,
  1998. size_t len_b)
  1999. {
  2000. uint64_t tsc_a, tsc_b;
  2001. unsigned char *p;
  2002. size_t len;
  2003. p = intel_pt_last_psb(buf_a, len_a);
  2004. if (!p)
  2005. return buf_b; /* No PSB in buf_a => no overlap */
  2006. len = len_a - (p - buf_a);
  2007. if (!intel_pt_next_tsc(p, len, &tsc_a)) {
  2008. /* The last PSB+ in buf_a is incomplete, so go back one more */
  2009. len_a -= len;
  2010. p = intel_pt_last_psb(buf_a, len_a);
  2011. if (!p)
  2012. return buf_b; /* No full PSB+ => assume no overlap */
  2013. len = len_a - (p - buf_a);
  2014. if (!intel_pt_next_tsc(p, len, &tsc_a))
  2015. return buf_b; /* No TSC in buf_a => assume no overlap */
  2016. }
  2017. while (1) {
  2018. /* Ignore PSB+ with no TSC */
  2019. if (intel_pt_next_tsc(buf_b, len_b, &tsc_b) &&
  2020. intel_pt_tsc_cmp(tsc_a, tsc_b) < 0)
  2021. return buf_b; /* tsc_a < tsc_b => no overlap */
  2022. if (!intel_pt_step_psb(&buf_b, &len_b))
  2023. return buf_b + len_b; /* No PSB in buf_b => no data */
  2024. }
  2025. }
  2026. /**
  2027. * intel_pt_find_overlap - determine start of non-overlapped trace data.
  2028. * @buf_a: first buffer
  2029. * @len_a: size of first buffer
  2030. * @buf_b: second buffer
  2031. * @len_b: size of second buffer
  2032. * @have_tsc: can use TSC packets to detect overlap
  2033. *
  2034. * When trace samples or snapshots are recorded there is the possibility that
  2035. * the data overlaps. Note that, for the purposes of decoding, data is only
  2036. * useful if it begins with a PSB packet.
  2037. *
  2038. * Return: A pointer into @buf_b from where non-overlapped data starts, or
  2039. * @buf_b + @len_b if there is no non-overlapped data.
  2040. */
  2041. unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
  2042. unsigned char *buf_b, size_t len_b,
  2043. bool have_tsc)
  2044. {
  2045. unsigned char *found;
  2046. /* Buffer 'b' must start at PSB so throw away everything before that */
  2047. if (!intel_pt_next_psb(&buf_b, &len_b))
  2048. return buf_b + len_b; /* No PSB */
  2049. if (!intel_pt_next_psb(&buf_a, &len_a))
  2050. return buf_b; /* No overlap */
  2051. if (have_tsc) {
  2052. found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b);
  2053. if (found)
  2054. return found;
  2055. }
  2056. /*
  2057. * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
  2058. * we can ignore the first part of buffer 'a'.
  2059. */
  2060. while (len_b < len_a) {
  2061. if (!intel_pt_step_psb(&buf_a, &len_a))
  2062. return buf_b; /* No overlap */
  2063. }
  2064. /* Now len_b >= len_a */
  2065. if (len_b > len_a) {
  2066. /* The leftover buffer 'b' must start at a PSB */
  2067. while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
  2068. if (!intel_pt_step_psb(&buf_a, &len_a))
  2069. return buf_b; /* No overlap */
  2070. }
  2071. }
  2072. while (1) {
  2073. /* Potential overlap so check the bytes */
  2074. found = memmem(buf_a, len_a, buf_b, len_a);
  2075. if (found)
  2076. return buf_b + len_a;
  2077. /* Try again at next PSB in buffer 'a' */
  2078. if (!intel_pt_step_psb(&buf_a, &len_a))
  2079. return buf_b; /* No overlap */
  2080. /* The leftover buffer 'b' must start at a PSB */
  2081. while (!intel_pt_at_psb(buf_b + len_a, len_b - len_a)) {
  2082. if (!intel_pt_step_psb(&buf_a, &len_a))
  2083. return buf_b; /* No overlap */
  2084. }
  2085. }
  2086. }