intel-pt-decoder.c 66 KB

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