intel-pt-decoder.c 65 KB

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