intel-pt-decoder.c 58 KB

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