intel-pt-decoder.c 56 KB

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