intel-pt-decoder.c 65 KB

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