intel-pt-decoder.c 58 KB

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