intel-pt-decoder.c 66 KB

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