intel-pt-decoder.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * intel_pt_decoder.c: Intel Processor Trace support
  4. * Copyright (c) 2013-2014, Intel Corporation.
  5. */
  6. #ifndef _GNU_SOURCE
  7. #define _GNU_SOURCE
  8. #endif
  9. #include <stdlib.h>
  10. #include <stdbool.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <stdint.h>
  14. #include <inttypes.h>
  15. #include <linux/compiler.h>
  16. #include <linux/string.h>
  17. #include <linux/zalloc.h>
  18. #include "../auxtrace.h"
  19. #include "intel-pt-insn-decoder.h"
  20. #include "intel-pt-pkt-decoder.h"
  21. #include "intel-pt-decoder.h"
  22. #include "intel-pt-log.h"
  23. #define BITULL(x) (1ULL << (x))
  24. /* IA32_RTIT_CTL MSR bits */
  25. #define INTEL_PT_CYC_ENABLE BITULL(1)
  26. #define INTEL_PT_CYC_THRESHOLD (BITULL(22) | BITULL(21) | BITULL(20) | BITULL(19))
  27. #define INTEL_PT_CYC_THRESHOLD_SHIFT 19
  28. #define INTEL_PT_BLK_SIZE 1024
  29. #define BIT63 (((uint64_t)1 << 63))
  30. #define SEVEN_BYTES 0xffffffffffffffULL
  31. #define NO_VMCS 0xffffffffffULL
  32. #define INTEL_PT_RETURN 1
  33. /*
  34. * Default maximum number of loops with no packets consumed i.e. stuck in a
  35. * loop.
  36. */
  37. #define INTEL_PT_MAX_LOOPS 100000
  38. struct intel_pt_blk {
  39. struct intel_pt_blk *prev;
  40. uint64_t ip[INTEL_PT_BLK_SIZE];
  41. };
  42. struct intel_pt_stack {
  43. struct intel_pt_blk *blk;
  44. struct intel_pt_blk *spare;
  45. int pos;
  46. };
  47. enum intel_pt_p_once {
  48. INTEL_PT_PRT_ONCE_UNK_VMCS,
  49. INTEL_PT_PRT_ONCE_ERANGE,
  50. };
  51. enum intel_pt_pkt_state {
  52. INTEL_PT_STATE_NO_PSB,
  53. INTEL_PT_STATE_NO_IP,
  54. INTEL_PT_STATE_ERR_RESYNC,
  55. INTEL_PT_STATE_IN_SYNC,
  56. INTEL_PT_STATE_TNT_CONT,
  57. INTEL_PT_STATE_TNT,
  58. INTEL_PT_STATE_TIP,
  59. INTEL_PT_STATE_TIP_PGD,
  60. INTEL_PT_STATE_FUP,
  61. INTEL_PT_STATE_FUP_NO_TIP,
  62. INTEL_PT_STATE_FUP_IN_PSB,
  63. INTEL_PT_STATE_RESAMPLE,
  64. INTEL_PT_STATE_VM_TIME_CORRELATION,
  65. };
  66. static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
  67. {
  68. switch (pkt_state) {
  69. case INTEL_PT_STATE_NO_PSB:
  70. case INTEL_PT_STATE_NO_IP:
  71. case INTEL_PT_STATE_ERR_RESYNC:
  72. case INTEL_PT_STATE_IN_SYNC:
  73. case INTEL_PT_STATE_TNT_CONT:
  74. case INTEL_PT_STATE_RESAMPLE:
  75. case INTEL_PT_STATE_VM_TIME_CORRELATION:
  76. return true;
  77. case INTEL_PT_STATE_TNT:
  78. case INTEL_PT_STATE_TIP:
  79. case INTEL_PT_STATE_TIP_PGD:
  80. case INTEL_PT_STATE_FUP:
  81. case INTEL_PT_STATE_FUP_NO_TIP:
  82. case INTEL_PT_STATE_FUP_IN_PSB:
  83. return false;
  84. default:
  85. return true;
  86. };
  87. }
  88. #ifdef INTEL_PT_STRICT
  89. #define INTEL_PT_STATE_ERR1 INTEL_PT_STATE_NO_PSB
  90. #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_PSB
  91. #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_NO_PSB
  92. #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_NO_PSB
  93. #else
  94. #define INTEL_PT_STATE_ERR1 (decoder->pkt_state)
  95. #define INTEL_PT_STATE_ERR2 INTEL_PT_STATE_NO_IP
  96. #define INTEL_PT_STATE_ERR3 INTEL_PT_STATE_ERR_RESYNC
  97. #define INTEL_PT_STATE_ERR4 INTEL_PT_STATE_IN_SYNC
  98. #endif
  99. struct intel_pt_decoder {
  100. int (*get_trace)(struct intel_pt_buffer *buffer, void *data);
  101. int (*walk_insn)(struct intel_pt_insn *intel_pt_insn,
  102. uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
  103. uint64_t max_insn_cnt, void *data);
  104. bool (*pgd_ip)(uint64_t ip, void *data);
  105. int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
  106. struct intel_pt_vmcs_info *(*findnew_vmcs_info)(void *data, uint64_t vmcs);
  107. void *data;
  108. struct intel_pt_state state;
  109. const unsigned char *buf;
  110. size_t len;
  111. bool return_compression;
  112. bool branch_enable;
  113. bool mtc_insn;
  114. bool pge;
  115. bool have_tma;
  116. bool have_cyc;
  117. bool fixup_last_mtc;
  118. bool have_last_ip;
  119. bool in_psb;
  120. bool hop;
  121. bool leap;
  122. bool emulated_ptwrite;
  123. bool vm_time_correlation;
  124. bool vm_tm_corr_dry_run;
  125. bool vm_tm_corr_reliable;
  126. bool vm_tm_corr_same_buf;
  127. bool vm_tm_corr_continuous;
  128. bool nr;
  129. bool next_nr;
  130. bool iflag;
  131. bool next_iflag;
  132. enum intel_pt_param_flags flags;
  133. uint64_t pos;
  134. uint64_t last_ip;
  135. uint64_t ip;
  136. uint64_t pip_payload;
  137. uint64_t timestamp;
  138. uint64_t tsc_timestamp;
  139. uint64_t ref_timestamp;
  140. uint64_t buf_timestamp;
  141. uint64_t sample_timestamp;
  142. uint64_t ret_addr;
  143. uint64_t ctc_timestamp;
  144. uint64_t ctc_delta;
  145. uint64_t cycle_cnt;
  146. uint64_t cyc_ref_timestamp;
  147. uint64_t first_timestamp;
  148. uint64_t last_reliable_timestamp;
  149. uint64_t vmcs;
  150. uint64_t print_once;
  151. uint64_t last_ctc;
  152. uint32_t last_mtc;
  153. uint32_t tsc_ctc_ratio_n;
  154. uint32_t tsc_ctc_ratio_d;
  155. uint32_t tsc_ctc_mult;
  156. uint32_t tsc_slip;
  157. uint32_t ctc_rem_mask;
  158. int mtc_shift;
  159. struct intel_pt_stack stack;
  160. enum intel_pt_pkt_state pkt_state;
  161. enum intel_pt_pkt_ctx pkt_ctx;
  162. enum intel_pt_pkt_ctx prev_pkt_ctx;
  163. enum intel_pt_blk_type blk_type;
  164. int blk_type_pos;
  165. struct intel_pt_pkt packet;
  166. struct intel_pt_pkt tnt;
  167. int pkt_step;
  168. int pkt_len;
  169. int last_packet_type;
  170. unsigned int cbr;
  171. unsigned int cbr_seen;
  172. unsigned int max_non_turbo_ratio;
  173. double max_non_turbo_ratio_fp;
  174. double cbr_cyc_to_tsc;
  175. double calc_cyc_to_tsc;
  176. bool have_calc_cyc_to_tsc;
  177. int exec_mode;
  178. unsigned int insn_bytes;
  179. uint64_t period;
  180. enum intel_pt_period_type period_type;
  181. uint64_t tot_insn_cnt;
  182. uint64_t period_insn_cnt;
  183. uint64_t period_mask;
  184. uint64_t period_ticks;
  185. uint64_t last_masked_timestamp;
  186. uint64_t tot_cyc_cnt;
  187. uint64_t sample_tot_cyc_cnt;
  188. uint64_t base_cyc_cnt;
  189. uint64_t cyc_cnt_timestamp;
  190. uint64_t ctl;
  191. uint64_t cyc_threshold;
  192. double tsc_to_cyc;
  193. bool continuous_period;
  194. bool overflow;
  195. bool set_fup_tx_flags;
  196. bool set_fup_ptw;
  197. bool set_fup_mwait;
  198. bool set_fup_pwre;
  199. bool set_fup_exstop;
  200. bool set_fup_bep;
  201. bool set_fup_cfe_ip;
  202. bool set_fup_cfe;
  203. bool set_fup_mode_exec;
  204. bool sample_cyc;
  205. unsigned int fup_tx_flags;
  206. unsigned int tx_flags;
  207. uint64_t fup_ptw_payload;
  208. uint64_t fup_mwait_payload;
  209. uint64_t fup_pwre_payload;
  210. uint64_t cbr_payload;
  211. uint64_t timestamp_insn_cnt;
  212. uint64_t sample_insn_cnt;
  213. uint64_t stuck_ip;
  214. struct intel_pt_pkt fup_cfe_pkt;
  215. int max_loops;
  216. int no_progress;
  217. int stuck_ip_prd;
  218. int stuck_ip_cnt;
  219. uint64_t psb_ip;
  220. const unsigned char *next_buf;
  221. size_t next_len;
  222. unsigned char temp_buf[INTEL_PT_PKT_MAX_SZ];
  223. int evd_cnt;
  224. struct intel_pt_evd evd[INTEL_PT_MAX_EVDS];
  225. };
  226. static uint64_t intel_pt_lower_power_of_2(uint64_t x)
  227. {
  228. int i;
  229. for (i = 0; x != 1; i++)
  230. x >>= 1;
  231. return x << i;
  232. }
  233. __printf(1, 2)
  234. static void p_log(const char *fmt, ...)
  235. {
  236. char buf[512];
  237. va_list args;
  238. va_start(args, fmt);
  239. vsnprintf(buf, sizeof(buf), fmt, args);
  240. va_end(args);
  241. fprintf(stderr, "%s\n", buf);
  242. intel_pt_log("%s\n", buf);
  243. }
  244. static bool intel_pt_print_once(struct intel_pt_decoder *decoder,
  245. enum intel_pt_p_once id)
  246. {
  247. uint64_t bit = 1ULL << id;
  248. if (decoder->print_once & bit)
  249. return false;
  250. decoder->print_once |= bit;
  251. return true;
  252. }
  253. static uint64_t intel_pt_cyc_threshold(uint64_t ctl)
  254. {
  255. if (!(ctl & INTEL_PT_CYC_ENABLE))
  256. return 0;
  257. return (ctl & INTEL_PT_CYC_THRESHOLD) >> INTEL_PT_CYC_THRESHOLD_SHIFT;
  258. }
  259. static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
  260. {
  261. if (decoder->period_type == INTEL_PT_PERIOD_TICKS) {
  262. uint64_t period;
  263. period = intel_pt_lower_power_of_2(decoder->period);
  264. decoder->period_mask = ~(period - 1);
  265. decoder->period_ticks = period;
  266. }
  267. }
  268. static uint64_t multdiv(uint64_t t, uint32_t n, uint32_t d)
  269. {
  270. if (!d)
  271. return 0;
  272. return (t / d) * n + ((t % d) * n) / d;
  273. }
  274. struct intel_pt_decoder *intel_pt_decoder_new(struct intel_pt_params *params)
  275. {
  276. struct intel_pt_decoder *decoder;
  277. if (!params->get_trace || !params->walk_insn)
  278. return NULL;
  279. decoder = zalloc(sizeof(struct intel_pt_decoder));
  280. if (!decoder)
  281. return NULL;
  282. decoder->get_trace = params->get_trace;
  283. decoder->walk_insn = params->walk_insn;
  284. decoder->pgd_ip = params->pgd_ip;
  285. decoder->lookahead = params->lookahead;
  286. decoder->findnew_vmcs_info = params->findnew_vmcs_info;
  287. decoder->data = params->data;
  288. decoder->return_compression = params->return_compression;
  289. decoder->branch_enable = params->branch_enable;
  290. decoder->hop = params->quick >= 1;
  291. decoder->leap = params->quick >= 2;
  292. decoder->vm_time_correlation = params->vm_time_correlation;
  293. decoder->vm_tm_corr_dry_run = params->vm_tm_corr_dry_run;
  294. decoder->first_timestamp = params->first_timestamp;
  295. decoder->last_reliable_timestamp = params->first_timestamp;
  296. decoder->max_loops = params->max_loops ? params->max_loops : INTEL_PT_MAX_LOOPS;
  297. decoder->flags = params->flags;
  298. decoder->ctl = params->ctl;
  299. decoder->period = params->period;
  300. decoder->period_type = params->period_type;
  301. decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
  302. decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
  303. decoder->cyc_threshold = intel_pt_cyc_threshold(decoder->ctl);
  304. intel_pt_setup_period(decoder);
  305. decoder->mtc_shift = params->mtc_period;
  306. decoder->ctc_rem_mask = (1 << decoder->mtc_shift) - 1;
  307. decoder->tsc_ctc_ratio_n = params->tsc_ctc_ratio_n;
  308. decoder->tsc_ctc_ratio_d = params->tsc_ctc_ratio_d;
  309. if (!decoder->tsc_ctc_ratio_n)
  310. decoder->tsc_ctc_ratio_d = 0;
  311. if (decoder->tsc_ctc_ratio_d) {
  312. if (!(decoder->tsc_ctc_ratio_n % decoder->tsc_ctc_ratio_d))
  313. decoder->tsc_ctc_mult = decoder->tsc_ctc_ratio_n /
  314. decoder->tsc_ctc_ratio_d;
  315. }
  316. /*
  317. * A TSC packet can slip past MTC packets so that the timestamp appears
  318. * to go backwards. One estimate is that can be up to about 40 CPU
  319. * cycles, which is certainly less than 0x1000 TSC ticks, but accept
  320. * slippage an order of magnitude more to be on the safe side.
  321. */
  322. decoder->tsc_slip = 0x10000;
  323. intel_pt_log("timestamp: mtc_shift %u\n", decoder->mtc_shift);
  324. intel_pt_log("timestamp: tsc_ctc_ratio_n %u\n", decoder->tsc_ctc_ratio_n);
  325. intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
  326. intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
  327. intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
  328. if (decoder->hop)
  329. intel_pt_log("Hop mode: decoding FUP and TIPs, but not TNT\n");
  330. return decoder;
  331. }
  332. void intel_pt_set_first_timestamp(struct intel_pt_decoder *decoder,
  333. uint64_t first_timestamp)
  334. {
  335. decoder->first_timestamp = first_timestamp;
  336. }
  337. static void intel_pt_pop_blk(struct intel_pt_stack *stack)
  338. {
  339. struct intel_pt_blk *blk = stack->blk;
  340. stack->blk = blk->prev;
  341. if (!stack->spare)
  342. stack->spare = blk;
  343. else
  344. free(blk);
  345. }
  346. static uint64_t intel_pt_pop(struct intel_pt_stack *stack)
  347. {
  348. if (!stack->pos) {
  349. if (!stack->blk)
  350. return 0;
  351. intel_pt_pop_blk(stack);
  352. if (!stack->blk)
  353. return 0;
  354. stack->pos = INTEL_PT_BLK_SIZE;
  355. }
  356. return stack->blk->ip[--stack->pos];
  357. }
  358. static int intel_pt_alloc_blk(struct intel_pt_stack *stack)
  359. {
  360. struct intel_pt_blk *blk;
  361. if (stack->spare) {
  362. blk = stack->spare;
  363. stack->spare = NULL;
  364. } else {
  365. blk = malloc(sizeof(struct intel_pt_blk));
  366. if (!blk)
  367. return -ENOMEM;
  368. }
  369. blk->prev = stack->blk;
  370. stack->blk = blk;
  371. stack->pos = 0;
  372. return 0;
  373. }
  374. static int intel_pt_push(struct intel_pt_stack *stack, uint64_t ip)
  375. {
  376. int err;
  377. if (!stack->blk || stack->pos == INTEL_PT_BLK_SIZE) {
  378. err = intel_pt_alloc_blk(stack);
  379. if (err)
  380. return err;
  381. }
  382. stack->blk->ip[stack->pos++] = ip;
  383. return 0;
  384. }
  385. static void intel_pt_clear_stack(struct intel_pt_stack *stack)
  386. {
  387. while (stack->blk)
  388. intel_pt_pop_blk(stack);
  389. stack->pos = 0;
  390. }
  391. static void intel_pt_free_stack(struct intel_pt_stack *stack)
  392. {
  393. intel_pt_clear_stack(stack);
  394. zfree(&stack->blk);
  395. zfree(&stack->spare);
  396. }
  397. void intel_pt_decoder_free(struct intel_pt_decoder *decoder)
  398. {
  399. intel_pt_free_stack(&decoder->stack);
  400. free(decoder);
  401. }
  402. static int intel_pt_ext_err(int code)
  403. {
  404. switch (code) {
  405. case -ENOMEM:
  406. return INTEL_PT_ERR_NOMEM;
  407. case -ENOSYS:
  408. return INTEL_PT_ERR_INTERN;
  409. case -EBADMSG:
  410. return INTEL_PT_ERR_BADPKT;
  411. case -ENODATA:
  412. return INTEL_PT_ERR_NODATA;
  413. case -EILSEQ:
  414. return INTEL_PT_ERR_NOINSN;
  415. case -ENOENT:
  416. return INTEL_PT_ERR_MISMAT;
  417. case -EOVERFLOW:
  418. return INTEL_PT_ERR_OVR;
  419. case -ENOSPC:
  420. return INTEL_PT_ERR_LOST;
  421. case -ELOOP:
  422. return INTEL_PT_ERR_NELOOP;
  423. case -ECONNRESET:
  424. return INTEL_PT_ERR_EPTW;
  425. default:
  426. return INTEL_PT_ERR_UNK;
  427. }
  428. }
  429. static const char *intel_pt_err_msgs[] = {
  430. [INTEL_PT_ERR_NOMEM] = "Memory allocation failed",
  431. [INTEL_PT_ERR_INTERN] = "Internal error",
  432. [INTEL_PT_ERR_BADPKT] = "Bad packet",
  433. [INTEL_PT_ERR_NODATA] = "No more data",
  434. [INTEL_PT_ERR_NOINSN] = "Failed to get instruction",
  435. [INTEL_PT_ERR_MISMAT] = "Trace doesn't match instruction",
  436. [INTEL_PT_ERR_OVR] = "Overflow packet",
  437. [INTEL_PT_ERR_LOST] = "Lost trace data",
  438. [INTEL_PT_ERR_UNK] = "Unknown error!",
  439. [INTEL_PT_ERR_NELOOP] = "Never-ending loop (refer perf config intel-pt.max-loops)",
  440. [INTEL_PT_ERR_EPTW] = "Broken emulated ptwrite",
  441. };
  442. int intel_pt__strerror(int code, char *buf, size_t buflen)
  443. {
  444. if (code < 1 || code >= INTEL_PT_ERR_MAX)
  445. code = INTEL_PT_ERR_UNK;
  446. strlcpy(buf, intel_pt_err_msgs[code], buflen);
  447. return 0;
  448. }
  449. static uint64_t intel_pt_calc_ip(const struct intel_pt_pkt *packet,
  450. uint64_t last_ip)
  451. {
  452. uint64_t ip;
  453. switch (packet->count) {
  454. case 1:
  455. ip = (last_ip & (uint64_t)0xffffffffffff0000ULL) |
  456. packet->payload;
  457. break;
  458. case 2:
  459. ip = (last_ip & (uint64_t)0xffffffff00000000ULL) |
  460. packet->payload;
  461. break;
  462. case 3:
  463. ip = packet->payload;
  464. /* Sign-extend 6-byte ip */
  465. if (ip & (uint64_t)0x800000000000ULL)
  466. ip |= (uint64_t)0xffff000000000000ULL;
  467. break;
  468. case 4:
  469. ip = (last_ip & (uint64_t)0xffff000000000000ULL) |
  470. packet->payload;
  471. break;
  472. case 6:
  473. ip = packet->payload;
  474. break;
  475. default:
  476. return 0;
  477. }
  478. return ip;
  479. }
  480. static inline void intel_pt_set_last_ip(struct intel_pt_decoder *decoder)
  481. {
  482. decoder->last_ip = intel_pt_calc_ip(&decoder->packet, decoder->last_ip);
  483. decoder->have_last_ip = true;
  484. }
  485. static inline void intel_pt_set_ip(struct intel_pt_decoder *decoder)
  486. {
  487. intel_pt_set_last_ip(decoder);
  488. decoder->ip = decoder->last_ip;
  489. }
  490. static void intel_pt_decoder_log_packet(struct intel_pt_decoder *decoder)
  491. {
  492. intel_pt_log_packet(&decoder->packet, decoder->pkt_len, decoder->pos,
  493. decoder->buf);
  494. }
  495. static int intel_pt_bug(struct intel_pt_decoder *decoder)
  496. {
  497. intel_pt_log("ERROR: Internal error\n");
  498. decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
  499. return -ENOSYS;
  500. }
  501. static inline void intel_pt_clear_tx_flags(struct intel_pt_decoder *decoder)
  502. {
  503. decoder->tx_flags = 0;
  504. }
  505. static inline void intel_pt_update_in_tx(struct intel_pt_decoder *decoder)
  506. {
  507. decoder->tx_flags = decoder->packet.payload & INTEL_PT_IN_TX;
  508. }
  509. static inline void intel_pt_update_pip(struct intel_pt_decoder *decoder)
  510. {
  511. decoder->pip_payload = decoder->packet.payload;
  512. }
  513. static inline void intel_pt_update_nr(struct intel_pt_decoder *decoder)
  514. {
  515. decoder->next_nr = decoder->pip_payload & 1;
  516. }
  517. static inline void intel_pt_set_nr(struct intel_pt_decoder *decoder)
  518. {
  519. decoder->nr = decoder->pip_payload & 1;
  520. decoder->next_nr = decoder->nr;
  521. }
  522. static inline void intel_pt_set_pip(struct intel_pt_decoder *decoder)
  523. {
  524. intel_pt_update_pip(decoder);
  525. intel_pt_set_nr(decoder);
  526. }
  527. static int intel_pt_bad_packet(struct intel_pt_decoder *decoder)
  528. {
  529. intel_pt_clear_tx_flags(decoder);
  530. decoder->have_tma = false;
  531. decoder->pkt_len = 1;
  532. decoder->pkt_step = 1;
  533. intel_pt_decoder_log_packet(decoder);
  534. if (decoder->pkt_state != INTEL_PT_STATE_NO_PSB) {
  535. intel_pt_log("ERROR: Bad packet\n");
  536. decoder->pkt_state = INTEL_PT_STATE_ERR1;
  537. }
  538. return -EBADMSG;
  539. }
  540. static inline void intel_pt_update_sample_time(struct intel_pt_decoder *decoder)
  541. {
  542. decoder->sample_timestamp = decoder->timestamp;
  543. decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
  544. decoder->state.cycles = decoder->tot_cyc_cnt;
  545. }
  546. static void intel_pt_reposition(struct intel_pt_decoder *decoder)
  547. {
  548. decoder->ip = 0;
  549. decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
  550. decoder->timestamp = 0;
  551. decoder->have_tma = false;
  552. }
  553. static int intel_pt_get_data(struct intel_pt_decoder *decoder, bool reposition)
  554. {
  555. struct intel_pt_buffer buffer = { .buf = 0, };
  556. int ret;
  557. decoder->pkt_step = 0;
  558. intel_pt_log("Getting more data\n");
  559. ret = decoder->get_trace(&buffer, decoder->data);
  560. if (ret)
  561. return ret;
  562. decoder->buf = buffer.buf;
  563. decoder->len = buffer.len;
  564. if (!decoder->len) {
  565. intel_pt_log("No more data\n");
  566. return -ENODATA;
  567. }
  568. decoder->buf_timestamp = buffer.ref_timestamp;
  569. if (!buffer.consecutive || reposition) {
  570. intel_pt_reposition(decoder);
  571. decoder->ref_timestamp = buffer.ref_timestamp;
  572. decoder->state.trace_nr = buffer.trace_nr;
  573. decoder->vm_tm_corr_same_buf = false;
  574. intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
  575. decoder->ref_timestamp);
  576. return -ENOLINK;
  577. }
  578. return 0;
  579. }
  580. static int intel_pt_get_next_data(struct intel_pt_decoder *decoder,
  581. bool reposition)
  582. {
  583. if (!decoder->next_buf)
  584. return intel_pt_get_data(decoder, reposition);
  585. decoder->buf = decoder->next_buf;
  586. decoder->len = decoder->next_len;
  587. decoder->next_buf = 0;
  588. decoder->next_len = 0;
  589. return 0;
  590. }
  591. static int intel_pt_get_split_packet(struct intel_pt_decoder *decoder)
  592. {
  593. unsigned char *buf = decoder->temp_buf;
  594. size_t old_len, len, n;
  595. int ret;
  596. old_len = decoder->len;
  597. len = decoder->len;
  598. memcpy(buf, decoder->buf, len);
  599. ret = intel_pt_get_data(decoder, false);
  600. if (ret) {
  601. decoder->pos += old_len;
  602. return ret < 0 ? ret : -EINVAL;
  603. }
  604. n = INTEL_PT_PKT_MAX_SZ - len;
  605. if (n > decoder->len)
  606. n = decoder->len;
  607. memcpy(buf + len, decoder->buf, n);
  608. len += n;
  609. decoder->prev_pkt_ctx = decoder->pkt_ctx;
  610. ret = intel_pt_get_packet(buf, len, &decoder->packet, &decoder->pkt_ctx);
  611. if (ret < (int)old_len) {
  612. decoder->next_buf = decoder->buf;
  613. decoder->next_len = decoder->len;
  614. decoder->buf = buf;
  615. decoder->len = old_len;
  616. return intel_pt_bad_packet(decoder);
  617. }
  618. decoder->next_buf = decoder->buf + (ret - old_len);
  619. decoder->next_len = decoder->len - (ret - old_len);
  620. decoder->buf = buf;
  621. decoder->len = ret;
  622. return ret;
  623. }
  624. struct intel_pt_pkt_info {
  625. struct intel_pt_decoder *decoder;
  626. struct intel_pt_pkt packet;
  627. uint64_t pos;
  628. int pkt_len;
  629. int last_packet_type;
  630. void *data;
  631. };
  632. typedef int (*intel_pt_pkt_cb_t)(struct intel_pt_pkt_info *pkt_info);
  633. /* Lookahead packets in current buffer */
  634. static int intel_pt_pkt_lookahead(struct intel_pt_decoder *decoder,
  635. intel_pt_pkt_cb_t cb, void *data)
  636. {
  637. struct intel_pt_pkt_info pkt_info;
  638. const unsigned char *buf = decoder->buf;
  639. enum intel_pt_pkt_ctx pkt_ctx = decoder->pkt_ctx;
  640. size_t len = decoder->len;
  641. int ret;
  642. pkt_info.decoder = decoder;
  643. pkt_info.pos = decoder->pos;
  644. pkt_info.pkt_len = decoder->pkt_step;
  645. pkt_info.last_packet_type = decoder->last_packet_type;
  646. pkt_info.data = data;
  647. while (1) {
  648. do {
  649. pkt_info.pos += pkt_info.pkt_len;
  650. buf += pkt_info.pkt_len;
  651. len -= pkt_info.pkt_len;
  652. if (!len)
  653. return INTEL_PT_NEED_MORE_BYTES;
  654. ret = intel_pt_get_packet(buf, len, &pkt_info.packet,
  655. &pkt_ctx);
  656. if (!ret)
  657. return INTEL_PT_NEED_MORE_BYTES;
  658. if (ret < 0)
  659. return ret;
  660. pkt_info.pkt_len = ret;
  661. } while (pkt_info.packet.type == INTEL_PT_PAD);
  662. ret = cb(&pkt_info);
  663. if (ret)
  664. return 0;
  665. pkt_info.last_packet_type = pkt_info.packet.type;
  666. }
  667. }
  668. struct intel_pt_calc_cyc_to_tsc_info {
  669. uint64_t cycle_cnt;
  670. unsigned int cbr;
  671. uint32_t last_mtc;
  672. uint64_t ctc_timestamp;
  673. uint64_t ctc_delta;
  674. uint64_t tsc_timestamp;
  675. uint64_t timestamp;
  676. bool have_tma;
  677. bool fixup_last_mtc;
  678. bool from_mtc;
  679. double cbr_cyc_to_tsc;
  680. };
  681. /*
  682. * MTC provides a 8-bit slice of CTC but the TMA packet only provides the lower
  683. * 16 bits of CTC. If mtc_shift > 8 then some of the MTC bits are not in the CTC
  684. * provided by the TMA packet. Fix-up the last_mtc calculated from the TMA
  685. * packet by copying the missing bits from the current MTC assuming the least
  686. * difference between the two, and that the current MTC comes after last_mtc.
  687. */
  688. static void intel_pt_fixup_last_mtc(uint32_t mtc, int mtc_shift,
  689. uint32_t *last_mtc)
  690. {
  691. uint32_t first_missing_bit = 1U << (16 - mtc_shift);
  692. uint32_t mask = ~(first_missing_bit - 1);
  693. *last_mtc |= mtc & mask;
  694. if (*last_mtc >= mtc) {
  695. *last_mtc -= first_missing_bit;
  696. *last_mtc &= 0xff;
  697. }
  698. }
  699. static int intel_pt_calc_cyc_cb(struct intel_pt_pkt_info *pkt_info)
  700. {
  701. struct intel_pt_decoder *decoder = pkt_info->decoder;
  702. struct intel_pt_calc_cyc_to_tsc_info *data = pkt_info->data;
  703. uint64_t timestamp;
  704. double cyc_to_tsc;
  705. unsigned int cbr;
  706. uint32_t mtc, mtc_delta, ctc, fc, ctc_rem;
  707. switch (pkt_info->packet.type) {
  708. case INTEL_PT_TNT:
  709. case INTEL_PT_TIP_PGE:
  710. case INTEL_PT_TIP:
  711. case INTEL_PT_FUP:
  712. case INTEL_PT_PSB:
  713. case INTEL_PT_PIP:
  714. case INTEL_PT_MODE_EXEC:
  715. case INTEL_PT_MODE_TSX:
  716. case INTEL_PT_PSBEND:
  717. case INTEL_PT_PAD:
  718. case INTEL_PT_VMCS:
  719. case INTEL_PT_MNT:
  720. case INTEL_PT_PTWRITE:
  721. case INTEL_PT_PTWRITE_IP:
  722. case INTEL_PT_BBP:
  723. case INTEL_PT_BIP:
  724. case INTEL_PT_BEP:
  725. case INTEL_PT_BEP_IP:
  726. case INTEL_PT_CFE:
  727. case INTEL_PT_CFE_IP:
  728. case INTEL_PT_EVD:
  729. return 0;
  730. case INTEL_PT_MTC:
  731. if (!data->have_tma)
  732. return 0;
  733. mtc = pkt_info->packet.payload;
  734. if (decoder->mtc_shift > 8 && data->fixup_last_mtc) {
  735. data->fixup_last_mtc = false;
  736. intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
  737. &data->last_mtc);
  738. }
  739. if (mtc > data->last_mtc)
  740. mtc_delta = mtc - data->last_mtc;
  741. else
  742. mtc_delta = mtc + 256 - data->last_mtc;
  743. data->ctc_delta += mtc_delta << decoder->mtc_shift;
  744. data->last_mtc = mtc;
  745. if (decoder->tsc_ctc_mult) {
  746. timestamp = data->ctc_timestamp +
  747. data->ctc_delta * decoder->tsc_ctc_mult;
  748. } else {
  749. timestamp = data->ctc_timestamp +
  750. multdiv(data->ctc_delta,
  751. decoder->tsc_ctc_ratio_n,
  752. decoder->tsc_ctc_ratio_d);
  753. }
  754. if (timestamp < data->timestamp)
  755. return 1;
  756. if (pkt_info->last_packet_type != INTEL_PT_CYC) {
  757. data->timestamp = timestamp;
  758. return 0;
  759. }
  760. break;
  761. case INTEL_PT_TSC:
  762. /*
  763. * For now, do not support using TSC packets - refer
  764. * intel_pt_calc_cyc_to_tsc().
  765. */
  766. if (data->from_mtc)
  767. return 1;
  768. timestamp = pkt_info->packet.payload |
  769. (data->timestamp & (0xffULL << 56));
  770. if (data->from_mtc && timestamp < data->timestamp &&
  771. data->timestamp - timestamp < decoder->tsc_slip)
  772. return 1;
  773. if (timestamp < data->timestamp)
  774. timestamp += (1ULL << 56);
  775. if (pkt_info->last_packet_type != INTEL_PT_CYC) {
  776. if (data->from_mtc)
  777. return 1;
  778. data->tsc_timestamp = timestamp;
  779. data->timestamp = timestamp;
  780. return 0;
  781. }
  782. break;
  783. case INTEL_PT_TMA:
  784. if (data->from_mtc)
  785. return 1;
  786. if (!decoder->tsc_ctc_ratio_d)
  787. return 0;
  788. ctc = pkt_info->packet.payload;
  789. fc = pkt_info->packet.count;
  790. ctc_rem = ctc & decoder->ctc_rem_mask;
  791. data->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
  792. data->ctc_timestamp = data->tsc_timestamp - fc;
  793. if (decoder->tsc_ctc_mult) {
  794. data->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
  795. } else {
  796. data->ctc_timestamp -=
  797. multdiv(ctc_rem, decoder->tsc_ctc_ratio_n,
  798. decoder->tsc_ctc_ratio_d);
  799. }
  800. data->ctc_delta = 0;
  801. data->have_tma = true;
  802. data->fixup_last_mtc = true;
  803. return 0;
  804. case INTEL_PT_CYC:
  805. data->cycle_cnt += pkt_info->packet.payload;
  806. return 0;
  807. case INTEL_PT_CBR:
  808. cbr = pkt_info->packet.payload;
  809. if (data->cbr && data->cbr != cbr)
  810. return 1;
  811. data->cbr = cbr;
  812. data->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
  813. return 0;
  814. case INTEL_PT_TIP_PGD:
  815. case INTEL_PT_TRACESTOP:
  816. case INTEL_PT_EXSTOP:
  817. case INTEL_PT_EXSTOP_IP:
  818. case INTEL_PT_MWAIT:
  819. case INTEL_PT_PWRE:
  820. case INTEL_PT_PWRX:
  821. case INTEL_PT_OVF:
  822. case INTEL_PT_BAD: /* Does not happen */
  823. default:
  824. return 1;
  825. }
  826. if (!data->cbr && decoder->cbr) {
  827. data->cbr = decoder->cbr;
  828. data->cbr_cyc_to_tsc = decoder->cbr_cyc_to_tsc;
  829. }
  830. if (!data->cycle_cnt)
  831. return 1;
  832. cyc_to_tsc = (double)(timestamp - decoder->timestamp) / data->cycle_cnt;
  833. if (data->cbr && cyc_to_tsc > data->cbr_cyc_to_tsc &&
  834. cyc_to_tsc / data->cbr_cyc_to_tsc > 1.25) {
  835. intel_pt_log("Timestamp: calculated %g TSC ticks per cycle too big (c.f. CBR-based value %g), pos " x64_fmt "\n",
  836. cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
  837. return 1;
  838. }
  839. decoder->calc_cyc_to_tsc = cyc_to_tsc;
  840. decoder->have_calc_cyc_to_tsc = true;
  841. if (data->cbr) {
  842. intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. CBR-based value %g, pos " x64_fmt "\n",
  843. cyc_to_tsc, data->cbr_cyc_to_tsc, pkt_info->pos);
  844. } else {
  845. intel_pt_log("Timestamp: calculated %g TSC ticks per cycle c.f. unknown CBR-based value, pos " x64_fmt "\n",
  846. cyc_to_tsc, pkt_info->pos);
  847. }
  848. return 1;
  849. }
  850. static void intel_pt_calc_cyc_to_tsc(struct intel_pt_decoder *decoder,
  851. bool from_mtc)
  852. {
  853. struct intel_pt_calc_cyc_to_tsc_info data = {
  854. .cycle_cnt = 0,
  855. .cbr = 0,
  856. .last_mtc = decoder->last_mtc,
  857. .ctc_timestamp = decoder->ctc_timestamp,
  858. .ctc_delta = decoder->ctc_delta,
  859. .tsc_timestamp = decoder->tsc_timestamp,
  860. .timestamp = decoder->timestamp,
  861. .have_tma = decoder->have_tma,
  862. .fixup_last_mtc = decoder->fixup_last_mtc,
  863. .from_mtc = from_mtc,
  864. .cbr_cyc_to_tsc = 0,
  865. };
  866. /*
  867. * For now, do not support using TSC packets for at least the reasons:
  868. * 1) timing might have stopped
  869. * 2) TSC packets within PSB+ can slip against CYC packets
  870. */
  871. if (!from_mtc)
  872. return;
  873. intel_pt_pkt_lookahead(decoder, intel_pt_calc_cyc_cb, &data);
  874. }
  875. static int intel_pt_get_next_packet(struct intel_pt_decoder *decoder)
  876. {
  877. int ret;
  878. decoder->last_packet_type = decoder->packet.type;
  879. do {
  880. decoder->pos += decoder->pkt_step;
  881. decoder->buf += decoder->pkt_step;
  882. decoder->len -= decoder->pkt_step;
  883. if (!decoder->len) {
  884. ret = intel_pt_get_next_data(decoder, false);
  885. if (ret)
  886. return ret;
  887. }
  888. decoder->prev_pkt_ctx = decoder->pkt_ctx;
  889. ret = intel_pt_get_packet(decoder->buf, decoder->len,
  890. &decoder->packet, &decoder->pkt_ctx);
  891. if (ret == INTEL_PT_NEED_MORE_BYTES && BITS_PER_LONG == 32 &&
  892. decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
  893. ret = intel_pt_get_split_packet(decoder);
  894. if (ret < 0)
  895. return ret;
  896. }
  897. if (ret <= 0)
  898. return intel_pt_bad_packet(decoder);
  899. decoder->pkt_len = ret;
  900. decoder->pkt_step = ret;
  901. intel_pt_decoder_log_packet(decoder);
  902. } while (decoder->packet.type == INTEL_PT_PAD);
  903. return 0;
  904. }
  905. static uint64_t intel_pt_next_period(struct intel_pt_decoder *decoder)
  906. {
  907. uint64_t timestamp, masked_timestamp;
  908. timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
  909. masked_timestamp = timestamp & decoder->period_mask;
  910. if (decoder->continuous_period) {
  911. if (masked_timestamp > decoder->last_masked_timestamp)
  912. return 1;
  913. } else {
  914. timestamp += 1;
  915. masked_timestamp = timestamp & decoder->period_mask;
  916. if (masked_timestamp > decoder->last_masked_timestamp) {
  917. decoder->last_masked_timestamp = masked_timestamp;
  918. decoder->continuous_period = true;
  919. }
  920. }
  921. if (masked_timestamp < decoder->last_masked_timestamp)
  922. return decoder->period_ticks;
  923. return decoder->period_ticks - (timestamp - masked_timestamp);
  924. }
  925. static uint64_t intel_pt_next_sample(struct intel_pt_decoder *decoder)
  926. {
  927. switch (decoder->period_type) {
  928. case INTEL_PT_PERIOD_INSTRUCTIONS:
  929. return decoder->period - decoder->period_insn_cnt;
  930. case INTEL_PT_PERIOD_TICKS:
  931. return intel_pt_next_period(decoder);
  932. case INTEL_PT_PERIOD_NONE:
  933. case INTEL_PT_PERIOD_MTC:
  934. default:
  935. return 0;
  936. }
  937. }
  938. static void intel_pt_sample_insn(struct intel_pt_decoder *decoder)
  939. {
  940. uint64_t timestamp, masked_timestamp;
  941. switch (decoder->period_type) {
  942. case INTEL_PT_PERIOD_INSTRUCTIONS:
  943. decoder->period_insn_cnt = 0;
  944. break;
  945. case INTEL_PT_PERIOD_TICKS:
  946. timestamp = decoder->timestamp + decoder->timestamp_insn_cnt;
  947. masked_timestamp = timestamp & decoder->period_mask;
  948. if (masked_timestamp > decoder->last_masked_timestamp)
  949. decoder->last_masked_timestamp = masked_timestamp;
  950. else
  951. decoder->last_masked_timestamp += decoder->period_ticks;
  952. break;
  953. case INTEL_PT_PERIOD_NONE:
  954. case INTEL_PT_PERIOD_MTC:
  955. default:
  956. break;
  957. }
  958. decoder->state.type |= INTEL_PT_INSTRUCTION;
  959. }
  960. /*
  961. * Sample FUP instruction at the same time as reporting the FUP event, so the
  962. * instruction sample gets the same flags as the FUP event.
  963. */
  964. static void intel_pt_sample_fup_insn(struct intel_pt_decoder *decoder)
  965. {
  966. struct intel_pt_insn intel_pt_insn;
  967. uint64_t max_insn_cnt, insn_cnt = 0;
  968. int err;
  969. decoder->state.insn_op = INTEL_PT_OP_OTHER;
  970. decoder->state.insn_len = 0;
  971. if (!decoder->branch_enable || !decoder->pge || decoder->hop ||
  972. decoder->ip != decoder->last_ip)
  973. return;
  974. if (!decoder->mtc_insn)
  975. decoder->mtc_insn = true;
  976. max_insn_cnt = intel_pt_next_sample(decoder);
  977. if (max_insn_cnt != 1)
  978. return;
  979. err = decoder->walk_insn(&intel_pt_insn, &insn_cnt, &decoder->ip,
  980. 0, max_insn_cnt, decoder->data);
  981. /* Ignore error, it will be reported next walk anyway */
  982. if (err)
  983. return;
  984. if (intel_pt_insn.branch != INTEL_PT_BR_NO_BRANCH) {
  985. intel_pt_log_at("ERROR: Unexpected branch at FUP instruction", decoder->ip);
  986. return;
  987. }
  988. decoder->tot_insn_cnt += insn_cnt;
  989. decoder->timestamp_insn_cnt += insn_cnt;
  990. decoder->sample_insn_cnt += insn_cnt;
  991. decoder->period_insn_cnt += insn_cnt;
  992. intel_pt_sample_insn(decoder);
  993. decoder->state.type |= INTEL_PT_INSTRUCTION;
  994. decoder->ip += intel_pt_insn.length;
  995. }
  996. static int intel_pt_walk_insn(struct intel_pt_decoder *decoder,
  997. struct intel_pt_insn *intel_pt_insn, uint64_t ip)
  998. {
  999. uint64_t max_insn_cnt, insn_cnt = 0;
  1000. int err;
  1001. if (!decoder->mtc_insn)
  1002. decoder->mtc_insn = true;
  1003. max_insn_cnt = intel_pt_next_sample(decoder);
  1004. err = decoder->walk_insn(intel_pt_insn, &insn_cnt, &decoder->ip, ip,
  1005. max_insn_cnt, decoder->data);
  1006. decoder->tot_insn_cnt += insn_cnt;
  1007. decoder->timestamp_insn_cnt += insn_cnt;
  1008. decoder->sample_insn_cnt += insn_cnt;
  1009. decoder->period_insn_cnt += insn_cnt;
  1010. if (err) {
  1011. decoder->no_progress = 0;
  1012. decoder->pkt_state = INTEL_PT_STATE_ERR2;
  1013. intel_pt_log_at("ERROR: Failed to get instruction",
  1014. decoder->ip);
  1015. if (err == -ENOENT)
  1016. return -ENOLINK;
  1017. return -EILSEQ;
  1018. }
  1019. if (ip && decoder->ip == ip) {
  1020. err = -EAGAIN;
  1021. goto out;
  1022. }
  1023. if (max_insn_cnt && insn_cnt >= max_insn_cnt)
  1024. intel_pt_sample_insn(decoder);
  1025. if (intel_pt_insn->branch == INTEL_PT_BR_NO_BRANCH) {
  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. err = INTEL_PT_RETURN;
  1031. goto out;
  1032. }
  1033. if (intel_pt_insn->op == INTEL_PT_OP_CALL) {
  1034. /* Zero-length calls are excluded */
  1035. if (intel_pt_insn->branch != INTEL_PT_BR_UNCONDITIONAL ||
  1036. intel_pt_insn->rel) {
  1037. err = intel_pt_push(&decoder->stack, decoder->ip +
  1038. intel_pt_insn->length);
  1039. if (err)
  1040. goto out;
  1041. }
  1042. } else if (intel_pt_insn->op == INTEL_PT_OP_RET) {
  1043. decoder->ret_addr = intel_pt_pop(&decoder->stack);
  1044. }
  1045. if (intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL) {
  1046. int cnt = decoder->no_progress++;
  1047. decoder->state.from_ip = decoder->ip;
  1048. decoder->ip += intel_pt_insn->length +
  1049. intel_pt_insn->rel;
  1050. decoder->state.to_ip = decoder->ip;
  1051. err = INTEL_PT_RETURN;
  1052. /*
  1053. * Check for being stuck in a loop. This can happen if a
  1054. * decoder error results in the decoder erroneously setting the
  1055. * ip to an address that is itself in an infinite loop that
  1056. * consumes no packets. When that happens, there must be an
  1057. * unconditional branch.
  1058. */
  1059. if (cnt) {
  1060. if (cnt == 1) {
  1061. decoder->stuck_ip = decoder->state.to_ip;
  1062. decoder->stuck_ip_prd = 1;
  1063. decoder->stuck_ip_cnt = 1;
  1064. } else if (cnt > decoder->max_loops ||
  1065. decoder->state.to_ip == decoder->stuck_ip) {
  1066. intel_pt_log_at("ERROR: Never-ending loop",
  1067. decoder->state.to_ip);
  1068. decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
  1069. err = -ELOOP;
  1070. goto out;
  1071. } else if (!--decoder->stuck_ip_cnt) {
  1072. decoder->stuck_ip_prd += 1;
  1073. decoder->stuck_ip_cnt = decoder->stuck_ip_prd;
  1074. decoder->stuck_ip = decoder->state.to_ip;
  1075. }
  1076. }
  1077. goto out_no_progress;
  1078. }
  1079. out:
  1080. decoder->no_progress = 0;
  1081. out_no_progress:
  1082. decoder->state.insn_op = intel_pt_insn->op;
  1083. decoder->state.insn_len = intel_pt_insn->length;
  1084. memcpy(decoder->state.insn, intel_pt_insn->buf,
  1085. INTEL_PT_INSN_BUF_SZ);
  1086. if (decoder->tx_flags & INTEL_PT_IN_TX)
  1087. decoder->state.flags |= INTEL_PT_IN_TX;
  1088. return err;
  1089. }
  1090. static void intel_pt_mode_exec_status(struct intel_pt_decoder *decoder)
  1091. {
  1092. bool iflag = decoder->packet.count & INTEL_PT_IFLAG;
  1093. decoder->exec_mode = decoder->packet.payload;
  1094. decoder->iflag = iflag;
  1095. decoder->next_iflag = iflag;
  1096. decoder->state.from_iflag = iflag;
  1097. decoder->state.to_iflag = iflag;
  1098. }
  1099. static void intel_pt_mode_exec(struct intel_pt_decoder *decoder)
  1100. {
  1101. bool iflag = decoder->packet.count & INTEL_PT_IFLAG;
  1102. decoder->exec_mode = decoder->packet.payload;
  1103. decoder->next_iflag = iflag;
  1104. }
  1105. static void intel_pt_sample_iflag(struct intel_pt_decoder *decoder)
  1106. {
  1107. decoder->state.type |= INTEL_PT_IFLAG_CHG;
  1108. decoder->state.from_iflag = decoder->iflag;
  1109. decoder->state.to_iflag = decoder->next_iflag;
  1110. decoder->iflag = decoder->next_iflag;
  1111. }
  1112. static void intel_pt_sample_iflag_chg(struct intel_pt_decoder *decoder)
  1113. {
  1114. if (decoder->iflag != decoder->next_iflag)
  1115. intel_pt_sample_iflag(decoder);
  1116. }
  1117. static void intel_pt_clear_fup_event(struct intel_pt_decoder *decoder)
  1118. {
  1119. decoder->set_fup_tx_flags = false;
  1120. decoder->set_fup_ptw = false;
  1121. decoder->set_fup_mwait = false;
  1122. decoder->set_fup_pwre = false;
  1123. decoder->set_fup_exstop = false;
  1124. decoder->set_fup_bep = false;
  1125. decoder->set_fup_cfe_ip = false;
  1126. decoder->set_fup_cfe = false;
  1127. decoder->evd_cnt = 0;
  1128. decoder->set_fup_mode_exec = false;
  1129. decoder->iflag = decoder->next_iflag;
  1130. }
  1131. static bool intel_pt_fup_event(struct intel_pt_decoder *decoder, bool no_tip)
  1132. {
  1133. enum intel_pt_sample_type type = decoder->state.type;
  1134. bool sample_fup_insn = false;
  1135. bool ret = false;
  1136. decoder->state.type &= ~INTEL_PT_BRANCH;
  1137. decoder->state.insn_op = INTEL_PT_OP_OTHER;
  1138. decoder->state.insn_len = 0;
  1139. if (decoder->set_fup_cfe_ip || decoder->set_fup_cfe) {
  1140. bool ip = decoder->set_fup_cfe_ip;
  1141. decoder->set_fup_cfe_ip = false;
  1142. decoder->set_fup_cfe = false;
  1143. decoder->state.type |= INTEL_PT_EVT;
  1144. if (!ip && decoder->pge)
  1145. decoder->state.type |= INTEL_PT_BRANCH;
  1146. decoder->state.cfe_type = decoder->fup_cfe_pkt.count;
  1147. decoder->state.cfe_vector = decoder->fup_cfe_pkt.payload;
  1148. decoder->state.evd_cnt = decoder->evd_cnt;
  1149. decoder->state.evd = decoder->evd;
  1150. decoder->evd_cnt = 0;
  1151. if (ip || decoder->pge)
  1152. decoder->state.flags |= INTEL_PT_FUP_IP;
  1153. ret = true;
  1154. }
  1155. if (decoder->set_fup_mode_exec) {
  1156. decoder->set_fup_mode_exec = false;
  1157. intel_pt_sample_iflag(decoder);
  1158. sample_fup_insn = no_tip;
  1159. ret = true;
  1160. }
  1161. if (decoder->set_fup_tx_flags) {
  1162. decoder->set_fup_tx_flags = false;
  1163. decoder->tx_flags = decoder->fup_tx_flags;
  1164. decoder->state.type |= INTEL_PT_TRANSACTION;
  1165. if (decoder->fup_tx_flags & INTEL_PT_ABORT_TX)
  1166. decoder->state.type |= INTEL_PT_BRANCH;
  1167. decoder->state.flags = decoder->fup_tx_flags;
  1168. ret = true;
  1169. }
  1170. if (decoder->set_fup_ptw) {
  1171. decoder->set_fup_ptw = false;
  1172. decoder->state.type |= INTEL_PT_PTW;
  1173. decoder->state.flags |= INTEL_PT_FUP_IP;
  1174. decoder->state.ptw_payload = decoder->fup_ptw_payload;
  1175. ret = true;
  1176. }
  1177. if (decoder->set_fup_mwait) {
  1178. decoder->set_fup_mwait = false;
  1179. decoder->state.type |= INTEL_PT_MWAIT_OP;
  1180. decoder->state.mwait_payload = decoder->fup_mwait_payload;
  1181. ret = true;
  1182. }
  1183. if (decoder->set_fup_pwre) {
  1184. decoder->set_fup_pwre = false;
  1185. decoder->state.type |= INTEL_PT_PWR_ENTRY;
  1186. decoder->state.pwre_payload = decoder->fup_pwre_payload;
  1187. ret = true;
  1188. }
  1189. if (decoder->set_fup_exstop) {
  1190. decoder->set_fup_exstop = false;
  1191. decoder->state.type |= INTEL_PT_EX_STOP;
  1192. decoder->state.flags |= INTEL_PT_FUP_IP;
  1193. ret = true;
  1194. }
  1195. if (decoder->set_fup_bep) {
  1196. decoder->set_fup_bep = false;
  1197. decoder->state.type |= INTEL_PT_BLK_ITEMS;
  1198. ret = true;
  1199. }
  1200. if (decoder->overflow) {
  1201. decoder->overflow = false;
  1202. if (!ret && !decoder->pge) {
  1203. if (decoder->hop) {
  1204. decoder->state.type = 0;
  1205. decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
  1206. }
  1207. decoder->pge = true;
  1208. decoder->state.type |= INTEL_PT_BRANCH | INTEL_PT_TRACE_BEGIN;
  1209. decoder->state.from_ip = 0;
  1210. decoder->state.to_ip = decoder->ip;
  1211. return true;
  1212. }
  1213. }
  1214. if (ret) {
  1215. decoder->state.from_ip = decoder->ip;
  1216. decoder->state.to_ip = 0;
  1217. if (sample_fup_insn)
  1218. intel_pt_sample_fup_insn(decoder);
  1219. } else {
  1220. decoder->state.type = type;
  1221. }
  1222. return ret;
  1223. }
  1224. static inline bool intel_pt_fup_with_nlip(struct intel_pt_decoder *decoder,
  1225. struct intel_pt_insn *intel_pt_insn,
  1226. uint64_t ip, int err)
  1227. {
  1228. return decoder->flags & INTEL_PT_FUP_WITH_NLIP && !err &&
  1229. intel_pt_insn->branch == INTEL_PT_BR_INDIRECT &&
  1230. ip == decoder->ip + intel_pt_insn->length;
  1231. }
  1232. static int intel_pt_walk_fup(struct intel_pt_decoder *decoder)
  1233. {
  1234. struct intel_pt_insn intel_pt_insn;
  1235. uint64_t ip;
  1236. int err;
  1237. ip = decoder->last_ip;
  1238. while (1) {
  1239. err = intel_pt_walk_insn(decoder, &intel_pt_insn, ip);
  1240. if (err == INTEL_PT_RETURN)
  1241. return 0;
  1242. if (err == -EAGAIN ||
  1243. intel_pt_fup_with_nlip(decoder, &intel_pt_insn, ip, err)) {
  1244. bool no_tip = decoder->pkt_state != INTEL_PT_STATE_FUP;
  1245. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1246. if (intel_pt_fup_event(decoder, no_tip) && no_tip)
  1247. return 0;
  1248. return -EAGAIN;
  1249. }
  1250. decoder->set_fup_tx_flags = false;
  1251. if (err)
  1252. return err;
  1253. if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
  1254. intel_pt_log_at("ERROR: Unexpected indirect branch",
  1255. decoder->ip);
  1256. decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
  1257. return -ENOENT;
  1258. }
  1259. if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
  1260. intel_pt_log_at("ERROR: Unexpected conditional branch",
  1261. decoder->ip);
  1262. decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
  1263. return -ENOENT;
  1264. }
  1265. intel_pt_bug(decoder);
  1266. }
  1267. }
  1268. static int intel_pt_walk_tip(struct intel_pt_decoder *decoder)
  1269. {
  1270. struct intel_pt_insn intel_pt_insn;
  1271. int err;
  1272. err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
  1273. if (err == INTEL_PT_RETURN &&
  1274. decoder->pgd_ip &&
  1275. decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
  1276. (decoder->state.type & INTEL_PT_BRANCH) &&
  1277. decoder->pgd_ip(decoder->state.to_ip, decoder->data)) {
  1278. /* Unconditional branch leaving filter region */
  1279. decoder->no_progress = 0;
  1280. decoder->pge = false;
  1281. decoder->continuous_period = false;
  1282. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1283. decoder->state.type |= INTEL_PT_TRACE_END;
  1284. intel_pt_update_nr(decoder);
  1285. return 0;
  1286. }
  1287. if (err == INTEL_PT_RETURN)
  1288. return 0;
  1289. if (err)
  1290. return err;
  1291. intel_pt_update_nr(decoder);
  1292. intel_pt_sample_iflag_chg(decoder);
  1293. if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
  1294. if (decoder->pkt_state == INTEL_PT_STATE_TIP_PGD) {
  1295. decoder->pge = false;
  1296. decoder->continuous_period = false;
  1297. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1298. decoder->state.from_ip = decoder->ip;
  1299. if (decoder->packet.count == 0) {
  1300. decoder->state.to_ip = 0;
  1301. } else {
  1302. decoder->state.to_ip = decoder->last_ip;
  1303. decoder->ip = decoder->last_ip;
  1304. }
  1305. decoder->state.type |= INTEL_PT_TRACE_END;
  1306. } else {
  1307. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1308. decoder->state.from_ip = decoder->ip;
  1309. if (decoder->packet.count == 0) {
  1310. decoder->state.to_ip = 0;
  1311. } else {
  1312. decoder->state.to_ip = decoder->last_ip;
  1313. decoder->ip = decoder->last_ip;
  1314. }
  1315. }
  1316. return 0;
  1317. }
  1318. if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
  1319. uint64_t to_ip = decoder->ip + intel_pt_insn.length +
  1320. intel_pt_insn.rel;
  1321. if (decoder->pgd_ip &&
  1322. decoder->pkt_state == INTEL_PT_STATE_TIP_PGD &&
  1323. decoder->pgd_ip(to_ip, decoder->data)) {
  1324. /* Conditional branch leaving filter region */
  1325. decoder->pge = false;
  1326. decoder->continuous_period = false;
  1327. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1328. decoder->ip = to_ip;
  1329. decoder->state.from_ip = decoder->ip;
  1330. decoder->state.to_ip = to_ip;
  1331. decoder->state.type |= INTEL_PT_TRACE_END;
  1332. return 0;
  1333. }
  1334. intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
  1335. decoder->ip);
  1336. decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
  1337. return -ENOENT;
  1338. }
  1339. return intel_pt_bug(decoder);
  1340. }
  1341. struct eptw_data {
  1342. int bit_countdown;
  1343. uint64_t payload;
  1344. };
  1345. static int intel_pt_eptw_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
  1346. {
  1347. struct eptw_data *data = pkt_info->data;
  1348. int nr_bits;
  1349. switch (pkt_info->packet.type) {
  1350. case INTEL_PT_PAD:
  1351. case INTEL_PT_MNT:
  1352. case INTEL_PT_MODE_EXEC:
  1353. case INTEL_PT_MODE_TSX:
  1354. case INTEL_PT_MTC:
  1355. case INTEL_PT_FUP:
  1356. case INTEL_PT_CYC:
  1357. case INTEL_PT_CBR:
  1358. case INTEL_PT_TSC:
  1359. case INTEL_PT_TMA:
  1360. case INTEL_PT_PIP:
  1361. case INTEL_PT_VMCS:
  1362. case INTEL_PT_PSB:
  1363. case INTEL_PT_PSBEND:
  1364. case INTEL_PT_PTWRITE:
  1365. case INTEL_PT_PTWRITE_IP:
  1366. case INTEL_PT_EXSTOP:
  1367. case INTEL_PT_EXSTOP_IP:
  1368. case INTEL_PT_MWAIT:
  1369. case INTEL_PT_PWRE:
  1370. case INTEL_PT_PWRX:
  1371. case INTEL_PT_BBP:
  1372. case INTEL_PT_BIP:
  1373. case INTEL_PT_BEP:
  1374. case INTEL_PT_BEP_IP:
  1375. case INTEL_PT_CFE:
  1376. case INTEL_PT_CFE_IP:
  1377. case INTEL_PT_EVD:
  1378. break;
  1379. case INTEL_PT_TNT:
  1380. nr_bits = data->bit_countdown;
  1381. if (nr_bits > pkt_info->packet.count)
  1382. nr_bits = pkt_info->packet.count;
  1383. data->payload <<= nr_bits;
  1384. data->payload |= pkt_info->packet.payload >> (64 - nr_bits);
  1385. data->bit_countdown -= nr_bits;
  1386. return !data->bit_countdown;
  1387. case INTEL_PT_TIP_PGE:
  1388. case INTEL_PT_TIP_PGD:
  1389. case INTEL_PT_TIP:
  1390. case INTEL_PT_BAD:
  1391. case INTEL_PT_OVF:
  1392. case INTEL_PT_TRACESTOP:
  1393. default:
  1394. return 1;
  1395. }
  1396. return 0;
  1397. }
  1398. static int intel_pt_emulated_ptwrite(struct intel_pt_decoder *decoder)
  1399. {
  1400. int n = 64 - decoder->tnt.count;
  1401. struct eptw_data data = {
  1402. .bit_countdown = n,
  1403. .payload = decoder->tnt.payload >> n,
  1404. };
  1405. decoder->emulated_ptwrite = false;
  1406. intel_pt_log("Emulated ptwrite detected\n");
  1407. intel_pt_pkt_lookahead(decoder, intel_pt_eptw_lookahead_cb, &data);
  1408. if (data.bit_countdown)
  1409. return -ECONNRESET;
  1410. decoder->state.type = INTEL_PT_PTW;
  1411. decoder->state.from_ip = decoder->ip;
  1412. decoder->state.to_ip = 0;
  1413. decoder->state.ptw_payload = data.payload;
  1414. return 0;
  1415. }
  1416. static int intel_pt_walk_tnt(struct intel_pt_decoder *decoder)
  1417. {
  1418. struct intel_pt_insn intel_pt_insn;
  1419. int err;
  1420. while (1) {
  1421. if (decoder->emulated_ptwrite)
  1422. return intel_pt_emulated_ptwrite(decoder);
  1423. err = intel_pt_walk_insn(decoder, &intel_pt_insn, 0);
  1424. if (err == INTEL_PT_RETURN) {
  1425. decoder->emulated_ptwrite = intel_pt_insn.emulated_ptwrite;
  1426. return 0;
  1427. }
  1428. if (err) {
  1429. decoder->emulated_ptwrite = false;
  1430. return err;
  1431. }
  1432. if (intel_pt_insn.op == INTEL_PT_OP_RET) {
  1433. if (!decoder->return_compression) {
  1434. intel_pt_log_at("ERROR: RET when expecting conditional branch",
  1435. decoder->ip);
  1436. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  1437. return -ENOENT;
  1438. }
  1439. if (!decoder->ret_addr) {
  1440. intel_pt_log_at("ERROR: Bad RET compression (stack empty)",
  1441. decoder->ip);
  1442. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  1443. return -ENOENT;
  1444. }
  1445. if (!(decoder->tnt.payload & BIT63)) {
  1446. intel_pt_log_at("ERROR: Bad RET compression (TNT=N)",
  1447. decoder->ip);
  1448. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  1449. return -ENOENT;
  1450. }
  1451. decoder->tnt.count -= 1;
  1452. if (decoder->tnt.count)
  1453. decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
  1454. else
  1455. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1456. decoder->tnt.payload <<= 1;
  1457. decoder->state.from_ip = decoder->ip;
  1458. decoder->ip = decoder->ret_addr;
  1459. decoder->state.to_ip = decoder->ip;
  1460. return 0;
  1461. }
  1462. if (intel_pt_insn.branch == INTEL_PT_BR_INDIRECT) {
  1463. /* Handle deferred TIPs */
  1464. err = intel_pt_get_next_packet(decoder);
  1465. if (err)
  1466. return err;
  1467. if (decoder->packet.type != INTEL_PT_TIP ||
  1468. decoder->packet.count == 0) {
  1469. intel_pt_log_at("ERROR: Missing deferred TIP for indirect branch",
  1470. decoder->ip);
  1471. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  1472. decoder->pkt_step = 0;
  1473. return -ENOENT;
  1474. }
  1475. intel_pt_set_last_ip(decoder);
  1476. decoder->state.from_ip = decoder->ip;
  1477. decoder->state.to_ip = decoder->last_ip;
  1478. decoder->ip = decoder->last_ip;
  1479. intel_pt_update_nr(decoder);
  1480. intel_pt_sample_iflag_chg(decoder);
  1481. return 0;
  1482. }
  1483. if (intel_pt_insn.branch == INTEL_PT_BR_CONDITIONAL) {
  1484. decoder->tnt.count -= 1;
  1485. if (decoder->tnt.count)
  1486. decoder->pkt_state = INTEL_PT_STATE_TNT_CONT;
  1487. else
  1488. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1489. if (decoder->tnt.payload & BIT63) {
  1490. decoder->tnt.payload <<= 1;
  1491. decoder->state.from_ip = decoder->ip;
  1492. decoder->ip += intel_pt_insn.length +
  1493. intel_pt_insn.rel;
  1494. decoder->state.to_ip = decoder->ip;
  1495. return 0;
  1496. }
  1497. /* Instruction sample for a non-taken branch */
  1498. if (decoder->state.type & INTEL_PT_INSTRUCTION) {
  1499. decoder->tnt.payload <<= 1;
  1500. decoder->state.type = INTEL_PT_INSTRUCTION;
  1501. decoder->state.from_ip = decoder->ip;
  1502. decoder->state.to_ip = 0;
  1503. decoder->ip += intel_pt_insn.length;
  1504. return 0;
  1505. }
  1506. decoder->sample_cyc = false;
  1507. decoder->ip += intel_pt_insn.length;
  1508. if (!decoder->tnt.count) {
  1509. intel_pt_update_sample_time(decoder);
  1510. return -EAGAIN;
  1511. }
  1512. decoder->tnt.payload <<= 1;
  1513. continue;
  1514. }
  1515. return intel_pt_bug(decoder);
  1516. }
  1517. }
  1518. static int intel_pt_mode_tsx(struct intel_pt_decoder *decoder, bool *no_tip)
  1519. {
  1520. unsigned int fup_tx_flags;
  1521. int err;
  1522. fup_tx_flags = decoder->packet.payload &
  1523. (INTEL_PT_IN_TX | INTEL_PT_ABORT_TX);
  1524. err = intel_pt_get_next_packet(decoder);
  1525. if (err)
  1526. return err;
  1527. if (decoder->packet.type == INTEL_PT_FUP) {
  1528. decoder->fup_tx_flags = fup_tx_flags;
  1529. decoder->set_fup_tx_flags = true;
  1530. if (!(decoder->fup_tx_flags & INTEL_PT_ABORT_TX))
  1531. *no_tip = true;
  1532. } else {
  1533. intel_pt_log_at("ERROR: Missing FUP after MODE.TSX",
  1534. decoder->pos);
  1535. intel_pt_update_in_tx(decoder);
  1536. }
  1537. return 0;
  1538. }
  1539. static int intel_pt_evd(struct intel_pt_decoder *decoder)
  1540. {
  1541. if (decoder->evd_cnt >= INTEL_PT_MAX_EVDS) {
  1542. intel_pt_log_at("ERROR: Too many EVD packets", decoder->pos);
  1543. return -ENOSYS;
  1544. }
  1545. decoder->evd[decoder->evd_cnt++] = (struct intel_pt_evd){
  1546. .type = decoder->packet.count,
  1547. .payload = decoder->packet.payload,
  1548. };
  1549. return 0;
  1550. }
  1551. static uint64_t intel_pt_8b_tsc(uint64_t timestamp, uint64_t ref_timestamp)
  1552. {
  1553. timestamp |= (ref_timestamp & (0xffULL << 56));
  1554. if (timestamp < ref_timestamp) {
  1555. if (ref_timestamp - timestamp > (1ULL << 55))
  1556. timestamp += (1ULL << 56);
  1557. } else {
  1558. if (timestamp - ref_timestamp > (1ULL << 55))
  1559. timestamp -= (1ULL << 56);
  1560. }
  1561. return timestamp;
  1562. }
  1563. /* For use only when decoder->vm_time_correlation is true */
  1564. static bool intel_pt_time_in_range(struct intel_pt_decoder *decoder,
  1565. uint64_t timestamp)
  1566. {
  1567. uint64_t max_timestamp = decoder->buf_timestamp;
  1568. if (!max_timestamp) {
  1569. max_timestamp = decoder->last_reliable_timestamp +
  1570. 0x400000000ULL;
  1571. }
  1572. return timestamp >= decoder->last_reliable_timestamp &&
  1573. timestamp < decoder->buf_timestamp;
  1574. }
  1575. static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
  1576. {
  1577. uint64_t timestamp;
  1578. bool bad = false;
  1579. decoder->have_tma = false;
  1580. if (decoder->ref_timestamp) {
  1581. timestamp = intel_pt_8b_tsc(decoder->packet.payload,
  1582. decoder->ref_timestamp);
  1583. decoder->tsc_timestamp = timestamp;
  1584. decoder->timestamp = timestamp;
  1585. decoder->ref_timestamp = 0;
  1586. decoder->timestamp_insn_cnt = 0;
  1587. } else if (decoder->timestamp) {
  1588. timestamp = decoder->packet.payload |
  1589. (decoder->timestamp & (0xffULL << 56));
  1590. decoder->tsc_timestamp = timestamp;
  1591. if (timestamp < decoder->timestamp &&
  1592. decoder->timestamp - timestamp < decoder->tsc_slip) {
  1593. intel_pt_log_to("Suppressing backwards timestamp",
  1594. timestamp);
  1595. timestamp = decoder->timestamp;
  1596. }
  1597. if (timestamp < decoder->timestamp) {
  1598. if (!decoder->buf_timestamp ||
  1599. (timestamp + (1ULL << 56) < decoder->buf_timestamp)) {
  1600. intel_pt_log_to("Wraparound timestamp", timestamp);
  1601. timestamp += (1ULL << 56);
  1602. decoder->tsc_timestamp = timestamp;
  1603. } else {
  1604. intel_pt_log_to("Suppressing bad timestamp", timestamp);
  1605. timestamp = decoder->timestamp;
  1606. bad = true;
  1607. }
  1608. }
  1609. if (decoder->vm_time_correlation &&
  1610. (bad || !intel_pt_time_in_range(decoder, timestamp)) &&
  1611. intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_ERANGE))
  1612. p_log("Timestamp out of range");
  1613. decoder->timestamp = timestamp;
  1614. decoder->timestamp_insn_cnt = 0;
  1615. }
  1616. if (decoder->last_packet_type == INTEL_PT_CYC) {
  1617. decoder->cyc_ref_timestamp = decoder->timestamp;
  1618. decoder->cycle_cnt = 0;
  1619. decoder->have_calc_cyc_to_tsc = false;
  1620. intel_pt_calc_cyc_to_tsc(decoder, false);
  1621. }
  1622. intel_pt_log_to("Setting timestamp", decoder->timestamp);
  1623. }
  1624. static int intel_pt_overflow(struct intel_pt_decoder *decoder)
  1625. {
  1626. intel_pt_log("ERROR: Buffer overflow\n");
  1627. intel_pt_clear_tx_flags(decoder);
  1628. intel_pt_set_nr(decoder);
  1629. decoder->timestamp_insn_cnt = 0;
  1630. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  1631. decoder->state.from_ip = decoder->ip;
  1632. decoder->ip = 0;
  1633. decoder->pge = false;
  1634. intel_pt_clear_fup_event(decoder);
  1635. decoder->overflow = true;
  1636. return -EOVERFLOW;
  1637. }
  1638. static inline void intel_pt_mtc_cyc_cnt_pge(struct intel_pt_decoder *decoder)
  1639. {
  1640. if (decoder->have_cyc)
  1641. return;
  1642. decoder->cyc_cnt_timestamp = decoder->timestamp;
  1643. decoder->base_cyc_cnt = decoder->tot_cyc_cnt;
  1644. }
  1645. static inline void intel_pt_mtc_cyc_cnt_cbr(struct intel_pt_decoder *decoder)
  1646. {
  1647. decoder->tsc_to_cyc = decoder->cbr / decoder->max_non_turbo_ratio_fp;
  1648. if (decoder->pge)
  1649. intel_pt_mtc_cyc_cnt_pge(decoder);
  1650. }
  1651. static inline void intel_pt_mtc_cyc_cnt_upd(struct intel_pt_decoder *decoder)
  1652. {
  1653. uint64_t tot_cyc_cnt, tsc_delta;
  1654. if (decoder->have_cyc)
  1655. return;
  1656. decoder->sample_cyc = true;
  1657. if (!decoder->pge || decoder->timestamp <= decoder->cyc_cnt_timestamp)
  1658. return;
  1659. tsc_delta = decoder->timestamp - decoder->cyc_cnt_timestamp;
  1660. tot_cyc_cnt = tsc_delta * decoder->tsc_to_cyc + decoder->base_cyc_cnt;
  1661. if (tot_cyc_cnt > decoder->tot_cyc_cnt)
  1662. decoder->tot_cyc_cnt = tot_cyc_cnt;
  1663. }
  1664. static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
  1665. {
  1666. uint32_t ctc = decoder->packet.payload;
  1667. uint32_t fc = decoder->packet.count;
  1668. uint32_t ctc_rem = ctc & decoder->ctc_rem_mask;
  1669. if (!decoder->tsc_ctc_ratio_d)
  1670. return;
  1671. if (decoder->pge && !decoder->in_psb)
  1672. intel_pt_mtc_cyc_cnt_pge(decoder);
  1673. else
  1674. intel_pt_mtc_cyc_cnt_upd(decoder);
  1675. decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
  1676. decoder->last_ctc = ctc - ctc_rem;
  1677. decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
  1678. if (decoder->tsc_ctc_mult) {
  1679. decoder->ctc_timestamp -= ctc_rem * decoder->tsc_ctc_mult;
  1680. } else {
  1681. decoder->ctc_timestamp -= multdiv(ctc_rem,
  1682. decoder->tsc_ctc_ratio_n,
  1683. decoder->tsc_ctc_ratio_d);
  1684. }
  1685. decoder->ctc_delta = 0;
  1686. decoder->have_tma = true;
  1687. decoder->fixup_last_mtc = true;
  1688. intel_pt_log("CTC timestamp " x64_fmt " last MTC %#x CTC rem %#x\n",
  1689. decoder->ctc_timestamp, decoder->last_mtc, ctc_rem);
  1690. }
  1691. static void intel_pt_calc_mtc_timestamp(struct intel_pt_decoder *decoder)
  1692. {
  1693. uint64_t timestamp;
  1694. uint32_t mtc, mtc_delta;
  1695. if (!decoder->have_tma)
  1696. return;
  1697. mtc = decoder->packet.payload;
  1698. if (decoder->mtc_shift > 8 && decoder->fixup_last_mtc) {
  1699. decoder->fixup_last_mtc = false;
  1700. intel_pt_fixup_last_mtc(mtc, decoder->mtc_shift,
  1701. &decoder->last_mtc);
  1702. }
  1703. if (mtc > decoder->last_mtc)
  1704. mtc_delta = mtc - decoder->last_mtc;
  1705. else
  1706. mtc_delta = mtc + 256 - decoder->last_mtc;
  1707. decoder->ctc_delta += mtc_delta << decoder->mtc_shift;
  1708. if (decoder->tsc_ctc_mult) {
  1709. timestamp = decoder->ctc_timestamp +
  1710. decoder->ctc_delta * decoder->tsc_ctc_mult;
  1711. } else {
  1712. timestamp = decoder->ctc_timestamp +
  1713. multdiv(decoder->ctc_delta,
  1714. decoder->tsc_ctc_ratio_n,
  1715. decoder->tsc_ctc_ratio_d);
  1716. }
  1717. if (timestamp < decoder->timestamp)
  1718. intel_pt_log("Suppressing MTC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
  1719. timestamp, decoder->timestamp);
  1720. else
  1721. decoder->timestamp = timestamp;
  1722. intel_pt_mtc_cyc_cnt_upd(decoder);
  1723. decoder->timestamp_insn_cnt = 0;
  1724. decoder->last_mtc = mtc;
  1725. if (decoder->last_packet_type == INTEL_PT_CYC) {
  1726. decoder->cyc_ref_timestamp = decoder->timestamp;
  1727. decoder->cycle_cnt = 0;
  1728. decoder->have_calc_cyc_to_tsc = false;
  1729. intel_pt_calc_cyc_to_tsc(decoder, true);
  1730. }
  1731. intel_pt_log_to("Setting timestamp", decoder->timestamp);
  1732. }
  1733. static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
  1734. {
  1735. unsigned int cbr = decoder->packet.payload & 0xff;
  1736. decoder->cbr_payload = decoder->packet.payload;
  1737. if (decoder->cbr == cbr)
  1738. return;
  1739. decoder->cbr = cbr;
  1740. decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
  1741. decoder->cyc_ref_timestamp = decoder->timestamp;
  1742. decoder->cycle_cnt = 0;
  1743. intel_pt_mtc_cyc_cnt_cbr(decoder);
  1744. }
  1745. static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
  1746. {
  1747. uint64_t timestamp = decoder->cyc_ref_timestamp;
  1748. decoder->have_cyc = true;
  1749. decoder->cycle_cnt += decoder->packet.payload;
  1750. if (decoder->pge)
  1751. decoder->tot_cyc_cnt += decoder->packet.payload;
  1752. decoder->sample_cyc = true;
  1753. if (!decoder->cyc_ref_timestamp)
  1754. return;
  1755. if (decoder->have_calc_cyc_to_tsc)
  1756. timestamp += decoder->cycle_cnt * decoder->calc_cyc_to_tsc;
  1757. else if (decoder->cbr)
  1758. timestamp += decoder->cycle_cnt * decoder->cbr_cyc_to_tsc;
  1759. else
  1760. return;
  1761. if (timestamp < decoder->timestamp)
  1762. intel_pt_log("Suppressing CYC timestamp " x64_fmt " less than current timestamp " x64_fmt "\n",
  1763. timestamp, decoder->timestamp);
  1764. else
  1765. decoder->timestamp = timestamp;
  1766. decoder->timestamp_insn_cnt = 0;
  1767. intel_pt_log_to("Setting timestamp", decoder->timestamp);
  1768. }
  1769. static void intel_pt_bbp(struct intel_pt_decoder *decoder)
  1770. {
  1771. if (decoder->prev_pkt_ctx == INTEL_PT_NO_CTX) {
  1772. memset(decoder->state.items.mask, 0, sizeof(decoder->state.items.mask));
  1773. decoder->state.items.is_32_bit = false;
  1774. }
  1775. decoder->blk_type = decoder->packet.payload;
  1776. decoder->blk_type_pos = intel_pt_blk_type_pos(decoder->blk_type);
  1777. if (decoder->blk_type == INTEL_PT_GP_REGS)
  1778. decoder->state.items.is_32_bit = decoder->packet.count;
  1779. if (decoder->blk_type_pos < 0) {
  1780. intel_pt_log("WARNING: Unknown block type %u\n",
  1781. decoder->blk_type);
  1782. } else if (decoder->state.items.mask[decoder->blk_type_pos]) {
  1783. intel_pt_log("WARNING: Duplicate block type %u\n",
  1784. decoder->blk_type);
  1785. }
  1786. }
  1787. static void intel_pt_bip(struct intel_pt_decoder *decoder)
  1788. {
  1789. uint32_t id = decoder->packet.count;
  1790. uint32_t bit = 1 << id;
  1791. int pos = decoder->blk_type_pos;
  1792. if (pos < 0 || id >= INTEL_PT_BLK_ITEM_ID_CNT) {
  1793. intel_pt_log("WARNING: Unknown block item %u type %d\n",
  1794. id, decoder->blk_type);
  1795. return;
  1796. }
  1797. if (decoder->state.items.mask[pos] & bit) {
  1798. intel_pt_log("WARNING: Duplicate block item %u type %d\n",
  1799. id, decoder->blk_type);
  1800. }
  1801. decoder->state.items.mask[pos] |= bit;
  1802. decoder->state.items.val[pos][id] = decoder->packet.payload;
  1803. }
  1804. /* Walk PSB+ packets when already in sync. */
  1805. static int intel_pt_walk_psbend(struct intel_pt_decoder *decoder)
  1806. {
  1807. int err;
  1808. decoder->in_psb = true;
  1809. while (1) {
  1810. err = intel_pt_get_next_packet(decoder);
  1811. if (err)
  1812. goto out;
  1813. switch (decoder->packet.type) {
  1814. case INTEL_PT_PSBEND:
  1815. err = 0;
  1816. goto out;
  1817. case INTEL_PT_TIP_PGD:
  1818. case INTEL_PT_TIP_PGE:
  1819. case INTEL_PT_TIP:
  1820. case INTEL_PT_TNT:
  1821. case INTEL_PT_TRACESTOP:
  1822. case INTEL_PT_BAD:
  1823. case INTEL_PT_PSB:
  1824. case INTEL_PT_PTWRITE:
  1825. case INTEL_PT_PTWRITE_IP:
  1826. case INTEL_PT_EXSTOP:
  1827. case INTEL_PT_EXSTOP_IP:
  1828. case INTEL_PT_MWAIT:
  1829. case INTEL_PT_PWRE:
  1830. case INTEL_PT_PWRX:
  1831. case INTEL_PT_BBP:
  1832. case INTEL_PT_BIP:
  1833. case INTEL_PT_BEP:
  1834. case INTEL_PT_BEP_IP:
  1835. case INTEL_PT_CFE:
  1836. case INTEL_PT_CFE_IP:
  1837. case INTEL_PT_EVD:
  1838. decoder->have_tma = false;
  1839. intel_pt_log("ERROR: Unexpected packet\n");
  1840. err = -EAGAIN;
  1841. goto out;
  1842. case INTEL_PT_OVF:
  1843. err = intel_pt_overflow(decoder);
  1844. goto out;
  1845. case INTEL_PT_TSC:
  1846. intel_pt_calc_tsc_timestamp(decoder);
  1847. break;
  1848. case INTEL_PT_TMA:
  1849. intel_pt_calc_tma(decoder);
  1850. break;
  1851. case INTEL_PT_CBR:
  1852. intel_pt_calc_cbr(decoder);
  1853. break;
  1854. case INTEL_PT_MODE_EXEC:
  1855. intel_pt_mode_exec_status(decoder);
  1856. break;
  1857. case INTEL_PT_PIP:
  1858. intel_pt_set_pip(decoder);
  1859. break;
  1860. case INTEL_PT_FUP:
  1861. decoder->pge = true;
  1862. if (decoder->packet.count) {
  1863. intel_pt_set_last_ip(decoder);
  1864. decoder->psb_ip = decoder->last_ip;
  1865. }
  1866. break;
  1867. case INTEL_PT_MODE_TSX:
  1868. intel_pt_update_in_tx(decoder);
  1869. break;
  1870. case INTEL_PT_MTC:
  1871. intel_pt_calc_mtc_timestamp(decoder);
  1872. if (decoder->period_type == INTEL_PT_PERIOD_MTC)
  1873. decoder->state.type |= INTEL_PT_INSTRUCTION;
  1874. break;
  1875. case INTEL_PT_CYC:
  1876. intel_pt_calc_cyc_timestamp(decoder);
  1877. break;
  1878. case INTEL_PT_VMCS:
  1879. case INTEL_PT_MNT:
  1880. case INTEL_PT_PAD:
  1881. default:
  1882. break;
  1883. }
  1884. }
  1885. out:
  1886. decoder->in_psb = false;
  1887. return err;
  1888. }
  1889. static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
  1890. {
  1891. int err;
  1892. if (decoder->tx_flags & INTEL_PT_ABORT_TX) {
  1893. decoder->tx_flags = 0;
  1894. decoder->state.flags &= ~INTEL_PT_IN_TX;
  1895. decoder->state.flags |= INTEL_PT_ABORT_TX;
  1896. } else {
  1897. decoder->state.flags |= INTEL_PT_ASYNC;
  1898. }
  1899. while (1) {
  1900. err = intel_pt_get_next_packet(decoder);
  1901. if (err)
  1902. return err;
  1903. switch (decoder->packet.type) {
  1904. case INTEL_PT_TNT:
  1905. case INTEL_PT_FUP:
  1906. case INTEL_PT_TRACESTOP:
  1907. case INTEL_PT_PSB:
  1908. case INTEL_PT_TSC:
  1909. case INTEL_PT_TMA:
  1910. case INTEL_PT_MODE_TSX:
  1911. case INTEL_PT_BAD:
  1912. case INTEL_PT_PSBEND:
  1913. case INTEL_PT_PTWRITE:
  1914. case INTEL_PT_PTWRITE_IP:
  1915. case INTEL_PT_EXSTOP:
  1916. case INTEL_PT_EXSTOP_IP:
  1917. case INTEL_PT_MWAIT:
  1918. case INTEL_PT_PWRE:
  1919. case INTEL_PT_PWRX:
  1920. case INTEL_PT_BBP:
  1921. case INTEL_PT_BIP:
  1922. case INTEL_PT_BEP:
  1923. case INTEL_PT_BEP_IP:
  1924. case INTEL_PT_CFE:
  1925. case INTEL_PT_CFE_IP:
  1926. case INTEL_PT_EVD:
  1927. intel_pt_log("ERROR: Missing TIP after FUP\n");
  1928. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  1929. decoder->pkt_step = 0;
  1930. return -ENOENT;
  1931. case INTEL_PT_CBR:
  1932. intel_pt_calc_cbr(decoder);
  1933. break;
  1934. case INTEL_PT_OVF:
  1935. return intel_pt_overflow(decoder);
  1936. case INTEL_PT_TIP_PGD:
  1937. decoder->state.from_ip = decoder->ip;
  1938. if (decoder->packet.count == 0) {
  1939. decoder->state.to_ip = 0;
  1940. } else {
  1941. intel_pt_set_ip(decoder);
  1942. decoder->state.to_ip = decoder->ip;
  1943. }
  1944. decoder->pge = false;
  1945. decoder->continuous_period = false;
  1946. decoder->state.type |= INTEL_PT_TRACE_END;
  1947. intel_pt_update_nr(decoder);
  1948. return 0;
  1949. case INTEL_PT_TIP_PGE:
  1950. decoder->pge = true;
  1951. intel_pt_log("Omitting PGE ip " x64_fmt "\n",
  1952. decoder->ip);
  1953. decoder->state.from_ip = 0;
  1954. if (decoder->packet.count == 0) {
  1955. decoder->state.to_ip = 0;
  1956. } else {
  1957. intel_pt_set_ip(decoder);
  1958. decoder->state.to_ip = decoder->ip;
  1959. }
  1960. decoder->state.type |= INTEL_PT_TRACE_BEGIN;
  1961. intel_pt_mtc_cyc_cnt_pge(decoder);
  1962. intel_pt_set_nr(decoder);
  1963. return 0;
  1964. case INTEL_PT_TIP:
  1965. decoder->state.from_ip = decoder->ip;
  1966. if (decoder->packet.count == 0) {
  1967. decoder->state.to_ip = 0;
  1968. } else {
  1969. intel_pt_set_ip(decoder);
  1970. decoder->state.to_ip = decoder->ip;
  1971. }
  1972. intel_pt_update_nr(decoder);
  1973. intel_pt_sample_iflag_chg(decoder);
  1974. return 0;
  1975. case INTEL_PT_PIP:
  1976. intel_pt_update_pip(decoder);
  1977. break;
  1978. case INTEL_PT_MTC:
  1979. intel_pt_calc_mtc_timestamp(decoder);
  1980. if (decoder->period_type == INTEL_PT_PERIOD_MTC)
  1981. decoder->state.type |= INTEL_PT_INSTRUCTION;
  1982. break;
  1983. case INTEL_PT_CYC:
  1984. intel_pt_calc_cyc_timestamp(decoder);
  1985. break;
  1986. case INTEL_PT_MODE_EXEC:
  1987. intel_pt_mode_exec(decoder);
  1988. break;
  1989. case INTEL_PT_VMCS:
  1990. case INTEL_PT_MNT:
  1991. case INTEL_PT_PAD:
  1992. break;
  1993. default:
  1994. return intel_pt_bug(decoder);
  1995. }
  1996. }
  1997. }
  1998. static int intel_pt_resample(struct intel_pt_decoder *decoder)
  1999. {
  2000. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  2001. decoder->state.type = INTEL_PT_INSTRUCTION;
  2002. decoder->state.from_ip = decoder->ip;
  2003. decoder->state.to_ip = 0;
  2004. return 0;
  2005. }
  2006. struct intel_pt_vm_tsc_info {
  2007. struct intel_pt_pkt pip_packet;
  2008. struct intel_pt_pkt vmcs_packet;
  2009. struct intel_pt_pkt tma_packet;
  2010. bool tsc, pip, vmcs, tma, psbend;
  2011. uint64_t ctc_delta;
  2012. uint64_t last_ctc;
  2013. int max_lookahead;
  2014. };
  2015. /* Lookahead and get the PIP, VMCS and TMA packets from PSB+ */
  2016. static int intel_pt_vm_psb_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
  2017. {
  2018. struct intel_pt_vm_tsc_info *data = pkt_info->data;
  2019. switch (pkt_info->packet.type) {
  2020. case INTEL_PT_PAD:
  2021. case INTEL_PT_MNT:
  2022. case INTEL_PT_MODE_EXEC:
  2023. case INTEL_PT_MODE_TSX:
  2024. case INTEL_PT_MTC:
  2025. case INTEL_PT_FUP:
  2026. case INTEL_PT_CYC:
  2027. case INTEL_PT_CBR:
  2028. break;
  2029. case INTEL_PT_TSC:
  2030. data->tsc = true;
  2031. break;
  2032. case INTEL_PT_TMA:
  2033. data->tma_packet = pkt_info->packet;
  2034. data->tma = true;
  2035. break;
  2036. case INTEL_PT_PIP:
  2037. data->pip_packet = pkt_info->packet;
  2038. data->pip = true;
  2039. break;
  2040. case INTEL_PT_VMCS:
  2041. data->vmcs_packet = pkt_info->packet;
  2042. data->vmcs = true;
  2043. break;
  2044. case INTEL_PT_PSBEND:
  2045. data->psbend = true;
  2046. return 1;
  2047. case INTEL_PT_TIP_PGE:
  2048. case INTEL_PT_PTWRITE:
  2049. case INTEL_PT_PTWRITE_IP:
  2050. case INTEL_PT_EXSTOP:
  2051. case INTEL_PT_EXSTOP_IP:
  2052. case INTEL_PT_MWAIT:
  2053. case INTEL_PT_PWRE:
  2054. case INTEL_PT_PWRX:
  2055. case INTEL_PT_BBP:
  2056. case INTEL_PT_BIP:
  2057. case INTEL_PT_BEP:
  2058. case INTEL_PT_BEP_IP:
  2059. case INTEL_PT_OVF:
  2060. case INTEL_PT_BAD:
  2061. case INTEL_PT_TNT:
  2062. case INTEL_PT_TIP_PGD:
  2063. case INTEL_PT_TIP:
  2064. case INTEL_PT_PSB:
  2065. case INTEL_PT_TRACESTOP:
  2066. case INTEL_PT_CFE:
  2067. case INTEL_PT_CFE_IP:
  2068. case INTEL_PT_EVD:
  2069. default:
  2070. return 1;
  2071. }
  2072. return 0;
  2073. }
  2074. struct intel_pt_ovf_fup_info {
  2075. int max_lookahead;
  2076. bool found;
  2077. };
  2078. /* Lookahead to detect a FUP packet after OVF */
  2079. static int intel_pt_ovf_fup_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
  2080. {
  2081. struct intel_pt_ovf_fup_info *data = pkt_info->data;
  2082. if (pkt_info->packet.type == INTEL_PT_CYC ||
  2083. pkt_info->packet.type == INTEL_PT_MTC ||
  2084. pkt_info->packet.type == INTEL_PT_TSC)
  2085. return !--(data->max_lookahead);
  2086. data->found = pkt_info->packet.type == INTEL_PT_FUP;
  2087. return 1;
  2088. }
  2089. static bool intel_pt_ovf_fup_lookahead(struct intel_pt_decoder *decoder)
  2090. {
  2091. struct intel_pt_ovf_fup_info data = {
  2092. .max_lookahead = 16,
  2093. .found = false,
  2094. };
  2095. intel_pt_pkt_lookahead(decoder, intel_pt_ovf_fup_lookahead_cb, &data);
  2096. return data.found;
  2097. }
  2098. /* Lookahead and get the TMA packet after TSC */
  2099. static int intel_pt_tma_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
  2100. {
  2101. struct intel_pt_vm_tsc_info *data = pkt_info->data;
  2102. if (pkt_info->packet.type == INTEL_PT_CYC ||
  2103. pkt_info->packet.type == INTEL_PT_MTC)
  2104. return !--(data->max_lookahead);
  2105. if (pkt_info->packet.type == INTEL_PT_TMA) {
  2106. data->tma_packet = pkt_info->packet;
  2107. data->tma = true;
  2108. }
  2109. return 1;
  2110. }
  2111. static uint64_t intel_pt_ctc_to_tsc(struct intel_pt_decoder *decoder, uint64_t ctc)
  2112. {
  2113. if (decoder->tsc_ctc_mult)
  2114. return ctc * decoder->tsc_ctc_mult;
  2115. else
  2116. return multdiv(ctc, decoder->tsc_ctc_ratio_n, decoder->tsc_ctc_ratio_d);
  2117. }
  2118. static uint64_t intel_pt_calc_expected_tsc(struct intel_pt_decoder *decoder,
  2119. uint32_t ctc,
  2120. uint32_t fc,
  2121. uint64_t last_ctc_timestamp,
  2122. uint64_t ctc_delta,
  2123. uint32_t last_ctc)
  2124. {
  2125. /* Number of CTC ticks from last_ctc_timestamp to last_mtc */
  2126. uint64_t last_mtc_ctc = last_ctc + ctc_delta;
  2127. /*
  2128. * Number of CTC ticks from there until current TMA packet. We would
  2129. * expect last_mtc_ctc to be before ctc, but the TSC packet can slip
  2130. * past an MTC, so a sign-extended value is used.
  2131. */
  2132. uint64_t delta = (int16_t)((uint16_t)ctc - (uint16_t)last_mtc_ctc);
  2133. /* Total CTC ticks from last_ctc_timestamp to current TMA packet */
  2134. uint64_t new_ctc_delta = ctc_delta + delta;
  2135. uint64_t expected_tsc;
  2136. /*
  2137. * Convert CTC ticks to TSC ticks, add the starting point
  2138. * (last_ctc_timestamp) and the fast counter from the TMA packet.
  2139. */
  2140. expected_tsc = last_ctc_timestamp + intel_pt_ctc_to_tsc(decoder, new_ctc_delta) + fc;
  2141. if (intel_pt_enable_logging) {
  2142. intel_pt_log_x64(last_mtc_ctc);
  2143. intel_pt_log_x32(last_ctc);
  2144. intel_pt_log_x64(ctc_delta);
  2145. intel_pt_log_x64(delta);
  2146. intel_pt_log_x32(ctc);
  2147. intel_pt_log_x64(new_ctc_delta);
  2148. intel_pt_log_x64(last_ctc_timestamp);
  2149. intel_pt_log_x32(fc);
  2150. intel_pt_log_x64(intel_pt_ctc_to_tsc(decoder, new_ctc_delta));
  2151. intel_pt_log_x64(expected_tsc);
  2152. }
  2153. return expected_tsc;
  2154. }
  2155. static uint64_t intel_pt_expected_tsc(struct intel_pt_decoder *decoder,
  2156. struct intel_pt_vm_tsc_info *data)
  2157. {
  2158. uint32_t ctc = data->tma_packet.payload;
  2159. uint32_t fc = data->tma_packet.count;
  2160. return intel_pt_calc_expected_tsc(decoder, ctc, fc,
  2161. decoder->ctc_timestamp,
  2162. data->ctc_delta, data->last_ctc);
  2163. }
  2164. static void intel_pt_translate_vm_tsc(struct intel_pt_decoder *decoder,
  2165. struct intel_pt_vmcs_info *vmcs_info)
  2166. {
  2167. uint64_t payload = decoder->packet.payload;
  2168. /* VMX adds the TSC Offset, so subtract to get host TSC */
  2169. decoder->packet.payload -= vmcs_info->tsc_offset;
  2170. /* TSC packet has only 7 bytes */
  2171. decoder->packet.payload &= SEVEN_BYTES;
  2172. /*
  2173. * The buffer is mmapped from the data file, so this also updates the
  2174. * data file.
  2175. */
  2176. if (!decoder->vm_tm_corr_dry_run)
  2177. memcpy((void *)decoder->buf + 1, &decoder->packet.payload, 7);
  2178. intel_pt_log("Translated VM TSC %#" PRIx64 " -> %#" PRIx64
  2179. " VMCS %#" PRIx64 " TSC Offset %#" PRIx64 "\n",
  2180. payload, decoder->packet.payload, vmcs_info->vmcs,
  2181. vmcs_info->tsc_offset);
  2182. }
  2183. static void intel_pt_translate_vm_tsc_offset(struct intel_pt_decoder *decoder,
  2184. uint64_t tsc_offset)
  2185. {
  2186. struct intel_pt_vmcs_info vmcs_info = {
  2187. .vmcs = NO_VMCS,
  2188. .tsc_offset = tsc_offset
  2189. };
  2190. intel_pt_translate_vm_tsc(decoder, &vmcs_info);
  2191. }
  2192. static inline bool in_vm(uint64_t pip_payload)
  2193. {
  2194. return pip_payload & 1;
  2195. }
  2196. static inline bool pip_in_vm(struct intel_pt_pkt *pip_packet)
  2197. {
  2198. return pip_packet->payload & 1;
  2199. }
  2200. static void intel_pt_print_vmcs_info(struct intel_pt_vmcs_info *vmcs_info)
  2201. {
  2202. p_log("VMCS: %#" PRIx64 " TSC Offset %#" PRIx64,
  2203. vmcs_info->vmcs, vmcs_info->tsc_offset);
  2204. }
  2205. static void intel_pt_vm_tm_corr_psb(struct intel_pt_decoder *decoder,
  2206. struct intel_pt_vm_tsc_info *data)
  2207. {
  2208. memset(data, 0, sizeof(*data));
  2209. data->ctc_delta = decoder->ctc_delta;
  2210. data->last_ctc = decoder->last_ctc;
  2211. intel_pt_pkt_lookahead(decoder, intel_pt_vm_psb_lookahead_cb, data);
  2212. if (data->tsc && !data->psbend)
  2213. p_log("ERROR: PSB without PSBEND");
  2214. decoder->in_psb = data->psbend;
  2215. }
  2216. static void intel_pt_vm_tm_corr_first_tsc(struct intel_pt_decoder *decoder,
  2217. struct intel_pt_vm_tsc_info *data,
  2218. struct intel_pt_vmcs_info *vmcs_info,
  2219. uint64_t host_tsc)
  2220. {
  2221. if (!decoder->in_psb) {
  2222. /* Can't happen */
  2223. p_log("ERROR: First TSC is not in PSB+");
  2224. }
  2225. if (data->pip) {
  2226. if (pip_in_vm(&data->pip_packet)) { /* Guest */
  2227. if (vmcs_info && vmcs_info->tsc_offset) {
  2228. intel_pt_translate_vm_tsc(decoder, vmcs_info);
  2229. decoder->vm_tm_corr_reliable = true;
  2230. } else {
  2231. p_log("ERROR: First TSC, unknown TSC Offset");
  2232. }
  2233. } else { /* Host */
  2234. decoder->vm_tm_corr_reliable = true;
  2235. }
  2236. } else { /* Host or Guest */
  2237. decoder->vm_tm_corr_reliable = false;
  2238. if (intel_pt_time_in_range(decoder, host_tsc)) {
  2239. /* Assume Host */
  2240. } else {
  2241. /* Assume Guest */
  2242. if (vmcs_info && vmcs_info->tsc_offset)
  2243. intel_pt_translate_vm_tsc(decoder, vmcs_info);
  2244. else
  2245. p_log("ERROR: First TSC, no PIP, unknown TSC Offset");
  2246. }
  2247. }
  2248. }
  2249. static void intel_pt_vm_tm_corr_tsc(struct intel_pt_decoder *decoder,
  2250. struct intel_pt_vm_tsc_info *data)
  2251. {
  2252. struct intel_pt_vmcs_info *vmcs_info;
  2253. uint64_t tsc_offset = 0;
  2254. uint64_t vmcs;
  2255. bool reliable = true;
  2256. uint64_t expected_tsc;
  2257. uint64_t host_tsc;
  2258. uint64_t ref_timestamp;
  2259. bool assign = false;
  2260. bool assign_reliable = false;
  2261. /* Already have 'data' for the in_psb case */
  2262. if (!decoder->in_psb) {
  2263. memset(data, 0, sizeof(*data));
  2264. data->ctc_delta = decoder->ctc_delta;
  2265. data->last_ctc = decoder->last_ctc;
  2266. data->max_lookahead = 16;
  2267. intel_pt_pkt_lookahead(decoder, intel_pt_tma_lookahead_cb, data);
  2268. if (decoder->pge) {
  2269. data->pip = true;
  2270. data->pip_packet.payload = decoder->pip_payload;
  2271. }
  2272. }
  2273. /* Calculations depend on having TMA packets */
  2274. if (!data->tma) {
  2275. p_log("ERROR: TSC without TMA");
  2276. return;
  2277. }
  2278. vmcs = data->vmcs ? data->vmcs_packet.payload : decoder->vmcs;
  2279. if (vmcs == NO_VMCS)
  2280. vmcs = 0;
  2281. vmcs_info = decoder->findnew_vmcs_info(decoder->data, vmcs);
  2282. ref_timestamp = decoder->timestamp ? decoder->timestamp : decoder->buf_timestamp;
  2283. host_tsc = intel_pt_8b_tsc(decoder->packet.payload, ref_timestamp);
  2284. if (!decoder->ctc_timestamp) {
  2285. intel_pt_vm_tm_corr_first_tsc(decoder, data, vmcs_info, host_tsc);
  2286. return;
  2287. }
  2288. expected_tsc = intel_pt_expected_tsc(decoder, data);
  2289. tsc_offset = host_tsc - expected_tsc;
  2290. /* Determine if TSC is from Host or Guest */
  2291. if (data->pip) {
  2292. if (pip_in_vm(&data->pip_packet)) { /* Guest */
  2293. if (!vmcs_info) {
  2294. /* PIP NR=1 without VMCS cannot happen */
  2295. p_log("ERROR: Missing VMCS");
  2296. intel_pt_translate_vm_tsc_offset(decoder, tsc_offset);
  2297. decoder->vm_tm_corr_reliable = false;
  2298. return;
  2299. }
  2300. } else { /* Host */
  2301. decoder->last_reliable_timestamp = host_tsc;
  2302. decoder->vm_tm_corr_reliable = true;
  2303. return;
  2304. }
  2305. } else { /* Host or Guest */
  2306. reliable = false; /* Host/Guest is a guess, so not reliable */
  2307. if (decoder->in_psb) {
  2308. if (!tsc_offset)
  2309. return; /* Zero TSC Offset, assume Host */
  2310. /*
  2311. * TSC packet has only 7 bytes of TSC. We have no
  2312. * information about the Guest's 8th byte, but it
  2313. * doesn't matter because we only need 7 bytes.
  2314. * Here, since the 8th byte is unreliable and
  2315. * irrelevant, compare only 7 byes.
  2316. */
  2317. if (vmcs_info &&
  2318. (tsc_offset & SEVEN_BYTES) ==
  2319. (vmcs_info->tsc_offset & SEVEN_BYTES)) {
  2320. /* Same TSC Offset as last VMCS, assume Guest */
  2321. goto guest;
  2322. }
  2323. }
  2324. /*
  2325. * Check if the host_tsc is within the expected range.
  2326. * Note, we could narrow the range more by looking ahead for
  2327. * the next host TSC in the same buffer, but we don't bother to
  2328. * do that because this is probably good enough.
  2329. */
  2330. if (host_tsc >= expected_tsc && intel_pt_time_in_range(decoder, host_tsc)) {
  2331. /* Within expected range for Host TSC, assume Host */
  2332. decoder->vm_tm_corr_reliable = false;
  2333. return;
  2334. }
  2335. }
  2336. guest: /* Assuming Guest */
  2337. /* Determine whether to assign TSC Offset */
  2338. if (vmcs_info && vmcs_info->vmcs) {
  2339. if (vmcs_info->tsc_offset && vmcs_info->reliable) {
  2340. assign = false;
  2341. } else if (decoder->in_psb && data->pip && decoder->vm_tm_corr_reliable &&
  2342. decoder->vm_tm_corr_continuous && decoder->vm_tm_corr_same_buf) {
  2343. /* Continuous tracing, TSC in a PSB is not a time loss */
  2344. assign = true;
  2345. assign_reliable = true;
  2346. } else if (decoder->in_psb && data->pip && decoder->vm_tm_corr_same_buf) {
  2347. /*
  2348. * Unlikely to be a time loss TSC in a PSB which is not
  2349. * at the start of a buffer.
  2350. */
  2351. assign = true;
  2352. assign_reliable = false;
  2353. }
  2354. }
  2355. /* Record VMCS TSC Offset */
  2356. if (assign && (vmcs_info->tsc_offset != tsc_offset ||
  2357. vmcs_info->reliable != assign_reliable)) {
  2358. bool print = vmcs_info->tsc_offset != tsc_offset;
  2359. vmcs_info->tsc_offset = tsc_offset;
  2360. vmcs_info->reliable = assign_reliable;
  2361. if (print)
  2362. intel_pt_print_vmcs_info(vmcs_info);
  2363. }
  2364. /* Determine what TSC Offset to use */
  2365. if (vmcs_info && vmcs_info->tsc_offset) {
  2366. if (!vmcs_info->reliable)
  2367. reliable = false;
  2368. intel_pt_translate_vm_tsc(decoder, vmcs_info);
  2369. } else {
  2370. reliable = false;
  2371. if (vmcs_info) {
  2372. if (!vmcs_info->error_printed) {
  2373. p_log("ERROR: Unknown TSC Offset for VMCS %#" PRIx64,
  2374. vmcs_info->vmcs);
  2375. vmcs_info->error_printed = true;
  2376. }
  2377. } else {
  2378. if (intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_UNK_VMCS))
  2379. p_log("ERROR: Unknown VMCS");
  2380. }
  2381. intel_pt_translate_vm_tsc_offset(decoder, tsc_offset);
  2382. }
  2383. decoder->vm_tm_corr_reliable = reliable;
  2384. }
  2385. static void intel_pt_vm_tm_corr_pebs_tsc(struct intel_pt_decoder *decoder)
  2386. {
  2387. uint64_t host_tsc = decoder->packet.payload;
  2388. uint64_t guest_tsc = decoder->packet.payload;
  2389. struct intel_pt_vmcs_info *vmcs_info;
  2390. uint64_t vmcs;
  2391. vmcs = decoder->vmcs;
  2392. if (vmcs == NO_VMCS)
  2393. vmcs = 0;
  2394. vmcs_info = decoder->findnew_vmcs_info(decoder->data, vmcs);
  2395. if (decoder->pge) {
  2396. if (in_vm(decoder->pip_payload)) { /* Guest */
  2397. if (!vmcs_info) {
  2398. /* PIP NR=1 without VMCS cannot happen */
  2399. p_log("ERROR: Missing VMCS");
  2400. }
  2401. } else { /* Host */
  2402. return;
  2403. }
  2404. } else { /* Host or Guest */
  2405. if (intel_pt_time_in_range(decoder, host_tsc)) {
  2406. /* Within expected range for Host TSC, assume Host */
  2407. return;
  2408. }
  2409. }
  2410. if (vmcs_info) {
  2411. /* Translate Guest TSC to Host TSC */
  2412. host_tsc = ((guest_tsc & SEVEN_BYTES) - vmcs_info->tsc_offset) & SEVEN_BYTES;
  2413. host_tsc = intel_pt_8b_tsc(host_tsc, decoder->timestamp);
  2414. intel_pt_log("Translated VM TSC %#" PRIx64 " -> %#" PRIx64
  2415. " VMCS %#" PRIx64 " TSC Offset %#" PRIx64 "\n",
  2416. guest_tsc, host_tsc, vmcs_info->vmcs,
  2417. vmcs_info->tsc_offset);
  2418. if (!intel_pt_time_in_range(decoder, host_tsc) &&
  2419. intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_ERANGE))
  2420. p_log("Timestamp out of range");
  2421. } else {
  2422. if (intel_pt_print_once(decoder, INTEL_PT_PRT_ONCE_UNK_VMCS))
  2423. p_log("ERROR: Unknown VMCS");
  2424. host_tsc = decoder->timestamp;
  2425. }
  2426. decoder->packet.payload = host_tsc;
  2427. if (!decoder->vm_tm_corr_dry_run)
  2428. memcpy((void *)decoder->buf + 1, &host_tsc, 8);
  2429. }
  2430. static int intel_pt_vm_time_correlation(struct intel_pt_decoder *decoder)
  2431. {
  2432. struct intel_pt_vm_tsc_info data = { .psbend = false };
  2433. bool pge;
  2434. int err;
  2435. if (decoder->in_psb)
  2436. intel_pt_vm_tm_corr_psb(decoder, &data);
  2437. while (1) {
  2438. err = intel_pt_get_next_packet(decoder);
  2439. if (err == -ENOLINK)
  2440. continue;
  2441. if (err)
  2442. break;
  2443. switch (decoder->packet.type) {
  2444. case INTEL_PT_TIP_PGD:
  2445. decoder->pge = false;
  2446. decoder->vm_tm_corr_continuous = false;
  2447. break;
  2448. case INTEL_PT_TNT:
  2449. case INTEL_PT_TIP:
  2450. case INTEL_PT_TIP_PGE:
  2451. decoder->pge = true;
  2452. break;
  2453. case INTEL_PT_OVF:
  2454. decoder->in_psb = false;
  2455. pge = decoder->pge;
  2456. decoder->pge = intel_pt_ovf_fup_lookahead(decoder);
  2457. if (pge != decoder->pge)
  2458. intel_pt_log("Surprising PGE change in OVF!");
  2459. if (!decoder->pge)
  2460. decoder->vm_tm_corr_continuous = false;
  2461. break;
  2462. case INTEL_PT_FUP:
  2463. if (decoder->in_psb)
  2464. decoder->pge = true;
  2465. break;
  2466. case INTEL_PT_TRACESTOP:
  2467. decoder->pge = false;
  2468. decoder->vm_tm_corr_continuous = false;
  2469. decoder->have_tma = false;
  2470. break;
  2471. case INTEL_PT_PSB:
  2472. intel_pt_vm_tm_corr_psb(decoder, &data);
  2473. break;
  2474. case INTEL_PT_PIP:
  2475. decoder->pip_payload = decoder->packet.payload;
  2476. break;
  2477. case INTEL_PT_MTC:
  2478. intel_pt_calc_mtc_timestamp(decoder);
  2479. break;
  2480. case INTEL_PT_TSC:
  2481. intel_pt_vm_tm_corr_tsc(decoder, &data);
  2482. intel_pt_calc_tsc_timestamp(decoder);
  2483. decoder->vm_tm_corr_same_buf = true;
  2484. decoder->vm_tm_corr_continuous = decoder->pge;
  2485. break;
  2486. case INTEL_PT_TMA:
  2487. intel_pt_calc_tma(decoder);
  2488. break;
  2489. case INTEL_PT_CYC:
  2490. intel_pt_calc_cyc_timestamp(decoder);
  2491. break;
  2492. case INTEL_PT_CBR:
  2493. intel_pt_calc_cbr(decoder);
  2494. break;
  2495. case INTEL_PT_PSBEND:
  2496. decoder->in_psb = false;
  2497. data.psbend = false;
  2498. break;
  2499. case INTEL_PT_VMCS:
  2500. if (decoder->packet.payload != NO_VMCS)
  2501. decoder->vmcs = decoder->packet.payload;
  2502. break;
  2503. case INTEL_PT_BBP:
  2504. decoder->blk_type = decoder->packet.payload;
  2505. break;
  2506. case INTEL_PT_BIP:
  2507. if (decoder->blk_type == INTEL_PT_PEBS_BASIC &&
  2508. decoder->packet.count == 2)
  2509. intel_pt_vm_tm_corr_pebs_tsc(decoder);
  2510. break;
  2511. case INTEL_PT_BEP:
  2512. case INTEL_PT_BEP_IP:
  2513. decoder->blk_type = 0;
  2514. break;
  2515. case INTEL_PT_CFE:
  2516. case INTEL_PT_CFE_IP:
  2517. case INTEL_PT_EVD:
  2518. case INTEL_PT_MODE_EXEC:
  2519. case INTEL_PT_MODE_TSX:
  2520. case INTEL_PT_MNT:
  2521. case INTEL_PT_PAD:
  2522. case INTEL_PT_PTWRITE_IP:
  2523. case INTEL_PT_PTWRITE:
  2524. case INTEL_PT_MWAIT:
  2525. case INTEL_PT_PWRE:
  2526. case INTEL_PT_EXSTOP_IP:
  2527. case INTEL_PT_EXSTOP:
  2528. case INTEL_PT_PWRX:
  2529. case INTEL_PT_BAD: /* Does not happen */
  2530. default:
  2531. break;
  2532. }
  2533. }
  2534. return err;
  2535. }
  2536. #define HOP_PROCESS 0
  2537. #define HOP_IGNORE 1
  2538. #define HOP_RETURN 2
  2539. #define HOP_AGAIN 3
  2540. static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder);
  2541. /* Hop mode: Ignore TNT, do not walk code, but get ip from FUPs and TIPs */
  2542. static int intel_pt_hop_trace(struct intel_pt_decoder *decoder, bool *no_tip, int *err)
  2543. {
  2544. *err = 0;
  2545. /* Leap from PSB to PSB, getting ip from FUP within PSB+ */
  2546. if (decoder->leap && !decoder->in_psb && decoder->packet.type != INTEL_PT_PSB) {
  2547. *err = intel_pt_scan_for_psb(decoder);
  2548. if (*err)
  2549. return HOP_RETURN;
  2550. }
  2551. switch (decoder->packet.type) {
  2552. case INTEL_PT_TNT:
  2553. return HOP_IGNORE;
  2554. case INTEL_PT_TIP_PGD:
  2555. decoder->pge = false;
  2556. if (!decoder->packet.count) {
  2557. intel_pt_set_nr(decoder);
  2558. return HOP_IGNORE;
  2559. }
  2560. intel_pt_set_ip(decoder);
  2561. decoder->state.type |= INTEL_PT_TRACE_END;
  2562. decoder->state.from_ip = 0;
  2563. decoder->state.to_ip = decoder->ip;
  2564. intel_pt_update_nr(decoder);
  2565. return HOP_RETURN;
  2566. case INTEL_PT_TIP:
  2567. if (!decoder->packet.count) {
  2568. intel_pt_set_nr(decoder);
  2569. return HOP_IGNORE;
  2570. }
  2571. intel_pt_set_ip(decoder);
  2572. decoder->state.type = INTEL_PT_INSTRUCTION;
  2573. decoder->state.from_ip = decoder->ip;
  2574. decoder->state.to_ip = 0;
  2575. intel_pt_update_nr(decoder);
  2576. intel_pt_sample_iflag_chg(decoder);
  2577. return HOP_RETURN;
  2578. case INTEL_PT_FUP:
  2579. if (!decoder->packet.count)
  2580. return HOP_IGNORE;
  2581. intel_pt_set_ip(decoder);
  2582. if (decoder->set_fup_mwait || decoder->set_fup_pwre)
  2583. *no_tip = true;
  2584. if (!decoder->branch_enable || !decoder->pge)
  2585. *no_tip = true;
  2586. if (*no_tip) {
  2587. decoder->state.type = INTEL_PT_INSTRUCTION;
  2588. decoder->state.from_ip = decoder->ip;
  2589. decoder->state.to_ip = 0;
  2590. intel_pt_fup_event(decoder, *no_tip);
  2591. return HOP_RETURN;
  2592. }
  2593. intel_pt_fup_event(decoder, *no_tip);
  2594. decoder->state.type |= INTEL_PT_INSTRUCTION | INTEL_PT_BRANCH;
  2595. *err = intel_pt_walk_fup_tip(decoder);
  2596. if (!*err && decoder->state.to_ip)
  2597. decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
  2598. return HOP_RETURN;
  2599. case INTEL_PT_PSB:
  2600. decoder->state.psb_offset = decoder->pos;
  2601. decoder->psb_ip = 0;
  2602. decoder->last_ip = 0;
  2603. decoder->have_last_ip = true;
  2604. *err = intel_pt_walk_psbend(decoder);
  2605. if (*err == -EAGAIN)
  2606. return HOP_AGAIN;
  2607. if (*err)
  2608. return HOP_RETURN;
  2609. decoder->state.type = INTEL_PT_PSB_EVT;
  2610. if (decoder->psb_ip) {
  2611. decoder->state.type |= INTEL_PT_INSTRUCTION;
  2612. decoder->ip = decoder->psb_ip;
  2613. }
  2614. decoder->state.from_ip = decoder->psb_ip;
  2615. decoder->state.to_ip = 0;
  2616. return HOP_RETURN;
  2617. case INTEL_PT_BAD:
  2618. case INTEL_PT_PAD:
  2619. case INTEL_PT_TIP_PGE:
  2620. case INTEL_PT_TSC:
  2621. case INTEL_PT_TMA:
  2622. case INTEL_PT_MODE_EXEC:
  2623. case INTEL_PT_MODE_TSX:
  2624. case INTEL_PT_MTC:
  2625. case INTEL_PT_CYC:
  2626. case INTEL_PT_VMCS:
  2627. case INTEL_PT_PSBEND:
  2628. case INTEL_PT_CBR:
  2629. case INTEL_PT_TRACESTOP:
  2630. case INTEL_PT_PIP:
  2631. case INTEL_PT_OVF:
  2632. case INTEL_PT_MNT:
  2633. case INTEL_PT_PTWRITE:
  2634. case INTEL_PT_PTWRITE_IP:
  2635. case INTEL_PT_EXSTOP:
  2636. case INTEL_PT_EXSTOP_IP:
  2637. case INTEL_PT_MWAIT:
  2638. case INTEL_PT_PWRE:
  2639. case INTEL_PT_PWRX:
  2640. case INTEL_PT_BBP:
  2641. case INTEL_PT_BIP:
  2642. case INTEL_PT_BEP:
  2643. case INTEL_PT_BEP_IP:
  2644. case INTEL_PT_CFE:
  2645. case INTEL_PT_CFE_IP:
  2646. case INTEL_PT_EVD:
  2647. default:
  2648. return HOP_PROCESS;
  2649. }
  2650. }
  2651. struct intel_pt_psb_info {
  2652. struct intel_pt_pkt fup_packet;
  2653. bool fup;
  2654. int after_psbend;
  2655. };
  2656. /* Lookahead and get the FUP packet from PSB+ */
  2657. static int intel_pt_psb_lookahead_cb(struct intel_pt_pkt_info *pkt_info)
  2658. {
  2659. struct intel_pt_psb_info *data = pkt_info->data;
  2660. switch (pkt_info->packet.type) {
  2661. case INTEL_PT_PAD:
  2662. case INTEL_PT_MNT:
  2663. case INTEL_PT_TSC:
  2664. case INTEL_PT_TMA:
  2665. case INTEL_PT_MODE_EXEC:
  2666. case INTEL_PT_MODE_TSX:
  2667. case INTEL_PT_MTC:
  2668. case INTEL_PT_CYC:
  2669. case INTEL_PT_VMCS:
  2670. case INTEL_PT_CBR:
  2671. case INTEL_PT_PIP:
  2672. if (data->after_psbend) {
  2673. data->after_psbend -= 1;
  2674. if (!data->after_psbend)
  2675. return 1;
  2676. }
  2677. break;
  2678. case INTEL_PT_FUP:
  2679. if (data->after_psbend)
  2680. return 1;
  2681. if (data->fup || pkt_info->packet.count == 0)
  2682. return 1;
  2683. data->fup_packet = pkt_info->packet;
  2684. data->fup = true;
  2685. break;
  2686. case INTEL_PT_PSBEND:
  2687. if (!data->fup)
  2688. return 1;
  2689. /* Keep going to check for a TIP.PGE */
  2690. data->after_psbend = 6;
  2691. break;
  2692. case INTEL_PT_TIP_PGE:
  2693. /* Ignore FUP in PSB+ if followed by TIP.PGE */
  2694. if (data->after_psbend)
  2695. data->fup = false;
  2696. return 1;
  2697. case INTEL_PT_PTWRITE:
  2698. case INTEL_PT_PTWRITE_IP:
  2699. case INTEL_PT_EXSTOP:
  2700. case INTEL_PT_EXSTOP_IP:
  2701. case INTEL_PT_MWAIT:
  2702. case INTEL_PT_PWRE:
  2703. case INTEL_PT_PWRX:
  2704. case INTEL_PT_BBP:
  2705. case INTEL_PT_BIP:
  2706. case INTEL_PT_BEP:
  2707. case INTEL_PT_BEP_IP:
  2708. case INTEL_PT_CFE:
  2709. case INTEL_PT_CFE_IP:
  2710. case INTEL_PT_EVD:
  2711. if (data->after_psbend) {
  2712. data->after_psbend -= 1;
  2713. if (!data->after_psbend)
  2714. return 1;
  2715. break;
  2716. }
  2717. return 1;
  2718. case INTEL_PT_OVF:
  2719. case INTEL_PT_BAD:
  2720. case INTEL_PT_TNT:
  2721. case INTEL_PT_TIP_PGD:
  2722. case INTEL_PT_TIP:
  2723. case INTEL_PT_PSB:
  2724. case INTEL_PT_TRACESTOP:
  2725. default:
  2726. return 1;
  2727. }
  2728. return 0;
  2729. }
  2730. static int intel_pt_psb(struct intel_pt_decoder *decoder)
  2731. {
  2732. int err;
  2733. decoder->last_ip = 0;
  2734. decoder->psb_ip = 0;
  2735. decoder->have_last_ip = true;
  2736. intel_pt_clear_stack(&decoder->stack);
  2737. err = intel_pt_walk_psbend(decoder);
  2738. if (err)
  2739. return err;
  2740. decoder->state.type = INTEL_PT_PSB_EVT;
  2741. decoder->state.from_ip = decoder->psb_ip;
  2742. decoder->state.to_ip = 0;
  2743. return 0;
  2744. }
  2745. static int intel_pt_fup_in_psb(struct intel_pt_decoder *decoder)
  2746. {
  2747. int err;
  2748. if (decoder->ip != decoder->last_ip) {
  2749. err = intel_pt_walk_fup(decoder);
  2750. if (!err || err != -EAGAIN)
  2751. return err;
  2752. }
  2753. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  2754. err = intel_pt_psb(decoder);
  2755. if (err) {
  2756. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  2757. return -ENOENT;
  2758. }
  2759. return 0;
  2760. }
  2761. static bool intel_pt_psb_with_fup(struct intel_pt_decoder *decoder, int *err)
  2762. {
  2763. struct intel_pt_psb_info data = { .fup = false };
  2764. if (!decoder->branch_enable)
  2765. return false;
  2766. intel_pt_pkt_lookahead(decoder, intel_pt_psb_lookahead_cb, &data);
  2767. if (!data.fup)
  2768. return false;
  2769. decoder->packet = data.fup_packet;
  2770. intel_pt_set_last_ip(decoder);
  2771. decoder->pkt_state = INTEL_PT_STATE_FUP_IN_PSB;
  2772. *err = intel_pt_fup_in_psb(decoder);
  2773. return true;
  2774. }
  2775. static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
  2776. {
  2777. int last_packet_type = INTEL_PT_PAD;
  2778. bool no_tip = false;
  2779. int err;
  2780. while (1) {
  2781. err = intel_pt_get_next_packet(decoder);
  2782. if (err)
  2783. return err;
  2784. next:
  2785. err = 0;
  2786. if (decoder->cyc_threshold) {
  2787. if (decoder->sample_cyc && last_packet_type != INTEL_PT_CYC)
  2788. decoder->sample_cyc = false;
  2789. last_packet_type = decoder->packet.type;
  2790. }
  2791. if (decoder->hop) {
  2792. switch (intel_pt_hop_trace(decoder, &no_tip, &err)) {
  2793. case HOP_IGNORE:
  2794. continue;
  2795. case HOP_RETURN:
  2796. return err;
  2797. case HOP_AGAIN:
  2798. goto next;
  2799. default:
  2800. break;
  2801. }
  2802. }
  2803. switch (decoder->packet.type) {
  2804. case INTEL_PT_TNT:
  2805. if (!decoder->packet.count)
  2806. break;
  2807. decoder->tnt = decoder->packet;
  2808. decoder->pkt_state = INTEL_PT_STATE_TNT;
  2809. err = intel_pt_walk_tnt(decoder);
  2810. if (err == -EAGAIN)
  2811. break;
  2812. return err;
  2813. case INTEL_PT_TIP_PGD:
  2814. if (decoder->packet.count != 0)
  2815. intel_pt_set_last_ip(decoder);
  2816. decoder->pkt_state = INTEL_PT_STATE_TIP_PGD;
  2817. return intel_pt_walk_tip(decoder);
  2818. case INTEL_PT_TIP_PGE: {
  2819. decoder->pge = true;
  2820. decoder->overflow = false;
  2821. intel_pt_mtc_cyc_cnt_pge(decoder);
  2822. intel_pt_set_nr(decoder);
  2823. if (decoder->packet.count == 0) {
  2824. intel_pt_log_at("Skipping zero TIP.PGE",
  2825. decoder->pos);
  2826. break;
  2827. }
  2828. intel_pt_sample_iflag_chg(decoder);
  2829. intel_pt_set_ip(decoder);
  2830. decoder->state.from_ip = 0;
  2831. decoder->state.to_ip = decoder->ip;
  2832. decoder->state.type |= INTEL_PT_TRACE_BEGIN;
  2833. /*
  2834. * In hop mode, resample to get the to_ip as an
  2835. * "instruction" sample.
  2836. */
  2837. if (decoder->hop)
  2838. decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
  2839. return 0;
  2840. }
  2841. case INTEL_PT_OVF:
  2842. return intel_pt_overflow(decoder);
  2843. case INTEL_PT_TIP:
  2844. if (decoder->packet.count != 0)
  2845. intel_pt_set_last_ip(decoder);
  2846. decoder->pkt_state = INTEL_PT_STATE_TIP;
  2847. return intel_pt_walk_tip(decoder);
  2848. case INTEL_PT_FUP:
  2849. if (decoder->packet.count == 0) {
  2850. intel_pt_log_at("Skipping zero FUP",
  2851. decoder->pos);
  2852. no_tip = false;
  2853. break;
  2854. }
  2855. intel_pt_set_last_ip(decoder);
  2856. if (!decoder->branch_enable || !decoder->pge) {
  2857. decoder->ip = decoder->last_ip;
  2858. if (intel_pt_fup_event(decoder, no_tip))
  2859. return 0;
  2860. no_tip = false;
  2861. break;
  2862. }
  2863. if (decoder->set_fup_mwait)
  2864. no_tip = true;
  2865. if (no_tip)
  2866. decoder->pkt_state = INTEL_PT_STATE_FUP_NO_TIP;
  2867. else
  2868. decoder->pkt_state = INTEL_PT_STATE_FUP;
  2869. err = intel_pt_walk_fup(decoder);
  2870. if (err != -EAGAIN)
  2871. return err;
  2872. if (no_tip) {
  2873. no_tip = false;
  2874. break;
  2875. }
  2876. return intel_pt_walk_fup_tip(decoder);
  2877. case INTEL_PT_TRACESTOP:
  2878. decoder->pge = false;
  2879. decoder->continuous_period = false;
  2880. intel_pt_clear_tx_flags(decoder);
  2881. decoder->have_tma = false;
  2882. break;
  2883. case INTEL_PT_PSB:
  2884. decoder->state.psb_offset = decoder->pos;
  2885. decoder->psb_ip = 0;
  2886. if (intel_pt_psb_with_fup(decoder, &err))
  2887. return err;
  2888. err = intel_pt_psb(decoder);
  2889. if (err == -EAGAIN)
  2890. goto next;
  2891. return err;
  2892. case INTEL_PT_PIP:
  2893. intel_pt_update_pip(decoder);
  2894. break;
  2895. case INTEL_PT_MTC:
  2896. intel_pt_calc_mtc_timestamp(decoder);
  2897. if (decoder->period_type != INTEL_PT_PERIOD_MTC)
  2898. break;
  2899. /*
  2900. * Ensure that there has been an instruction since the
  2901. * last MTC.
  2902. */
  2903. if (!decoder->mtc_insn)
  2904. break;
  2905. decoder->mtc_insn = false;
  2906. /* Ensure that there is a timestamp */
  2907. if (!decoder->timestamp)
  2908. break;
  2909. decoder->state.type = INTEL_PT_INSTRUCTION;
  2910. decoder->state.from_ip = decoder->ip;
  2911. decoder->state.to_ip = 0;
  2912. decoder->mtc_insn = false;
  2913. return 0;
  2914. case INTEL_PT_TSC:
  2915. intel_pt_calc_tsc_timestamp(decoder);
  2916. break;
  2917. case INTEL_PT_TMA:
  2918. intel_pt_calc_tma(decoder);
  2919. break;
  2920. case INTEL_PT_CYC:
  2921. intel_pt_calc_cyc_timestamp(decoder);
  2922. break;
  2923. case INTEL_PT_CBR:
  2924. intel_pt_calc_cbr(decoder);
  2925. if (decoder->cbr != decoder->cbr_seen) {
  2926. decoder->state.type = 0;
  2927. return 0;
  2928. }
  2929. break;
  2930. case INTEL_PT_MODE_EXEC:
  2931. intel_pt_mode_exec(decoder);
  2932. err = intel_pt_get_next_packet(decoder);
  2933. if (err)
  2934. return err;
  2935. if (decoder->packet.type == INTEL_PT_FUP) {
  2936. decoder->set_fup_mode_exec = true;
  2937. no_tip = true;
  2938. }
  2939. goto next;
  2940. case INTEL_PT_MODE_TSX:
  2941. /* MODE_TSX need not be followed by FUP */
  2942. if (!decoder->pge || decoder->in_psb) {
  2943. intel_pt_update_in_tx(decoder);
  2944. break;
  2945. }
  2946. err = intel_pt_mode_tsx(decoder, &no_tip);
  2947. if (err)
  2948. return err;
  2949. goto next;
  2950. case INTEL_PT_BAD: /* Does not happen */
  2951. return intel_pt_bug(decoder);
  2952. case INTEL_PT_PSBEND:
  2953. case INTEL_PT_VMCS:
  2954. case INTEL_PT_MNT:
  2955. case INTEL_PT_PAD:
  2956. break;
  2957. case INTEL_PT_PTWRITE_IP:
  2958. decoder->fup_ptw_payload = decoder->packet.payload;
  2959. err = intel_pt_get_next_packet(decoder);
  2960. if (err)
  2961. return err;
  2962. if (decoder->packet.type == INTEL_PT_FUP) {
  2963. decoder->set_fup_ptw = true;
  2964. no_tip = true;
  2965. } else {
  2966. intel_pt_log_at("ERROR: Missing FUP after PTWRITE",
  2967. decoder->pos);
  2968. }
  2969. goto next;
  2970. case INTEL_PT_PTWRITE:
  2971. decoder->state.type = INTEL_PT_PTW;
  2972. decoder->state.from_ip = decoder->ip;
  2973. decoder->state.to_ip = 0;
  2974. decoder->state.ptw_payload = decoder->packet.payload;
  2975. return 0;
  2976. case INTEL_PT_MWAIT:
  2977. decoder->fup_mwait_payload = decoder->packet.payload;
  2978. decoder->set_fup_mwait = true;
  2979. break;
  2980. case INTEL_PT_PWRE:
  2981. if (decoder->set_fup_mwait) {
  2982. decoder->fup_pwre_payload =
  2983. decoder->packet.payload;
  2984. decoder->set_fup_pwre = true;
  2985. break;
  2986. }
  2987. decoder->state.type = INTEL_PT_PWR_ENTRY;
  2988. decoder->state.from_ip = decoder->ip;
  2989. decoder->state.to_ip = 0;
  2990. decoder->state.pwrx_payload = decoder->packet.payload;
  2991. return 0;
  2992. case INTEL_PT_EXSTOP_IP:
  2993. err = intel_pt_get_next_packet(decoder);
  2994. if (err)
  2995. return err;
  2996. if (decoder->packet.type == INTEL_PT_FUP) {
  2997. decoder->set_fup_exstop = true;
  2998. no_tip = true;
  2999. } else {
  3000. intel_pt_log_at("ERROR: Missing FUP after EXSTOP",
  3001. decoder->pos);
  3002. }
  3003. goto next;
  3004. case INTEL_PT_EXSTOP:
  3005. decoder->state.type = INTEL_PT_EX_STOP;
  3006. decoder->state.from_ip = decoder->ip;
  3007. decoder->state.to_ip = 0;
  3008. return 0;
  3009. case INTEL_PT_PWRX:
  3010. decoder->state.type = INTEL_PT_PWR_EXIT;
  3011. decoder->state.from_ip = decoder->ip;
  3012. decoder->state.to_ip = 0;
  3013. decoder->state.pwrx_payload = decoder->packet.payload;
  3014. return 0;
  3015. case INTEL_PT_BBP:
  3016. intel_pt_bbp(decoder);
  3017. break;
  3018. case INTEL_PT_BIP:
  3019. intel_pt_bip(decoder);
  3020. break;
  3021. case INTEL_PT_BEP:
  3022. decoder->state.type = INTEL_PT_BLK_ITEMS;
  3023. decoder->state.from_ip = decoder->ip;
  3024. decoder->state.to_ip = 0;
  3025. return 0;
  3026. case INTEL_PT_BEP_IP:
  3027. err = intel_pt_get_next_packet(decoder);
  3028. if (err)
  3029. return err;
  3030. if (decoder->packet.type == INTEL_PT_FUP) {
  3031. decoder->set_fup_bep = true;
  3032. no_tip = true;
  3033. } else {
  3034. intel_pt_log_at("ERROR: Missing FUP after BEP",
  3035. decoder->pos);
  3036. }
  3037. goto next;
  3038. case INTEL_PT_CFE:
  3039. decoder->fup_cfe_pkt = decoder->packet;
  3040. decoder->set_fup_cfe = true;
  3041. if (!decoder->pge) {
  3042. intel_pt_fup_event(decoder, true);
  3043. return 0;
  3044. }
  3045. break;
  3046. case INTEL_PT_CFE_IP:
  3047. decoder->fup_cfe_pkt = decoder->packet;
  3048. err = intel_pt_get_next_packet(decoder);
  3049. if (err)
  3050. return err;
  3051. if (decoder->packet.type == INTEL_PT_FUP) {
  3052. decoder->set_fup_cfe_ip = true;
  3053. no_tip = true;
  3054. } else {
  3055. intel_pt_log_at("ERROR: Missing FUP after CFE",
  3056. decoder->pos);
  3057. }
  3058. goto next;
  3059. case INTEL_PT_EVD:
  3060. err = intel_pt_evd(decoder);
  3061. if (err)
  3062. return err;
  3063. break;
  3064. default:
  3065. return intel_pt_bug(decoder);
  3066. }
  3067. }
  3068. }
  3069. static inline bool intel_pt_have_ip(struct intel_pt_decoder *decoder)
  3070. {
  3071. return decoder->packet.count &&
  3072. (decoder->have_last_ip || decoder->packet.count == 3 ||
  3073. decoder->packet.count == 6);
  3074. }
  3075. /* Walk PSB+ packets to get in sync. */
  3076. static int intel_pt_walk_psb(struct intel_pt_decoder *decoder)
  3077. {
  3078. int err;
  3079. decoder->in_psb = true;
  3080. while (1) {
  3081. err = intel_pt_get_next_packet(decoder);
  3082. if (err)
  3083. goto out;
  3084. switch (decoder->packet.type) {
  3085. case INTEL_PT_TIP_PGD:
  3086. decoder->continuous_period = false;
  3087. fallthrough;
  3088. case INTEL_PT_TIP_PGE:
  3089. case INTEL_PT_TIP:
  3090. case INTEL_PT_PTWRITE:
  3091. case INTEL_PT_PTWRITE_IP:
  3092. case INTEL_PT_EXSTOP:
  3093. case INTEL_PT_EXSTOP_IP:
  3094. case INTEL_PT_MWAIT:
  3095. case INTEL_PT_PWRE:
  3096. case INTEL_PT_PWRX:
  3097. case INTEL_PT_BBP:
  3098. case INTEL_PT_BIP:
  3099. case INTEL_PT_BEP:
  3100. case INTEL_PT_BEP_IP:
  3101. case INTEL_PT_CFE:
  3102. case INTEL_PT_CFE_IP:
  3103. case INTEL_PT_EVD:
  3104. intel_pt_log("ERROR: Unexpected packet\n");
  3105. err = -ENOENT;
  3106. goto out;
  3107. case INTEL_PT_FUP:
  3108. decoder->pge = true;
  3109. if (intel_pt_have_ip(decoder)) {
  3110. uint64_t current_ip = decoder->ip;
  3111. intel_pt_set_ip(decoder);
  3112. decoder->psb_ip = decoder->ip;
  3113. if (current_ip)
  3114. intel_pt_log_to("Setting IP",
  3115. decoder->ip);
  3116. }
  3117. break;
  3118. case INTEL_PT_MTC:
  3119. intel_pt_calc_mtc_timestamp(decoder);
  3120. break;
  3121. case INTEL_PT_TSC:
  3122. intel_pt_calc_tsc_timestamp(decoder);
  3123. break;
  3124. case INTEL_PT_TMA:
  3125. intel_pt_calc_tma(decoder);
  3126. break;
  3127. case INTEL_PT_CYC:
  3128. intel_pt_calc_cyc_timestamp(decoder);
  3129. break;
  3130. case INTEL_PT_CBR:
  3131. intel_pt_calc_cbr(decoder);
  3132. break;
  3133. case INTEL_PT_PIP:
  3134. intel_pt_set_pip(decoder);
  3135. break;
  3136. case INTEL_PT_MODE_EXEC:
  3137. intel_pt_mode_exec_status(decoder);
  3138. break;
  3139. case INTEL_PT_MODE_TSX:
  3140. intel_pt_update_in_tx(decoder);
  3141. break;
  3142. case INTEL_PT_TRACESTOP:
  3143. decoder->pge = false;
  3144. decoder->continuous_period = false;
  3145. intel_pt_clear_tx_flags(decoder);
  3146. fallthrough;
  3147. case INTEL_PT_TNT:
  3148. decoder->have_tma = false;
  3149. intel_pt_log("ERROR: Unexpected packet\n");
  3150. if (decoder->ip)
  3151. decoder->pkt_state = INTEL_PT_STATE_ERR4;
  3152. else
  3153. decoder->pkt_state = INTEL_PT_STATE_ERR3;
  3154. err = -ENOENT;
  3155. goto out;
  3156. case INTEL_PT_BAD: /* Does not happen */
  3157. err = intel_pt_bug(decoder);
  3158. goto out;
  3159. case INTEL_PT_OVF:
  3160. err = intel_pt_overflow(decoder);
  3161. goto out;
  3162. case INTEL_PT_PSBEND:
  3163. err = 0;
  3164. goto out;
  3165. case INTEL_PT_PSB:
  3166. case INTEL_PT_VMCS:
  3167. case INTEL_PT_MNT:
  3168. case INTEL_PT_PAD:
  3169. default:
  3170. break;
  3171. }
  3172. }
  3173. out:
  3174. decoder->in_psb = false;
  3175. return err;
  3176. }
  3177. static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
  3178. {
  3179. int err;
  3180. while (1) {
  3181. err = intel_pt_get_next_packet(decoder);
  3182. if (err)
  3183. return err;
  3184. switch (decoder->packet.type) {
  3185. case INTEL_PT_TIP_PGD:
  3186. decoder->continuous_period = false;
  3187. decoder->pge = false;
  3188. if (intel_pt_have_ip(decoder))
  3189. intel_pt_set_ip(decoder);
  3190. if (!decoder->ip)
  3191. break;
  3192. decoder->state.type |= INTEL_PT_TRACE_END;
  3193. return 0;
  3194. case INTEL_PT_TIP_PGE:
  3195. decoder->pge = true;
  3196. intel_pt_mtc_cyc_cnt_pge(decoder);
  3197. if (intel_pt_have_ip(decoder))
  3198. intel_pt_set_ip(decoder);
  3199. if (!decoder->ip)
  3200. break;
  3201. decoder->state.type |= INTEL_PT_TRACE_BEGIN;
  3202. return 0;
  3203. case INTEL_PT_TIP:
  3204. decoder->pge = true;
  3205. if (intel_pt_have_ip(decoder))
  3206. intel_pt_set_ip(decoder);
  3207. if (!decoder->ip)
  3208. break;
  3209. return 0;
  3210. case INTEL_PT_FUP:
  3211. if (intel_pt_have_ip(decoder))
  3212. intel_pt_set_ip(decoder);
  3213. if (decoder->ip)
  3214. return 0;
  3215. break;
  3216. case INTEL_PT_MTC:
  3217. intel_pt_calc_mtc_timestamp(decoder);
  3218. break;
  3219. case INTEL_PT_TSC:
  3220. intel_pt_calc_tsc_timestamp(decoder);
  3221. break;
  3222. case INTEL_PT_TMA:
  3223. intel_pt_calc_tma(decoder);
  3224. break;
  3225. case INTEL_PT_CYC:
  3226. intel_pt_calc_cyc_timestamp(decoder);
  3227. break;
  3228. case INTEL_PT_CBR:
  3229. intel_pt_calc_cbr(decoder);
  3230. break;
  3231. case INTEL_PT_PIP:
  3232. intel_pt_set_pip(decoder);
  3233. break;
  3234. case INTEL_PT_MODE_EXEC:
  3235. intel_pt_mode_exec_status(decoder);
  3236. break;
  3237. case INTEL_PT_MODE_TSX:
  3238. intel_pt_update_in_tx(decoder);
  3239. break;
  3240. case INTEL_PT_OVF:
  3241. return intel_pt_overflow(decoder);
  3242. case INTEL_PT_BAD: /* Does not happen */
  3243. return intel_pt_bug(decoder);
  3244. case INTEL_PT_TRACESTOP:
  3245. decoder->pge = false;
  3246. decoder->continuous_period = false;
  3247. intel_pt_clear_tx_flags(decoder);
  3248. decoder->have_tma = false;
  3249. break;
  3250. case INTEL_PT_PSB:
  3251. decoder->state.psb_offset = decoder->pos;
  3252. decoder->psb_ip = 0;
  3253. decoder->last_ip = 0;
  3254. decoder->have_last_ip = true;
  3255. intel_pt_clear_stack(&decoder->stack);
  3256. err = intel_pt_walk_psb(decoder);
  3257. if (err)
  3258. return err;
  3259. decoder->state.type = INTEL_PT_PSB_EVT;
  3260. decoder->state.from_ip = decoder->psb_ip;
  3261. decoder->state.to_ip = 0;
  3262. return 0;
  3263. case INTEL_PT_TNT:
  3264. case INTEL_PT_PSBEND:
  3265. case INTEL_PT_VMCS:
  3266. case INTEL_PT_MNT:
  3267. case INTEL_PT_PAD:
  3268. case INTEL_PT_PTWRITE:
  3269. case INTEL_PT_PTWRITE_IP:
  3270. case INTEL_PT_EXSTOP:
  3271. case INTEL_PT_EXSTOP_IP:
  3272. case INTEL_PT_MWAIT:
  3273. case INTEL_PT_PWRE:
  3274. case INTEL_PT_PWRX:
  3275. case INTEL_PT_BBP:
  3276. case INTEL_PT_BIP:
  3277. case INTEL_PT_BEP:
  3278. case INTEL_PT_BEP_IP:
  3279. case INTEL_PT_CFE:
  3280. case INTEL_PT_CFE_IP:
  3281. case INTEL_PT_EVD:
  3282. default:
  3283. break;
  3284. }
  3285. }
  3286. }
  3287. static int intel_pt_sync_ip(struct intel_pt_decoder *decoder)
  3288. {
  3289. int err;
  3290. intel_pt_clear_fup_event(decoder);
  3291. decoder->overflow = false;
  3292. if (!decoder->branch_enable) {
  3293. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  3294. decoder->state.type = 0; /* Do not have a sample */
  3295. return 0;
  3296. }
  3297. intel_pt_log("Scanning for full IP\n");
  3298. err = intel_pt_walk_to_ip(decoder);
  3299. if (err || ((decoder->state.type & INTEL_PT_PSB_EVT) && !decoder->ip))
  3300. return err;
  3301. /* In hop mode, resample to get the to_ip as an "instruction" sample */
  3302. if (decoder->hop)
  3303. decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
  3304. else
  3305. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  3306. decoder->state.from_ip = 0;
  3307. decoder->state.to_ip = decoder->ip;
  3308. intel_pt_log_to("Setting IP", decoder->ip);
  3309. return 0;
  3310. }
  3311. static int intel_pt_part_psb(struct intel_pt_decoder *decoder)
  3312. {
  3313. const unsigned char *end = decoder->buf + decoder->len;
  3314. size_t i;
  3315. for (i = INTEL_PT_PSB_LEN - 1; i; i--) {
  3316. if (i > decoder->len)
  3317. continue;
  3318. if (!memcmp(end - i, INTEL_PT_PSB_STR, i))
  3319. return i;
  3320. }
  3321. return 0;
  3322. }
  3323. static int intel_pt_rest_psb(struct intel_pt_decoder *decoder, int part_psb)
  3324. {
  3325. size_t rest_psb = INTEL_PT_PSB_LEN - part_psb;
  3326. const char *psb = INTEL_PT_PSB_STR;
  3327. if (rest_psb > decoder->len ||
  3328. memcmp(decoder->buf, psb + part_psb, rest_psb))
  3329. return 0;
  3330. return rest_psb;
  3331. }
  3332. static int intel_pt_get_split_psb(struct intel_pt_decoder *decoder,
  3333. int part_psb)
  3334. {
  3335. int rest_psb, ret;
  3336. decoder->pos += decoder->len;
  3337. decoder->len = 0;
  3338. ret = intel_pt_get_next_data(decoder, false);
  3339. if (ret)
  3340. return ret;
  3341. rest_psb = intel_pt_rest_psb(decoder, part_psb);
  3342. if (!rest_psb)
  3343. return 0;
  3344. decoder->pos -= part_psb;
  3345. decoder->next_buf = decoder->buf + rest_psb;
  3346. decoder->next_len = decoder->len - rest_psb;
  3347. memcpy(decoder->temp_buf, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
  3348. decoder->buf = decoder->temp_buf;
  3349. decoder->len = INTEL_PT_PSB_LEN;
  3350. return 0;
  3351. }
  3352. static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder)
  3353. {
  3354. unsigned char *next;
  3355. int ret;
  3356. intel_pt_log("Scanning for PSB\n");
  3357. while (1) {
  3358. if (!decoder->len) {
  3359. ret = intel_pt_get_next_data(decoder, false);
  3360. if (ret)
  3361. return ret;
  3362. }
  3363. next = memmem(decoder->buf, decoder->len, INTEL_PT_PSB_STR,
  3364. INTEL_PT_PSB_LEN);
  3365. if (!next) {
  3366. int part_psb;
  3367. part_psb = intel_pt_part_psb(decoder);
  3368. if (part_psb) {
  3369. ret = intel_pt_get_split_psb(decoder, part_psb);
  3370. if (ret)
  3371. return ret;
  3372. } else {
  3373. decoder->pos += decoder->len;
  3374. decoder->len = 0;
  3375. }
  3376. continue;
  3377. }
  3378. decoder->pkt_step = next - decoder->buf;
  3379. return intel_pt_get_next_packet(decoder);
  3380. }
  3381. }
  3382. static int intel_pt_sync(struct intel_pt_decoder *decoder)
  3383. {
  3384. int err;
  3385. decoder->pge = false;
  3386. decoder->continuous_period = false;
  3387. decoder->have_last_ip = false;
  3388. decoder->last_ip = 0;
  3389. decoder->psb_ip = 0;
  3390. decoder->ip = 0;
  3391. intel_pt_clear_stack(&decoder->stack);
  3392. err = intel_pt_scan_for_psb(decoder);
  3393. if (err)
  3394. return err;
  3395. if (decoder->vm_time_correlation) {
  3396. decoder->in_psb = true;
  3397. if (!decoder->timestamp)
  3398. decoder->timestamp = 1;
  3399. decoder->state.type = 0;
  3400. decoder->pkt_state = INTEL_PT_STATE_VM_TIME_CORRELATION;
  3401. return 0;
  3402. }
  3403. decoder->have_last_ip = true;
  3404. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  3405. err = intel_pt_walk_psb(decoder);
  3406. if (err)
  3407. return err;
  3408. decoder->state.type = INTEL_PT_PSB_EVT; /* Only PSB sample */
  3409. decoder->state.from_ip = decoder->psb_ip;
  3410. decoder->state.to_ip = 0;
  3411. if (decoder->ip) {
  3412. /*
  3413. * In hop mode, resample to get the PSB FUP ip as an
  3414. * "instruction" sample.
  3415. */
  3416. if (decoder->hop)
  3417. decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
  3418. else
  3419. decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
  3420. }
  3421. return 0;
  3422. }
  3423. static uint64_t intel_pt_est_timestamp(struct intel_pt_decoder *decoder)
  3424. {
  3425. uint64_t est = decoder->sample_insn_cnt << 1;
  3426. if (!decoder->cbr || !decoder->max_non_turbo_ratio)
  3427. goto out;
  3428. est *= decoder->max_non_turbo_ratio;
  3429. est /= decoder->cbr;
  3430. out:
  3431. return decoder->sample_timestamp + est;
  3432. }
  3433. const struct intel_pt_state *intel_pt_decode(struct intel_pt_decoder *decoder)
  3434. {
  3435. int err;
  3436. do {
  3437. decoder->state.type = INTEL_PT_BRANCH;
  3438. decoder->state.flags = 0;
  3439. switch (decoder->pkt_state) {
  3440. case INTEL_PT_STATE_NO_PSB:
  3441. err = intel_pt_sync(decoder);
  3442. break;
  3443. case INTEL_PT_STATE_NO_IP:
  3444. decoder->have_last_ip = false;
  3445. decoder->last_ip = 0;
  3446. decoder->ip = 0;
  3447. fallthrough;
  3448. case INTEL_PT_STATE_ERR_RESYNC:
  3449. err = intel_pt_sync_ip(decoder);
  3450. break;
  3451. case INTEL_PT_STATE_IN_SYNC:
  3452. err = intel_pt_walk_trace(decoder);
  3453. break;
  3454. case INTEL_PT_STATE_TNT:
  3455. case INTEL_PT_STATE_TNT_CONT:
  3456. err = intel_pt_walk_tnt(decoder);
  3457. if (err == -EAGAIN)
  3458. err = intel_pt_walk_trace(decoder);
  3459. break;
  3460. case INTEL_PT_STATE_TIP:
  3461. case INTEL_PT_STATE_TIP_PGD:
  3462. err = intel_pt_walk_tip(decoder);
  3463. break;
  3464. case INTEL_PT_STATE_FUP:
  3465. err = intel_pt_walk_fup(decoder);
  3466. if (err == -EAGAIN)
  3467. err = intel_pt_walk_fup_tip(decoder);
  3468. break;
  3469. case INTEL_PT_STATE_FUP_NO_TIP:
  3470. err = intel_pt_walk_fup(decoder);
  3471. if (err == -EAGAIN)
  3472. err = intel_pt_walk_trace(decoder);
  3473. break;
  3474. case INTEL_PT_STATE_FUP_IN_PSB:
  3475. err = intel_pt_fup_in_psb(decoder);
  3476. break;
  3477. case INTEL_PT_STATE_RESAMPLE:
  3478. err = intel_pt_resample(decoder);
  3479. break;
  3480. case INTEL_PT_STATE_VM_TIME_CORRELATION:
  3481. err = intel_pt_vm_time_correlation(decoder);
  3482. break;
  3483. default:
  3484. err = intel_pt_bug(decoder);
  3485. break;
  3486. }
  3487. } while (err == -ENOLINK);
  3488. if (err) {
  3489. decoder->state.err = intel_pt_ext_err(err);
  3490. if (err != -EOVERFLOW)
  3491. decoder->state.from_ip = decoder->ip;
  3492. intel_pt_update_sample_time(decoder);
  3493. decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
  3494. intel_pt_set_nr(decoder);
  3495. } else {
  3496. decoder->state.err = 0;
  3497. if (decoder->cbr != decoder->cbr_seen) {
  3498. decoder->cbr_seen = decoder->cbr;
  3499. if (!decoder->state.type) {
  3500. decoder->state.from_ip = decoder->ip;
  3501. decoder->state.to_ip = 0;
  3502. }
  3503. decoder->state.type |= INTEL_PT_CBR_CHG;
  3504. decoder->state.cbr_payload = decoder->cbr_payload;
  3505. decoder->state.cbr = decoder->cbr;
  3506. }
  3507. if (intel_pt_sample_time(decoder->pkt_state)) {
  3508. intel_pt_update_sample_time(decoder);
  3509. if (decoder->sample_cyc) {
  3510. decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
  3511. decoder->state.flags |= INTEL_PT_SAMPLE_IPC;
  3512. decoder->sample_cyc = false;
  3513. }
  3514. }
  3515. /*
  3516. * When using only TSC/MTC to compute cycles, IPC can be
  3517. * sampled as soon as the cycle count changes.
  3518. */
  3519. if (!decoder->have_cyc)
  3520. decoder->state.flags |= INTEL_PT_SAMPLE_IPC;
  3521. }
  3522. /* Let PSB event always have TSC timestamp */
  3523. if ((decoder->state.type & INTEL_PT_PSB_EVT) && decoder->tsc_timestamp)
  3524. decoder->sample_timestamp = decoder->tsc_timestamp;
  3525. decoder->state.from_nr = decoder->nr;
  3526. decoder->state.to_nr = decoder->next_nr;
  3527. decoder->nr = decoder->next_nr;
  3528. decoder->state.timestamp = decoder->sample_timestamp;
  3529. decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
  3530. decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
  3531. decoder->state.tot_cyc_cnt = decoder->sample_tot_cyc_cnt;
  3532. return &decoder->state;
  3533. }
  3534. /**
  3535. * intel_pt_next_psb - move buffer pointer to the start of the next PSB packet.
  3536. * @buf: pointer to buffer pointer
  3537. * @len: size of buffer
  3538. *
  3539. * Updates the buffer pointer to point to the start of the next PSB packet if
  3540. * there is one, otherwise the buffer pointer is unchanged. If @buf is updated,
  3541. * @len is adjusted accordingly.
  3542. *
  3543. * Return: %true if a PSB packet is found, %false otherwise.
  3544. */
  3545. static bool intel_pt_next_psb(unsigned char **buf, size_t *len)
  3546. {
  3547. unsigned char *next;
  3548. next = memmem(*buf, *len, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
  3549. if (next) {
  3550. *len -= next - *buf;
  3551. *buf = next;
  3552. return true;
  3553. }
  3554. return false;
  3555. }
  3556. /**
  3557. * intel_pt_step_psb - move buffer pointer to the start of the following PSB
  3558. * packet.
  3559. * @buf: pointer to buffer pointer
  3560. * @len: size of buffer
  3561. *
  3562. * Updates the buffer pointer to point to the start of the following PSB packet
  3563. * (skipping the PSB at @buf itself) if there is one, otherwise the buffer
  3564. * pointer is unchanged. If @buf is updated, @len is adjusted accordingly.
  3565. *
  3566. * Return: %true if a PSB packet is found, %false otherwise.
  3567. */
  3568. static bool intel_pt_step_psb(unsigned char **buf, size_t *len)
  3569. {
  3570. unsigned char *next;
  3571. if (!*len)
  3572. return false;
  3573. next = memmem(*buf + 1, *len - 1, INTEL_PT_PSB_STR, INTEL_PT_PSB_LEN);
  3574. if (next) {
  3575. *len -= next - *buf;
  3576. *buf = next;
  3577. return true;
  3578. }
  3579. return false;
  3580. }
  3581. /**
  3582. * intel_pt_last_psb - find the last PSB packet in a buffer.
  3583. * @buf: buffer
  3584. * @len: size of buffer
  3585. *
  3586. * This function finds the last PSB in a buffer.
  3587. *
  3588. * Return: A pointer to the last PSB in @buf if found, %NULL otherwise.
  3589. */
  3590. static unsigned char *intel_pt_last_psb(unsigned char *buf, size_t len)
  3591. {
  3592. const char *n = INTEL_PT_PSB_STR;
  3593. unsigned char *p;
  3594. size_t k;
  3595. if (len < INTEL_PT_PSB_LEN)
  3596. return NULL;
  3597. k = len - INTEL_PT_PSB_LEN + 1;
  3598. while (1) {
  3599. p = memrchr(buf, n[0], k);
  3600. if (!p)
  3601. return NULL;
  3602. if (!memcmp(p + 1, n + 1, INTEL_PT_PSB_LEN - 1))
  3603. return p;
  3604. k = p - buf;
  3605. if (!k)
  3606. return NULL;
  3607. }
  3608. }
  3609. /**
  3610. * intel_pt_next_tsc - find and return next TSC.
  3611. * @buf: buffer
  3612. * @len: size of buffer
  3613. * @tsc: TSC value returned
  3614. * @rem: returns remaining size when TSC is found
  3615. *
  3616. * Find a TSC packet in @buf and return the TSC value. This function assumes
  3617. * that @buf starts at a PSB and that PSB+ will contain TSC and so stops if a
  3618. * PSBEND packet is found.
  3619. *
  3620. * Return: %true if TSC is found, false otherwise.
  3621. */
  3622. static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
  3623. size_t *rem)
  3624. {
  3625. enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
  3626. struct intel_pt_pkt packet;
  3627. int ret;
  3628. while (len) {
  3629. ret = intel_pt_get_packet(buf, len, &packet, &ctx);
  3630. if (ret <= 0)
  3631. return false;
  3632. if (packet.type == INTEL_PT_TSC) {
  3633. *tsc = packet.payload;
  3634. *rem = len;
  3635. return true;
  3636. }
  3637. if (packet.type == INTEL_PT_PSBEND)
  3638. return false;
  3639. buf += ret;
  3640. len -= ret;
  3641. }
  3642. return false;
  3643. }
  3644. /**
  3645. * intel_pt_tsc_cmp - compare 7-byte TSCs.
  3646. * @tsc1: first TSC to compare
  3647. * @tsc2: second TSC to compare
  3648. *
  3649. * This function compares 7-byte TSC values allowing for the possibility that
  3650. * TSC wrapped around. Generally it is not possible to know if TSC has wrapped
  3651. * around so for that purpose this function assumes the absolute difference is
  3652. * less than half the maximum difference.
  3653. *
  3654. * Return: %-1 if @tsc1 is before @tsc2, %0 if @tsc1 == @tsc2, %1 if @tsc1 is
  3655. * after @tsc2.
  3656. */
  3657. static int intel_pt_tsc_cmp(uint64_t tsc1, uint64_t tsc2)
  3658. {
  3659. const uint64_t halfway = (1ULL << 55);
  3660. if (tsc1 == tsc2)
  3661. return 0;
  3662. if (tsc1 < tsc2) {
  3663. if (tsc2 - tsc1 < halfway)
  3664. return -1;
  3665. else
  3666. return 1;
  3667. } else {
  3668. if (tsc1 - tsc2 < halfway)
  3669. return 1;
  3670. else
  3671. return -1;
  3672. }
  3673. }
  3674. #define MAX_PADDING (PERF_AUXTRACE_RECORD_ALIGNMENT - 1)
  3675. /**
  3676. * adj_for_padding - adjust overlap to account for padding.
  3677. * @buf_b: second buffer
  3678. * @buf_a: first buffer
  3679. * @len_a: size of first buffer
  3680. *
  3681. * @buf_a might have up to 7 bytes of padding appended. Adjust the overlap
  3682. * accordingly.
  3683. *
  3684. * Return: A pointer into @buf_b from where non-overlapped data starts
  3685. */
  3686. static unsigned char *adj_for_padding(unsigned char *buf_b,
  3687. unsigned char *buf_a, size_t len_a)
  3688. {
  3689. unsigned char *p = buf_b - MAX_PADDING;
  3690. unsigned char *q = buf_a + len_a - MAX_PADDING;
  3691. int i;
  3692. for (i = MAX_PADDING; i; i--, p++, q++) {
  3693. if (*p != *q)
  3694. break;
  3695. }
  3696. return p;
  3697. }
  3698. /**
  3699. * intel_pt_find_overlap_tsc - determine start of non-overlapped trace data
  3700. * using TSC.
  3701. * @buf_a: first buffer
  3702. * @len_a: size of first buffer
  3703. * @buf_b: second buffer
  3704. * @len_b: size of second buffer
  3705. * @consecutive: returns true if there is data in buf_b that is consecutive
  3706. * to buf_a
  3707. * @ooo_tsc: out-of-order TSC due to VM TSC offset / scaling
  3708. *
  3709. * If the trace contains TSC we can look at the last TSC of @buf_a and the
  3710. * first TSC of @buf_b in order to determine if the buffers overlap, and then
  3711. * walk forward in @buf_b until a later TSC is found. A precondition is that
  3712. * @buf_a and @buf_b are positioned at a PSB.
  3713. *
  3714. * Return: A pointer into @buf_b from where non-overlapped data starts, or
  3715. * @buf_b + @len_b if there is no non-overlapped data.
  3716. */
  3717. static unsigned char *intel_pt_find_overlap_tsc(unsigned char *buf_a,
  3718. size_t len_a,
  3719. unsigned char *buf_b,
  3720. size_t len_b, bool *consecutive,
  3721. bool ooo_tsc)
  3722. {
  3723. uint64_t tsc_a, tsc_b;
  3724. unsigned char *p;
  3725. size_t len, rem_a, rem_b;
  3726. p = intel_pt_last_psb(buf_a, len_a);
  3727. if (!p)
  3728. return buf_b; /* No PSB in buf_a => no overlap */
  3729. len = len_a - (p - buf_a);
  3730. if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a)) {
  3731. /* The last PSB+ in buf_a is incomplete, so go back one more */
  3732. len_a -= len;
  3733. p = intel_pt_last_psb(buf_a, len_a);
  3734. if (!p)
  3735. return buf_b; /* No full PSB+ => assume no overlap */
  3736. len = len_a - (p - buf_a);
  3737. if (!intel_pt_next_tsc(p, len, &tsc_a, &rem_a))
  3738. return buf_b; /* No TSC in buf_a => assume no overlap */
  3739. }
  3740. while (1) {
  3741. /* Ignore PSB+ with no TSC */
  3742. if (intel_pt_next_tsc(buf_b, len_b, &tsc_b, &rem_b)) {
  3743. int cmp = intel_pt_tsc_cmp(tsc_a, tsc_b);
  3744. /* Same TSC, so buffers are consecutive */
  3745. if (!cmp && rem_b >= rem_a) {
  3746. unsigned char *start;
  3747. *consecutive = true;
  3748. start = buf_b + len_b - (rem_b - rem_a);
  3749. return adj_for_padding(start, buf_a, len_a);
  3750. }
  3751. if (cmp < 0 && !ooo_tsc)
  3752. return buf_b; /* tsc_a < tsc_b => no overlap */
  3753. }
  3754. if (!intel_pt_step_psb(&buf_b, &len_b))
  3755. return buf_b + len_b; /* No PSB in buf_b => no data */
  3756. }
  3757. }
  3758. /**
  3759. * intel_pt_find_overlap - determine start of non-overlapped trace data.
  3760. * @buf_a: first buffer
  3761. * @len_a: size of first buffer
  3762. * @buf_b: second buffer
  3763. * @len_b: size of second buffer
  3764. * @have_tsc: can use TSC packets to detect overlap
  3765. * @consecutive: returns true if there is data in buf_b that is consecutive
  3766. * to buf_a
  3767. * @ooo_tsc: out-of-order TSC due to VM TSC offset / scaling
  3768. *
  3769. * When trace samples or snapshots are recorded there is the possibility that
  3770. * the data overlaps. Note that, for the purposes of decoding, data is only
  3771. * useful if it begins with a PSB packet.
  3772. *
  3773. * Return: A pointer into @buf_b from where non-overlapped data starts, or
  3774. * @buf_b + @len_b if there is no non-overlapped data.
  3775. */
  3776. unsigned char *intel_pt_find_overlap(unsigned char *buf_a, size_t len_a,
  3777. unsigned char *buf_b, size_t len_b,
  3778. bool have_tsc, bool *consecutive,
  3779. bool ooo_tsc)
  3780. {
  3781. unsigned char *found;
  3782. /* Buffer 'b' must start at PSB so throw away everything before that */
  3783. if (!intel_pt_next_psb(&buf_b, &len_b))
  3784. return buf_b + len_b; /* No PSB */
  3785. if (!intel_pt_next_psb(&buf_a, &len_a))
  3786. return buf_b; /* No overlap */
  3787. if (have_tsc) {
  3788. found = intel_pt_find_overlap_tsc(buf_a, len_a, buf_b, len_b,
  3789. consecutive, ooo_tsc);
  3790. if (found)
  3791. return found;
  3792. }
  3793. /*
  3794. * Buffer 'b' cannot end within buffer 'a' so, for comparison purposes,
  3795. * we can ignore the first part of buffer 'a'.
  3796. */
  3797. while (len_b < len_a) {
  3798. if (!intel_pt_step_psb(&buf_a, &len_a))
  3799. return buf_b; /* No overlap */
  3800. }
  3801. /* Now len_b >= len_a */
  3802. while (1) {
  3803. /* Potential overlap so check the bytes */
  3804. found = memmem(buf_a, len_a, buf_b, len_a);
  3805. if (found) {
  3806. *consecutive = true;
  3807. return adj_for_padding(buf_b + len_a, buf_a, len_a);
  3808. }
  3809. /* Try again at next PSB in buffer 'a' */
  3810. if (!intel_pt_step_psb(&buf_a, &len_a))
  3811. return buf_b; /* No overlap */
  3812. }
  3813. }
  3814. /**
  3815. * struct fast_forward_data - data used by intel_pt_ff_cb().
  3816. * @timestamp: timestamp to fast forward towards
  3817. * @buf_timestamp: buffer timestamp of last buffer with trace data earlier than
  3818. * the fast forward timestamp.
  3819. */
  3820. struct fast_forward_data {
  3821. uint64_t timestamp;
  3822. uint64_t buf_timestamp;
  3823. };
  3824. /**
  3825. * intel_pt_ff_cb - fast forward lookahead callback.
  3826. * @buffer: Intel PT trace buffer
  3827. * @data: opaque pointer to fast forward data (struct fast_forward_data)
  3828. *
  3829. * Determine if @buffer trace is past the fast forward timestamp.
  3830. *
  3831. * Return: 1 (stop lookahead) if @buffer trace is past the fast forward
  3832. * timestamp, and 0 otherwise.
  3833. */
  3834. static int intel_pt_ff_cb(struct intel_pt_buffer *buffer, void *data)
  3835. {
  3836. struct fast_forward_data *d = data;
  3837. unsigned char *buf;
  3838. uint64_t tsc;
  3839. size_t rem;
  3840. size_t len;
  3841. buf = (unsigned char *)buffer->buf;
  3842. len = buffer->len;
  3843. if (!intel_pt_next_psb(&buf, &len) ||
  3844. !intel_pt_next_tsc(buf, len, &tsc, &rem))
  3845. return 0;
  3846. tsc = intel_pt_8b_tsc(tsc, buffer->ref_timestamp);
  3847. intel_pt_log("Buffer 1st timestamp " x64_fmt " ref timestamp " x64_fmt "\n",
  3848. tsc, buffer->ref_timestamp);
  3849. /*
  3850. * If the buffer contains a timestamp earlier that the fast forward
  3851. * timestamp, then record it, else stop.
  3852. */
  3853. if (tsc < d->timestamp)
  3854. d->buf_timestamp = buffer->ref_timestamp;
  3855. else
  3856. return 1;
  3857. return 0;
  3858. }
  3859. /**
  3860. * intel_pt_fast_forward - reposition decoder forwards.
  3861. * @decoder: Intel PT decoder
  3862. * @timestamp: timestamp to fast forward towards
  3863. *
  3864. * Reposition decoder at the last PSB with a timestamp earlier than @timestamp.
  3865. *
  3866. * Return: 0 on success or negative error code on failure.
  3867. */
  3868. int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp)
  3869. {
  3870. struct fast_forward_data d = { .timestamp = timestamp };
  3871. unsigned char *buf;
  3872. size_t len;
  3873. int err;
  3874. intel_pt_log("Fast forward towards timestamp " x64_fmt "\n", timestamp);
  3875. /* Find buffer timestamp of buffer to fast forward to */
  3876. err = decoder->lookahead(decoder->data, intel_pt_ff_cb, &d);
  3877. if (err < 0)
  3878. return err;
  3879. /* Walk to buffer with same buffer timestamp */
  3880. if (d.buf_timestamp) {
  3881. do {
  3882. decoder->pos += decoder->len;
  3883. decoder->len = 0;
  3884. err = intel_pt_get_next_data(decoder, true);
  3885. /* -ENOLINK means non-consecutive trace */
  3886. if (err && err != -ENOLINK)
  3887. return err;
  3888. } while (decoder->buf_timestamp != d.buf_timestamp);
  3889. }
  3890. if (!decoder->buf)
  3891. return 0;
  3892. buf = (unsigned char *)decoder->buf;
  3893. len = decoder->len;
  3894. if (!intel_pt_next_psb(&buf, &len))
  3895. return 0;
  3896. /*
  3897. * Walk PSBs while the PSB timestamp is less than the fast forward
  3898. * timestamp.
  3899. */
  3900. do {
  3901. uint64_t tsc;
  3902. size_t rem;
  3903. if (!intel_pt_next_tsc(buf, len, &tsc, &rem))
  3904. break;
  3905. tsc = intel_pt_8b_tsc(tsc, decoder->buf_timestamp);
  3906. /*
  3907. * A TSC packet can slip past MTC packets but, after fast
  3908. * forward, decoding starts at the TSC timestamp. That means
  3909. * the timestamps may not be exactly the same as the timestamps
  3910. * that would have been decoded without fast forward.
  3911. */
  3912. if (tsc < timestamp) {
  3913. intel_pt_log("Fast forward to next PSB timestamp " x64_fmt "\n", tsc);
  3914. decoder->pos += decoder->len - len;
  3915. decoder->buf = buf;
  3916. decoder->len = len;
  3917. intel_pt_reposition(decoder);
  3918. } else {
  3919. break;
  3920. }
  3921. } while (intel_pt_step_psb(&buf, &len));
  3922. return 0;
  3923. }