intel-pt-decoder.c 59 KB

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