intel-pt-decoder.c 60 KB

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