intel-pt-decoder.c 56 KB

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