perfmon.c 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762
  1. /*
  2. * This file implements the perfmon-2 subsystem which is used
  3. * to program the IA-64 Performance Monitoring Unit (PMU).
  4. *
  5. * The initial version of perfmon.c was written by
  6. * Ganesh Venkitachalam, IBM Corp.
  7. *
  8. * Then it was modified for perfmon-1.x by Stephane Eranian and
  9. * David Mosberger, Hewlett Packard Co.
  10. *
  11. * Version Perfmon-2.x is a rewrite of perfmon-1.x
  12. * by Stephane Eranian, Hewlett Packard Co.
  13. *
  14. * Copyright (C) 1999-2005 Hewlett Packard Co
  15. * Stephane Eranian <eranian@hpl.hp.com>
  16. * David Mosberger-Tang <davidm@hpl.hp.com>
  17. *
  18. * More information about perfmon available at:
  19. * http://www.hpl.hp.com/research/linux/perfmon
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/sched/task.h>
  25. #include <linux/sched/task_stack.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/init.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/mm.h>
  32. #include <linux/sysctl.h>
  33. #include <linux/list.h>
  34. #include <linux/file.h>
  35. #include <linux/poll.h>
  36. #include <linux/vfs.h>
  37. #include <linux/smp.h>
  38. #include <linux/pagemap.h>
  39. #include <linux/mount.h>
  40. #include <linux/bitops.h>
  41. #include <linux/capability.h>
  42. #include <linux/rcupdate.h>
  43. #include <linux/completion.h>
  44. #include <linux/tracehook.h>
  45. #include <linux/slab.h>
  46. #include <linux/cpu.h>
  47. #include <asm/errno.h>
  48. #include <asm/intrinsics.h>
  49. #include <asm/page.h>
  50. #include <asm/perfmon.h>
  51. #include <asm/processor.h>
  52. #include <asm/signal.h>
  53. #include <linux/uaccess.h>
  54. #include <asm/delay.h>
  55. #ifdef CONFIG_PERFMON
  56. /*
  57. * perfmon context state
  58. */
  59. #define PFM_CTX_UNLOADED 1 /* context is not loaded onto any task */
  60. #define PFM_CTX_LOADED 2 /* context is loaded onto a task */
  61. #define PFM_CTX_MASKED 3 /* context is loaded but monitoring is masked due to overflow */
  62. #define PFM_CTX_ZOMBIE 4 /* owner of the context is closing it */
  63. #define PFM_INVALID_ACTIVATION (~0UL)
  64. #define PFM_NUM_PMC_REGS 64 /* PMC save area for ctxsw */
  65. #define PFM_NUM_PMD_REGS 64 /* PMD save area for ctxsw */
  66. /*
  67. * depth of message queue
  68. */
  69. #define PFM_MAX_MSGS 32
  70. #define PFM_CTXQ_EMPTY(g) ((g)->ctx_msgq_head == (g)->ctx_msgq_tail)
  71. /*
  72. * type of a PMU register (bitmask).
  73. * bitmask structure:
  74. * bit0 : register implemented
  75. * bit1 : end marker
  76. * bit2-3 : reserved
  77. * bit4 : pmc has pmc.pm
  78. * bit5 : pmc controls a counter (has pmc.oi), pmd is used as counter
  79. * bit6-7 : register type
  80. * bit8-31: reserved
  81. */
  82. #define PFM_REG_NOTIMPL 0x0 /* not implemented at all */
  83. #define PFM_REG_IMPL 0x1 /* register implemented */
  84. #define PFM_REG_END 0x2 /* end marker */
  85. #define PFM_REG_MONITOR (0x1<<4|PFM_REG_IMPL) /* a PMC with a pmc.pm field only */
  86. #define PFM_REG_COUNTING (0x2<<4|PFM_REG_MONITOR) /* a monitor + pmc.oi+ PMD used as a counter */
  87. #define PFM_REG_CONTROL (0x4<<4|PFM_REG_IMPL) /* PMU control register */
  88. #define PFM_REG_CONFIG (0x8<<4|PFM_REG_IMPL) /* configuration register */
  89. #define PFM_REG_BUFFER (0xc<<4|PFM_REG_IMPL) /* PMD used as buffer */
  90. #define PMC_IS_LAST(i) (pmu_conf->pmc_desc[i].type & PFM_REG_END)
  91. #define PMD_IS_LAST(i) (pmu_conf->pmd_desc[i].type & PFM_REG_END)
  92. #define PMC_OVFL_NOTIFY(ctx, i) ((ctx)->ctx_pmds[i].flags & PFM_REGFL_OVFL_NOTIFY)
  93. /* i assumed unsigned */
  94. #define PMC_IS_IMPL(i) (i< PMU_MAX_PMCS && (pmu_conf->pmc_desc[i].type & PFM_REG_IMPL))
  95. #define PMD_IS_IMPL(i) (i< PMU_MAX_PMDS && (pmu_conf->pmd_desc[i].type & PFM_REG_IMPL))
  96. /* XXX: these assume that register i is implemented */
  97. #define PMD_IS_COUNTING(i) ((pmu_conf->pmd_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
  98. #define PMC_IS_COUNTING(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
  99. #define PMC_IS_MONITOR(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_MONITOR) == PFM_REG_MONITOR)
  100. #define PMC_IS_CONTROL(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_CONTROL) == PFM_REG_CONTROL)
  101. #define PMC_DFL_VAL(i) pmu_conf->pmc_desc[i].default_value
  102. #define PMC_RSVD_MASK(i) pmu_conf->pmc_desc[i].reserved_mask
  103. #define PMD_PMD_DEP(i) pmu_conf->pmd_desc[i].dep_pmd[0]
  104. #define PMC_PMD_DEP(i) pmu_conf->pmc_desc[i].dep_pmd[0]
  105. #define PFM_NUM_IBRS IA64_NUM_DBG_REGS
  106. #define PFM_NUM_DBRS IA64_NUM_DBG_REGS
  107. #define CTX_OVFL_NOBLOCK(c) ((c)->ctx_fl_block == 0)
  108. #define CTX_HAS_SMPL(c) ((c)->ctx_fl_is_sampling)
  109. #define PFM_CTX_TASK(h) (h)->ctx_task
  110. #define PMU_PMC_OI 5 /* position of pmc.oi bit */
  111. /* XXX: does not support more than 64 PMDs */
  112. #define CTX_USED_PMD(ctx, mask) (ctx)->ctx_used_pmds[0] |= (mask)
  113. #define CTX_IS_USED_PMD(ctx, c) (((ctx)->ctx_used_pmds[0] & (1UL << (c))) != 0UL)
  114. #define CTX_USED_MONITOR(ctx, mask) (ctx)->ctx_used_monitors[0] |= (mask)
  115. #define CTX_USED_IBR(ctx,n) (ctx)->ctx_used_ibrs[(n)>>6] |= 1UL<< ((n) % 64)
  116. #define CTX_USED_DBR(ctx,n) (ctx)->ctx_used_dbrs[(n)>>6] |= 1UL<< ((n) % 64)
  117. #define CTX_USES_DBREGS(ctx) (((pfm_context_t *)(ctx))->ctx_fl_using_dbreg==1)
  118. #define PFM_CODE_RR 0 /* requesting code range restriction */
  119. #define PFM_DATA_RR 1 /* requestion data range restriction */
  120. #define PFM_CPUINFO_CLEAR(v) pfm_get_cpu_var(pfm_syst_info) &= ~(v)
  121. #define PFM_CPUINFO_SET(v) pfm_get_cpu_var(pfm_syst_info) |= (v)
  122. #define PFM_CPUINFO_GET() pfm_get_cpu_var(pfm_syst_info)
  123. #define RDEP(x) (1UL<<(x))
  124. /*
  125. * context protection macros
  126. * in SMP:
  127. * - we need to protect against CPU concurrency (spin_lock)
  128. * - we need to protect against PMU overflow interrupts (local_irq_disable)
  129. * in UP:
  130. * - we need to protect against PMU overflow interrupts (local_irq_disable)
  131. *
  132. * spin_lock_irqsave()/spin_unlock_irqrestore():
  133. * in SMP: local_irq_disable + spin_lock
  134. * in UP : local_irq_disable
  135. *
  136. * spin_lock()/spin_lock():
  137. * in UP : removed automatically
  138. * in SMP: protect against context accesses from other CPU. interrupts
  139. * are not masked. This is useful for the PMU interrupt handler
  140. * because we know we will not get PMU concurrency in that code.
  141. */
  142. #define PROTECT_CTX(c, f) \
  143. do { \
  144. DPRINT(("spinlock_irq_save ctx %p by [%d]\n", c, task_pid_nr(current))); \
  145. spin_lock_irqsave(&(c)->ctx_lock, f); \
  146. DPRINT(("spinlocked ctx %p by [%d]\n", c, task_pid_nr(current))); \
  147. } while(0)
  148. #define UNPROTECT_CTX(c, f) \
  149. do { \
  150. DPRINT(("spinlock_irq_restore ctx %p by [%d]\n", c, task_pid_nr(current))); \
  151. spin_unlock_irqrestore(&(c)->ctx_lock, f); \
  152. } while(0)
  153. #define PROTECT_CTX_NOPRINT(c, f) \
  154. do { \
  155. spin_lock_irqsave(&(c)->ctx_lock, f); \
  156. } while(0)
  157. #define UNPROTECT_CTX_NOPRINT(c, f) \
  158. do { \
  159. spin_unlock_irqrestore(&(c)->ctx_lock, f); \
  160. } while(0)
  161. #define PROTECT_CTX_NOIRQ(c) \
  162. do { \
  163. spin_lock(&(c)->ctx_lock); \
  164. } while(0)
  165. #define UNPROTECT_CTX_NOIRQ(c) \
  166. do { \
  167. spin_unlock(&(c)->ctx_lock); \
  168. } while(0)
  169. #ifdef CONFIG_SMP
  170. #define GET_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)
  171. #define INC_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)++
  172. #define SET_ACTIVATION(c) (c)->ctx_last_activation = GET_ACTIVATION()
  173. #else /* !CONFIG_SMP */
  174. #define SET_ACTIVATION(t) do {} while(0)
  175. #define GET_ACTIVATION(t) do {} while(0)
  176. #define INC_ACTIVATION(t) do {} while(0)
  177. #endif /* CONFIG_SMP */
  178. #define SET_PMU_OWNER(t, c) do { pfm_get_cpu_var(pmu_owner) = (t); pfm_get_cpu_var(pmu_ctx) = (c); } while(0)
  179. #define GET_PMU_OWNER() pfm_get_cpu_var(pmu_owner)
  180. #define GET_PMU_CTX() pfm_get_cpu_var(pmu_ctx)
  181. #define LOCK_PFS(g) spin_lock_irqsave(&pfm_sessions.pfs_lock, g)
  182. #define UNLOCK_PFS(g) spin_unlock_irqrestore(&pfm_sessions.pfs_lock, g)
  183. #define PFM_REG_RETFLAG_SET(flags, val) do { flags &= ~PFM_REG_RETFL_MASK; flags |= (val); } while(0)
  184. /*
  185. * cmp0 must be the value of pmc0
  186. */
  187. #define PMC0_HAS_OVFL(cmp0) (cmp0 & ~0x1UL)
  188. #define PFMFS_MAGIC 0xa0b4d889
  189. /*
  190. * debugging
  191. */
  192. #define PFM_DEBUGGING 1
  193. #ifdef PFM_DEBUGGING
  194. #define DPRINT(a) \
  195. do { \
  196. if (unlikely(pfm_sysctl.debug >0)) { printk("%s.%d: CPU%d [%d] ", __func__, __LINE__, smp_processor_id(), task_pid_nr(current)); printk a; } \
  197. } while (0)
  198. #define DPRINT_ovfl(a) \
  199. do { \
  200. if (unlikely(pfm_sysctl.debug > 0 && pfm_sysctl.debug_ovfl >0)) { printk("%s.%d: CPU%d [%d] ", __func__, __LINE__, smp_processor_id(), task_pid_nr(current)); printk a; } \
  201. } while (0)
  202. #endif
  203. /*
  204. * 64-bit software counter structure
  205. *
  206. * the next_reset_type is applied to the next call to pfm_reset_regs()
  207. */
  208. typedef struct {
  209. unsigned long val; /* virtual 64bit counter value */
  210. unsigned long lval; /* last reset value */
  211. unsigned long long_reset; /* reset value on sampling overflow */
  212. unsigned long short_reset; /* reset value on overflow */
  213. unsigned long reset_pmds[4]; /* which other pmds to reset when this counter overflows */
  214. unsigned long smpl_pmds[4]; /* which pmds are accessed when counter overflow */
  215. unsigned long seed; /* seed for random-number generator */
  216. unsigned long mask; /* mask for random-number generator */
  217. unsigned int flags; /* notify/do not notify */
  218. unsigned long eventid; /* overflow event identifier */
  219. } pfm_counter_t;
  220. /*
  221. * context flags
  222. */
  223. typedef struct {
  224. unsigned int block:1; /* when 1, task will blocked on user notifications */
  225. unsigned int system:1; /* do system wide monitoring */
  226. unsigned int using_dbreg:1; /* using range restrictions (debug registers) */
  227. unsigned int is_sampling:1; /* true if using a custom format */
  228. unsigned int excl_idle:1; /* exclude idle task in system wide session */
  229. unsigned int going_zombie:1; /* context is zombie (MASKED+blocking) */
  230. unsigned int trap_reason:2; /* reason for going into pfm_handle_work() */
  231. unsigned int no_msg:1; /* no message sent on overflow */
  232. unsigned int can_restart:1; /* allowed to issue a PFM_RESTART */
  233. unsigned int reserved:22;
  234. } pfm_context_flags_t;
  235. #define PFM_TRAP_REASON_NONE 0x0 /* default value */
  236. #define PFM_TRAP_REASON_BLOCK 0x1 /* we need to block on overflow */
  237. #define PFM_TRAP_REASON_RESET 0x2 /* we need to reset PMDs */
  238. /*
  239. * perfmon context: encapsulates all the state of a monitoring session
  240. */
  241. typedef struct pfm_context {
  242. spinlock_t ctx_lock; /* context protection */
  243. pfm_context_flags_t ctx_flags; /* bitmask of flags (block reason incl.) */
  244. unsigned int ctx_state; /* state: active/inactive (no bitfield) */
  245. struct task_struct *ctx_task; /* task to which context is attached */
  246. unsigned long ctx_ovfl_regs[4]; /* which registers overflowed (notification) */
  247. struct completion ctx_restart_done; /* use for blocking notification mode */
  248. unsigned long ctx_used_pmds[4]; /* bitmask of PMD used */
  249. unsigned long ctx_all_pmds[4]; /* bitmask of all accessible PMDs */
  250. unsigned long ctx_reload_pmds[4]; /* bitmask of force reload PMD on ctxsw in */
  251. unsigned long ctx_all_pmcs[4]; /* bitmask of all accessible PMCs */
  252. unsigned long ctx_reload_pmcs[4]; /* bitmask of force reload PMC on ctxsw in */
  253. unsigned long ctx_used_monitors[4]; /* bitmask of monitor PMC being used */
  254. unsigned long ctx_pmcs[PFM_NUM_PMC_REGS]; /* saved copies of PMC values */
  255. unsigned int ctx_used_ibrs[1]; /* bitmask of used IBR (speedup ctxsw in) */
  256. unsigned int ctx_used_dbrs[1]; /* bitmask of used DBR (speedup ctxsw in) */
  257. unsigned long ctx_dbrs[IA64_NUM_DBG_REGS]; /* DBR values (cache) when not loaded */
  258. unsigned long ctx_ibrs[IA64_NUM_DBG_REGS]; /* IBR values (cache) when not loaded */
  259. pfm_counter_t ctx_pmds[PFM_NUM_PMD_REGS]; /* software state for PMDS */
  260. unsigned long th_pmcs[PFM_NUM_PMC_REGS]; /* PMC thread save state */
  261. unsigned long th_pmds[PFM_NUM_PMD_REGS]; /* PMD thread save state */
  262. unsigned long ctx_saved_psr_up; /* only contains psr.up value */
  263. unsigned long ctx_last_activation; /* context last activation number for last_cpu */
  264. unsigned int ctx_last_cpu; /* CPU id of current or last CPU used (SMP only) */
  265. unsigned int ctx_cpu; /* cpu to which perfmon is applied (system wide) */
  266. int ctx_fd; /* file descriptor used my this context */
  267. pfm_ovfl_arg_t ctx_ovfl_arg; /* argument to custom buffer format handler */
  268. pfm_buffer_fmt_t *ctx_buf_fmt; /* buffer format callbacks */
  269. void *ctx_smpl_hdr; /* points to sampling buffer header kernel vaddr */
  270. unsigned long ctx_smpl_size; /* size of sampling buffer */
  271. void *ctx_smpl_vaddr; /* user level virtual address of smpl buffer */
  272. wait_queue_head_t ctx_msgq_wait;
  273. pfm_msg_t ctx_msgq[PFM_MAX_MSGS];
  274. int ctx_msgq_head;
  275. int ctx_msgq_tail;
  276. struct fasync_struct *ctx_async_queue;
  277. wait_queue_head_t ctx_zombieq; /* termination cleanup wait queue */
  278. } pfm_context_t;
  279. /*
  280. * magic number used to verify that structure is really
  281. * a perfmon context
  282. */
  283. #define PFM_IS_FILE(f) ((f)->f_op == &pfm_file_ops)
  284. #define PFM_GET_CTX(t) ((pfm_context_t *)(t)->thread.pfm_context)
  285. #ifdef CONFIG_SMP
  286. #define SET_LAST_CPU(ctx, v) (ctx)->ctx_last_cpu = (v)
  287. #define GET_LAST_CPU(ctx) (ctx)->ctx_last_cpu
  288. #else
  289. #define SET_LAST_CPU(ctx, v) do {} while(0)
  290. #define GET_LAST_CPU(ctx) do {} while(0)
  291. #endif
  292. #define ctx_fl_block ctx_flags.block
  293. #define ctx_fl_system ctx_flags.system
  294. #define ctx_fl_using_dbreg ctx_flags.using_dbreg
  295. #define ctx_fl_is_sampling ctx_flags.is_sampling
  296. #define ctx_fl_excl_idle ctx_flags.excl_idle
  297. #define ctx_fl_going_zombie ctx_flags.going_zombie
  298. #define ctx_fl_trap_reason ctx_flags.trap_reason
  299. #define ctx_fl_no_msg ctx_flags.no_msg
  300. #define ctx_fl_can_restart ctx_flags.can_restart
  301. #define PFM_SET_WORK_PENDING(t, v) do { (t)->thread.pfm_needs_checking = v; } while(0);
  302. #define PFM_GET_WORK_PENDING(t) (t)->thread.pfm_needs_checking
  303. /*
  304. * global information about all sessions
  305. * mostly used to synchronize between system wide and per-process
  306. */
  307. typedef struct {
  308. spinlock_t pfs_lock; /* lock the structure */
  309. unsigned int pfs_task_sessions; /* number of per task sessions */
  310. unsigned int pfs_sys_sessions; /* number of per system wide sessions */
  311. unsigned int pfs_sys_use_dbregs; /* incremented when a system wide session uses debug regs */
  312. unsigned int pfs_ptrace_use_dbregs; /* incremented when a process uses debug regs */
  313. struct task_struct *pfs_sys_session[NR_CPUS]; /* point to task owning a system-wide session */
  314. } pfm_session_t;
  315. /*
  316. * information about a PMC or PMD.
  317. * dep_pmd[]: a bitmask of dependent PMD registers
  318. * dep_pmc[]: a bitmask of dependent PMC registers
  319. */
  320. typedef int (*pfm_reg_check_t)(struct task_struct *task, pfm_context_t *ctx, unsigned int cnum, unsigned long *val, struct pt_regs *regs);
  321. typedef struct {
  322. unsigned int type;
  323. int pm_pos;
  324. unsigned long default_value; /* power-on default value */
  325. unsigned long reserved_mask; /* bitmask of reserved bits */
  326. pfm_reg_check_t read_check;
  327. pfm_reg_check_t write_check;
  328. unsigned long dep_pmd[4];
  329. unsigned long dep_pmc[4];
  330. } pfm_reg_desc_t;
  331. /* assume cnum is a valid monitor */
  332. #define PMC_PM(cnum, val) (((val) >> (pmu_conf->pmc_desc[cnum].pm_pos)) & 0x1)
  333. /*
  334. * This structure is initialized at boot time and contains
  335. * a description of the PMU main characteristics.
  336. *
  337. * If the probe function is defined, detection is based
  338. * on its return value:
  339. * - 0 means recognized PMU
  340. * - anything else means not supported
  341. * When the probe function is not defined, then the pmu_family field
  342. * is used and it must match the host CPU family such that:
  343. * - cpu->family & config->pmu_family != 0
  344. */
  345. typedef struct {
  346. unsigned long ovfl_val; /* overflow value for counters */
  347. pfm_reg_desc_t *pmc_desc; /* detailed PMC register dependencies descriptions */
  348. pfm_reg_desc_t *pmd_desc; /* detailed PMD register dependencies descriptions */
  349. unsigned int num_pmcs; /* number of PMCS: computed at init time */
  350. unsigned int num_pmds; /* number of PMDS: computed at init time */
  351. unsigned long impl_pmcs[4]; /* bitmask of implemented PMCS */
  352. unsigned long impl_pmds[4]; /* bitmask of implemented PMDS */
  353. char *pmu_name; /* PMU family name */
  354. unsigned int pmu_family; /* cpuid family pattern used to identify pmu */
  355. unsigned int flags; /* pmu specific flags */
  356. unsigned int num_ibrs; /* number of IBRS: computed at init time */
  357. unsigned int num_dbrs; /* number of DBRS: computed at init time */
  358. unsigned int num_counters; /* PMC/PMD counting pairs : computed at init time */
  359. int (*probe)(void); /* customized probe routine */
  360. unsigned int use_rr_dbregs:1; /* set if debug registers used for range restriction */
  361. } pmu_config_t;
  362. /*
  363. * PMU specific flags
  364. */
  365. #define PFM_PMU_IRQ_RESEND 1 /* PMU needs explicit IRQ resend */
  366. /*
  367. * debug register related type definitions
  368. */
  369. typedef struct {
  370. unsigned long ibr_mask:56;
  371. unsigned long ibr_plm:4;
  372. unsigned long ibr_ig:3;
  373. unsigned long ibr_x:1;
  374. } ibr_mask_reg_t;
  375. typedef struct {
  376. unsigned long dbr_mask:56;
  377. unsigned long dbr_plm:4;
  378. unsigned long dbr_ig:2;
  379. unsigned long dbr_w:1;
  380. unsigned long dbr_r:1;
  381. } dbr_mask_reg_t;
  382. typedef union {
  383. unsigned long val;
  384. ibr_mask_reg_t ibr;
  385. dbr_mask_reg_t dbr;
  386. } dbreg_t;
  387. /*
  388. * perfmon command descriptions
  389. */
  390. typedef struct {
  391. int (*cmd_func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
  392. char *cmd_name;
  393. int cmd_flags;
  394. unsigned int cmd_narg;
  395. size_t cmd_argsize;
  396. int (*cmd_getsize)(void *arg, size_t *sz);
  397. } pfm_cmd_desc_t;
  398. #define PFM_CMD_FD 0x01 /* command requires a file descriptor */
  399. #define PFM_CMD_ARG_READ 0x02 /* command must read argument(s) */
  400. #define PFM_CMD_ARG_RW 0x04 /* command must read/write argument(s) */
  401. #define PFM_CMD_STOP 0x08 /* command does not work on zombie context */
  402. #define PFM_CMD_NAME(cmd) pfm_cmd_tab[(cmd)].cmd_name
  403. #define PFM_CMD_READ_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_READ)
  404. #define PFM_CMD_RW_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_RW)
  405. #define PFM_CMD_USE_FD(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_FD)
  406. #define PFM_CMD_STOPPED(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_STOP)
  407. #define PFM_CMD_ARG_MANY -1 /* cannot be zero */
  408. typedef struct {
  409. unsigned long pfm_spurious_ovfl_intr_count; /* keep track of spurious ovfl interrupts */
  410. unsigned long pfm_replay_ovfl_intr_count; /* keep track of replayed ovfl interrupts */
  411. unsigned long pfm_ovfl_intr_count; /* keep track of ovfl interrupts */
  412. unsigned long pfm_ovfl_intr_cycles; /* cycles spent processing ovfl interrupts */
  413. unsigned long pfm_ovfl_intr_cycles_min; /* min cycles spent processing ovfl interrupts */
  414. unsigned long pfm_ovfl_intr_cycles_max; /* max cycles spent processing ovfl interrupts */
  415. unsigned long pfm_smpl_handler_calls;
  416. unsigned long pfm_smpl_handler_cycles;
  417. char pad[SMP_CACHE_BYTES] ____cacheline_aligned;
  418. } pfm_stats_t;
  419. /*
  420. * perfmon internal variables
  421. */
  422. static pfm_stats_t pfm_stats[NR_CPUS];
  423. static pfm_session_t pfm_sessions; /* global sessions information */
  424. static DEFINE_SPINLOCK(pfm_alt_install_check);
  425. static pfm_intr_handler_desc_t *pfm_alt_intr_handler;
  426. static struct proc_dir_entry *perfmon_dir;
  427. static pfm_uuid_t pfm_null_uuid = {0,};
  428. static spinlock_t pfm_buffer_fmt_lock;
  429. static LIST_HEAD(pfm_buffer_fmt_list);
  430. static pmu_config_t *pmu_conf;
  431. /* sysctl() controls */
  432. pfm_sysctl_t pfm_sysctl;
  433. EXPORT_SYMBOL(pfm_sysctl);
  434. static struct ctl_table pfm_ctl_table[] = {
  435. {
  436. .procname = "debug",
  437. .data = &pfm_sysctl.debug,
  438. .maxlen = sizeof(int),
  439. .mode = 0666,
  440. .proc_handler = proc_dointvec,
  441. },
  442. {
  443. .procname = "debug_ovfl",
  444. .data = &pfm_sysctl.debug_ovfl,
  445. .maxlen = sizeof(int),
  446. .mode = 0666,
  447. .proc_handler = proc_dointvec,
  448. },
  449. {
  450. .procname = "fastctxsw",
  451. .data = &pfm_sysctl.fastctxsw,
  452. .maxlen = sizeof(int),
  453. .mode = 0600,
  454. .proc_handler = proc_dointvec,
  455. },
  456. {
  457. .procname = "expert_mode",
  458. .data = &pfm_sysctl.expert_mode,
  459. .maxlen = sizeof(int),
  460. .mode = 0600,
  461. .proc_handler = proc_dointvec,
  462. },
  463. {}
  464. };
  465. static struct ctl_table pfm_sysctl_dir[] = {
  466. {
  467. .procname = "perfmon",
  468. .mode = 0555,
  469. .child = pfm_ctl_table,
  470. },
  471. {}
  472. };
  473. static struct ctl_table pfm_sysctl_root[] = {
  474. {
  475. .procname = "kernel",
  476. .mode = 0555,
  477. .child = pfm_sysctl_dir,
  478. },
  479. {}
  480. };
  481. static struct ctl_table_header *pfm_sysctl_header;
  482. static int pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
  483. #define pfm_get_cpu_var(v) __ia64_per_cpu_var(v)
  484. #define pfm_get_cpu_data(a,b) per_cpu(a, b)
  485. static inline void
  486. pfm_put_task(struct task_struct *task)
  487. {
  488. if (task != current) put_task_struct(task);
  489. }
  490. static inline void
  491. pfm_reserve_page(unsigned long a)
  492. {
  493. SetPageReserved(vmalloc_to_page((void *)a));
  494. }
  495. static inline void
  496. pfm_unreserve_page(unsigned long a)
  497. {
  498. ClearPageReserved(vmalloc_to_page((void*)a));
  499. }
  500. static inline unsigned long
  501. pfm_protect_ctx_ctxsw(pfm_context_t *x)
  502. {
  503. spin_lock(&(x)->ctx_lock);
  504. return 0UL;
  505. }
  506. static inline void
  507. pfm_unprotect_ctx_ctxsw(pfm_context_t *x, unsigned long f)
  508. {
  509. spin_unlock(&(x)->ctx_lock);
  510. }
  511. /* forward declaration */
  512. static const struct dentry_operations pfmfs_dentry_operations;
  513. static struct dentry *
  514. pfmfs_mount(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
  515. {
  516. return mount_pseudo(fs_type, "pfm:", NULL, &pfmfs_dentry_operations,
  517. PFMFS_MAGIC);
  518. }
  519. static struct file_system_type pfm_fs_type = {
  520. .name = "pfmfs",
  521. .mount = pfmfs_mount,
  522. .kill_sb = kill_anon_super,
  523. };
  524. MODULE_ALIAS_FS("pfmfs");
  525. DEFINE_PER_CPU(unsigned long, pfm_syst_info);
  526. DEFINE_PER_CPU(struct task_struct *, pmu_owner);
  527. DEFINE_PER_CPU(pfm_context_t *, pmu_ctx);
  528. DEFINE_PER_CPU(unsigned long, pmu_activation_number);
  529. EXPORT_PER_CPU_SYMBOL_GPL(pfm_syst_info);
  530. /* forward declaration */
  531. static const struct file_operations pfm_file_ops;
  532. /*
  533. * forward declarations
  534. */
  535. #ifndef CONFIG_SMP
  536. static void pfm_lazy_save_regs (struct task_struct *ta);
  537. #endif
  538. void dump_pmu_state(const char *);
  539. static int pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
  540. #include "perfmon_itanium.h"
  541. #include "perfmon_mckinley.h"
  542. #include "perfmon_montecito.h"
  543. #include "perfmon_generic.h"
  544. static pmu_config_t *pmu_confs[]={
  545. &pmu_conf_mont,
  546. &pmu_conf_mck,
  547. &pmu_conf_ita,
  548. &pmu_conf_gen, /* must be last */
  549. NULL
  550. };
  551. static int pfm_end_notify_user(pfm_context_t *ctx);
  552. static inline void
  553. pfm_clear_psr_pp(void)
  554. {
  555. ia64_rsm(IA64_PSR_PP);
  556. ia64_srlz_i();
  557. }
  558. static inline void
  559. pfm_set_psr_pp(void)
  560. {
  561. ia64_ssm(IA64_PSR_PP);
  562. ia64_srlz_i();
  563. }
  564. static inline void
  565. pfm_clear_psr_up(void)
  566. {
  567. ia64_rsm(IA64_PSR_UP);
  568. ia64_srlz_i();
  569. }
  570. static inline void
  571. pfm_set_psr_up(void)
  572. {
  573. ia64_ssm(IA64_PSR_UP);
  574. ia64_srlz_i();
  575. }
  576. static inline unsigned long
  577. pfm_get_psr(void)
  578. {
  579. unsigned long tmp;
  580. tmp = ia64_getreg(_IA64_REG_PSR);
  581. ia64_srlz_i();
  582. return tmp;
  583. }
  584. static inline void
  585. pfm_set_psr_l(unsigned long val)
  586. {
  587. ia64_setreg(_IA64_REG_PSR_L, val);
  588. ia64_srlz_i();
  589. }
  590. static inline void
  591. pfm_freeze_pmu(void)
  592. {
  593. ia64_set_pmc(0,1UL);
  594. ia64_srlz_d();
  595. }
  596. static inline void
  597. pfm_unfreeze_pmu(void)
  598. {
  599. ia64_set_pmc(0,0UL);
  600. ia64_srlz_d();
  601. }
  602. static inline void
  603. pfm_restore_ibrs(unsigned long *ibrs, unsigned int nibrs)
  604. {
  605. int i;
  606. for (i=0; i < nibrs; i++) {
  607. ia64_set_ibr(i, ibrs[i]);
  608. ia64_dv_serialize_instruction();
  609. }
  610. ia64_srlz_i();
  611. }
  612. static inline void
  613. pfm_restore_dbrs(unsigned long *dbrs, unsigned int ndbrs)
  614. {
  615. int i;
  616. for (i=0; i < ndbrs; i++) {
  617. ia64_set_dbr(i, dbrs[i]);
  618. ia64_dv_serialize_data();
  619. }
  620. ia64_srlz_d();
  621. }
  622. /*
  623. * PMD[i] must be a counter. no check is made
  624. */
  625. static inline unsigned long
  626. pfm_read_soft_counter(pfm_context_t *ctx, int i)
  627. {
  628. return ctx->ctx_pmds[i].val + (ia64_get_pmd(i) & pmu_conf->ovfl_val);
  629. }
  630. /*
  631. * PMD[i] must be a counter. no check is made
  632. */
  633. static inline void
  634. pfm_write_soft_counter(pfm_context_t *ctx, int i, unsigned long val)
  635. {
  636. unsigned long ovfl_val = pmu_conf->ovfl_val;
  637. ctx->ctx_pmds[i].val = val & ~ovfl_val;
  638. /*
  639. * writing to unimplemented part is ignore, so we do not need to
  640. * mask off top part
  641. */
  642. ia64_set_pmd(i, val & ovfl_val);
  643. }
  644. static pfm_msg_t *
  645. pfm_get_new_msg(pfm_context_t *ctx)
  646. {
  647. int idx, next;
  648. next = (ctx->ctx_msgq_tail+1) % PFM_MAX_MSGS;
  649. DPRINT(("ctx_fd=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
  650. if (next == ctx->ctx_msgq_head) return NULL;
  651. idx = ctx->ctx_msgq_tail;
  652. ctx->ctx_msgq_tail = next;
  653. DPRINT(("ctx=%p head=%d tail=%d msg=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, idx));
  654. return ctx->ctx_msgq+idx;
  655. }
  656. static pfm_msg_t *
  657. pfm_get_next_msg(pfm_context_t *ctx)
  658. {
  659. pfm_msg_t *msg;
  660. DPRINT(("ctx=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
  661. if (PFM_CTXQ_EMPTY(ctx)) return NULL;
  662. /*
  663. * get oldest message
  664. */
  665. msg = ctx->ctx_msgq+ctx->ctx_msgq_head;
  666. /*
  667. * and move forward
  668. */
  669. ctx->ctx_msgq_head = (ctx->ctx_msgq_head+1) % PFM_MAX_MSGS;
  670. DPRINT(("ctx=%p head=%d tail=%d type=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, msg->pfm_gen_msg.msg_type));
  671. return msg;
  672. }
  673. static void
  674. pfm_reset_msgq(pfm_context_t *ctx)
  675. {
  676. ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
  677. DPRINT(("ctx=%p msgq reset\n", ctx));
  678. }
  679. static void *
  680. pfm_rvmalloc(unsigned long size)
  681. {
  682. void *mem;
  683. unsigned long addr;
  684. size = PAGE_ALIGN(size);
  685. mem = vzalloc(size);
  686. if (mem) {
  687. //printk("perfmon: CPU%d pfm_rvmalloc(%ld)=%p\n", smp_processor_id(), size, mem);
  688. addr = (unsigned long)mem;
  689. while (size > 0) {
  690. pfm_reserve_page(addr);
  691. addr+=PAGE_SIZE;
  692. size-=PAGE_SIZE;
  693. }
  694. }
  695. return mem;
  696. }
  697. static void
  698. pfm_rvfree(void *mem, unsigned long size)
  699. {
  700. unsigned long addr;
  701. if (mem) {
  702. DPRINT(("freeing physical buffer @%p size=%lu\n", mem, size));
  703. addr = (unsigned long) mem;
  704. while ((long) size > 0) {
  705. pfm_unreserve_page(addr);
  706. addr+=PAGE_SIZE;
  707. size-=PAGE_SIZE;
  708. }
  709. vfree(mem);
  710. }
  711. return;
  712. }
  713. static pfm_context_t *
  714. pfm_context_alloc(int ctx_flags)
  715. {
  716. pfm_context_t *ctx;
  717. /*
  718. * allocate context descriptor
  719. * must be able to free with interrupts disabled
  720. */
  721. ctx = kzalloc(sizeof(pfm_context_t), GFP_KERNEL);
  722. if (ctx) {
  723. DPRINT(("alloc ctx @%p\n", ctx));
  724. /*
  725. * init context protection lock
  726. */
  727. spin_lock_init(&ctx->ctx_lock);
  728. /*
  729. * context is unloaded
  730. */
  731. ctx->ctx_state = PFM_CTX_UNLOADED;
  732. /*
  733. * initialization of context's flags
  734. */
  735. ctx->ctx_fl_block = (ctx_flags & PFM_FL_NOTIFY_BLOCK) ? 1 : 0;
  736. ctx->ctx_fl_system = (ctx_flags & PFM_FL_SYSTEM_WIDE) ? 1: 0;
  737. ctx->ctx_fl_no_msg = (ctx_flags & PFM_FL_OVFL_NO_MSG) ? 1: 0;
  738. /*
  739. * will move to set properties
  740. * ctx->ctx_fl_excl_idle = (ctx_flags & PFM_FL_EXCL_IDLE) ? 1: 0;
  741. */
  742. /*
  743. * init restart semaphore to locked
  744. */
  745. init_completion(&ctx->ctx_restart_done);
  746. /*
  747. * activation is used in SMP only
  748. */
  749. ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
  750. SET_LAST_CPU(ctx, -1);
  751. /*
  752. * initialize notification message queue
  753. */
  754. ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
  755. init_waitqueue_head(&ctx->ctx_msgq_wait);
  756. init_waitqueue_head(&ctx->ctx_zombieq);
  757. }
  758. return ctx;
  759. }
  760. static void
  761. pfm_context_free(pfm_context_t *ctx)
  762. {
  763. if (ctx) {
  764. DPRINT(("free ctx @%p\n", ctx));
  765. kfree(ctx);
  766. }
  767. }
  768. static void
  769. pfm_mask_monitoring(struct task_struct *task)
  770. {
  771. pfm_context_t *ctx = PFM_GET_CTX(task);
  772. unsigned long mask, val, ovfl_mask;
  773. int i;
  774. DPRINT_ovfl(("masking monitoring for [%d]\n", task_pid_nr(task)));
  775. ovfl_mask = pmu_conf->ovfl_val;
  776. /*
  777. * monitoring can only be masked as a result of a valid
  778. * counter overflow. In UP, it means that the PMU still
  779. * has an owner. Note that the owner can be different
  780. * from the current task. However the PMU state belongs
  781. * to the owner.
  782. * In SMP, a valid overflow only happens when task is
  783. * current. Therefore if we come here, we know that
  784. * the PMU state belongs to the current task, therefore
  785. * we can access the live registers.
  786. *
  787. * So in both cases, the live register contains the owner's
  788. * state. We can ONLY touch the PMU registers and NOT the PSR.
  789. *
  790. * As a consequence to this call, the ctx->th_pmds[] array
  791. * contains stale information which must be ignored
  792. * when context is reloaded AND monitoring is active (see
  793. * pfm_restart).
  794. */
  795. mask = ctx->ctx_used_pmds[0];
  796. for (i = 0; mask; i++, mask>>=1) {
  797. /* skip non used pmds */
  798. if ((mask & 0x1) == 0) continue;
  799. val = ia64_get_pmd(i);
  800. if (PMD_IS_COUNTING(i)) {
  801. /*
  802. * we rebuild the full 64 bit value of the counter
  803. */
  804. ctx->ctx_pmds[i].val += (val & ovfl_mask);
  805. } else {
  806. ctx->ctx_pmds[i].val = val;
  807. }
  808. DPRINT_ovfl(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
  809. i,
  810. ctx->ctx_pmds[i].val,
  811. val & ovfl_mask));
  812. }
  813. /*
  814. * mask monitoring by setting the privilege level to 0
  815. * we cannot use psr.pp/psr.up for this, it is controlled by
  816. * the user
  817. *
  818. * if task is current, modify actual registers, otherwise modify
  819. * thread save state, i.e., what will be restored in pfm_load_regs()
  820. */
  821. mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
  822. for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
  823. if ((mask & 0x1) == 0UL) continue;
  824. ia64_set_pmc(i, ctx->th_pmcs[i] & ~0xfUL);
  825. ctx->th_pmcs[i] &= ~0xfUL;
  826. DPRINT_ovfl(("pmc[%d]=0x%lx\n", i, ctx->th_pmcs[i]));
  827. }
  828. /*
  829. * make all of this visible
  830. */
  831. ia64_srlz_d();
  832. }
  833. /*
  834. * must always be done with task == current
  835. *
  836. * context must be in MASKED state when calling
  837. */
  838. static void
  839. pfm_restore_monitoring(struct task_struct *task)
  840. {
  841. pfm_context_t *ctx = PFM_GET_CTX(task);
  842. unsigned long mask, ovfl_mask;
  843. unsigned long psr, val;
  844. int i, is_system;
  845. is_system = ctx->ctx_fl_system;
  846. ovfl_mask = pmu_conf->ovfl_val;
  847. if (task != current) {
  848. printk(KERN_ERR "perfmon.%d: invalid task[%d] current[%d]\n", __LINE__, task_pid_nr(task), task_pid_nr(current));
  849. return;
  850. }
  851. if (ctx->ctx_state != PFM_CTX_MASKED) {
  852. printk(KERN_ERR "perfmon.%d: task[%d] current[%d] invalid state=%d\n", __LINE__,
  853. task_pid_nr(task), task_pid_nr(current), ctx->ctx_state);
  854. return;
  855. }
  856. psr = pfm_get_psr();
  857. /*
  858. * monitoring is masked via the PMC.
  859. * As we restore their value, we do not want each counter to
  860. * restart right away. We stop monitoring using the PSR,
  861. * restore the PMC (and PMD) and then re-establish the psr
  862. * as it was. Note that there can be no pending overflow at
  863. * this point, because monitoring was MASKED.
  864. *
  865. * system-wide session are pinned and self-monitoring
  866. */
  867. if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
  868. /* disable dcr pp */
  869. ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
  870. pfm_clear_psr_pp();
  871. } else {
  872. pfm_clear_psr_up();
  873. }
  874. /*
  875. * first, we restore the PMD
  876. */
  877. mask = ctx->ctx_used_pmds[0];
  878. for (i = 0; mask; i++, mask>>=1) {
  879. /* skip non used pmds */
  880. if ((mask & 0x1) == 0) continue;
  881. if (PMD_IS_COUNTING(i)) {
  882. /*
  883. * we split the 64bit value according to
  884. * counter width
  885. */
  886. val = ctx->ctx_pmds[i].val & ovfl_mask;
  887. ctx->ctx_pmds[i].val &= ~ovfl_mask;
  888. } else {
  889. val = ctx->ctx_pmds[i].val;
  890. }
  891. ia64_set_pmd(i, val);
  892. DPRINT(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
  893. i,
  894. ctx->ctx_pmds[i].val,
  895. val));
  896. }
  897. /*
  898. * restore the PMCs
  899. */
  900. mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
  901. for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
  902. if ((mask & 0x1) == 0UL) continue;
  903. ctx->th_pmcs[i] = ctx->ctx_pmcs[i];
  904. ia64_set_pmc(i, ctx->th_pmcs[i]);
  905. DPRINT(("[%d] pmc[%d]=0x%lx\n",
  906. task_pid_nr(task), i, ctx->th_pmcs[i]));
  907. }
  908. ia64_srlz_d();
  909. /*
  910. * must restore DBR/IBR because could be modified while masked
  911. * XXX: need to optimize
  912. */
  913. if (ctx->ctx_fl_using_dbreg) {
  914. pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
  915. pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
  916. }
  917. /*
  918. * now restore PSR
  919. */
  920. if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
  921. /* enable dcr pp */
  922. ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
  923. ia64_srlz_i();
  924. }
  925. pfm_set_psr_l(psr);
  926. }
  927. static inline void
  928. pfm_save_pmds(unsigned long *pmds, unsigned long mask)
  929. {
  930. int i;
  931. ia64_srlz_d();
  932. for (i=0; mask; i++, mask>>=1) {
  933. if (mask & 0x1) pmds[i] = ia64_get_pmd(i);
  934. }
  935. }
  936. /*
  937. * reload from thread state (used for ctxw only)
  938. */
  939. static inline void
  940. pfm_restore_pmds(unsigned long *pmds, unsigned long mask)
  941. {
  942. int i;
  943. unsigned long val, ovfl_val = pmu_conf->ovfl_val;
  944. for (i=0; mask; i++, mask>>=1) {
  945. if ((mask & 0x1) == 0) continue;
  946. val = PMD_IS_COUNTING(i) ? pmds[i] & ovfl_val : pmds[i];
  947. ia64_set_pmd(i, val);
  948. }
  949. ia64_srlz_d();
  950. }
  951. /*
  952. * propagate PMD from context to thread-state
  953. */
  954. static inline void
  955. pfm_copy_pmds(struct task_struct *task, pfm_context_t *ctx)
  956. {
  957. unsigned long ovfl_val = pmu_conf->ovfl_val;
  958. unsigned long mask = ctx->ctx_all_pmds[0];
  959. unsigned long val;
  960. int i;
  961. DPRINT(("mask=0x%lx\n", mask));
  962. for (i=0; mask; i++, mask>>=1) {
  963. val = ctx->ctx_pmds[i].val;
  964. /*
  965. * We break up the 64 bit value into 2 pieces
  966. * the lower bits go to the machine state in the
  967. * thread (will be reloaded on ctxsw in).
  968. * The upper part stays in the soft-counter.
  969. */
  970. if (PMD_IS_COUNTING(i)) {
  971. ctx->ctx_pmds[i].val = val & ~ovfl_val;
  972. val &= ovfl_val;
  973. }
  974. ctx->th_pmds[i] = val;
  975. DPRINT(("pmd[%d]=0x%lx soft_val=0x%lx\n",
  976. i,
  977. ctx->th_pmds[i],
  978. ctx->ctx_pmds[i].val));
  979. }
  980. }
  981. /*
  982. * propagate PMC from context to thread-state
  983. */
  984. static inline void
  985. pfm_copy_pmcs(struct task_struct *task, pfm_context_t *ctx)
  986. {
  987. unsigned long mask = ctx->ctx_all_pmcs[0];
  988. int i;
  989. DPRINT(("mask=0x%lx\n", mask));
  990. for (i=0; mask; i++, mask>>=1) {
  991. /* masking 0 with ovfl_val yields 0 */
  992. ctx->th_pmcs[i] = ctx->ctx_pmcs[i];
  993. DPRINT(("pmc[%d]=0x%lx\n", i, ctx->th_pmcs[i]));
  994. }
  995. }
  996. static inline void
  997. pfm_restore_pmcs(unsigned long *pmcs, unsigned long mask)
  998. {
  999. int i;
  1000. for (i=0; mask; i++, mask>>=1) {
  1001. if ((mask & 0x1) == 0) continue;
  1002. ia64_set_pmc(i, pmcs[i]);
  1003. }
  1004. ia64_srlz_d();
  1005. }
  1006. static inline int
  1007. pfm_uuid_cmp(pfm_uuid_t a, pfm_uuid_t b)
  1008. {
  1009. return memcmp(a, b, sizeof(pfm_uuid_t));
  1010. }
  1011. static inline int
  1012. pfm_buf_fmt_exit(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, struct pt_regs *regs)
  1013. {
  1014. int ret = 0;
  1015. if (fmt->fmt_exit) ret = (*fmt->fmt_exit)(task, buf, regs);
  1016. return ret;
  1017. }
  1018. static inline int
  1019. pfm_buf_fmt_getsize(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags, int cpu, void *arg, unsigned long *size)
  1020. {
  1021. int ret = 0;
  1022. if (fmt->fmt_getsize) ret = (*fmt->fmt_getsize)(task, flags, cpu, arg, size);
  1023. return ret;
  1024. }
  1025. static inline int
  1026. pfm_buf_fmt_validate(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags,
  1027. int cpu, void *arg)
  1028. {
  1029. int ret = 0;
  1030. if (fmt->fmt_validate) ret = (*fmt->fmt_validate)(task, flags, cpu, arg);
  1031. return ret;
  1032. }
  1033. static inline int
  1034. pfm_buf_fmt_init(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, unsigned int flags,
  1035. int cpu, void *arg)
  1036. {
  1037. int ret = 0;
  1038. if (fmt->fmt_init) ret = (*fmt->fmt_init)(task, buf, flags, cpu, arg);
  1039. return ret;
  1040. }
  1041. static inline int
  1042. pfm_buf_fmt_restart(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
  1043. {
  1044. int ret = 0;
  1045. if (fmt->fmt_restart) ret = (*fmt->fmt_restart)(task, ctrl, buf, regs);
  1046. return ret;
  1047. }
  1048. static inline int
  1049. pfm_buf_fmt_restart_active(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
  1050. {
  1051. int ret = 0;
  1052. if (fmt->fmt_restart_active) ret = (*fmt->fmt_restart_active)(task, ctrl, buf, regs);
  1053. return ret;
  1054. }
  1055. static pfm_buffer_fmt_t *
  1056. __pfm_find_buffer_fmt(pfm_uuid_t uuid)
  1057. {
  1058. struct list_head * pos;
  1059. pfm_buffer_fmt_t * entry;
  1060. list_for_each(pos, &pfm_buffer_fmt_list) {
  1061. entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
  1062. if (pfm_uuid_cmp(uuid, entry->fmt_uuid) == 0)
  1063. return entry;
  1064. }
  1065. return NULL;
  1066. }
  1067. /*
  1068. * find a buffer format based on its uuid
  1069. */
  1070. static pfm_buffer_fmt_t *
  1071. pfm_find_buffer_fmt(pfm_uuid_t uuid)
  1072. {
  1073. pfm_buffer_fmt_t * fmt;
  1074. spin_lock(&pfm_buffer_fmt_lock);
  1075. fmt = __pfm_find_buffer_fmt(uuid);
  1076. spin_unlock(&pfm_buffer_fmt_lock);
  1077. return fmt;
  1078. }
  1079. int
  1080. pfm_register_buffer_fmt(pfm_buffer_fmt_t *fmt)
  1081. {
  1082. int ret = 0;
  1083. /* some sanity checks */
  1084. if (fmt == NULL || fmt->fmt_name == NULL) return -EINVAL;
  1085. /* we need at least a handler */
  1086. if (fmt->fmt_handler == NULL) return -EINVAL;
  1087. /*
  1088. * XXX: need check validity of fmt_arg_size
  1089. */
  1090. spin_lock(&pfm_buffer_fmt_lock);
  1091. if (__pfm_find_buffer_fmt(fmt->fmt_uuid)) {
  1092. printk(KERN_ERR "perfmon: duplicate sampling format: %s\n", fmt->fmt_name);
  1093. ret = -EBUSY;
  1094. goto out;
  1095. }
  1096. list_add(&fmt->fmt_list, &pfm_buffer_fmt_list);
  1097. printk(KERN_INFO "perfmon: added sampling format %s\n", fmt->fmt_name);
  1098. out:
  1099. spin_unlock(&pfm_buffer_fmt_lock);
  1100. return ret;
  1101. }
  1102. EXPORT_SYMBOL(pfm_register_buffer_fmt);
  1103. int
  1104. pfm_unregister_buffer_fmt(pfm_uuid_t uuid)
  1105. {
  1106. pfm_buffer_fmt_t *fmt;
  1107. int ret = 0;
  1108. spin_lock(&pfm_buffer_fmt_lock);
  1109. fmt = __pfm_find_buffer_fmt(uuid);
  1110. if (!fmt) {
  1111. printk(KERN_ERR "perfmon: cannot unregister format, not found\n");
  1112. ret = -EINVAL;
  1113. goto out;
  1114. }
  1115. list_del_init(&fmt->fmt_list);
  1116. printk(KERN_INFO "perfmon: removed sampling format: %s\n", fmt->fmt_name);
  1117. out:
  1118. spin_unlock(&pfm_buffer_fmt_lock);
  1119. return ret;
  1120. }
  1121. EXPORT_SYMBOL(pfm_unregister_buffer_fmt);
  1122. static int
  1123. pfm_reserve_session(struct task_struct *task, int is_syswide, unsigned int cpu)
  1124. {
  1125. unsigned long flags;
  1126. /*
  1127. * validity checks on cpu_mask have been done upstream
  1128. */
  1129. LOCK_PFS(flags);
  1130. DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
  1131. pfm_sessions.pfs_sys_sessions,
  1132. pfm_sessions.pfs_task_sessions,
  1133. pfm_sessions.pfs_sys_use_dbregs,
  1134. is_syswide,
  1135. cpu));
  1136. if (is_syswide) {
  1137. /*
  1138. * cannot mix system wide and per-task sessions
  1139. */
  1140. if (pfm_sessions.pfs_task_sessions > 0UL) {
  1141. DPRINT(("system wide not possible, %u conflicting task_sessions\n",
  1142. pfm_sessions.pfs_task_sessions));
  1143. goto abort;
  1144. }
  1145. if (pfm_sessions.pfs_sys_session[cpu]) goto error_conflict;
  1146. DPRINT(("reserving system wide session on CPU%u currently on CPU%u\n", cpu, smp_processor_id()));
  1147. pfm_sessions.pfs_sys_session[cpu] = task;
  1148. pfm_sessions.pfs_sys_sessions++ ;
  1149. } else {
  1150. if (pfm_sessions.pfs_sys_sessions) goto abort;
  1151. pfm_sessions.pfs_task_sessions++;
  1152. }
  1153. DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
  1154. pfm_sessions.pfs_sys_sessions,
  1155. pfm_sessions.pfs_task_sessions,
  1156. pfm_sessions.pfs_sys_use_dbregs,
  1157. is_syswide,
  1158. cpu));
  1159. /*
  1160. * Force idle() into poll mode
  1161. */
  1162. cpu_idle_poll_ctrl(true);
  1163. UNLOCK_PFS(flags);
  1164. return 0;
  1165. error_conflict:
  1166. DPRINT(("system wide not possible, conflicting session [%d] on CPU%d\n",
  1167. task_pid_nr(pfm_sessions.pfs_sys_session[cpu]),
  1168. cpu));
  1169. abort:
  1170. UNLOCK_PFS(flags);
  1171. return -EBUSY;
  1172. }
  1173. static int
  1174. pfm_unreserve_session(pfm_context_t *ctx, int is_syswide, unsigned int cpu)
  1175. {
  1176. unsigned long flags;
  1177. /*
  1178. * validity checks on cpu_mask have been done upstream
  1179. */
  1180. LOCK_PFS(flags);
  1181. DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
  1182. pfm_sessions.pfs_sys_sessions,
  1183. pfm_sessions.pfs_task_sessions,
  1184. pfm_sessions.pfs_sys_use_dbregs,
  1185. is_syswide,
  1186. cpu));
  1187. if (is_syswide) {
  1188. pfm_sessions.pfs_sys_session[cpu] = NULL;
  1189. /*
  1190. * would not work with perfmon+more than one bit in cpu_mask
  1191. */
  1192. if (ctx && ctx->ctx_fl_using_dbreg) {
  1193. if (pfm_sessions.pfs_sys_use_dbregs == 0) {
  1194. printk(KERN_ERR "perfmon: invalid release for ctx %p sys_use_dbregs=0\n", ctx);
  1195. } else {
  1196. pfm_sessions.pfs_sys_use_dbregs--;
  1197. }
  1198. }
  1199. pfm_sessions.pfs_sys_sessions--;
  1200. } else {
  1201. pfm_sessions.pfs_task_sessions--;
  1202. }
  1203. DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
  1204. pfm_sessions.pfs_sys_sessions,
  1205. pfm_sessions.pfs_task_sessions,
  1206. pfm_sessions.pfs_sys_use_dbregs,
  1207. is_syswide,
  1208. cpu));
  1209. /* Undo forced polling. Last session reenables pal_halt */
  1210. cpu_idle_poll_ctrl(false);
  1211. UNLOCK_PFS(flags);
  1212. return 0;
  1213. }
  1214. /*
  1215. * removes virtual mapping of the sampling buffer.
  1216. * IMPORTANT: cannot be called with interrupts disable, e.g. inside
  1217. * a PROTECT_CTX() section.
  1218. */
  1219. static int
  1220. pfm_remove_smpl_mapping(void *vaddr, unsigned long size)
  1221. {
  1222. struct task_struct *task = current;
  1223. int r;
  1224. /* sanity checks */
  1225. if (task->mm == NULL || size == 0UL || vaddr == NULL) {
  1226. printk(KERN_ERR "perfmon: pfm_remove_smpl_mapping [%d] invalid context mm=%p\n", task_pid_nr(task), task->mm);
  1227. return -EINVAL;
  1228. }
  1229. DPRINT(("smpl_vaddr=%p size=%lu\n", vaddr, size));
  1230. /*
  1231. * does the actual unmapping
  1232. */
  1233. r = vm_munmap((unsigned long)vaddr, size);
  1234. if (r !=0) {
  1235. printk(KERN_ERR "perfmon: [%d] unable to unmap sampling buffer @%p size=%lu\n", task_pid_nr(task), vaddr, size);
  1236. }
  1237. DPRINT(("do_unmap(%p, %lu)=%d\n", vaddr, size, r));
  1238. return 0;
  1239. }
  1240. /*
  1241. * free actual physical storage used by sampling buffer
  1242. */
  1243. #if 0
  1244. static int
  1245. pfm_free_smpl_buffer(pfm_context_t *ctx)
  1246. {
  1247. pfm_buffer_fmt_t *fmt;
  1248. if (ctx->ctx_smpl_hdr == NULL) goto invalid_free;
  1249. /*
  1250. * we won't use the buffer format anymore
  1251. */
  1252. fmt = ctx->ctx_buf_fmt;
  1253. DPRINT(("sampling buffer @%p size %lu vaddr=%p\n",
  1254. ctx->ctx_smpl_hdr,
  1255. ctx->ctx_smpl_size,
  1256. ctx->ctx_smpl_vaddr));
  1257. pfm_buf_fmt_exit(fmt, current, NULL, NULL);
  1258. /*
  1259. * free the buffer
  1260. */
  1261. pfm_rvfree(ctx->ctx_smpl_hdr, ctx->ctx_smpl_size);
  1262. ctx->ctx_smpl_hdr = NULL;
  1263. ctx->ctx_smpl_size = 0UL;
  1264. return 0;
  1265. invalid_free:
  1266. printk(KERN_ERR "perfmon: pfm_free_smpl_buffer [%d] no buffer\n", task_pid_nr(current));
  1267. return -EINVAL;
  1268. }
  1269. #endif
  1270. static inline void
  1271. pfm_exit_smpl_buffer(pfm_buffer_fmt_t *fmt)
  1272. {
  1273. if (fmt == NULL) return;
  1274. pfm_buf_fmt_exit(fmt, current, NULL, NULL);
  1275. }
  1276. /*
  1277. * pfmfs should _never_ be mounted by userland - too much of security hassle,
  1278. * no real gain from having the whole whorehouse mounted. So we don't need
  1279. * any operations on the root directory. However, we need a non-trivial
  1280. * d_name - pfm: will go nicely and kill the special-casing in procfs.
  1281. */
  1282. static struct vfsmount *pfmfs_mnt __read_mostly;
  1283. static int __init
  1284. init_pfm_fs(void)
  1285. {
  1286. int err = register_filesystem(&pfm_fs_type);
  1287. if (!err) {
  1288. pfmfs_mnt = kern_mount(&pfm_fs_type);
  1289. err = PTR_ERR(pfmfs_mnt);
  1290. if (IS_ERR(pfmfs_mnt))
  1291. unregister_filesystem(&pfm_fs_type);
  1292. else
  1293. err = 0;
  1294. }
  1295. return err;
  1296. }
  1297. static ssize_t
  1298. pfm_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
  1299. {
  1300. pfm_context_t *ctx;
  1301. pfm_msg_t *msg;
  1302. ssize_t ret;
  1303. unsigned long flags;
  1304. DECLARE_WAITQUEUE(wait, current);
  1305. if (PFM_IS_FILE(filp) == 0) {
  1306. printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", task_pid_nr(current));
  1307. return -EINVAL;
  1308. }
  1309. ctx = filp->private_data;
  1310. if (ctx == NULL) {
  1311. printk(KERN_ERR "perfmon: pfm_read: NULL ctx [%d]\n", task_pid_nr(current));
  1312. return -EINVAL;
  1313. }
  1314. /*
  1315. * check even when there is no message
  1316. */
  1317. if (size < sizeof(pfm_msg_t)) {
  1318. DPRINT(("message is too small ctx=%p (>=%ld)\n", ctx, sizeof(pfm_msg_t)));
  1319. return -EINVAL;
  1320. }
  1321. PROTECT_CTX(ctx, flags);
  1322. /*
  1323. * put ourselves on the wait queue
  1324. */
  1325. add_wait_queue(&ctx->ctx_msgq_wait, &wait);
  1326. for(;;) {
  1327. /*
  1328. * check wait queue
  1329. */
  1330. set_current_state(TASK_INTERRUPTIBLE);
  1331. DPRINT(("head=%d tail=%d\n", ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
  1332. ret = 0;
  1333. if(PFM_CTXQ_EMPTY(ctx) == 0) break;
  1334. UNPROTECT_CTX(ctx, flags);
  1335. /*
  1336. * check non-blocking read
  1337. */
  1338. ret = -EAGAIN;
  1339. if(filp->f_flags & O_NONBLOCK) break;
  1340. /*
  1341. * check pending signals
  1342. */
  1343. if(signal_pending(current)) {
  1344. ret = -EINTR;
  1345. break;
  1346. }
  1347. /*
  1348. * no message, so wait
  1349. */
  1350. schedule();
  1351. PROTECT_CTX(ctx, flags);
  1352. }
  1353. DPRINT(("[%d] back to running ret=%ld\n", task_pid_nr(current), ret));
  1354. set_current_state(TASK_RUNNING);
  1355. remove_wait_queue(&ctx->ctx_msgq_wait, &wait);
  1356. if (ret < 0) goto abort;
  1357. ret = -EINVAL;
  1358. msg = pfm_get_next_msg(ctx);
  1359. if (msg == NULL) {
  1360. printk(KERN_ERR "perfmon: pfm_read no msg for ctx=%p [%d]\n", ctx, task_pid_nr(current));
  1361. goto abort_locked;
  1362. }
  1363. DPRINT(("fd=%d type=%d\n", msg->pfm_gen_msg.msg_ctx_fd, msg->pfm_gen_msg.msg_type));
  1364. ret = -EFAULT;
  1365. if(copy_to_user(buf, msg, sizeof(pfm_msg_t)) == 0) ret = sizeof(pfm_msg_t);
  1366. abort_locked:
  1367. UNPROTECT_CTX(ctx, flags);
  1368. abort:
  1369. return ret;
  1370. }
  1371. static ssize_t
  1372. pfm_write(struct file *file, const char __user *ubuf,
  1373. size_t size, loff_t *ppos)
  1374. {
  1375. DPRINT(("pfm_write called\n"));
  1376. return -EINVAL;
  1377. }
  1378. static __poll_t
  1379. pfm_poll(struct file *filp, poll_table * wait)
  1380. {
  1381. pfm_context_t *ctx;
  1382. unsigned long flags;
  1383. __poll_t mask = 0;
  1384. if (PFM_IS_FILE(filp) == 0) {
  1385. printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", task_pid_nr(current));
  1386. return 0;
  1387. }
  1388. ctx = filp->private_data;
  1389. if (ctx == NULL) {
  1390. printk(KERN_ERR "perfmon: pfm_poll: NULL ctx [%d]\n", task_pid_nr(current));
  1391. return 0;
  1392. }
  1393. DPRINT(("pfm_poll ctx_fd=%d before poll_wait\n", ctx->ctx_fd));
  1394. poll_wait(filp, &ctx->ctx_msgq_wait, wait);
  1395. PROTECT_CTX(ctx, flags);
  1396. if (PFM_CTXQ_EMPTY(ctx) == 0)
  1397. mask = EPOLLIN | EPOLLRDNORM;
  1398. UNPROTECT_CTX(ctx, flags);
  1399. DPRINT(("pfm_poll ctx_fd=%d mask=0x%x\n", ctx->ctx_fd, mask));
  1400. return mask;
  1401. }
  1402. static long
  1403. pfm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1404. {
  1405. DPRINT(("pfm_ioctl called\n"));
  1406. return -EINVAL;
  1407. }
  1408. /*
  1409. * interrupt cannot be masked when coming here
  1410. */
  1411. static inline int
  1412. pfm_do_fasync(int fd, struct file *filp, pfm_context_t *ctx, int on)
  1413. {
  1414. int ret;
  1415. ret = fasync_helper (fd, filp, on, &ctx->ctx_async_queue);
  1416. DPRINT(("pfm_fasync called by [%d] on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
  1417. task_pid_nr(current),
  1418. fd,
  1419. on,
  1420. ctx->ctx_async_queue, ret));
  1421. return ret;
  1422. }
  1423. static int
  1424. pfm_fasync(int fd, struct file *filp, int on)
  1425. {
  1426. pfm_context_t *ctx;
  1427. int ret;
  1428. if (PFM_IS_FILE(filp) == 0) {
  1429. printk(KERN_ERR "perfmon: pfm_fasync bad magic [%d]\n", task_pid_nr(current));
  1430. return -EBADF;
  1431. }
  1432. ctx = filp->private_data;
  1433. if (ctx == NULL) {
  1434. printk(KERN_ERR "perfmon: pfm_fasync NULL ctx [%d]\n", task_pid_nr(current));
  1435. return -EBADF;
  1436. }
  1437. /*
  1438. * we cannot mask interrupts during this call because this may
  1439. * may go to sleep if memory is not readily avalaible.
  1440. *
  1441. * We are protected from the conetxt disappearing by the get_fd()/put_fd()
  1442. * done in caller. Serialization of this function is ensured by caller.
  1443. */
  1444. ret = pfm_do_fasync(fd, filp, ctx, on);
  1445. DPRINT(("pfm_fasync called on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
  1446. fd,
  1447. on,
  1448. ctx->ctx_async_queue, ret));
  1449. return ret;
  1450. }
  1451. #ifdef CONFIG_SMP
  1452. /*
  1453. * this function is exclusively called from pfm_close().
  1454. * The context is not protected at that time, nor are interrupts
  1455. * on the remote CPU. That's necessary to avoid deadlocks.
  1456. */
  1457. static void
  1458. pfm_syswide_force_stop(void *info)
  1459. {
  1460. pfm_context_t *ctx = (pfm_context_t *)info;
  1461. struct pt_regs *regs = task_pt_regs(current);
  1462. struct task_struct *owner;
  1463. unsigned long flags;
  1464. int ret;
  1465. if (ctx->ctx_cpu != smp_processor_id()) {
  1466. printk(KERN_ERR "perfmon: pfm_syswide_force_stop for CPU%d but on CPU%d\n",
  1467. ctx->ctx_cpu,
  1468. smp_processor_id());
  1469. return;
  1470. }
  1471. owner = GET_PMU_OWNER();
  1472. if (owner != ctx->ctx_task) {
  1473. printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected owner [%d] instead of [%d]\n",
  1474. smp_processor_id(),
  1475. task_pid_nr(owner), task_pid_nr(ctx->ctx_task));
  1476. return;
  1477. }
  1478. if (GET_PMU_CTX() != ctx) {
  1479. printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected ctx %p instead of %p\n",
  1480. smp_processor_id(),
  1481. GET_PMU_CTX(), ctx);
  1482. return;
  1483. }
  1484. DPRINT(("on CPU%d forcing system wide stop for [%d]\n", smp_processor_id(), task_pid_nr(ctx->ctx_task)));
  1485. /*
  1486. * the context is already protected in pfm_close(), we simply
  1487. * need to mask interrupts to avoid a PMU interrupt race on
  1488. * this CPU
  1489. */
  1490. local_irq_save(flags);
  1491. ret = pfm_context_unload(ctx, NULL, 0, regs);
  1492. if (ret) {
  1493. DPRINT(("context_unload returned %d\n", ret));
  1494. }
  1495. /*
  1496. * unmask interrupts, PMU interrupts are now spurious here
  1497. */
  1498. local_irq_restore(flags);
  1499. }
  1500. static void
  1501. pfm_syswide_cleanup_other_cpu(pfm_context_t *ctx)
  1502. {
  1503. int ret;
  1504. DPRINT(("calling CPU%d for cleanup\n", ctx->ctx_cpu));
  1505. ret = smp_call_function_single(ctx->ctx_cpu, pfm_syswide_force_stop, ctx, 1);
  1506. DPRINT(("called CPU%d for cleanup ret=%d\n", ctx->ctx_cpu, ret));
  1507. }
  1508. #endif /* CONFIG_SMP */
  1509. /*
  1510. * called for each close(). Partially free resources.
  1511. * When caller is self-monitoring, the context is unloaded.
  1512. */
  1513. static int
  1514. pfm_flush(struct file *filp, fl_owner_t id)
  1515. {
  1516. pfm_context_t *ctx;
  1517. struct task_struct *task;
  1518. struct pt_regs *regs;
  1519. unsigned long flags;
  1520. unsigned long smpl_buf_size = 0UL;
  1521. void *smpl_buf_vaddr = NULL;
  1522. int state, is_system;
  1523. if (PFM_IS_FILE(filp) == 0) {
  1524. DPRINT(("bad magic for\n"));
  1525. return -EBADF;
  1526. }
  1527. ctx = filp->private_data;
  1528. if (ctx == NULL) {
  1529. printk(KERN_ERR "perfmon: pfm_flush: NULL ctx [%d]\n", task_pid_nr(current));
  1530. return -EBADF;
  1531. }
  1532. /*
  1533. * remove our file from the async queue, if we use this mode.
  1534. * This can be done without the context being protected. We come
  1535. * here when the context has become unreachable by other tasks.
  1536. *
  1537. * We may still have active monitoring at this point and we may
  1538. * end up in pfm_overflow_handler(). However, fasync_helper()
  1539. * operates with interrupts disabled and it cleans up the
  1540. * queue. If the PMU handler is called prior to entering
  1541. * fasync_helper() then it will send a signal. If it is
  1542. * invoked after, it will find an empty queue and no
  1543. * signal will be sent. In both case, we are safe
  1544. */
  1545. PROTECT_CTX(ctx, flags);
  1546. state = ctx->ctx_state;
  1547. is_system = ctx->ctx_fl_system;
  1548. task = PFM_CTX_TASK(ctx);
  1549. regs = task_pt_regs(task);
  1550. DPRINT(("ctx_state=%d is_current=%d\n",
  1551. state,
  1552. task == current ? 1 : 0));
  1553. /*
  1554. * if state == UNLOADED, then task is NULL
  1555. */
  1556. /*
  1557. * we must stop and unload because we are losing access to the context.
  1558. */
  1559. if (task == current) {
  1560. #ifdef CONFIG_SMP
  1561. /*
  1562. * the task IS the owner but it migrated to another CPU: that's bad
  1563. * but we must handle this cleanly. Unfortunately, the kernel does
  1564. * not provide a mechanism to block migration (while the context is loaded).
  1565. *
  1566. * We need to release the resource on the ORIGINAL cpu.
  1567. */
  1568. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  1569. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  1570. /*
  1571. * keep context protected but unmask interrupt for IPI
  1572. */
  1573. local_irq_restore(flags);
  1574. pfm_syswide_cleanup_other_cpu(ctx);
  1575. /*
  1576. * restore interrupt masking
  1577. */
  1578. local_irq_save(flags);
  1579. /*
  1580. * context is unloaded at this point
  1581. */
  1582. } else
  1583. #endif /* CONFIG_SMP */
  1584. {
  1585. DPRINT(("forcing unload\n"));
  1586. /*
  1587. * stop and unload, returning with state UNLOADED
  1588. * and session unreserved.
  1589. */
  1590. pfm_context_unload(ctx, NULL, 0, regs);
  1591. DPRINT(("ctx_state=%d\n", ctx->ctx_state));
  1592. }
  1593. }
  1594. /*
  1595. * remove virtual mapping, if any, for the calling task.
  1596. * cannot reset ctx field until last user is calling close().
  1597. *
  1598. * ctx_smpl_vaddr must never be cleared because it is needed
  1599. * by every task with access to the context
  1600. *
  1601. * When called from do_exit(), the mm context is gone already, therefore
  1602. * mm is NULL, i.e., the VMA is already gone and we do not have to
  1603. * do anything here
  1604. */
  1605. if (ctx->ctx_smpl_vaddr && current->mm) {
  1606. smpl_buf_vaddr = ctx->ctx_smpl_vaddr;
  1607. smpl_buf_size = ctx->ctx_smpl_size;
  1608. }
  1609. UNPROTECT_CTX(ctx, flags);
  1610. /*
  1611. * if there was a mapping, then we systematically remove it
  1612. * at this point. Cannot be done inside critical section
  1613. * because some VM function reenables interrupts.
  1614. *
  1615. */
  1616. if (smpl_buf_vaddr) pfm_remove_smpl_mapping(smpl_buf_vaddr, smpl_buf_size);
  1617. return 0;
  1618. }
  1619. /*
  1620. * called either on explicit close() or from exit_files().
  1621. * Only the LAST user of the file gets to this point, i.e., it is
  1622. * called only ONCE.
  1623. *
  1624. * IMPORTANT: we get called ONLY when the refcnt on the file gets to zero
  1625. * (fput()),i.e, last task to access the file. Nobody else can access the
  1626. * file at this point.
  1627. *
  1628. * When called from exit_files(), the VMA has been freed because exit_mm()
  1629. * is executed before exit_files().
  1630. *
  1631. * When called from exit_files(), the current task is not yet ZOMBIE but we
  1632. * flush the PMU state to the context.
  1633. */
  1634. static int
  1635. pfm_close(struct inode *inode, struct file *filp)
  1636. {
  1637. pfm_context_t *ctx;
  1638. struct task_struct *task;
  1639. struct pt_regs *regs;
  1640. DECLARE_WAITQUEUE(wait, current);
  1641. unsigned long flags;
  1642. unsigned long smpl_buf_size = 0UL;
  1643. void *smpl_buf_addr = NULL;
  1644. int free_possible = 1;
  1645. int state, is_system;
  1646. DPRINT(("pfm_close called private=%p\n", filp->private_data));
  1647. if (PFM_IS_FILE(filp) == 0) {
  1648. DPRINT(("bad magic\n"));
  1649. return -EBADF;
  1650. }
  1651. ctx = filp->private_data;
  1652. if (ctx == NULL) {
  1653. printk(KERN_ERR "perfmon: pfm_close: NULL ctx [%d]\n", task_pid_nr(current));
  1654. return -EBADF;
  1655. }
  1656. PROTECT_CTX(ctx, flags);
  1657. state = ctx->ctx_state;
  1658. is_system = ctx->ctx_fl_system;
  1659. task = PFM_CTX_TASK(ctx);
  1660. regs = task_pt_regs(task);
  1661. DPRINT(("ctx_state=%d is_current=%d\n",
  1662. state,
  1663. task == current ? 1 : 0));
  1664. /*
  1665. * if task == current, then pfm_flush() unloaded the context
  1666. */
  1667. if (state == PFM_CTX_UNLOADED) goto doit;
  1668. /*
  1669. * context is loaded/masked and task != current, we need to
  1670. * either force an unload or go zombie
  1671. */
  1672. /*
  1673. * The task is currently blocked or will block after an overflow.
  1674. * we must force it to wakeup to get out of the
  1675. * MASKED state and transition to the unloaded state by itself.
  1676. *
  1677. * This situation is only possible for per-task mode
  1678. */
  1679. if (state == PFM_CTX_MASKED && CTX_OVFL_NOBLOCK(ctx) == 0) {
  1680. /*
  1681. * set a "partial" zombie state to be checked
  1682. * upon return from down() in pfm_handle_work().
  1683. *
  1684. * We cannot use the ZOMBIE state, because it is checked
  1685. * by pfm_load_regs() which is called upon wakeup from down().
  1686. * In such case, it would free the context and then we would
  1687. * return to pfm_handle_work() which would access the
  1688. * stale context. Instead, we set a flag invisible to pfm_load_regs()
  1689. * but visible to pfm_handle_work().
  1690. *
  1691. * For some window of time, we have a zombie context with
  1692. * ctx_state = MASKED and not ZOMBIE
  1693. */
  1694. ctx->ctx_fl_going_zombie = 1;
  1695. /*
  1696. * force task to wake up from MASKED state
  1697. */
  1698. complete(&ctx->ctx_restart_done);
  1699. DPRINT(("waking up ctx_state=%d\n", state));
  1700. /*
  1701. * put ourself to sleep waiting for the other
  1702. * task to report completion
  1703. *
  1704. * the context is protected by mutex, therefore there
  1705. * is no risk of being notified of completion before
  1706. * begin actually on the waitq.
  1707. */
  1708. set_current_state(TASK_INTERRUPTIBLE);
  1709. add_wait_queue(&ctx->ctx_zombieq, &wait);
  1710. UNPROTECT_CTX(ctx, flags);
  1711. /*
  1712. * XXX: check for signals :
  1713. * - ok for explicit close
  1714. * - not ok when coming from exit_files()
  1715. */
  1716. schedule();
  1717. PROTECT_CTX(ctx, flags);
  1718. remove_wait_queue(&ctx->ctx_zombieq, &wait);
  1719. set_current_state(TASK_RUNNING);
  1720. /*
  1721. * context is unloaded at this point
  1722. */
  1723. DPRINT(("after zombie wakeup ctx_state=%d for\n", state));
  1724. }
  1725. else if (task != current) {
  1726. #ifdef CONFIG_SMP
  1727. /*
  1728. * switch context to zombie state
  1729. */
  1730. ctx->ctx_state = PFM_CTX_ZOMBIE;
  1731. DPRINT(("zombie ctx for [%d]\n", task_pid_nr(task)));
  1732. /*
  1733. * cannot free the context on the spot. deferred until
  1734. * the task notices the ZOMBIE state
  1735. */
  1736. free_possible = 0;
  1737. #else
  1738. pfm_context_unload(ctx, NULL, 0, regs);
  1739. #endif
  1740. }
  1741. doit:
  1742. /* reload state, may have changed during opening of critical section */
  1743. state = ctx->ctx_state;
  1744. /*
  1745. * the context is still attached to a task (possibly current)
  1746. * we cannot destroy it right now
  1747. */
  1748. /*
  1749. * we must free the sampling buffer right here because
  1750. * we cannot rely on it being cleaned up later by the
  1751. * monitored task. It is not possible to free vmalloc'ed
  1752. * memory in pfm_load_regs(). Instead, we remove the buffer
  1753. * now. should there be subsequent PMU overflow originally
  1754. * meant for sampling, the will be converted to spurious
  1755. * and that's fine because the monitoring tools is gone anyway.
  1756. */
  1757. if (ctx->ctx_smpl_hdr) {
  1758. smpl_buf_addr = ctx->ctx_smpl_hdr;
  1759. smpl_buf_size = ctx->ctx_smpl_size;
  1760. /* no more sampling */
  1761. ctx->ctx_smpl_hdr = NULL;
  1762. ctx->ctx_fl_is_sampling = 0;
  1763. }
  1764. DPRINT(("ctx_state=%d free_possible=%d addr=%p size=%lu\n",
  1765. state,
  1766. free_possible,
  1767. smpl_buf_addr,
  1768. smpl_buf_size));
  1769. if (smpl_buf_addr) pfm_exit_smpl_buffer(ctx->ctx_buf_fmt);
  1770. /*
  1771. * UNLOADED that the session has already been unreserved.
  1772. */
  1773. if (state == PFM_CTX_ZOMBIE) {
  1774. pfm_unreserve_session(ctx, ctx->ctx_fl_system , ctx->ctx_cpu);
  1775. }
  1776. /*
  1777. * disconnect file descriptor from context must be done
  1778. * before we unlock.
  1779. */
  1780. filp->private_data = NULL;
  1781. /*
  1782. * if we free on the spot, the context is now completely unreachable
  1783. * from the callers side. The monitored task side is also cut, so we
  1784. * can freely cut.
  1785. *
  1786. * If we have a deferred free, only the caller side is disconnected.
  1787. */
  1788. UNPROTECT_CTX(ctx, flags);
  1789. /*
  1790. * All memory free operations (especially for vmalloc'ed memory)
  1791. * MUST be done with interrupts ENABLED.
  1792. */
  1793. if (smpl_buf_addr) pfm_rvfree(smpl_buf_addr, smpl_buf_size);
  1794. /*
  1795. * return the memory used by the context
  1796. */
  1797. if (free_possible) pfm_context_free(ctx);
  1798. return 0;
  1799. }
  1800. static const struct file_operations pfm_file_ops = {
  1801. .llseek = no_llseek,
  1802. .read = pfm_read,
  1803. .write = pfm_write,
  1804. .poll = pfm_poll,
  1805. .unlocked_ioctl = pfm_ioctl,
  1806. .fasync = pfm_fasync,
  1807. .release = pfm_close,
  1808. .flush = pfm_flush
  1809. };
  1810. static char *pfmfs_dname(struct dentry *dentry, char *buffer, int buflen)
  1811. {
  1812. return dynamic_dname(dentry, buffer, buflen, "pfm:[%lu]",
  1813. d_inode(dentry)->i_ino);
  1814. }
  1815. static const struct dentry_operations pfmfs_dentry_operations = {
  1816. .d_delete = always_delete_dentry,
  1817. .d_dname = pfmfs_dname,
  1818. };
  1819. static struct file *
  1820. pfm_alloc_file(pfm_context_t *ctx)
  1821. {
  1822. struct file *file;
  1823. struct inode *inode;
  1824. struct path path;
  1825. struct qstr this = { .name = "" };
  1826. /*
  1827. * allocate a new inode
  1828. */
  1829. inode = new_inode(pfmfs_mnt->mnt_sb);
  1830. if (!inode)
  1831. return ERR_PTR(-ENOMEM);
  1832. DPRINT(("new inode ino=%ld @%p\n", inode->i_ino, inode));
  1833. inode->i_mode = S_IFCHR|S_IRUGO;
  1834. inode->i_uid = current_fsuid();
  1835. inode->i_gid = current_fsgid();
  1836. /*
  1837. * allocate a new dcache entry
  1838. */
  1839. path.dentry = d_alloc(pfmfs_mnt->mnt_root, &this);
  1840. if (!path.dentry) {
  1841. iput(inode);
  1842. return ERR_PTR(-ENOMEM);
  1843. }
  1844. path.mnt = mntget(pfmfs_mnt);
  1845. d_add(path.dentry, inode);
  1846. file = alloc_file(&path, FMODE_READ, &pfm_file_ops);
  1847. if (IS_ERR(file)) {
  1848. path_put(&path);
  1849. return file;
  1850. }
  1851. file->f_flags = O_RDONLY;
  1852. file->private_data = ctx;
  1853. return file;
  1854. }
  1855. static int
  1856. pfm_remap_buffer(struct vm_area_struct *vma, unsigned long buf, unsigned long addr, unsigned long size)
  1857. {
  1858. DPRINT(("CPU%d buf=0x%lx addr=0x%lx size=%ld\n", smp_processor_id(), buf, addr, size));
  1859. while (size > 0) {
  1860. unsigned long pfn = ia64_tpa(buf) >> PAGE_SHIFT;
  1861. if (remap_pfn_range(vma, addr, pfn, PAGE_SIZE, PAGE_READONLY))
  1862. return -ENOMEM;
  1863. addr += PAGE_SIZE;
  1864. buf += PAGE_SIZE;
  1865. size -= PAGE_SIZE;
  1866. }
  1867. return 0;
  1868. }
  1869. /*
  1870. * allocate a sampling buffer and remaps it into the user address space of the task
  1871. */
  1872. static int
  1873. pfm_smpl_buffer_alloc(struct task_struct *task, struct file *filp, pfm_context_t *ctx, unsigned long rsize, void **user_vaddr)
  1874. {
  1875. struct mm_struct *mm = task->mm;
  1876. struct vm_area_struct *vma = NULL;
  1877. unsigned long size;
  1878. void *smpl_buf;
  1879. /*
  1880. * the fixed header + requested size and align to page boundary
  1881. */
  1882. size = PAGE_ALIGN(rsize);
  1883. DPRINT(("sampling buffer rsize=%lu size=%lu bytes\n", rsize, size));
  1884. /*
  1885. * check requested size to avoid Denial-of-service attacks
  1886. * XXX: may have to refine this test
  1887. * Check against address space limit.
  1888. *
  1889. * if ((mm->total_vm << PAGE_SHIFT) + len> task->rlim[RLIMIT_AS].rlim_cur)
  1890. * return -ENOMEM;
  1891. */
  1892. if (size > task_rlimit(task, RLIMIT_MEMLOCK))
  1893. return -ENOMEM;
  1894. /*
  1895. * We do the easy to undo allocations first.
  1896. *
  1897. * pfm_rvmalloc(), clears the buffer, so there is no leak
  1898. */
  1899. smpl_buf = pfm_rvmalloc(size);
  1900. if (smpl_buf == NULL) {
  1901. DPRINT(("Can't allocate sampling buffer\n"));
  1902. return -ENOMEM;
  1903. }
  1904. DPRINT(("smpl_buf @%p\n", smpl_buf));
  1905. /* allocate vma */
  1906. vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  1907. if (!vma) {
  1908. DPRINT(("Cannot allocate vma\n"));
  1909. goto error_kmem;
  1910. }
  1911. INIT_LIST_HEAD(&vma->anon_vma_chain);
  1912. /*
  1913. * partially initialize the vma for the sampling buffer
  1914. */
  1915. vma->vm_mm = mm;
  1916. vma->vm_file = get_file(filp);
  1917. vma->vm_flags = VM_READ|VM_MAYREAD|VM_DONTEXPAND|VM_DONTDUMP;
  1918. vma->vm_page_prot = PAGE_READONLY; /* XXX may need to change */
  1919. /*
  1920. * Now we have everything we need and we can initialize
  1921. * and connect all the data structures
  1922. */
  1923. ctx->ctx_smpl_hdr = smpl_buf;
  1924. ctx->ctx_smpl_size = size; /* aligned size */
  1925. /*
  1926. * Let's do the difficult operations next.
  1927. *
  1928. * now we atomically find some area in the address space and
  1929. * remap the buffer in it.
  1930. */
  1931. down_write(&task->mm->mmap_sem);
  1932. /* find some free area in address space, must have mmap sem held */
  1933. vma->vm_start = get_unmapped_area(NULL, 0, size, 0, MAP_PRIVATE|MAP_ANONYMOUS);
  1934. if (IS_ERR_VALUE(vma->vm_start)) {
  1935. DPRINT(("Cannot find unmapped area for size %ld\n", size));
  1936. up_write(&task->mm->mmap_sem);
  1937. goto error;
  1938. }
  1939. vma->vm_end = vma->vm_start + size;
  1940. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  1941. DPRINT(("aligned size=%ld, hdr=%p mapped @0x%lx\n", size, ctx->ctx_smpl_hdr, vma->vm_start));
  1942. /* can only be applied to current task, need to have the mm semaphore held when called */
  1943. if (pfm_remap_buffer(vma, (unsigned long)smpl_buf, vma->vm_start, size)) {
  1944. DPRINT(("Can't remap buffer\n"));
  1945. up_write(&task->mm->mmap_sem);
  1946. goto error;
  1947. }
  1948. /*
  1949. * now insert the vma in the vm list for the process, must be
  1950. * done with mmap lock held
  1951. */
  1952. insert_vm_struct(mm, vma);
  1953. vm_stat_account(vma->vm_mm, vma->vm_flags, vma_pages(vma));
  1954. up_write(&task->mm->mmap_sem);
  1955. /*
  1956. * keep track of user level virtual address
  1957. */
  1958. ctx->ctx_smpl_vaddr = (void *)vma->vm_start;
  1959. *(unsigned long *)user_vaddr = vma->vm_start;
  1960. return 0;
  1961. error:
  1962. kmem_cache_free(vm_area_cachep, vma);
  1963. error_kmem:
  1964. pfm_rvfree(smpl_buf, size);
  1965. return -ENOMEM;
  1966. }
  1967. /*
  1968. * XXX: do something better here
  1969. */
  1970. static int
  1971. pfm_bad_permissions(struct task_struct *task)
  1972. {
  1973. const struct cred *tcred;
  1974. kuid_t uid = current_uid();
  1975. kgid_t gid = current_gid();
  1976. int ret;
  1977. rcu_read_lock();
  1978. tcred = __task_cred(task);
  1979. /* inspired by ptrace_attach() */
  1980. DPRINT(("cur: uid=%d gid=%d task: euid=%d suid=%d uid=%d egid=%d sgid=%d\n",
  1981. from_kuid(&init_user_ns, uid),
  1982. from_kgid(&init_user_ns, gid),
  1983. from_kuid(&init_user_ns, tcred->euid),
  1984. from_kuid(&init_user_ns, tcred->suid),
  1985. from_kuid(&init_user_ns, tcred->uid),
  1986. from_kgid(&init_user_ns, tcred->egid),
  1987. from_kgid(&init_user_ns, tcred->sgid)));
  1988. ret = ((!uid_eq(uid, tcred->euid))
  1989. || (!uid_eq(uid, tcred->suid))
  1990. || (!uid_eq(uid, tcred->uid))
  1991. || (!gid_eq(gid, tcred->egid))
  1992. || (!gid_eq(gid, tcred->sgid))
  1993. || (!gid_eq(gid, tcred->gid))) && !capable(CAP_SYS_PTRACE);
  1994. rcu_read_unlock();
  1995. return ret;
  1996. }
  1997. static int
  1998. pfarg_is_sane(struct task_struct *task, pfarg_context_t *pfx)
  1999. {
  2000. int ctx_flags;
  2001. /* valid signal */
  2002. ctx_flags = pfx->ctx_flags;
  2003. if (ctx_flags & PFM_FL_SYSTEM_WIDE) {
  2004. /*
  2005. * cannot block in this mode
  2006. */
  2007. if (ctx_flags & PFM_FL_NOTIFY_BLOCK) {
  2008. DPRINT(("cannot use blocking mode when in system wide monitoring\n"));
  2009. return -EINVAL;
  2010. }
  2011. } else {
  2012. }
  2013. /* probably more to add here */
  2014. return 0;
  2015. }
  2016. static int
  2017. pfm_setup_buffer_fmt(struct task_struct *task, struct file *filp, pfm_context_t *ctx, unsigned int ctx_flags,
  2018. unsigned int cpu, pfarg_context_t *arg)
  2019. {
  2020. pfm_buffer_fmt_t *fmt = NULL;
  2021. unsigned long size = 0UL;
  2022. void *uaddr = NULL;
  2023. void *fmt_arg = NULL;
  2024. int ret = 0;
  2025. #define PFM_CTXARG_BUF_ARG(a) (pfm_buffer_fmt_t *)(a+1)
  2026. /* invoke and lock buffer format, if found */
  2027. fmt = pfm_find_buffer_fmt(arg->ctx_smpl_buf_id);
  2028. if (fmt == NULL) {
  2029. DPRINT(("[%d] cannot find buffer format\n", task_pid_nr(task)));
  2030. return -EINVAL;
  2031. }
  2032. /*
  2033. * buffer argument MUST be contiguous to pfarg_context_t
  2034. */
  2035. if (fmt->fmt_arg_size) fmt_arg = PFM_CTXARG_BUF_ARG(arg);
  2036. ret = pfm_buf_fmt_validate(fmt, task, ctx_flags, cpu, fmt_arg);
  2037. DPRINT(("[%d] after validate(0x%x,%d,%p)=%d\n", task_pid_nr(task), ctx_flags, cpu, fmt_arg, ret));
  2038. if (ret) goto error;
  2039. /* link buffer format and context */
  2040. ctx->ctx_buf_fmt = fmt;
  2041. ctx->ctx_fl_is_sampling = 1; /* assume record() is defined */
  2042. /*
  2043. * check if buffer format wants to use perfmon buffer allocation/mapping service
  2044. */
  2045. ret = pfm_buf_fmt_getsize(fmt, task, ctx_flags, cpu, fmt_arg, &size);
  2046. if (ret) goto error;
  2047. if (size) {
  2048. /*
  2049. * buffer is always remapped into the caller's address space
  2050. */
  2051. ret = pfm_smpl_buffer_alloc(current, filp, ctx, size, &uaddr);
  2052. if (ret) goto error;
  2053. /* keep track of user address of buffer */
  2054. arg->ctx_smpl_vaddr = uaddr;
  2055. }
  2056. ret = pfm_buf_fmt_init(fmt, task, ctx->ctx_smpl_hdr, ctx_flags, cpu, fmt_arg);
  2057. error:
  2058. return ret;
  2059. }
  2060. static void
  2061. pfm_reset_pmu_state(pfm_context_t *ctx)
  2062. {
  2063. int i;
  2064. /*
  2065. * install reset values for PMC.
  2066. */
  2067. for (i=1; PMC_IS_LAST(i) == 0; i++) {
  2068. if (PMC_IS_IMPL(i) == 0) continue;
  2069. ctx->ctx_pmcs[i] = PMC_DFL_VAL(i);
  2070. DPRINT(("pmc[%d]=0x%lx\n", i, ctx->ctx_pmcs[i]));
  2071. }
  2072. /*
  2073. * PMD registers are set to 0UL when the context in memset()
  2074. */
  2075. /*
  2076. * On context switched restore, we must restore ALL pmc and ALL pmd even
  2077. * when they are not actively used by the task. In UP, the incoming process
  2078. * may otherwise pick up left over PMC, PMD state from the previous process.
  2079. * As opposed to PMD, stale PMC can cause harm to the incoming
  2080. * process because they may change what is being measured.
  2081. * Therefore, we must systematically reinstall the entire
  2082. * PMC state. In SMP, the same thing is possible on the
  2083. * same CPU but also on between 2 CPUs.
  2084. *
  2085. * The problem with PMD is information leaking especially
  2086. * to user level when psr.sp=0
  2087. *
  2088. * There is unfortunately no easy way to avoid this problem
  2089. * on either UP or SMP. This definitively slows down the
  2090. * pfm_load_regs() function.
  2091. */
  2092. /*
  2093. * bitmask of all PMCs accessible to this context
  2094. *
  2095. * PMC0 is treated differently.
  2096. */
  2097. ctx->ctx_all_pmcs[0] = pmu_conf->impl_pmcs[0] & ~0x1;
  2098. /*
  2099. * bitmask of all PMDs that are accessible to this context
  2100. */
  2101. ctx->ctx_all_pmds[0] = pmu_conf->impl_pmds[0];
  2102. DPRINT(("<%d> all_pmcs=0x%lx all_pmds=0x%lx\n", ctx->ctx_fd, ctx->ctx_all_pmcs[0],ctx->ctx_all_pmds[0]));
  2103. /*
  2104. * useful in case of re-enable after disable
  2105. */
  2106. ctx->ctx_used_ibrs[0] = 0UL;
  2107. ctx->ctx_used_dbrs[0] = 0UL;
  2108. }
  2109. static int
  2110. pfm_ctx_getsize(void *arg, size_t *sz)
  2111. {
  2112. pfarg_context_t *req = (pfarg_context_t *)arg;
  2113. pfm_buffer_fmt_t *fmt;
  2114. *sz = 0;
  2115. if (!pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) return 0;
  2116. fmt = pfm_find_buffer_fmt(req->ctx_smpl_buf_id);
  2117. if (fmt == NULL) {
  2118. DPRINT(("cannot find buffer format\n"));
  2119. return -EINVAL;
  2120. }
  2121. /* get just enough to copy in user parameters */
  2122. *sz = fmt->fmt_arg_size;
  2123. DPRINT(("arg_size=%lu\n", *sz));
  2124. return 0;
  2125. }
  2126. /*
  2127. * cannot attach if :
  2128. * - kernel task
  2129. * - task not owned by caller
  2130. * - task incompatible with context mode
  2131. */
  2132. static int
  2133. pfm_task_incompatible(pfm_context_t *ctx, struct task_struct *task)
  2134. {
  2135. /*
  2136. * no kernel task or task not owner by caller
  2137. */
  2138. if (task->mm == NULL) {
  2139. DPRINT(("task [%d] has not memory context (kernel thread)\n", task_pid_nr(task)));
  2140. return -EPERM;
  2141. }
  2142. if (pfm_bad_permissions(task)) {
  2143. DPRINT(("no permission to attach to [%d]\n", task_pid_nr(task)));
  2144. return -EPERM;
  2145. }
  2146. /*
  2147. * cannot block in self-monitoring mode
  2148. */
  2149. if (CTX_OVFL_NOBLOCK(ctx) == 0 && task == current) {
  2150. DPRINT(("cannot load a blocking context on self for [%d]\n", task_pid_nr(task)));
  2151. return -EINVAL;
  2152. }
  2153. if (task->exit_state == EXIT_ZOMBIE) {
  2154. DPRINT(("cannot attach to zombie task [%d]\n", task_pid_nr(task)));
  2155. return -EBUSY;
  2156. }
  2157. /*
  2158. * always ok for self
  2159. */
  2160. if (task == current) return 0;
  2161. if (!task_is_stopped_or_traced(task)) {
  2162. DPRINT(("cannot attach to non-stopped task [%d] state=%ld\n", task_pid_nr(task), task->state));
  2163. return -EBUSY;
  2164. }
  2165. /*
  2166. * make sure the task is off any CPU
  2167. */
  2168. wait_task_inactive(task, 0);
  2169. /* more to come... */
  2170. return 0;
  2171. }
  2172. static int
  2173. pfm_get_task(pfm_context_t *ctx, pid_t pid, struct task_struct **task)
  2174. {
  2175. struct task_struct *p = current;
  2176. int ret;
  2177. /* XXX: need to add more checks here */
  2178. if (pid < 2) return -EPERM;
  2179. if (pid != task_pid_vnr(current)) {
  2180. /* make sure task cannot go away while we operate on it */
  2181. p = find_get_task_by_vpid(pid);
  2182. if (!p)
  2183. return -ESRCH;
  2184. }
  2185. ret = pfm_task_incompatible(ctx, p);
  2186. if (ret == 0) {
  2187. *task = p;
  2188. } else if (p != current) {
  2189. pfm_put_task(p);
  2190. }
  2191. return ret;
  2192. }
  2193. static int
  2194. pfm_context_create(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2195. {
  2196. pfarg_context_t *req = (pfarg_context_t *)arg;
  2197. struct file *filp;
  2198. struct path path;
  2199. int ctx_flags;
  2200. int fd;
  2201. int ret;
  2202. /* let's check the arguments first */
  2203. ret = pfarg_is_sane(current, req);
  2204. if (ret < 0)
  2205. return ret;
  2206. ctx_flags = req->ctx_flags;
  2207. ret = -ENOMEM;
  2208. fd = get_unused_fd_flags(0);
  2209. if (fd < 0)
  2210. return fd;
  2211. ctx = pfm_context_alloc(ctx_flags);
  2212. if (!ctx)
  2213. goto error;
  2214. filp = pfm_alloc_file(ctx);
  2215. if (IS_ERR(filp)) {
  2216. ret = PTR_ERR(filp);
  2217. goto error_file;
  2218. }
  2219. req->ctx_fd = ctx->ctx_fd = fd;
  2220. /*
  2221. * does the user want to sample?
  2222. */
  2223. if (pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) {
  2224. ret = pfm_setup_buffer_fmt(current, filp, ctx, ctx_flags, 0, req);
  2225. if (ret)
  2226. goto buffer_error;
  2227. }
  2228. DPRINT(("ctx=%p flags=0x%x system=%d notify_block=%d excl_idle=%d no_msg=%d ctx_fd=%d\n",
  2229. ctx,
  2230. ctx_flags,
  2231. ctx->ctx_fl_system,
  2232. ctx->ctx_fl_block,
  2233. ctx->ctx_fl_excl_idle,
  2234. ctx->ctx_fl_no_msg,
  2235. ctx->ctx_fd));
  2236. /*
  2237. * initialize soft PMU state
  2238. */
  2239. pfm_reset_pmu_state(ctx);
  2240. fd_install(fd, filp);
  2241. return 0;
  2242. buffer_error:
  2243. path = filp->f_path;
  2244. put_filp(filp);
  2245. path_put(&path);
  2246. if (ctx->ctx_buf_fmt) {
  2247. pfm_buf_fmt_exit(ctx->ctx_buf_fmt, current, NULL, regs);
  2248. }
  2249. error_file:
  2250. pfm_context_free(ctx);
  2251. error:
  2252. put_unused_fd(fd);
  2253. return ret;
  2254. }
  2255. static inline unsigned long
  2256. pfm_new_counter_value (pfm_counter_t *reg, int is_long_reset)
  2257. {
  2258. unsigned long val = is_long_reset ? reg->long_reset : reg->short_reset;
  2259. unsigned long new_seed, old_seed = reg->seed, mask = reg->mask;
  2260. extern unsigned long carta_random32 (unsigned long seed);
  2261. if (reg->flags & PFM_REGFL_RANDOM) {
  2262. new_seed = carta_random32(old_seed);
  2263. val -= (old_seed & mask); /* counter values are negative numbers! */
  2264. if ((mask >> 32) != 0)
  2265. /* construct a full 64-bit random value: */
  2266. new_seed |= carta_random32(old_seed >> 32) << 32;
  2267. reg->seed = new_seed;
  2268. }
  2269. reg->lval = val;
  2270. return val;
  2271. }
  2272. static void
  2273. pfm_reset_regs_masked(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
  2274. {
  2275. unsigned long mask = ovfl_regs[0];
  2276. unsigned long reset_others = 0UL;
  2277. unsigned long val;
  2278. int i;
  2279. /*
  2280. * now restore reset value on sampling overflowed counters
  2281. */
  2282. mask >>= PMU_FIRST_COUNTER;
  2283. for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
  2284. if ((mask & 0x1UL) == 0UL) continue;
  2285. ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
  2286. reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
  2287. DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
  2288. }
  2289. /*
  2290. * Now take care of resetting the other registers
  2291. */
  2292. for(i = 0; reset_others; i++, reset_others >>= 1) {
  2293. if ((reset_others & 0x1) == 0) continue;
  2294. ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
  2295. DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
  2296. is_long_reset ? "long" : "short", i, val));
  2297. }
  2298. }
  2299. static void
  2300. pfm_reset_regs(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
  2301. {
  2302. unsigned long mask = ovfl_regs[0];
  2303. unsigned long reset_others = 0UL;
  2304. unsigned long val;
  2305. int i;
  2306. DPRINT_ovfl(("ovfl_regs=0x%lx is_long_reset=%d\n", ovfl_regs[0], is_long_reset));
  2307. if (ctx->ctx_state == PFM_CTX_MASKED) {
  2308. pfm_reset_regs_masked(ctx, ovfl_regs, is_long_reset);
  2309. return;
  2310. }
  2311. /*
  2312. * now restore reset value on sampling overflowed counters
  2313. */
  2314. mask >>= PMU_FIRST_COUNTER;
  2315. for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
  2316. if ((mask & 0x1UL) == 0UL) continue;
  2317. val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
  2318. reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
  2319. DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
  2320. pfm_write_soft_counter(ctx, i, val);
  2321. }
  2322. /*
  2323. * Now take care of resetting the other registers
  2324. */
  2325. for(i = 0; reset_others; i++, reset_others >>= 1) {
  2326. if ((reset_others & 0x1) == 0) continue;
  2327. val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
  2328. if (PMD_IS_COUNTING(i)) {
  2329. pfm_write_soft_counter(ctx, i, val);
  2330. } else {
  2331. ia64_set_pmd(i, val);
  2332. }
  2333. DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
  2334. is_long_reset ? "long" : "short", i, val));
  2335. }
  2336. ia64_srlz_d();
  2337. }
  2338. static int
  2339. pfm_write_pmcs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2340. {
  2341. struct task_struct *task;
  2342. pfarg_reg_t *req = (pfarg_reg_t *)arg;
  2343. unsigned long value, pmc_pm;
  2344. unsigned long smpl_pmds, reset_pmds, impl_pmds;
  2345. unsigned int cnum, reg_flags, flags, pmc_type;
  2346. int i, can_access_pmu = 0, is_loaded, is_system, expert_mode;
  2347. int is_monitor, is_counting, state;
  2348. int ret = -EINVAL;
  2349. pfm_reg_check_t wr_func;
  2350. #define PFM_CHECK_PMC_PM(x, y, z) ((x)->ctx_fl_system ^ PMC_PM(y, z))
  2351. state = ctx->ctx_state;
  2352. is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
  2353. is_system = ctx->ctx_fl_system;
  2354. task = ctx->ctx_task;
  2355. impl_pmds = pmu_conf->impl_pmds[0];
  2356. if (state == PFM_CTX_ZOMBIE) return -EINVAL;
  2357. if (is_loaded) {
  2358. /*
  2359. * In system wide and when the context is loaded, access can only happen
  2360. * when the caller is running on the CPU being monitored by the session.
  2361. * It does not have to be the owner (ctx_task) of the context per se.
  2362. */
  2363. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  2364. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  2365. return -EBUSY;
  2366. }
  2367. can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
  2368. }
  2369. expert_mode = pfm_sysctl.expert_mode;
  2370. for (i = 0; i < count; i++, req++) {
  2371. cnum = req->reg_num;
  2372. reg_flags = req->reg_flags;
  2373. value = req->reg_value;
  2374. smpl_pmds = req->reg_smpl_pmds[0];
  2375. reset_pmds = req->reg_reset_pmds[0];
  2376. flags = 0;
  2377. if (cnum >= PMU_MAX_PMCS) {
  2378. DPRINT(("pmc%u is invalid\n", cnum));
  2379. goto error;
  2380. }
  2381. pmc_type = pmu_conf->pmc_desc[cnum].type;
  2382. pmc_pm = (value >> pmu_conf->pmc_desc[cnum].pm_pos) & 0x1;
  2383. is_counting = (pmc_type & PFM_REG_COUNTING) == PFM_REG_COUNTING ? 1 : 0;
  2384. is_monitor = (pmc_type & PFM_REG_MONITOR) == PFM_REG_MONITOR ? 1 : 0;
  2385. /*
  2386. * we reject all non implemented PMC as well
  2387. * as attempts to modify PMC[0-3] which are used
  2388. * as status registers by the PMU
  2389. */
  2390. if ((pmc_type & PFM_REG_IMPL) == 0 || (pmc_type & PFM_REG_CONTROL) == PFM_REG_CONTROL) {
  2391. DPRINT(("pmc%u is unimplemented or no-access pmc_type=%x\n", cnum, pmc_type));
  2392. goto error;
  2393. }
  2394. wr_func = pmu_conf->pmc_desc[cnum].write_check;
  2395. /*
  2396. * If the PMC is a monitor, then if the value is not the default:
  2397. * - system-wide session: PMCx.pm=1 (privileged monitor)
  2398. * - per-task : PMCx.pm=0 (user monitor)
  2399. */
  2400. if (is_monitor && value != PMC_DFL_VAL(cnum) && is_system ^ pmc_pm) {
  2401. DPRINT(("pmc%u pmc_pm=%lu is_system=%d\n",
  2402. cnum,
  2403. pmc_pm,
  2404. is_system));
  2405. goto error;
  2406. }
  2407. if (is_counting) {
  2408. /*
  2409. * enforce generation of overflow interrupt. Necessary on all
  2410. * CPUs.
  2411. */
  2412. value |= 1 << PMU_PMC_OI;
  2413. if (reg_flags & PFM_REGFL_OVFL_NOTIFY) {
  2414. flags |= PFM_REGFL_OVFL_NOTIFY;
  2415. }
  2416. if (reg_flags & PFM_REGFL_RANDOM) flags |= PFM_REGFL_RANDOM;
  2417. /* verify validity of smpl_pmds */
  2418. if ((smpl_pmds & impl_pmds) != smpl_pmds) {
  2419. DPRINT(("invalid smpl_pmds 0x%lx for pmc%u\n", smpl_pmds, cnum));
  2420. goto error;
  2421. }
  2422. /* verify validity of reset_pmds */
  2423. if ((reset_pmds & impl_pmds) != reset_pmds) {
  2424. DPRINT(("invalid reset_pmds 0x%lx for pmc%u\n", reset_pmds, cnum));
  2425. goto error;
  2426. }
  2427. } else {
  2428. if (reg_flags & (PFM_REGFL_OVFL_NOTIFY|PFM_REGFL_RANDOM)) {
  2429. DPRINT(("cannot set ovfl_notify or random on pmc%u\n", cnum));
  2430. goto error;
  2431. }
  2432. /* eventid on non-counting monitors are ignored */
  2433. }
  2434. /*
  2435. * execute write checker, if any
  2436. */
  2437. if (likely(expert_mode == 0 && wr_func)) {
  2438. ret = (*wr_func)(task, ctx, cnum, &value, regs);
  2439. if (ret) goto error;
  2440. ret = -EINVAL;
  2441. }
  2442. /*
  2443. * no error on this register
  2444. */
  2445. PFM_REG_RETFLAG_SET(req->reg_flags, 0);
  2446. /*
  2447. * Now we commit the changes to the software state
  2448. */
  2449. /*
  2450. * update overflow information
  2451. */
  2452. if (is_counting) {
  2453. /*
  2454. * full flag update each time a register is programmed
  2455. */
  2456. ctx->ctx_pmds[cnum].flags = flags;
  2457. ctx->ctx_pmds[cnum].reset_pmds[0] = reset_pmds;
  2458. ctx->ctx_pmds[cnum].smpl_pmds[0] = smpl_pmds;
  2459. ctx->ctx_pmds[cnum].eventid = req->reg_smpl_eventid;
  2460. /*
  2461. * Mark all PMDS to be accessed as used.
  2462. *
  2463. * We do not keep track of PMC because we have to
  2464. * systematically restore ALL of them.
  2465. *
  2466. * We do not update the used_monitors mask, because
  2467. * if we have not programmed them, then will be in
  2468. * a quiescent state, therefore we will not need to
  2469. * mask/restore then when context is MASKED.
  2470. */
  2471. CTX_USED_PMD(ctx, reset_pmds);
  2472. CTX_USED_PMD(ctx, smpl_pmds);
  2473. /*
  2474. * make sure we do not try to reset on
  2475. * restart because we have established new values
  2476. */
  2477. if (state == PFM_CTX_MASKED) ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
  2478. }
  2479. /*
  2480. * Needed in case the user does not initialize the equivalent
  2481. * PMD. Clearing is done indirectly via pfm_reset_pmu_state() so there is no
  2482. * possible leak here.
  2483. */
  2484. CTX_USED_PMD(ctx, pmu_conf->pmc_desc[cnum].dep_pmd[0]);
  2485. /*
  2486. * keep track of the monitor PMC that we are using.
  2487. * we save the value of the pmc in ctx_pmcs[] and if
  2488. * the monitoring is not stopped for the context we also
  2489. * place it in the saved state area so that it will be
  2490. * picked up later by the context switch code.
  2491. *
  2492. * The value in ctx_pmcs[] can only be changed in pfm_write_pmcs().
  2493. *
  2494. * The value in th_pmcs[] may be modified on overflow, i.e., when
  2495. * monitoring needs to be stopped.
  2496. */
  2497. if (is_monitor) CTX_USED_MONITOR(ctx, 1UL << cnum);
  2498. /*
  2499. * update context state
  2500. */
  2501. ctx->ctx_pmcs[cnum] = value;
  2502. if (is_loaded) {
  2503. /*
  2504. * write thread state
  2505. */
  2506. if (is_system == 0) ctx->th_pmcs[cnum] = value;
  2507. /*
  2508. * write hardware register if we can
  2509. */
  2510. if (can_access_pmu) {
  2511. ia64_set_pmc(cnum, value);
  2512. }
  2513. #ifdef CONFIG_SMP
  2514. else {
  2515. /*
  2516. * per-task SMP only here
  2517. *
  2518. * we are guaranteed that the task is not running on the other CPU,
  2519. * we indicate that this PMD will need to be reloaded if the task
  2520. * is rescheduled on the CPU it ran last on.
  2521. */
  2522. ctx->ctx_reload_pmcs[0] |= 1UL << cnum;
  2523. }
  2524. #endif
  2525. }
  2526. DPRINT(("pmc[%u]=0x%lx ld=%d apmu=%d flags=0x%x all_pmcs=0x%lx used_pmds=0x%lx eventid=%ld smpl_pmds=0x%lx reset_pmds=0x%lx reloads_pmcs=0x%lx used_monitors=0x%lx ovfl_regs=0x%lx\n",
  2527. cnum,
  2528. value,
  2529. is_loaded,
  2530. can_access_pmu,
  2531. flags,
  2532. ctx->ctx_all_pmcs[0],
  2533. ctx->ctx_used_pmds[0],
  2534. ctx->ctx_pmds[cnum].eventid,
  2535. smpl_pmds,
  2536. reset_pmds,
  2537. ctx->ctx_reload_pmcs[0],
  2538. ctx->ctx_used_monitors[0],
  2539. ctx->ctx_ovfl_regs[0]));
  2540. }
  2541. /*
  2542. * make sure the changes are visible
  2543. */
  2544. if (can_access_pmu) ia64_srlz_d();
  2545. return 0;
  2546. error:
  2547. PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
  2548. return ret;
  2549. }
  2550. static int
  2551. pfm_write_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2552. {
  2553. struct task_struct *task;
  2554. pfarg_reg_t *req = (pfarg_reg_t *)arg;
  2555. unsigned long value, hw_value, ovfl_mask;
  2556. unsigned int cnum;
  2557. int i, can_access_pmu = 0, state;
  2558. int is_counting, is_loaded, is_system, expert_mode;
  2559. int ret = -EINVAL;
  2560. pfm_reg_check_t wr_func;
  2561. state = ctx->ctx_state;
  2562. is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
  2563. is_system = ctx->ctx_fl_system;
  2564. ovfl_mask = pmu_conf->ovfl_val;
  2565. task = ctx->ctx_task;
  2566. if (unlikely(state == PFM_CTX_ZOMBIE)) return -EINVAL;
  2567. /*
  2568. * on both UP and SMP, we can only write to the PMC when the task is
  2569. * the owner of the local PMU.
  2570. */
  2571. if (likely(is_loaded)) {
  2572. /*
  2573. * In system wide and when the context is loaded, access can only happen
  2574. * when the caller is running on the CPU being monitored by the session.
  2575. * It does not have to be the owner (ctx_task) of the context per se.
  2576. */
  2577. if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
  2578. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  2579. return -EBUSY;
  2580. }
  2581. can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
  2582. }
  2583. expert_mode = pfm_sysctl.expert_mode;
  2584. for (i = 0; i < count; i++, req++) {
  2585. cnum = req->reg_num;
  2586. value = req->reg_value;
  2587. if (!PMD_IS_IMPL(cnum)) {
  2588. DPRINT(("pmd[%u] is unimplemented or invalid\n", cnum));
  2589. goto abort_mission;
  2590. }
  2591. is_counting = PMD_IS_COUNTING(cnum);
  2592. wr_func = pmu_conf->pmd_desc[cnum].write_check;
  2593. /*
  2594. * execute write checker, if any
  2595. */
  2596. if (unlikely(expert_mode == 0 && wr_func)) {
  2597. unsigned long v = value;
  2598. ret = (*wr_func)(task, ctx, cnum, &v, regs);
  2599. if (ret) goto abort_mission;
  2600. value = v;
  2601. ret = -EINVAL;
  2602. }
  2603. /*
  2604. * no error on this register
  2605. */
  2606. PFM_REG_RETFLAG_SET(req->reg_flags, 0);
  2607. /*
  2608. * now commit changes to software state
  2609. */
  2610. hw_value = value;
  2611. /*
  2612. * update virtualized (64bits) counter
  2613. */
  2614. if (is_counting) {
  2615. /*
  2616. * write context state
  2617. */
  2618. ctx->ctx_pmds[cnum].lval = value;
  2619. /*
  2620. * when context is load we use the split value
  2621. */
  2622. if (is_loaded) {
  2623. hw_value = value & ovfl_mask;
  2624. value = value & ~ovfl_mask;
  2625. }
  2626. }
  2627. /*
  2628. * update reset values (not just for counters)
  2629. */
  2630. ctx->ctx_pmds[cnum].long_reset = req->reg_long_reset;
  2631. ctx->ctx_pmds[cnum].short_reset = req->reg_short_reset;
  2632. /*
  2633. * update randomization parameters (not just for counters)
  2634. */
  2635. ctx->ctx_pmds[cnum].seed = req->reg_random_seed;
  2636. ctx->ctx_pmds[cnum].mask = req->reg_random_mask;
  2637. /*
  2638. * update context value
  2639. */
  2640. ctx->ctx_pmds[cnum].val = value;
  2641. /*
  2642. * Keep track of what we use
  2643. *
  2644. * We do not keep track of PMC because we have to
  2645. * systematically restore ALL of them.
  2646. */
  2647. CTX_USED_PMD(ctx, PMD_PMD_DEP(cnum));
  2648. /*
  2649. * mark this PMD register used as well
  2650. */
  2651. CTX_USED_PMD(ctx, RDEP(cnum));
  2652. /*
  2653. * make sure we do not try to reset on
  2654. * restart because we have established new values
  2655. */
  2656. if (is_counting && state == PFM_CTX_MASKED) {
  2657. ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
  2658. }
  2659. if (is_loaded) {
  2660. /*
  2661. * write thread state
  2662. */
  2663. if (is_system == 0) ctx->th_pmds[cnum] = hw_value;
  2664. /*
  2665. * write hardware register if we can
  2666. */
  2667. if (can_access_pmu) {
  2668. ia64_set_pmd(cnum, hw_value);
  2669. } else {
  2670. #ifdef CONFIG_SMP
  2671. /*
  2672. * we are guaranteed that the task is not running on the other CPU,
  2673. * we indicate that this PMD will need to be reloaded if the task
  2674. * is rescheduled on the CPU it ran last on.
  2675. */
  2676. ctx->ctx_reload_pmds[0] |= 1UL << cnum;
  2677. #endif
  2678. }
  2679. }
  2680. DPRINT(("pmd[%u]=0x%lx ld=%d apmu=%d, hw_value=0x%lx ctx_pmd=0x%lx short_reset=0x%lx "
  2681. "long_reset=0x%lx notify=%c seed=0x%lx mask=0x%lx used_pmds=0x%lx reset_pmds=0x%lx reload_pmds=0x%lx all_pmds=0x%lx ovfl_regs=0x%lx\n",
  2682. cnum,
  2683. value,
  2684. is_loaded,
  2685. can_access_pmu,
  2686. hw_value,
  2687. ctx->ctx_pmds[cnum].val,
  2688. ctx->ctx_pmds[cnum].short_reset,
  2689. ctx->ctx_pmds[cnum].long_reset,
  2690. PMC_OVFL_NOTIFY(ctx, cnum) ? 'Y':'N',
  2691. ctx->ctx_pmds[cnum].seed,
  2692. ctx->ctx_pmds[cnum].mask,
  2693. ctx->ctx_used_pmds[0],
  2694. ctx->ctx_pmds[cnum].reset_pmds[0],
  2695. ctx->ctx_reload_pmds[0],
  2696. ctx->ctx_all_pmds[0],
  2697. ctx->ctx_ovfl_regs[0]));
  2698. }
  2699. /*
  2700. * make changes visible
  2701. */
  2702. if (can_access_pmu) ia64_srlz_d();
  2703. return 0;
  2704. abort_mission:
  2705. /*
  2706. * for now, we have only one possibility for error
  2707. */
  2708. PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
  2709. return ret;
  2710. }
  2711. /*
  2712. * By the way of PROTECT_CONTEXT(), interrupts are masked while we are in this function.
  2713. * Therefore we know, we do not have to worry about the PMU overflow interrupt. If an
  2714. * interrupt is delivered during the call, it will be kept pending until we leave, making
  2715. * it appears as if it had been generated at the UNPROTECT_CONTEXT(). At least we are
  2716. * guaranteed to return consistent data to the user, it may simply be old. It is not
  2717. * trivial to treat the overflow while inside the call because you may end up in
  2718. * some module sampling buffer code causing deadlocks.
  2719. */
  2720. static int
  2721. pfm_read_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2722. {
  2723. struct task_struct *task;
  2724. unsigned long val = 0UL, lval, ovfl_mask, sval;
  2725. pfarg_reg_t *req = (pfarg_reg_t *)arg;
  2726. unsigned int cnum, reg_flags = 0;
  2727. int i, can_access_pmu = 0, state;
  2728. int is_loaded, is_system, is_counting, expert_mode;
  2729. int ret = -EINVAL;
  2730. pfm_reg_check_t rd_func;
  2731. /*
  2732. * access is possible when loaded only for
  2733. * self-monitoring tasks or in UP mode
  2734. */
  2735. state = ctx->ctx_state;
  2736. is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
  2737. is_system = ctx->ctx_fl_system;
  2738. ovfl_mask = pmu_conf->ovfl_val;
  2739. task = ctx->ctx_task;
  2740. if (state == PFM_CTX_ZOMBIE) return -EINVAL;
  2741. if (likely(is_loaded)) {
  2742. /*
  2743. * In system wide and when the context is loaded, access can only happen
  2744. * when the caller is running on the CPU being monitored by the session.
  2745. * It does not have to be the owner (ctx_task) of the context per se.
  2746. */
  2747. if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
  2748. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  2749. return -EBUSY;
  2750. }
  2751. /*
  2752. * this can be true when not self-monitoring only in UP
  2753. */
  2754. can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
  2755. if (can_access_pmu) ia64_srlz_d();
  2756. }
  2757. expert_mode = pfm_sysctl.expert_mode;
  2758. DPRINT(("ld=%d apmu=%d ctx_state=%d\n",
  2759. is_loaded,
  2760. can_access_pmu,
  2761. state));
  2762. /*
  2763. * on both UP and SMP, we can only read the PMD from the hardware register when
  2764. * the task is the owner of the local PMU.
  2765. */
  2766. for (i = 0; i < count; i++, req++) {
  2767. cnum = req->reg_num;
  2768. reg_flags = req->reg_flags;
  2769. if (unlikely(!PMD_IS_IMPL(cnum))) goto error;
  2770. /*
  2771. * we can only read the register that we use. That includes
  2772. * the one we explicitly initialize AND the one we want included
  2773. * in the sampling buffer (smpl_regs).
  2774. *
  2775. * Having this restriction allows optimization in the ctxsw routine
  2776. * without compromising security (leaks)
  2777. */
  2778. if (unlikely(!CTX_IS_USED_PMD(ctx, cnum))) goto error;
  2779. sval = ctx->ctx_pmds[cnum].val;
  2780. lval = ctx->ctx_pmds[cnum].lval;
  2781. is_counting = PMD_IS_COUNTING(cnum);
  2782. /*
  2783. * If the task is not the current one, then we check if the
  2784. * PMU state is still in the local live register due to lazy ctxsw.
  2785. * If true, then we read directly from the registers.
  2786. */
  2787. if (can_access_pmu){
  2788. val = ia64_get_pmd(cnum);
  2789. } else {
  2790. /*
  2791. * context has been saved
  2792. * if context is zombie, then task does not exist anymore.
  2793. * In this case, we use the full value saved in the context (pfm_flush_regs()).
  2794. */
  2795. val = is_loaded ? ctx->th_pmds[cnum] : 0UL;
  2796. }
  2797. rd_func = pmu_conf->pmd_desc[cnum].read_check;
  2798. if (is_counting) {
  2799. /*
  2800. * XXX: need to check for overflow when loaded
  2801. */
  2802. val &= ovfl_mask;
  2803. val += sval;
  2804. }
  2805. /*
  2806. * execute read checker, if any
  2807. */
  2808. if (unlikely(expert_mode == 0 && rd_func)) {
  2809. unsigned long v = val;
  2810. ret = (*rd_func)(ctx->ctx_task, ctx, cnum, &v, regs);
  2811. if (ret) goto error;
  2812. val = v;
  2813. ret = -EINVAL;
  2814. }
  2815. PFM_REG_RETFLAG_SET(reg_flags, 0);
  2816. DPRINT(("pmd[%u]=0x%lx\n", cnum, val));
  2817. /*
  2818. * update register return value, abort all if problem during copy.
  2819. * we only modify the reg_flags field. no check mode is fine because
  2820. * access has been verified upfront in sys_perfmonctl().
  2821. */
  2822. req->reg_value = val;
  2823. req->reg_flags = reg_flags;
  2824. req->reg_last_reset_val = lval;
  2825. }
  2826. return 0;
  2827. error:
  2828. PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
  2829. return ret;
  2830. }
  2831. int
  2832. pfm_mod_write_pmcs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
  2833. {
  2834. pfm_context_t *ctx;
  2835. if (req == NULL) return -EINVAL;
  2836. ctx = GET_PMU_CTX();
  2837. if (ctx == NULL) return -EINVAL;
  2838. /*
  2839. * for now limit to current task, which is enough when calling
  2840. * from overflow handler
  2841. */
  2842. if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
  2843. return pfm_write_pmcs(ctx, req, nreq, regs);
  2844. }
  2845. EXPORT_SYMBOL(pfm_mod_write_pmcs);
  2846. int
  2847. pfm_mod_read_pmds(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
  2848. {
  2849. pfm_context_t *ctx;
  2850. if (req == NULL) return -EINVAL;
  2851. ctx = GET_PMU_CTX();
  2852. if (ctx == NULL) return -EINVAL;
  2853. /*
  2854. * for now limit to current task, which is enough when calling
  2855. * from overflow handler
  2856. */
  2857. if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
  2858. return pfm_read_pmds(ctx, req, nreq, regs);
  2859. }
  2860. EXPORT_SYMBOL(pfm_mod_read_pmds);
  2861. /*
  2862. * Only call this function when a process it trying to
  2863. * write the debug registers (reading is always allowed)
  2864. */
  2865. int
  2866. pfm_use_debug_registers(struct task_struct *task)
  2867. {
  2868. pfm_context_t *ctx = task->thread.pfm_context;
  2869. unsigned long flags;
  2870. int ret = 0;
  2871. if (pmu_conf->use_rr_dbregs == 0) return 0;
  2872. DPRINT(("called for [%d]\n", task_pid_nr(task)));
  2873. /*
  2874. * do it only once
  2875. */
  2876. if (task->thread.flags & IA64_THREAD_DBG_VALID) return 0;
  2877. /*
  2878. * Even on SMP, we do not need to use an atomic here because
  2879. * the only way in is via ptrace() and this is possible only when the
  2880. * process is stopped. Even in the case where the ctxsw out is not totally
  2881. * completed by the time we come here, there is no way the 'stopped' process
  2882. * could be in the middle of fiddling with the pfm_write_ibr_dbr() routine.
  2883. * So this is always safe.
  2884. */
  2885. if (ctx && ctx->ctx_fl_using_dbreg == 1) return -1;
  2886. LOCK_PFS(flags);
  2887. /*
  2888. * We cannot allow setting breakpoints when system wide monitoring
  2889. * sessions are using the debug registers.
  2890. */
  2891. if (pfm_sessions.pfs_sys_use_dbregs> 0)
  2892. ret = -1;
  2893. else
  2894. pfm_sessions.pfs_ptrace_use_dbregs++;
  2895. DPRINT(("ptrace_use_dbregs=%u sys_use_dbregs=%u by [%d] ret = %d\n",
  2896. pfm_sessions.pfs_ptrace_use_dbregs,
  2897. pfm_sessions.pfs_sys_use_dbregs,
  2898. task_pid_nr(task), ret));
  2899. UNLOCK_PFS(flags);
  2900. return ret;
  2901. }
  2902. /*
  2903. * This function is called for every task that exits with the
  2904. * IA64_THREAD_DBG_VALID set. This indicates a task which was
  2905. * able to use the debug registers for debugging purposes via
  2906. * ptrace(). Therefore we know it was not using them for
  2907. * performance monitoring, so we only decrement the number
  2908. * of "ptraced" debug register users to keep the count up to date
  2909. */
  2910. int
  2911. pfm_release_debug_registers(struct task_struct *task)
  2912. {
  2913. unsigned long flags;
  2914. int ret;
  2915. if (pmu_conf->use_rr_dbregs == 0) return 0;
  2916. LOCK_PFS(flags);
  2917. if (pfm_sessions.pfs_ptrace_use_dbregs == 0) {
  2918. printk(KERN_ERR "perfmon: invalid release for [%d] ptrace_use_dbregs=0\n", task_pid_nr(task));
  2919. ret = -1;
  2920. } else {
  2921. pfm_sessions.pfs_ptrace_use_dbregs--;
  2922. ret = 0;
  2923. }
  2924. UNLOCK_PFS(flags);
  2925. return ret;
  2926. }
  2927. static int
  2928. pfm_restart(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2929. {
  2930. struct task_struct *task;
  2931. pfm_buffer_fmt_t *fmt;
  2932. pfm_ovfl_ctrl_t rst_ctrl;
  2933. int state, is_system;
  2934. int ret = 0;
  2935. state = ctx->ctx_state;
  2936. fmt = ctx->ctx_buf_fmt;
  2937. is_system = ctx->ctx_fl_system;
  2938. task = PFM_CTX_TASK(ctx);
  2939. switch(state) {
  2940. case PFM_CTX_MASKED:
  2941. break;
  2942. case PFM_CTX_LOADED:
  2943. if (CTX_HAS_SMPL(ctx) && fmt->fmt_restart_active) break;
  2944. /* fall through */
  2945. case PFM_CTX_UNLOADED:
  2946. case PFM_CTX_ZOMBIE:
  2947. DPRINT(("invalid state=%d\n", state));
  2948. return -EBUSY;
  2949. default:
  2950. DPRINT(("state=%d, cannot operate (no active_restart handler)\n", state));
  2951. return -EINVAL;
  2952. }
  2953. /*
  2954. * In system wide and when the context is loaded, access can only happen
  2955. * when the caller is running on the CPU being monitored by the session.
  2956. * It does not have to be the owner (ctx_task) of the context per se.
  2957. */
  2958. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  2959. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  2960. return -EBUSY;
  2961. }
  2962. /* sanity check */
  2963. if (unlikely(task == NULL)) {
  2964. printk(KERN_ERR "perfmon: [%d] pfm_restart no task\n", task_pid_nr(current));
  2965. return -EINVAL;
  2966. }
  2967. if (task == current || is_system) {
  2968. fmt = ctx->ctx_buf_fmt;
  2969. DPRINT(("restarting self %d ovfl=0x%lx\n",
  2970. task_pid_nr(task),
  2971. ctx->ctx_ovfl_regs[0]));
  2972. if (CTX_HAS_SMPL(ctx)) {
  2973. prefetch(ctx->ctx_smpl_hdr);
  2974. rst_ctrl.bits.mask_monitoring = 0;
  2975. rst_ctrl.bits.reset_ovfl_pmds = 0;
  2976. if (state == PFM_CTX_LOADED)
  2977. ret = pfm_buf_fmt_restart_active(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
  2978. else
  2979. ret = pfm_buf_fmt_restart(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
  2980. } else {
  2981. rst_ctrl.bits.mask_monitoring = 0;
  2982. rst_ctrl.bits.reset_ovfl_pmds = 1;
  2983. }
  2984. if (ret == 0) {
  2985. if (rst_ctrl.bits.reset_ovfl_pmds)
  2986. pfm_reset_regs(ctx, ctx->ctx_ovfl_regs, PFM_PMD_LONG_RESET);
  2987. if (rst_ctrl.bits.mask_monitoring == 0) {
  2988. DPRINT(("resuming monitoring for [%d]\n", task_pid_nr(task)));
  2989. if (state == PFM_CTX_MASKED) pfm_restore_monitoring(task);
  2990. } else {
  2991. DPRINT(("keeping monitoring stopped for [%d]\n", task_pid_nr(task)));
  2992. // cannot use pfm_stop_monitoring(task, regs);
  2993. }
  2994. }
  2995. /*
  2996. * clear overflowed PMD mask to remove any stale information
  2997. */
  2998. ctx->ctx_ovfl_regs[0] = 0UL;
  2999. /*
  3000. * back to LOADED state
  3001. */
  3002. ctx->ctx_state = PFM_CTX_LOADED;
  3003. /*
  3004. * XXX: not really useful for self monitoring
  3005. */
  3006. ctx->ctx_fl_can_restart = 0;
  3007. return 0;
  3008. }
  3009. /*
  3010. * restart another task
  3011. */
  3012. /*
  3013. * When PFM_CTX_MASKED, we cannot issue a restart before the previous
  3014. * one is seen by the task.
  3015. */
  3016. if (state == PFM_CTX_MASKED) {
  3017. if (ctx->ctx_fl_can_restart == 0) return -EINVAL;
  3018. /*
  3019. * will prevent subsequent restart before this one is
  3020. * seen by other task
  3021. */
  3022. ctx->ctx_fl_can_restart = 0;
  3023. }
  3024. /*
  3025. * if blocking, then post the semaphore is PFM_CTX_MASKED, i.e.
  3026. * the task is blocked or on its way to block. That's the normal
  3027. * restart path. If the monitoring is not masked, then the task
  3028. * can be actively monitoring and we cannot directly intervene.
  3029. * Therefore we use the trap mechanism to catch the task and
  3030. * force it to reset the buffer/reset PMDs.
  3031. *
  3032. * if non-blocking, then we ensure that the task will go into
  3033. * pfm_handle_work() before returning to user mode.
  3034. *
  3035. * We cannot explicitly reset another task, it MUST always
  3036. * be done by the task itself. This works for system wide because
  3037. * the tool that is controlling the session is logically doing
  3038. * "self-monitoring".
  3039. */
  3040. if (CTX_OVFL_NOBLOCK(ctx) == 0 && state == PFM_CTX_MASKED) {
  3041. DPRINT(("unblocking [%d]\n", task_pid_nr(task)));
  3042. complete(&ctx->ctx_restart_done);
  3043. } else {
  3044. DPRINT(("[%d] armed exit trap\n", task_pid_nr(task)));
  3045. ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_RESET;
  3046. PFM_SET_WORK_PENDING(task, 1);
  3047. set_notify_resume(task);
  3048. /*
  3049. * XXX: send reschedule if task runs on another CPU
  3050. */
  3051. }
  3052. return 0;
  3053. }
  3054. static int
  3055. pfm_debug(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3056. {
  3057. unsigned int m = *(unsigned int *)arg;
  3058. pfm_sysctl.debug = m == 0 ? 0 : 1;
  3059. printk(KERN_INFO "perfmon debugging %s (timing reset)\n", pfm_sysctl.debug ? "on" : "off");
  3060. if (m == 0) {
  3061. memset(pfm_stats, 0, sizeof(pfm_stats));
  3062. for(m=0; m < NR_CPUS; m++) pfm_stats[m].pfm_ovfl_intr_cycles_min = ~0UL;
  3063. }
  3064. return 0;
  3065. }
  3066. /*
  3067. * arg can be NULL and count can be zero for this function
  3068. */
  3069. static int
  3070. pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3071. {
  3072. struct thread_struct *thread = NULL;
  3073. struct task_struct *task;
  3074. pfarg_dbreg_t *req = (pfarg_dbreg_t *)arg;
  3075. unsigned long flags;
  3076. dbreg_t dbreg;
  3077. unsigned int rnum;
  3078. int first_time;
  3079. int ret = 0, state;
  3080. int i, can_access_pmu = 0;
  3081. int is_system, is_loaded;
  3082. if (pmu_conf->use_rr_dbregs == 0) return -EINVAL;
  3083. state = ctx->ctx_state;
  3084. is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
  3085. is_system = ctx->ctx_fl_system;
  3086. task = ctx->ctx_task;
  3087. if (state == PFM_CTX_ZOMBIE) return -EINVAL;
  3088. /*
  3089. * on both UP and SMP, we can only write to the PMC when the task is
  3090. * the owner of the local PMU.
  3091. */
  3092. if (is_loaded) {
  3093. thread = &task->thread;
  3094. /*
  3095. * In system wide and when the context is loaded, access can only happen
  3096. * when the caller is running on the CPU being monitored by the session.
  3097. * It does not have to be the owner (ctx_task) of the context per se.
  3098. */
  3099. if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
  3100. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  3101. return -EBUSY;
  3102. }
  3103. can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
  3104. }
  3105. /*
  3106. * we do not need to check for ipsr.db because we do clear ibr.x, dbr.r, and dbr.w
  3107. * ensuring that no real breakpoint can be installed via this call.
  3108. *
  3109. * IMPORTANT: regs can be NULL in this function
  3110. */
  3111. first_time = ctx->ctx_fl_using_dbreg == 0;
  3112. /*
  3113. * don't bother if we are loaded and task is being debugged
  3114. */
  3115. if (is_loaded && (thread->flags & IA64_THREAD_DBG_VALID) != 0) {
  3116. DPRINT(("debug registers already in use for [%d]\n", task_pid_nr(task)));
  3117. return -EBUSY;
  3118. }
  3119. /*
  3120. * check for debug registers in system wide mode
  3121. *
  3122. * If though a check is done in pfm_context_load(),
  3123. * we must repeat it here, in case the registers are
  3124. * written after the context is loaded
  3125. */
  3126. if (is_loaded) {
  3127. LOCK_PFS(flags);
  3128. if (first_time && is_system) {
  3129. if (pfm_sessions.pfs_ptrace_use_dbregs)
  3130. ret = -EBUSY;
  3131. else
  3132. pfm_sessions.pfs_sys_use_dbregs++;
  3133. }
  3134. UNLOCK_PFS(flags);
  3135. }
  3136. if (ret != 0) return ret;
  3137. /*
  3138. * mark ourself as user of the debug registers for
  3139. * perfmon purposes.
  3140. */
  3141. ctx->ctx_fl_using_dbreg = 1;
  3142. /*
  3143. * clear hardware registers to make sure we don't
  3144. * pick up stale state.
  3145. *
  3146. * for a system wide session, we do not use
  3147. * thread.dbr, thread.ibr because this process
  3148. * never leaves the current CPU and the state
  3149. * is shared by all processes running on it
  3150. */
  3151. if (first_time && can_access_pmu) {
  3152. DPRINT(("[%d] clearing ibrs, dbrs\n", task_pid_nr(task)));
  3153. for (i=0; i < pmu_conf->num_ibrs; i++) {
  3154. ia64_set_ibr(i, 0UL);
  3155. ia64_dv_serialize_instruction();
  3156. }
  3157. ia64_srlz_i();
  3158. for (i=0; i < pmu_conf->num_dbrs; i++) {
  3159. ia64_set_dbr(i, 0UL);
  3160. ia64_dv_serialize_data();
  3161. }
  3162. ia64_srlz_d();
  3163. }
  3164. /*
  3165. * Now install the values into the registers
  3166. */
  3167. for (i = 0; i < count; i++, req++) {
  3168. rnum = req->dbreg_num;
  3169. dbreg.val = req->dbreg_value;
  3170. ret = -EINVAL;
  3171. if ((mode == PFM_CODE_RR && rnum >= PFM_NUM_IBRS) || ((mode == PFM_DATA_RR) && rnum >= PFM_NUM_DBRS)) {
  3172. DPRINT(("invalid register %u val=0x%lx mode=%d i=%d count=%d\n",
  3173. rnum, dbreg.val, mode, i, count));
  3174. goto abort_mission;
  3175. }
  3176. /*
  3177. * make sure we do not install enabled breakpoint
  3178. */
  3179. if (rnum & 0x1) {
  3180. if (mode == PFM_CODE_RR)
  3181. dbreg.ibr.ibr_x = 0;
  3182. else
  3183. dbreg.dbr.dbr_r = dbreg.dbr.dbr_w = 0;
  3184. }
  3185. PFM_REG_RETFLAG_SET(req->dbreg_flags, 0);
  3186. /*
  3187. * Debug registers, just like PMC, can only be modified
  3188. * by a kernel call. Moreover, perfmon() access to those
  3189. * registers are centralized in this routine. The hardware
  3190. * does not modify the value of these registers, therefore,
  3191. * if we save them as they are written, we can avoid having
  3192. * to save them on context switch out. This is made possible
  3193. * by the fact that when perfmon uses debug registers, ptrace()
  3194. * won't be able to modify them concurrently.
  3195. */
  3196. if (mode == PFM_CODE_RR) {
  3197. CTX_USED_IBR(ctx, rnum);
  3198. if (can_access_pmu) {
  3199. ia64_set_ibr(rnum, dbreg.val);
  3200. ia64_dv_serialize_instruction();
  3201. }
  3202. ctx->ctx_ibrs[rnum] = dbreg.val;
  3203. DPRINT(("write ibr%u=0x%lx used_ibrs=0x%x ld=%d apmu=%d\n",
  3204. rnum, dbreg.val, ctx->ctx_used_ibrs[0], is_loaded, can_access_pmu));
  3205. } else {
  3206. CTX_USED_DBR(ctx, rnum);
  3207. if (can_access_pmu) {
  3208. ia64_set_dbr(rnum, dbreg.val);
  3209. ia64_dv_serialize_data();
  3210. }
  3211. ctx->ctx_dbrs[rnum] = dbreg.val;
  3212. DPRINT(("write dbr%u=0x%lx used_dbrs=0x%x ld=%d apmu=%d\n",
  3213. rnum, dbreg.val, ctx->ctx_used_dbrs[0], is_loaded, can_access_pmu));
  3214. }
  3215. }
  3216. return 0;
  3217. abort_mission:
  3218. /*
  3219. * in case it was our first attempt, we undo the global modifications
  3220. */
  3221. if (first_time) {
  3222. LOCK_PFS(flags);
  3223. if (ctx->ctx_fl_system) {
  3224. pfm_sessions.pfs_sys_use_dbregs--;
  3225. }
  3226. UNLOCK_PFS(flags);
  3227. ctx->ctx_fl_using_dbreg = 0;
  3228. }
  3229. /*
  3230. * install error return flag
  3231. */
  3232. PFM_REG_RETFLAG_SET(req->dbreg_flags, PFM_REG_RETFL_EINVAL);
  3233. return ret;
  3234. }
  3235. static int
  3236. pfm_write_ibrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3237. {
  3238. return pfm_write_ibr_dbr(PFM_CODE_RR, ctx, arg, count, regs);
  3239. }
  3240. static int
  3241. pfm_write_dbrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3242. {
  3243. return pfm_write_ibr_dbr(PFM_DATA_RR, ctx, arg, count, regs);
  3244. }
  3245. int
  3246. pfm_mod_write_ibrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
  3247. {
  3248. pfm_context_t *ctx;
  3249. if (req == NULL) return -EINVAL;
  3250. ctx = GET_PMU_CTX();
  3251. if (ctx == NULL) return -EINVAL;
  3252. /*
  3253. * for now limit to current task, which is enough when calling
  3254. * from overflow handler
  3255. */
  3256. if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
  3257. return pfm_write_ibrs(ctx, req, nreq, regs);
  3258. }
  3259. EXPORT_SYMBOL(pfm_mod_write_ibrs);
  3260. int
  3261. pfm_mod_write_dbrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
  3262. {
  3263. pfm_context_t *ctx;
  3264. if (req == NULL) return -EINVAL;
  3265. ctx = GET_PMU_CTX();
  3266. if (ctx == NULL) return -EINVAL;
  3267. /*
  3268. * for now limit to current task, which is enough when calling
  3269. * from overflow handler
  3270. */
  3271. if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
  3272. return pfm_write_dbrs(ctx, req, nreq, regs);
  3273. }
  3274. EXPORT_SYMBOL(pfm_mod_write_dbrs);
  3275. static int
  3276. pfm_get_features(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3277. {
  3278. pfarg_features_t *req = (pfarg_features_t *)arg;
  3279. req->ft_version = PFM_VERSION;
  3280. return 0;
  3281. }
  3282. static int
  3283. pfm_stop(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3284. {
  3285. struct pt_regs *tregs;
  3286. struct task_struct *task = PFM_CTX_TASK(ctx);
  3287. int state, is_system;
  3288. state = ctx->ctx_state;
  3289. is_system = ctx->ctx_fl_system;
  3290. /*
  3291. * context must be attached to issue the stop command (includes LOADED,MASKED,ZOMBIE)
  3292. */
  3293. if (state == PFM_CTX_UNLOADED) return -EINVAL;
  3294. /*
  3295. * In system wide and when the context is loaded, access can only happen
  3296. * when the caller is running on the CPU being monitored by the session.
  3297. * It does not have to be the owner (ctx_task) of the context per se.
  3298. */
  3299. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  3300. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  3301. return -EBUSY;
  3302. }
  3303. DPRINT(("task [%d] ctx_state=%d is_system=%d\n",
  3304. task_pid_nr(PFM_CTX_TASK(ctx)),
  3305. state,
  3306. is_system));
  3307. /*
  3308. * in system mode, we need to update the PMU directly
  3309. * and the user level state of the caller, which may not
  3310. * necessarily be the creator of the context.
  3311. */
  3312. if (is_system) {
  3313. /*
  3314. * Update local PMU first
  3315. *
  3316. * disable dcr pp
  3317. */
  3318. ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
  3319. ia64_srlz_i();
  3320. /*
  3321. * update local cpuinfo
  3322. */
  3323. PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
  3324. /*
  3325. * stop monitoring, does srlz.i
  3326. */
  3327. pfm_clear_psr_pp();
  3328. /*
  3329. * stop monitoring in the caller
  3330. */
  3331. ia64_psr(regs)->pp = 0;
  3332. return 0;
  3333. }
  3334. /*
  3335. * per-task mode
  3336. */
  3337. if (task == current) {
  3338. /* stop monitoring at kernel level */
  3339. pfm_clear_psr_up();
  3340. /*
  3341. * stop monitoring at the user level
  3342. */
  3343. ia64_psr(regs)->up = 0;
  3344. } else {
  3345. tregs = task_pt_regs(task);
  3346. /*
  3347. * stop monitoring at the user level
  3348. */
  3349. ia64_psr(tregs)->up = 0;
  3350. /*
  3351. * monitoring disabled in kernel at next reschedule
  3352. */
  3353. ctx->ctx_saved_psr_up = 0;
  3354. DPRINT(("task=[%d]\n", task_pid_nr(task)));
  3355. }
  3356. return 0;
  3357. }
  3358. static int
  3359. pfm_start(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3360. {
  3361. struct pt_regs *tregs;
  3362. int state, is_system;
  3363. state = ctx->ctx_state;
  3364. is_system = ctx->ctx_fl_system;
  3365. if (state != PFM_CTX_LOADED) return -EINVAL;
  3366. /*
  3367. * In system wide and when the context is loaded, access can only happen
  3368. * when the caller is running on the CPU being monitored by the session.
  3369. * It does not have to be the owner (ctx_task) of the context per se.
  3370. */
  3371. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  3372. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  3373. return -EBUSY;
  3374. }
  3375. /*
  3376. * in system mode, we need to update the PMU directly
  3377. * and the user level state of the caller, which may not
  3378. * necessarily be the creator of the context.
  3379. */
  3380. if (is_system) {
  3381. /*
  3382. * set user level psr.pp for the caller
  3383. */
  3384. ia64_psr(regs)->pp = 1;
  3385. /*
  3386. * now update the local PMU and cpuinfo
  3387. */
  3388. PFM_CPUINFO_SET(PFM_CPUINFO_DCR_PP);
  3389. /*
  3390. * start monitoring at kernel level
  3391. */
  3392. pfm_set_psr_pp();
  3393. /* enable dcr pp */
  3394. ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
  3395. ia64_srlz_i();
  3396. return 0;
  3397. }
  3398. /*
  3399. * per-process mode
  3400. */
  3401. if (ctx->ctx_task == current) {
  3402. /* start monitoring at kernel level */
  3403. pfm_set_psr_up();
  3404. /*
  3405. * activate monitoring at user level
  3406. */
  3407. ia64_psr(regs)->up = 1;
  3408. } else {
  3409. tregs = task_pt_regs(ctx->ctx_task);
  3410. /*
  3411. * start monitoring at the kernel level the next
  3412. * time the task is scheduled
  3413. */
  3414. ctx->ctx_saved_psr_up = IA64_PSR_UP;
  3415. /*
  3416. * activate monitoring at user level
  3417. */
  3418. ia64_psr(tregs)->up = 1;
  3419. }
  3420. return 0;
  3421. }
  3422. static int
  3423. pfm_get_pmc_reset(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3424. {
  3425. pfarg_reg_t *req = (pfarg_reg_t *)arg;
  3426. unsigned int cnum;
  3427. int i;
  3428. int ret = -EINVAL;
  3429. for (i = 0; i < count; i++, req++) {
  3430. cnum = req->reg_num;
  3431. if (!PMC_IS_IMPL(cnum)) goto abort_mission;
  3432. req->reg_value = PMC_DFL_VAL(cnum);
  3433. PFM_REG_RETFLAG_SET(req->reg_flags, 0);
  3434. DPRINT(("pmc_reset_val pmc[%u]=0x%lx\n", cnum, req->reg_value));
  3435. }
  3436. return 0;
  3437. abort_mission:
  3438. PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
  3439. return ret;
  3440. }
  3441. static int
  3442. pfm_check_task_exist(pfm_context_t *ctx)
  3443. {
  3444. struct task_struct *g, *t;
  3445. int ret = -ESRCH;
  3446. read_lock(&tasklist_lock);
  3447. do_each_thread (g, t) {
  3448. if (t->thread.pfm_context == ctx) {
  3449. ret = 0;
  3450. goto out;
  3451. }
  3452. } while_each_thread (g, t);
  3453. out:
  3454. read_unlock(&tasklist_lock);
  3455. DPRINT(("pfm_check_task_exist: ret=%d ctx=%p\n", ret, ctx));
  3456. return ret;
  3457. }
  3458. static int
  3459. pfm_context_load(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3460. {
  3461. struct task_struct *task;
  3462. struct thread_struct *thread;
  3463. struct pfm_context_t *old;
  3464. unsigned long flags;
  3465. #ifndef CONFIG_SMP
  3466. struct task_struct *owner_task = NULL;
  3467. #endif
  3468. pfarg_load_t *req = (pfarg_load_t *)arg;
  3469. unsigned long *pmcs_source, *pmds_source;
  3470. int the_cpu;
  3471. int ret = 0;
  3472. int state, is_system, set_dbregs = 0;
  3473. state = ctx->ctx_state;
  3474. is_system = ctx->ctx_fl_system;
  3475. /*
  3476. * can only load from unloaded or terminated state
  3477. */
  3478. if (state != PFM_CTX_UNLOADED) {
  3479. DPRINT(("cannot load to [%d], invalid ctx_state=%d\n",
  3480. req->load_pid,
  3481. ctx->ctx_state));
  3482. return -EBUSY;
  3483. }
  3484. DPRINT(("load_pid [%d] using_dbreg=%d\n", req->load_pid, ctx->ctx_fl_using_dbreg));
  3485. if (CTX_OVFL_NOBLOCK(ctx) == 0 && req->load_pid == current->pid) {
  3486. DPRINT(("cannot use blocking mode on self\n"));
  3487. return -EINVAL;
  3488. }
  3489. ret = pfm_get_task(ctx, req->load_pid, &task);
  3490. if (ret) {
  3491. DPRINT(("load_pid [%d] get_task=%d\n", req->load_pid, ret));
  3492. return ret;
  3493. }
  3494. ret = -EINVAL;
  3495. /*
  3496. * system wide is self monitoring only
  3497. */
  3498. if (is_system && task != current) {
  3499. DPRINT(("system wide is self monitoring only load_pid=%d\n",
  3500. req->load_pid));
  3501. goto error;
  3502. }
  3503. thread = &task->thread;
  3504. ret = 0;
  3505. /*
  3506. * cannot load a context which is using range restrictions,
  3507. * into a task that is being debugged.
  3508. */
  3509. if (ctx->ctx_fl_using_dbreg) {
  3510. if (thread->flags & IA64_THREAD_DBG_VALID) {
  3511. ret = -EBUSY;
  3512. DPRINT(("load_pid [%d] task is debugged, cannot load range restrictions\n", req->load_pid));
  3513. goto error;
  3514. }
  3515. LOCK_PFS(flags);
  3516. if (is_system) {
  3517. if (pfm_sessions.pfs_ptrace_use_dbregs) {
  3518. DPRINT(("cannot load [%d] dbregs in use\n",
  3519. task_pid_nr(task)));
  3520. ret = -EBUSY;
  3521. } else {
  3522. pfm_sessions.pfs_sys_use_dbregs++;
  3523. DPRINT(("load [%d] increased sys_use_dbreg=%u\n", task_pid_nr(task), pfm_sessions.pfs_sys_use_dbregs));
  3524. set_dbregs = 1;
  3525. }
  3526. }
  3527. UNLOCK_PFS(flags);
  3528. if (ret) goto error;
  3529. }
  3530. /*
  3531. * SMP system-wide monitoring implies self-monitoring.
  3532. *
  3533. * The programming model expects the task to
  3534. * be pinned on a CPU throughout the session.
  3535. * Here we take note of the current CPU at the
  3536. * time the context is loaded. No call from
  3537. * another CPU will be allowed.
  3538. *
  3539. * The pinning via shed_setaffinity()
  3540. * must be done by the calling task prior
  3541. * to this call.
  3542. *
  3543. * systemwide: keep track of CPU this session is supposed to run on
  3544. */
  3545. the_cpu = ctx->ctx_cpu = smp_processor_id();
  3546. ret = -EBUSY;
  3547. /*
  3548. * now reserve the session
  3549. */
  3550. ret = pfm_reserve_session(current, is_system, the_cpu);
  3551. if (ret) goto error;
  3552. /*
  3553. * task is necessarily stopped at this point.
  3554. *
  3555. * If the previous context was zombie, then it got removed in
  3556. * pfm_save_regs(). Therefore we should not see it here.
  3557. * If we see a context, then this is an active context
  3558. *
  3559. * XXX: needs to be atomic
  3560. */
  3561. DPRINT(("before cmpxchg() old_ctx=%p new_ctx=%p\n",
  3562. thread->pfm_context, ctx));
  3563. ret = -EBUSY;
  3564. old = ia64_cmpxchg(acq, &thread->pfm_context, NULL, ctx, sizeof(pfm_context_t *));
  3565. if (old != NULL) {
  3566. DPRINT(("load_pid [%d] already has a context\n", req->load_pid));
  3567. goto error_unres;
  3568. }
  3569. pfm_reset_msgq(ctx);
  3570. ctx->ctx_state = PFM_CTX_LOADED;
  3571. /*
  3572. * link context to task
  3573. */
  3574. ctx->ctx_task = task;
  3575. if (is_system) {
  3576. /*
  3577. * we load as stopped
  3578. */
  3579. PFM_CPUINFO_SET(PFM_CPUINFO_SYST_WIDE);
  3580. PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
  3581. if (ctx->ctx_fl_excl_idle) PFM_CPUINFO_SET(PFM_CPUINFO_EXCL_IDLE);
  3582. } else {
  3583. thread->flags |= IA64_THREAD_PM_VALID;
  3584. }
  3585. /*
  3586. * propagate into thread-state
  3587. */
  3588. pfm_copy_pmds(task, ctx);
  3589. pfm_copy_pmcs(task, ctx);
  3590. pmcs_source = ctx->th_pmcs;
  3591. pmds_source = ctx->th_pmds;
  3592. /*
  3593. * always the case for system-wide
  3594. */
  3595. if (task == current) {
  3596. if (is_system == 0) {
  3597. /* allow user level control */
  3598. ia64_psr(regs)->sp = 0;
  3599. DPRINT(("clearing psr.sp for [%d]\n", task_pid_nr(task)));
  3600. SET_LAST_CPU(ctx, smp_processor_id());
  3601. INC_ACTIVATION();
  3602. SET_ACTIVATION(ctx);
  3603. #ifndef CONFIG_SMP
  3604. /*
  3605. * push the other task out, if any
  3606. */
  3607. owner_task = GET_PMU_OWNER();
  3608. if (owner_task) pfm_lazy_save_regs(owner_task);
  3609. #endif
  3610. }
  3611. /*
  3612. * load all PMD from ctx to PMU (as opposed to thread state)
  3613. * restore all PMC from ctx to PMU
  3614. */
  3615. pfm_restore_pmds(pmds_source, ctx->ctx_all_pmds[0]);
  3616. pfm_restore_pmcs(pmcs_source, ctx->ctx_all_pmcs[0]);
  3617. ctx->ctx_reload_pmcs[0] = 0UL;
  3618. ctx->ctx_reload_pmds[0] = 0UL;
  3619. /*
  3620. * guaranteed safe by earlier check against DBG_VALID
  3621. */
  3622. if (ctx->ctx_fl_using_dbreg) {
  3623. pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
  3624. pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
  3625. }
  3626. /*
  3627. * set new ownership
  3628. */
  3629. SET_PMU_OWNER(task, ctx);
  3630. DPRINT(("context loaded on PMU for [%d]\n", task_pid_nr(task)));
  3631. } else {
  3632. /*
  3633. * when not current, task MUST be stopped, so this is safe
  3634. */
  3635. regs = task_pt_regs(task);
  3636. /* force a full reload */
  3637. ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
  3638. SET_LAST_CPU(ctx, -1);
  3639. /* initial saved psr (stopped) */
  3640. ctx->ctx_saved_psr_up = 0UL;
  3641. ia64_psr(regs)->up = ia64_psr(regs)->pp = 0;
  3642. }
  3643. ret = 0;
  3644. error_unres:
  3645. if (ret) pfm_unreserve_session(ctx, ctx->ctx_fl_system, the_cpu);
  3646. error:
  3647. /*
  3648. * we must undo the dbregs setting (for system-wide)
  3649. */
  3650. if (ret && set_dbregs) {
  3651. LOCK_PFS(flags);
  3652. pfm_sessions.pfs_sys_use_dbregs--;
  3653. UNLOCK_PFS(flags);
  3654. }
  3655. /*
  3656. * release task, there is now a link with the context
  3657. */
  3658. if (is_system == 0 && task != current) {
  3659. pfm_put_task(task);
  3660. if (ret == 0) {
  3661. ret = pfm_check_task_exist(ctx);
  3662. if (ret) {
  3663. ctx->ctx_state = PFM_CTX_UNLOADED;
  3664. ctx->ctx_task = NULL;
  3665. }
  3666. }
  3667. }
  3668. return ret;
  3669. }
  3670. /*
  3671. * in this function, we do not need to increase the use count
  3672. * for the task via get_task_struct(), because we hold the
  3673. * context lock. If the task were to disappear while having
  3674. * a context attached, it would go through pfm_exit_thread()
  3675. * which also grabs the context lock and would therefore be blocked
  3676. * until we are here.
  3677. */
  3678. static void pfm_flush_pmds(struct task_struct *, pfm_context_t *ctx);
  3679. static int
  3680. pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3681. {
  3682. struct task_struct *task = PFM_CTX_TASK(ctx);
  3683. struct pt_regs *tregs;
  3684. int prev_state, is_system;
  3685. int ret;
  3686. DPRINT(("ctx_state=%d task [%d]\n", ctx->ctx_state, task ? task_pid_nr(task) : -1));
  3687. prev_state = ctx->ctx_state;
  3688. is_system = ctx->ctx_fl_system;
  3689. /*
  3690. * unload only when necessary
  3691. */
  3692. if (prev_state == PFM_CTX_UNLOADED) {
  3693. DPRINT(("ctx_state=%d, nothing to do\n", prev_state));
  3694. return 0;
  3695. }
  3696. /*
  3697. * clear psr and dcr bits
  3698. */
  3699. ret = pfm_stop(ctx, NULL, 0, regs);
  3700. if (ret) return ret;
  3701. ctx->ctx_state = PFM_CTX_UNLOADED;
  3702. /*
  3703. * in system mode, we need to update the PMU directly
  3704. * and the user level state of the caller, which may not
  3705. * necessarily be the creator of the context.
  3706. */
  3707. if (is_system) {
  3708. /*
  3709. * Update cpuinfo
  3710. *
  3711. * local PMU is taken care of in pfm_stop()
  3712. */
  3713. PFM_CPUINFO_CLEAR(PFM_CPUINFO_SYST_WIDE);
  3714. PFM_CPUINFO_CLEAR(PFM_CPUINFO_EXCL_IDLE);
  3715. /*
  3716. * save PMDs in context
  3717. * release ownership
  3718. */
  3719. pfm_flush_pmds(current, ctx);
  3720. /*
  3721. * at this point we are done with the PMU
  3722. * so we can unreserve the resource.
  3723. */
  3724. if (prev_state != PFM_CTX_ZOMBIE)
  3725. pfm_unreserve_session(ctx, 1 , ctx->ctx_cpu);
  3726. /*
  3727. * disconnect context from task
  3728. */
  3729. task->thread.pfm_context = NULL;
  3730. /*
  3731. * disconnect task from context
  3732. */
  3733. ctx->ctx_task = NULL;
  3734. /*
  3735. * There is nothing more to cleanup here.
  3736. */
  3737. return 0;
  3738. }
  3739. /*
  3740. * per-task mode
  3741. */
  3742. tregs = task == current ? regs : task_pt_regs(task);
  3743. if (task == current) {
  3744. /*
  3745. * cancel user level control
  3746. */
  3747. ia64_psr(regs)->sp = 1;
  3748. DPRINT(("setting psr.sp for [%d]\n", task_pid_nr(task)));
  3749. }
  3750. /*
  3751. * save PMDs to context
  3752. * release ownership
  3753. */
  3754. pfm_flush_pmds(task, ctx);
  3755. /*
  3756. * at this point we are done with the PMU
  3757. * so we can unreserve the resource.
  3758. *
  3759. * when state was ZOMBIE, we have already unreserved.
  3760. */
  3761. if (prev_state != PFM_CTX_ZOMBIE)
  3762. pfm_unreserve_session(ctx, 0 , ctx->ctx_cpu);
  3763. /*
  3764. * reset activation counter and psr
  3765. */
  3766. ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
  3767. SET_LAST_CPU(ctx, -1);
  3768. /*
  3769. * PMU state will not be restored
  3770. */
  3771. task->thread.flags &= ~IA64_THREAD_PM_VALID;
  3772. /*
  3773. * break links between context and task
  3774. */
  3775. task->thread.pfm_context = NULL;
  3776. ctx->ctx_task = NULL;
  3777. PFM_SET_WORK_PENDING(task, 0);
  3778. ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
  3779. ctx->ctx_fl_can_restart = 0;
  3780. ctx->ctx_fl_going_zombie = 0;
  3781. DPRINT(("disconnected [%d] from context\n", task_pid_nr(task)));
  3782. return 0;
  3783. }
  3784. /*
  3785. * called only from exit_thread()
  3786. * we come here only if the task has a context attached (loaded or masked)
  3787. */
  3788. void
  3789. pfm_exit_thread(struct task_struct *task)
  3790. {
  3791. pfm_context_t *ctx;
  3792. unsigned long flags;
  3793. struct pt_regs *regs = task_pt_regs(task);
  3794. int ret, state;
  3795. int free_ok = 0;
  3796. ctx = PFM_GET_CTX(task);
  3797. PROTECT_CTX(ctx, flags);
  3798. DPRINT(("state=%d task [%d]\n", ctx->ctx_state, task_pid_nr(task)));
  3799. state = ctx->ctx_state;
  3800. switch(state) {
  3801. case PFM_CTX_UNLOADED:
  3802. /*
  3803. * only comes to this function if pfm_context is not NULL, i.e., cannot
  3804. * be in unloaded state
  3805. */
  3806. printk(KERN_ERR "perfmon: pfm_exit_thread [%d] ctx unloaded\n", task_pid_nr(task));
  3807. break;
  3808. case PFM_CTX_LOADED:
  3809. case PFM_CTX_MASKED:
  3810. ret = pfm_context_unload(ctx, NULL, 0, regs);
  3811. if (ret) {
  3812. printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task_pid_nr(task), state, ret);
  3813. }
  3814. DPRINT(("ctx unloaded for current state was %d\n", state));
  3815. pfm_end_notify_user(ctx);
  3816. break;
  3817. case PFM_CTX_ZOMBIE:
  3818. ret = pfm_context_unload(ctx, NULL, 0, regs);
  3819. if (ret) {
  3820. printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task_pid_nr(task), state, ret);
  3821. }
  3822. free_ok = 1;
  3823. break;
  3824. default:
  3825. printk(KERN_ERR "perfmon: pfm_exit_thread [%d] unexpected state=%d\n", task_pid_nr(task), state);
  3826. break;
  3827. }
  3828. UNPROTECT_CTX(ctx, flags);
  3829. { u64 psr = pfm_get_psr();
  3830. BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
  3831. BUG_ON(GET_PMU_OWNER());
  3832. BUG_ON(ia64_psr(regs)->up);
  3833. BUG_ON(ia64_psr(regs)->pp);
  3834. }
  3835. /*
  3836. * All memory free operations (especially for vmalloc'ed memory)
  3837. * MUST be done with interrupts ENABLED.
  3838. */
  3839. if (free_ok) pfm_context_free(ctx);
  3840. }
  3841. /*
  3842. * functions MUST be listed in the increasing order of their index (see permfon.h)
  3843. */
  3844. #define PFM_CMD(name, flags, arg_count, arg_type, getsz) { name, #name, flags, arg_count, sizeof(arg_type), getsz }
  3845. #define PFM_CMD_S(name, flags) { name, #name, flags, 0, 0, NULL }
  3846. #define PFM_CMD_PCLRWS (PFM_CMD_FD|PFM_CMD_ARG_RW|PFM_CMD_STOP)
  3847. #define PFM_CMD_PCLRW (PFM_CMD_FD|PFM_CMD_ARG_RW)
  3848. #define PFM_CMD_NONE { NULL, "no-cmd", 0, 0, 0, NULL}
  3849. static pfm_cmd_desc_t pfm_cmd_tab[]={
  3850. /* 0 */PFM_CMD_NONE,
  3851. /* 1 */PFM_CMD(pfm_write_pmcs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
  3852. /* 2 */PFM_CMD(pfm_write_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
  3853. /* 3 */PFM_CMD(pfm_read_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
  3854. /* 4 */PFM_CMD_S(pfm_stop, PFM_CMD_PCLRWS),
  3855. /* 5 */PFM_CMD_S(pfm_start, PFM_CMD_PCLRWS),
  3856. /* 6 */PFM_CMD_NONE,
  3857. /* 7 */PFM_CMD_NONE,
  3858. /* 8 */PFM_CMD(pfm_context_create, PFM_CMD_ARG_RW, 1, pfarg_context_t, pfm_ctx_getsize),
  3859. /* 9 */PFM_CMD_NONE,
  3860. /* 10 */PFM_CMD_S(pfm_restart, PFM_CMD_PCLRW),
  3861. /* 11 */PFM_CMD_NONE,
  3862. /* 12 */PFM_CMD(pfm_get_features, PFM_CMD_ARG_RW, 1, pfarg_features_t, NULL),
  3863. /* 13 */PFM_CMD(pfm_debug, 0, 1, unsigned int, NULL),
  3864. /* 14 */PFM_CMD_NONE,
  3865. /* 15 */PFM_CMD(pfm_get_pmc_reset, PFM_CMD_ARG_RW, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
  3866. /* 16 */PFM_CMD(pfm_context_load, PFM_CMD_PCLRWS, 1, pfarg_load_t, NULL),
  3867. /* 17 */PFM_CMD_S(pfm_context_unload, PFM_CMD_PCLRWS),
  3868. /* 18 */PFM_CMD_NONE,
  3869. /* 19 */PFM_CMD_NONE,
  3870. /* 20 */PFM_CMD_NONE,
  3871. /* 21 */PFM_CMD_NONE,
  3872. /* 22 */PFM_CMD_NONE,
  3873. /* 23 */PFM_CMD_NONE,
  3874. /* 24 */PFM_CMD_NONE,
  3875. /* 25 */PFM_CMD_NONE,
  3876. /* 26 */PFM_CMD_NONE,
  3877. /* 27 */PFM_CMD_NONE,
  3878. /* 28 */PFM_CMD_NONE,
  3879. /* 29 */PFM_CMD_NONE,
  3880. /* 30 */PFM_CMD_NONE,
  3881. /* 31 */PFM_CMD_NONE,
  3882. /* 32 */PFM_CMD(pfm_write_ibrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL),
  3883. /* 33 */PFM_CMD(pfm_write_dbrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL)
  3884. };
  3885. #define PFM_CMD_COUNT (sizeof(pfm_cmd_tab)/sizeof(pfm_cmd_desc_t))
  3886. static int
  3887. pfm_check_task_state(pfm_context_t *ctx, int cmd, unsigned long flags)
  3888. {
  3889. struct task_struct *task;
  3890. int state, old_state;
  3891. recheck:
  3892. state = ctx->ctx_state;
  3893. task = ctx->ctx_task;
  3894. if (task == NULL) {
  3895. DPRINT(("context %d no task, state=%d\n", ctx->ctx_fd, state));
  3896. return 0;
  3897. }
  3898. DPRINT(("context %d state=%d [%d] task_state=%ld must_stop=%d\n",
  3899. ctx->ctx_fd,
  3900. state,
  3901. task_pid_nr(task),
  3902. task->state, PFM_CMD_STOPPED(cmd)));
  3903. /*
  3904. * self-monitoring always ok.
  3905. *
  3906. * for system-wide the caller can either be the creator of the
  3907. * context (to one to which the context is attached to) OR
  3908. * a task running on the same CPU as the session.
  3909. */
  3910. if (task == current || ctx->ctx_fl_system) return 0;
  3911. /*
  3912. * we are monitoring another thread
  3913. */
  3914. switch(state) {
  3915. case PFM_CTX_UNLOADED:
  3916. /*
  3917. * if context is UNLOADED we are safe to go
  3918. */
  3919. return 0;
  3920. case PFM_CTX_ZOMBIE:
  3921. /*
  3922. * no command can operate on a zombie context
  3923. */
  3924. DPRINT(("cmd %d state zombie cannot operate on context\n", cmd));
  3925. return -EINVAL;
  3926. case PFM_CTX_MASKED:
  3927. /*
  3928. * PMU state has been saved to software even though
  3929. * the thread may still be running.
  3930. */
  3931. if (cmd != PFM_UNLOAD_CONTEXT) return 0;
  3932. }
  3933. /*
  3934. * context is LOADED or MASKED. Some commands may need to have
  3935. * the task stopped.
  3936. *
  3937. * We could lift this restriction for UP but it would mean that
  3938. * the user has no guarantee the task would not run between
  3939. * two successive calls to perfmonctl(). That's probably OK.
  3940. * If this user wants to ensure the task does not run, then
  3941. * the task must be stopped.
  3942. */
  3943. if (PFM_CMD_STOPPED(cmd)) {
  3944. if (!task_is_stopped_or_traced(task)) {
  3945. DPRINT(("[%d] task not in stopped state\n", task_pid_nr(task)));
  3946. return -EBUSY;
  3947. }
  3948. /*
  3949. * task is now stopped, wait for ctxsw out
  3950. *
  3951. * This is an interesting point in the code.
  3952. * We need to unprotect the context because
  3953. * the pfm_save_regs() routines needs to grab
  3954. * the same lock. There are danger in doing
  3955. * this because it leaves a window open for
  3956. * another task to get access to the context
  3957. * and possibly change its state. The one thing
  3958. * that is not possible is for the context to disappear
  3959. * because we are protected by the VFS layer, i.e.,
  3960. * get_fd()/put_fd().
  3961. */
  3962. old_state = state;
  3963. UNPROTECT_CTX(ctx, flags);
  3964. wait_task_inactive(task, 0);
  3965. PROTECT_CTX(ctx, flags);
  3966. /*
  3967. * we must recheck to verify if state has changed
  3968. */
  3969. if (ctx->ctx_state != old_state) {
  3970. DPRINT(("old_state=%d new_state=%d\n", old_state, ctx->ctx_state));
  3971. goto recheck;
  3972. }
  3973. }
  3974. return 0;
  3975. }
  3976. /*
  3977. * system-call entry point (must return long)
  3978. */
  3979. asmlinkage long
  3980. sys_perfmonctl (int fd, int cmd, void __user *arg, int count)
  3981. {
  3982. struct fd f = {NULL, 0};
  3983. pfm_context_t *ctx = NULL;
  3984. unsigned long flags = 0UL;
  3985. void *args_k = NULL;
  3986. long ret; /* will expand int return types */
  3987. size_t base_sz, sz, xtra_sz = 0;
  3988. int narg, completed_args = 0, call_made = 0, cmd_flags;
  3989. int (*func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
  3990. int (*getsize)(void *arg, size_t *sz);
  3991. #define PFM_MAX_ARGSIZE 4096
  3992. /*
  3993. * reject any call if perfmon was disabled at initialization
  3994. */
  3995. if (unlikely(pmu_conf == NULL)) return -ENOSYS;
  3996. if (unlikely(cmd < 0 || cmd >= PFM_CMD_COUNT)) {
  3997. DPRINT(("invalid cmd=%d\n", cmd));
  3998. return -EINVAL;
  3999. }
  4000. func = pfm_cmd_tab[cmd].cmd_func;
  4001. narg = pfm_cmd_tab[cmd].cmd_narg;
  4002. base_sz = pfm_cmd_tab[cmd].cmd_argsize;
  4003. getsize = pfm_cmd_tab[cmd].cmd_getsize;
  4004. cmd_flags = pfm_cmd_tab[cmd].cmd_flags;
  4005. if (unlikely(func == NULL)) {
  4006. DPRINT(("invalid cmd=%d\n", cmd));
  4007. return -EINVAL;
  4008. }
  4009. DPRINT(("cmd=%s idx=%d narg=0x%x argsz=%lu count=%d\n",
  4010. PFM_CMD_NAME(cmd),
  4011. cmd,
  4012. narg,
  4013. base_sz,
  4014. count));
  4015. /*
  4016. * check if number of arguments matches what the command expects
  4017. */
  4018. if (unlikely((narg == PFM_CMD_ARG_MANY && count <= 0) || (narg > 0 && narg != count)))
  4019. return -EINVAL;
  4020. restart_args:
  4021. sz = xtra_sz + base_sz*count;
  4022. /*
  4023. * limit abuse to min page size
  4024. */
  4025. if (unlikely(sz > PFM_MAX_ARGSIZE)) {
  4026. printk(KERN_ERR "perfmon: [%d] argument too big %lu\n", task_pid_nr(current), sz);
  4027. return -E2BIG;
  4028. }
  4029. /*
  4030. * allocate default-sized argument buffer
  4031. */
  4032. if (likely(count && args_k == NULL)) {
  4033. args_k = kmalloc(PFM_MAX_ARGSIZE, GFP_KERNEL);
  4034. if (args_k == NULL) return -ENOMEM;
  4035. }
  4036. ret = -EFAULT;
  4037. /*
  4038. * copy arguments
  4039. *
  4040. * assume sz = 0 for command without parameters
  4041. */
  4042. if (sz && copy_from_user(args_k, arg, sz)) {
  4043. DPRINT(("cannot copy_from_user %lu bytes @%p\n", sz, arg));
  4044. goto error_args;
  4045. }
  4046. /*
  4047. * check if command supports extra parameters
  4048. */
  4049. if (completed_args == 0 && getsize) {
  4050. /*
  4051. * get extra parameters size (based on main argument)
  4052. */
  4053. ret = (*getsize)(args_k, &xtra_sz);
  4054. if (ret) goto error_args;
  4055. completed_args = 1;
  4056. DPRINT(("restart_args sz=%lu xtra_sz=%lu\n", sz, xtra_sz));
  4057. /* retry if necessary */
  4058. if (likely(xtra_sz)) goto restart_args;
  4059. }
  4060. if (unlikely((cmd_flags & PFM_CMD_FD) == 0)) goto skip_fd;
  4061. ret = -EBADF;
  4062. f = fdget(fd);
  4063. if (unlikely(f.file == NULL)) {
  4064. DPRINT(("invalid fd %d\n", fd));
  4065. goto error_args;
  4066. }
  4067. if (unlikely(PFM_IS_FILE(f.file) == 0)) {
  4068. DPRINT(("fd %d not related to perfmon\n", fd));
  4069. goto error_args;
  4070. }
  4071. ctx = f.file->private_data;
  4072. if (unlikely(ctx == NULL)) {
  4073. DPRINT(("no context for fd %d\n", fd));
  4074. goto error_args;
  4075. }
  4076. prefetch(&ctx->ctx_state);
  4077. PROTECT_CTX(ctx, flags);
  4078. /*
  4079. * check task is stopped
  4080. */
  4081. ret = pfm_check_task_state(ctx, cmd, flags);
  4082. if (unlikely(ret)) goto abort_locked;
  4083. skip_fd:
  4084. ret = (*func)(ctx, args_k, count, task_pt_regs(current));
  4085. call_made = 1;
  4086. abort_locked:
  4087. if (likely(ctx)) {
  4088. DPRINT(("context unlocked\n"));
  4089. UNPROTECT_CTX(ctx, flags);
  4090. }
  4091. /* copy argument back to user, if needed */
  4092. if (call_made && PFM_CMD_RW_ARG(cmd) && copy_to_user(arg, args_k, base_sz*count)) ret = -EFAULT;
  4093. error_args:
  4094. if (f.file)
  4095. fdput(f);
  4096. kfree(args_k);
  4097. DPRINT(("cmd=%s ret=%ld\n", PFM_CMD_NAME(cmd), ret));
  4098. return ret;
  4099. }
  4100. static void
  4101. pfm_resume_after_ovfl(pfm_context_t *ctx, unsigned long ovfl_regs, struct pt_regs *regs)
  4102. {
  4103. pfm_buffer_fmt_t *fmt = ctx->ctx_buf_fmt;
  4104. pfm_ovfl_ctrl_t rst_ctrl;
  4105. int state;
  4106. int ret = 0;
  4107. state = ctx->ctx_state;
  4108. /*
  4109. * Unlock sampling buffer and reset index atomically
  4110. * XXX: not really needed when blocking
  4111. */
  4112. if (CTX_HAS_SMPL(ctx)) {
  4113. rst_ctrl.bits.mask_monitoring = 0;
  4114. rst_ctrl.bits.reset_ovfl_pmds = 0;
  4115. if (state == PFM_CTX_LOADED)
  4116. ret = pfm_buf_fmt_restart_active(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
  4117. else
  4118. ret = pfm_buf_fmt_restart(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
  4119. } else {
  4120. rst_ctrl.bits.mask_monitoring = 0;
  4121. rst_ctrl.bits.reset_ovfl_pmds = 1;
  4122. }
  4123. if (ret == 0) {
  4124. if (rst_ctrl.bits.reset_ovfl_pmds) {
  4125. pfm_reset_regs(ctx, &ovfl_regs, PFM_PMD_LONG_RESET);
  4126. }
  4127. if (rst_ctrl.bits.mask_monitoring == 0) {
  4128. DPRINT(("resuming monitoring\n"));
  4129. if (ctx->ctx_state == PFM_CTX_MASKED) pfm_restore_monitoring(current);
  4130. } else {
  4131. DPRINT(("stopping monitoring\n"));
  4132. //pfm_stop_monitoring(current, regs);
  4133. }
  4134. ctx->ctx_state = PFM_CTX_LOADED;
  4135. }
  4136. }
  4137. /*
  4138. * context MUST BE LOCKED when calling
  4139. * can only be called for current
  4140. */
  4141. static void
  4142. pfm_context_force_terminate(pfm_context_t *ctx, struct pt_regs *regs)
  4143. {
  4144. int ret;
  4145. DPRINT(("entering for [%d]\n", task_pid_nr(current)));
  4146. ret = pfm_context_unload(ctx, NULL, 0, regs);
  4147. if (ret) {
  4148. printk(KERN_ERR "pfm_context_force_terminate: [%d] unloaded failed with %d\n", task_pid_nr(current), ret);
  4149. }
  4150. /*
  4151. * and wakeup controlling task, indicating we are now disconnected
  4152. */
  4153. wake_up_interruptible(&ctx->ctx_zombieq);
  4154. /*
  4155. * given that context is still locked, the controlling
  4156. * task will only get access when we return from
  4157. * pfm_handle_work().
  4158. */
  4159. }
  4160. static int pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds);
  4161. /*
  4162. * pfm_handle_work() can be called with interrupts enabled
  4163. * (TIF_NEED_RESCHED) or disabled. The down_interruptible
  4164. * call may sleep, therefore we must re-enable interrupts
  4165. * to avoid deadlocks. It is safe to do so because this function
  4166. * is called ONLY when returning to user level (pUStk=1), in which case
  4167. * there is no risk of kernel stack overflow due to deep
  4168. * interrupt nesting.
  4169. */
  4170. void
  4171. pfm_handle_work(void)
  4172. {
  4173. pfm_context_t *ctx;
  4174. struct pt_regs *regs;
  4175. unsigned long flags, dummy_flags;
  4176. unsigned long ovfl_regs;
  4177. unsigned int reason;
  4178. int ret;
  4179. ctx = PFM_GET_CTX(current);
  4180. if (ctx == NULL) {
  4181. printk(KERN_ERR "perfmon: [%d] has no PFM context\n",
  4182. task_pid_nr(current));
  4183. return;
  4184. }
  4185. PROTECT_CTX(ctx, flags);
  4186. PFM_SET_WORK_PENDING(current, 0);
  4187. regs = task_pt_regs(current);
  4188. /*
  4189. * extract reason for being here and clear
  4190. */
  4191. reason = ctx->ctx_fl_trap_reason;
  4192. ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
  4193. ovfl_regs = ctx->ctx_ovfl_regs[0];
  4194. DPRINT(("reason=%d state=%d\n", reason, ctx->ctx_state));
  4195. /*
  4196. * must be done before we check for simple-reset mode
  4197. */
  4198. if (ctx->ctx_fl_going_zombie || ctx->ctx_state == PFM_CTX_ZOMBIE)
  4199. goto do_zombie;
  4200. //if (CTX_OVFL_NOBLOCK(ctx)) goto skip_blocking;
  4201. if (reason == PFM_TRAP_REASON_RESET)
  4202. goto skip_blocking;
  4203. /*
  4204. * restore interrupt mask to what it was on entry.
  4205. * Could be enabled/diasbled.
  4206. */
  4207. UNPROTECT_CTX(ctx, flags);
  4208. /*
  4209. * force interrupt enable because of down_interruptible()
  4210. */
  4211. local_irq_enable();
  4212. DPRINT(("before block sleeping\n"));
  4213. /*
  4214. * may go through without blocking on SMP systems
  4215. * if restart has been received already by the time we call down()
  4216. */
  4217. ret = wait_for_completion_interruptible(&ctx->ctx_restart_done);
  4218. DPRINT(("after block sleeping ret=%d\n", ret));
  4219. /*
  4220. * lock context and mask interrupts again
  4221. * We save flags into a dummy because we may have
  4222. * altered interrupts mask compared to entry in this
  4223. * function.
  4224. */
  4225. PROTECT_CTX(ctx, dummy_flags);
  4226. /*
  4227. * we need to read the ovfl_regs only after wake-up
  4228. * because we may have had pfm_write_pmds() in between
  4229. * and that can changed PMD values and therefore
  4230. * ovfl_regs is reset for these new PMD values.
  4231. */
  4232. ovfl_regs = ctx->ctx_ovfl_regs[0];
  4233. if (ctx->ctx_fl_going_zombie) {
  4234. do_zombie:
  4235. DPRINT(("context is zombie, bailing out\n"));
  4236. pfm_context_force_terminate(ctx, regs);
  4237. goto nothing_to_do;
  4238. }
  4239. /*
  4240. * in case of interruption of down() we don't restart anything
  4241. */
  4242. if (ret < 0)
  4243. goto nothing_to_do;
  4244. skip_blocking:
  4245. pfm_resume_after_ovfl(ctx, ovfl_regs, regs);
  4246. ctx->ctx_ovfl_regs[0] = 0UL;
  4247. nothing_to_do:
  4248. /*
  4249. * restore flags as they were upon entry
  4250. */
  4251. UNPROTECT_CTX(ctx, flags);
  4252. }
  4253. static int
  4254. pfm_notify_user(pfm_context_t *ctx, pfm_msg_t *msg)
  4255. {
  4256. if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
  4257. DPRINT(("ignoring overflow notification, owner is zombie\n"));
  4258. return 0;
  4259. }
  4260. DPRINT(("waking up somebody\n"));
  4261. if (msg) wake_up_interruptible(&ctx->ctx_msgq_wait);
  4262. /*
  4263. * safe, we are not in intr handler, nor in ctxsw when
  4264. * we come here
  4265. */
  4266. kill_fasync (&ctx->ctx_async_queue, SIGIO, POLL_IN);
  4267. return 0;
  4268. }
  4269. static int
  4270. pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds)
  4271. {
  4272. pfm_msg_t *msg = NULL;
  4273. if (ctx->ctx_fl_no_msg == 0) {
  4274. msg = pfm_get_new_msg(ctx);
  4275. if (msg == NULL) {
  4276. printk(KERN_ERR "perfmon: pfm_ovfl_notify_user no more notification msgs\n");
  4277. return -1;
  4278. }
  4279. msg->pfm_ovfl_msg.msg_type = PFM_MSG_OVFL;
  4280. msg->pfm_ovfl_msg.msg_ctx_fd = ctx->ctx_fd;
  4281. msg->pfm_ovfl_msg.msg_active_set = 0;
  4282. msg->pfm_ovfl_msg.msg_ovfl_pmds[0] = ovfl_pmds;
  4283. msg->pfm_ovfl_msg.msg_ovfl_pmds[1] = 0UL;
  4284. msg->pfm_ovfl_msg.msg_ovfl_pmds[2] = 0UL;
  4285. msg->pfm_ovfl_msg.msg_ovfl_pmds[3] = 0UL;
  4286. msg->pfm_ovfl_msg.msg_tstamp = 0UL;
  4287. }
  4288. DPRINT(("ovfl msg: msg=%p no_msg=%d fd=%d ovfl_pmds=0x%lx\n",
  4289. msg,
  4290. ctx->ctx_fl_no_msg,
  4291. ctx->ctx_fd,
  4292. ovfl_pmds));
  4293. return pfm_notify_user(ctx, msg);
  4294. }
  4295. static int
  4296. pfm_end_notify_user(pfm_context_t *ctx)
  4297. {
  4298. pfm_msg_t *msg;
  4299. msg = pfm_get_new_msg(ctx);
  4300. if (msg == NULL) {
  4301. printk(KERN_ERR "perfmon: pfm_end_notify_user no more notification msgs\n");
  4302. return -1;
  4303. }
  4304. /* no leak */
  4305. memset(msg, 0, sizeof(*msg));
  4306. msg->pfm_end_msg.msg_type = PFM_MSG_END;
  4307. msg->pfm_end_msg.msg_ctx_fd = ctx->ctx_fd;
  4308. msg->pfm_ovfl_msg.msg_tstamp = 0UL;
  4309. DPRINT(("end msg: msg=%p no_msg=%d ctx_fd=%d\n",
  4310. msg,
  4311. ctx->ctx_fl_no_msg,
  4312. ctx->ctx_fd));
  4313. return pfm_notify_user(ctx, msg);
  4314. }
  4315. /*
  4316. * main overflow processing routine.
  4317. * it can be called from the interrupt path or explicitly during the context switch code
  4318. */
  4319. static void pfm_overflow_handler(struct task_struct *task, pfm_context_t *ctx,
  4320. unsigned long pmc0, struct pt_regs *regs)
  4321. {
  4322. pfm_ovfl_arg_t *ovfl_arg;
  4323. unsigned long mask;
  4324. unsigned long old_val, ovfl_val, new_val;
  4325. unsigned long ovfl_notify = 0UL, ovfl_pmds = 0UL, smpl_pmds = 0UL, reset_pmds;
  4326. unsigned long tstamp;
  4327. pfm_ovfl_ctrl_t ovfl_ctrl;
  4328. unsigned int i, has_smpl;
  4329. int must_notify = 0;
  4330. if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) goto stop_monitoring;
  4331. /*
  4332. * sanity test. Should never happen
  4333. */
  4334. if (unlikely((pmc0 & 0x1) == 0)) goto sanity_check;
  4335. tstamp = ia64_get_itc();
  4336. mask = pmc0 >> PMU_FIRST_COUNTER;
  4337. ovfl_val = pmu_conf->ovfl_val;
  4338. has_smpl = CTX_HAS_SMPL(ctx);
  4339. DPRINT_ovfl(("pmc0=0x%lx pid=%d iip=0x%lx, %s "
  4340. "used_pmds=0x%lx\n",
  4341. pmc0,
  4342. task ? task_pid_nr(task): -1,
  4343. (regs ? regs->cr_iip : 0),
  4344. CTX_OVFL_NOBLOCK(ctx) ? "nonblocking" : "blocking",
  4345. ctx->ctx_used_pmds[0]));
  4346. /*
  4347. * first we update the virtual counters
  4348. * assume there was a prior ia64_srlz_d() issued
  4349. */
  4350. for (i = PMU_FIRST_COUNTER; mask ; i++, mask >>= 1) {
  4351. /* skip pmd which did not overflow */
  4352. if ((mask & 0x1) == 0) continue;
  4353. /*
  4354. * Note that the pmd is not necessarily 0 at this point as qualified events
  4355. * may have happened before the PMU was frozen. The residual count is not
  4356. * taken into consideration here but will be with any read of the pmd via
  4357. * pfm_read_pmds().
  4358. */
  4359. old_val = new_val = ctx->ctx_pmds[i].val;
  4360. new_val += 1 + ovfl_val;
  4361. ctx->ctx_pmds[i].val = new_val;
  4362. /*
  4363. * check for overflow condition
  4364. */
  4365. if (likely(old_val > new_val)) {
  4366. ovfl_pmds |= 1UL << i;
  4367. if (PMC_OVFL_NOTIFY(ctx, i)) ovfl_notify |= 1UL << i;
  4368. }
  4369. DPRINT_ovfl(("ctx_pmd[%d].val=0x%lx old_val=0x%lx pmd=0x%lx ovfl_pmds=0x%lx ovfl_notify=0x%lx\n",
  4370. i,
  4371. new_val,
  4372. old_val,
  4373. ia64_get_pmd(i) & ovfl_val,
  4374. ovfl_pmds,
  4375. ovfl_notify));
  4376. }
  4377. /*
  4378. * there was no 64-bit overflow, nothing else to do
  4379. */
  4380. if (ovfl_pmds == 0UL) return;
  4381. /*
  4382. * reset all control bits
  4383. */
  4384. ovfl_ctrl.val = 0;
  4385. reset_pmds = 0UL;
  4386. /*
  4387. * if a sampling format module exists, then we "cache" the overflow by
  4388. * calling the module's handler() routine.
  4389. */
  4390. if (has_smpl) {
  4391. unsigned long start_cycles, end_cycles;
  4392. unsigned long pmd_mask;
  4393. int j, k, ret = 0;
  4394. int this_cpu = smp_processor_id();
  4395. pmd_mask = ovfl_pmds >> PMU_FIRST_COUNTER;
  4396. ovfl_arg = &ctx->ctx_ovfl_arg;
  4397. prefetch(ctx->ctx_smpl_hdr);
  4398. for(i=PMU_FIRST_COUNTER; pmd_mask && ret == 0; i++, pmd_mask >>=1) {
  4399. mask = 1UL << i;
  4400. if ((pmd_mask & 0x1) == 0) continue;
  4401. ovfl_arg->ovfl_pmd = (unsigned char )i;
  4402. ovfl_arg->ovfl_notify = ovfl_notify & mask ? 1 : 0;
  4403. ovfl_arg->active_set = 0;
  4404. ovfl_arg->ovfl_ctrl.val = 0; /* module must fill in all fields */
  4405. ovfl_arg->smpl_pmds[0] = smpl_pmds = ctx->ctx_pmds[i].smpl_pmds[0];
  4406. ovfl_arg->pmd_value = ctx->ctx_pmds[i].val;
  4407. ovfl_arg->pmd_last_reset = ctx->ctx_pmds[i].lval;
  4408. ovfl_arg->pmd_eventid = ctx->ctx_pmds[i].eventid;
  4409. /*
  4410. * copy values of pmds of interest. Sampling format may copy them
  4411. * into sampling buffer.
  4412. */
  4413. if (smpl_pmds) {
  4414. for(j=0, k=0; smpl_pmds; j++, smpl_pmds >>=1) {
  4415. if ((smpl_pmds & 0x1) == 0) continue;
  4416. ovfl_arg->smpl_pmds_values[k++] = PMD_IS_COUNTING(j) ? pfm_read_soft_counter(ctx, j) : ia64_get_pmd(j);
  4417. DPRINT_ovfl(("smpl_pmd[%d]=pmd%u=0x%lx\n", k-1, j, ovfl_arg->smpl_pmds_values[k-1]));
  4418. }
  4419. }
  4420. pfm_stats[this_cpu].pfm_smpl_handler_calls++;
  4421. start_cycles = ia64_get_itc();
  4422. /*
  4423. * call custom buffer format record (handler) routine
  4424. */
  4425. ret = (*ctx->ctx_buf_fmt->fmt_handler)(task, ctx->ctx_smpl_hdr, ovfl_arg, regs, tstamp);
  4426. end_cycles = ia64_get_itc();
  4427. /*
  4428. * For those controls, we take the union because they have
  4429. * an all or nothing behavior.
  4430. */
  4431. ovfl_ctrl.bits.notify_user |= ovfl_arg->ovfl_ctrl.bits.notify_user;
  4432. ovfl_ctrl.bits.block_task |= ovfl_arg->ovfl_ctrl.bits.block_task;
  4433. ovfl_ctrl.bits.mask_monitoring |= ovfl_arg->ovfl_ctrl.bits.mask_monitoring;
  4434. /*
  4435. * build the bitmask of pmds to reset now
  4436. */
  4437. if (ovfl_arg->ovfl_ctrl.bits.reset_ovfl_pmds) reset_pmds |= mask;
  4438. pfm_stats[this_cpu].pfm_smpl_handler_cycles += end_cycles - start_cycles;
  4439. }
  4440. /*
  4441. * when the module cannot handle the rest of the overflows, we abort right here
  4442. */
  4443. if (ret && pmd_mask) {
  4444. DPRINT(("handler aborts leftover ovfl_pmds=0x%lx\n",
  4445. pmd_mask<<PMU_FIRST_COUNTER));
  4446. }
  4447. /*
  4448. * remove the pmds we reset now from the set of pmds to reset in pfm_restart()
  4449. */
  4450. ovfl_pmds &= ~reset_pmds;
  4451. } else {
  4452. /*
  4453. * when no sampling module is used, then the default
  4454. * is to notify on overflow if requested by user
  4455. */
  4456. ovfl_ctrl.bits.notify_user = ovfl_notify ? 1 : 0;
  4457. ovfl_ctrl.bits.block_task = ovfl_notify ? 1 : 0;
  4458. ovfl_ctrl.bits.mask_monitoring = ovfl_notify ? 1 : 0; /* XXX: change for saturation */
  4459. ovfl_ctrl.bits.reset_ovfl_pmds = ovfl_notify ? 0 : 1;
  4460. /*
  4461. * if needed, we reset all overflowed pmds
  4462. */
  4463. if (ovfl_notify == 0) reset_pmds = ovfl_pmds;
  4464. }
  4465. DPRINT_ovfl(("ovfl_pmds=0x%lx reset_pmds=0x%lx\n", ovfl_pmds, reset_pmds));
  4466. /*
  4467. * reset the requested PMD registers using the short reset values
  4468. */
  4469. if (reset_pmds) {
  4470. unsigned long bm = reset_pmds;
  4471. pfm_reset_regs(ctx, &bm, PFM_PMD_SHORT_RESET);
  4472. }
  4473. if (ovfl_notify && ovfl_ctrl.bits.notify_user) {
  4474. /*
  4475. * keep track of what to reset when unblocking
  4476. */
  4477. ctx->ctx_ovfl_regs[0] = ovfl_pmds;
  4478. /*
  4479. * check for blocking context
  4480. */
  4481. if (CTX_OVFL_NOBLOCK(ctx) == 0 && ovfl_ctrl.bits.block_task) {
  4482. ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_BLOCK;
  4483. /*
  4484. * set the perfmon specific checking pending work for the task
  4485. */
  4486. PFM_SET_WORK_PENDING(task, 1);
  4487. /*
  4488. * when coming from ctxsw, current still points to the
  4489. * previous task, therefore we must work with task and not current.
  4490. */
  4491. set_notify_resume(task);
  4492. }
  4493. /*
  4494. * defer until state is changed (shorten spin window). the context is locked
  4495. * anyway, so the signal receiver would come spin for nothing.
  4496. */
  4497. must_notify = 1;
  4498. }
  4499. DPRINT_ovfl(("owner [%d] pending=%ld reason=%u ovfl_pmds=0x%lx ovfl_notify=0x%lx masked=%d\n",
  4500. GET_PMU_OWNER() ? task_pid_nr(GET_PMU_OWNER()) : -1,
  4501. PFM_GET_WORK_PENDING(task),
  4502. ctx->ctx_fl_trap_reason,
  4503. ovfl_pmds,
  4504. ovfl_notify,
  4505. ovfl_ctrl.bits.mask_monitoring ? 1 : 0));
  4506. /*
  4507. * in case monitoring must be stopped, we toggle the psr bits
  4508. */
  4509. if (ovfl_ctrl.bits.mask_monitoring) {
  4510. pfm_mask_monitoring(task);
  4511. ctx->ctx_state = PFM_CTX_MASKED;
  4512. ctx->ctx_fl_can_restart = 1;
  4513. }
  4514. /*
  4515. * send notification now
  4516. */
  4517. if (must_notify) pfm_ovfl_notify_user(ctx, ovfl_notify);
  4518. return;
  4519. sanity_check:
  4520. printk(KERN_ERR "perfmon: CPU%d overflow handler [%d] pmc0=0x%lx\n",
  4521. smp_processor_id(),
  4522. task ? task_pid_nr(task) : -1,
  4523. pmc0);
  4524. return;
  4525. stop_monitoring:
  4526. /*
  4527. * in SMP, zombie context is never restored but reclaimed in pfm_load_regs().
  4528. * Moreover, zombies are also reclaimed in pfm_save_regs(). Therefore we can
  4529. * come here as zombie only if the task is the current task. In which case, we
  4530. * can access the PMU hardware directly.
  4531. *
  4532. * Note that zombies do have PM_VALID set. So here we do the minimal.
  4533. *
  4534. * In case the context was zombified it could not be reclaimed at the time
  4535. * the monitoring program exited. At this point, the PMU reservation has been
  4536. * returned, the sampiing buffer has been freed. We must convert this call
  4537. * into a spurious interrupt. However, we must also avoid infinite overflows
  4538. * by stopping monitoring for this task. We can only come here for a per-task
  4539. * context. All we need to do is to stop monitoring using the psr bits which
  4540. * are always task private. By re-enabling secure montioring, we ensure that
  4541. * the monitored task will not be able to re-activate monitoring.
  4542. * The task will eventually be context switched out, at which point the context
  4543. * will be reclaimed (that includes releasing ownership of the PMU).
  4544. *
  4545. * So there might be a window of time where the number of per-task session is zero
  4546. * yet one PMU might have a owner and get at most one overflow interrupt for a zombie
  4547. * context. This is safe because if a per-task session comes in, it will push this one
  4548. * out and by the virtue on pfm_save_regs(), this one will disappear. If a system wide
  4549. * session is force on that CPU, given that we use task pinning, pfm_save_regs() will
  4550. * also push our zombie context out.
  4551. *
  4552. * Overall pretty hairy stuff....
  4553. */
  4554. DPRINT(("ctx is zombie for [%d], converted to spurious\n", task ? task_pid_nr(task): -1));
  4555. pfm_clear_psr_up();
  4556. ia64_psr(regs)->up = 0;
  4557. ia64_psr(regs)->sp = 1;
  4558. return;
  4559. }
  4560. static int
  4561. pfm_do_interrupt_handler(void *arg, struct pt_regs *regs)
  4562. {
  4563. struct task_struct *task;
  4564. pfm_context_t *ctx;
  4565. unsigned long flags;
  4566. u64 pmc0;
  4567. int this_cpu = smp_processor_id();
  4568. int retval = 0;
  4569. pfm_stats[this_cpu].pfm_ovfl_intr_count++;
  4570. /*
  4571. * srlz.d done before arriving here
  4572. */
  4573. pmc0 = ia64_get_pmc(0);
  4574. task = GET_PMU_OWNER();
  4575. ctx = GET_PMU_CTX();
  4576. /*
  4577. * if we have some pending bits set
  4578. * assumes : if any PMC0.bit[63-1] is set, then PMC0.fr = 1
  4579. */
  4580. if (PMC0_HAS_OVFL(pmc0) && task) {
  4581. /*
  4582. * we assume that pmc0.fr is always set here
  4583. */
  4584. /* sanity check */
  4585. if (!ctx) goto report_spurious1;
  4586. if (ctx->ctx_fl_system == 0 && (task->thread.flags & IA64_THREAD_PM_VALID) == 0)
  4587. goto report_spurious2;
  4588. PROTECT_CTX_NOPRINT(ctx, flags);
  4589. pfm_overflow_handler(task, ctx, pmc0, regs);
  4590. UNPROTECT_CTX_NOPRINT(ctx, flags);
  4591. } else {
  4592. pfm_stats[this_cpu].pfm_spurious_ovfl_intr_count++;
  4593. retval = -1;
  4594. }
  4595. /*
  4596. * keep it unfrozen at all times
  4597. */
  4598. pfm_unfreeze_pmu();
  4599. return retval;
  4600. report_spurious1:
  4601. printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d has no PFM context\n",
  4602. this_cpu, task_pid_nr(task));
  4603. pfm_unfreeze_pmu();
  4604. return -1;
  4605. report_spurious2:
  4606. printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d, invalid flag\n",
  4607. this_cpu,
  4608. task_pid_nr(task));
  4609. pfm_unfreeze_pmu();
  4610. return -1;
  4611. }
  4612. static irqreturn_t
  4613. pfm_interrupt_handler(int irq, void *arg)
  4614. {
  4615. unsigned long start_cycles, total_cycles;
  4616. unsigned long min, max;
  4617. int this_cpu;
  4618. int ret;
  4619. struct pt_regs *regs = get_irq_regs();
  4620. this_cpu = get_cpu();
  4621. if (likely(!pfm_alt_intr_handler)) {
  4622. min = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min;
  4623. max = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max;
  4624. start_cycles = ia64_get_itc();
  4625. ret = pfm_do_interrupt_handler(arg, regs);
  4626. total_cycles = ia64_get_itc();
  4627. /*
  4628. * don't measure spurious interrupts
  4629. */
  4630. if (likely(ret == 0)) {
  4631. total_cycles -= start_cycles;
  4632. if (total_cycles < min) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min = total_cycles;
  4633. if (total_cycles > max) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max = total_cycles;
  4634. pfm_stats[this_cpu].pfm_ovfl_intr_cycles += total_cycles;
  4635. }
  4636. }
  4637. else {
  4638. (*pfm_alt_intr_handler->handler)(irq, arg, regs);
  4639. }
  4640. put_cpu();
  4641. return IRQ_HANDLED;
  4642. }
  4643. /*
  4644. * /proc/perfmon interface, for debug only
  4645. */
  4646. #define PFM_PROC_SHOW_HEADER ((void *)(long)nr_cpu_ids+1)
  4647. static void *
  4648. pfm_proc_start(struct seq_file *m, loff_t *pos)
  4649. {
  4650. if (*pos == 0) {
  4651. return PFM_PROC_SHOW_HEADER;
  4652. }
  4653. while (*pos <= nr_cpu_ids) {
  4654. if (cpu_online(*pos - 1)) {
  4655. return (void *)*pos;
  4656. }
  4657. ++*pos;
  4658. }
  4659. return NULL;
  4660. }
  4661. static void *
  4662. pfm_proc_next(struct seq_file *m, void *v, loff_t *pos)
  4663. {
  4664. ++*pos;
  4665. return pfm_proc_start(m, pos);
  4666. }
  4667. static void
  4668. pfm_proc_stop(struct seq_file *m, void *v)
  4669. {
  4670. }
  4671. static void
  4672. pfm_proc_show_header(struct seq_file *m)
  4673. {
  4674. struct list_head * pos;
  4675. pfm_buffer_fmt_t * entry;
  4676. unsigned long flags;
  4677. seq_printf(m,
  4678. "perfmon version : %u.%u\n"
  4679. "model : %s\n"
  4680. "fastctxsw : %s\n"
  4681. "expert mode : %s\n"
  4682. "ovfl_mask : 0x%lx\n"
  4683. "PMU flags : 0x%x\n",
  4684. PFM_VERSION_MAJ, PFM_VERSION_MIN,
  4685. pmu_conf->pmu_name,
  4686. pfm_sysctl.fastctxsw > 0 ? "Yes": "No",
  4687. pfm_sysctl.expert_mode > 0 ? "Yes": "No",
  4688. pmu_conf->ovfl_val,
  4689. pmu_conf->flags);
  4690. LOCK_PFS(flags);
  4691. seq_printf(m,
  4692. "proc_sessions : %u\n"
  4693. "sys_sessions : %u\n"
  4694. "sys_use_dbregs : %u\n"
  4695. "ptrace_use_dbregs : %u\n",
  4696. pfm_sessions.pfs_task_sessions,
  4697. pfm_sessions.pfs_sys_sessions,
  4698. pfm_sessions.pfs_sys_use_dbregs,
  4699. pfm_sessions.pfs_ptrace_use_dbregs);
  4700. UNLOCK_PFS(flags);
  4701. spin_lock(&pfm_buffer_fmt_lock);
  4702. list_for_each(pos, &pfm_buffer_fmt_list) {
  4703. entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
  4704. seq_printf(m, "format : %16phD %s\n",
  4705. entry->fmt_uuid, entry->fmt_name);
  4706. }
  4707. spin_unlock(&pfm_buffer_fmt_lock);
  4708. }
  4709. static int
  4710. pfm_proc_show(struct seq_file *m, void *v)
  4711. {
  4712. unsigned long psr;
  4713. unsigned int i;
  4714. int cpu;
  4715. if (v == PFM_PROC_SHOW_HEADER) {
  4716. pfm_proc_show_header(m);
  4717. return 0;
  4718. }
  4719. /* show info for CPU (v - 1) */
  4720. cpu = (long)v - 1;
  4721. seq_printf(m,
  4722. "CPU%-2d overflow intrs : %lu\n"
  4723. "CPU%-2d overflow cycles : %lu\n"
  4724. "CPU%-2d overflow min : %lu\n"
  4725. "CPU%-2d overflow max : %lu\n"
  4726. "CPU%-2d smpl handler calls : %lu\n"
  4727. "CPU%-2d smpl handler cycles : %lu\n"
  4728. "CPU%-2d spurious intrs : %lu\n"
  4729. "CPU%-2d replay intrs : %lu\n"
  4730. "CPU%-2d syst_wide : %d\n"
  4731. "CPU%-2d dcr_pp : %d\n"
  4732. "CPU%-2d exclude idle : %d\n"
  4733. "CPU%-2d owner : %d\n"
  4734. "CPU%-2d context : %p\n"
  4735. "CPU%-2d activations : %lu\n",
  4736. cpu, pfm_stats[cpu].pfm_ovfl_intr_count,
  4737. cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles,
  4738. cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_min,
  4739. cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_max,
  4740. cpu, pfm_stats[cpu].pfm_smpl_handler_calls,
  4741. cpu, pfm_stats[cpu].pfm_smpl_handler_cycles,
  4742. cpu, pfm_stats[cpu].pfm_spurious_ovfl_intr_count,
  4743. cpu, pfm_stats[cpu].pfm_replay_ovfl_intr_count,
  4744. cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_SYST_WIDE ? 1 : 0,
  4745. cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_DCR_PP ? 1 : 0,
  4746. cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_EXCL_IDLE ? 1 : 0,
  4747. cpu, pfm_get_cpu_data(pmu_owner, cpu) ? pfm_get_cpu_data(pmu_owner, cpu)->pid: -1,
  4748. cpu, pfm_get_cpu_data(pmu_ctx, cpu),
  4749. cpu, pfm_get_cpu_data(pmu_activation_number, cpu));
  4750. if (num_online_cpus() == 1 && pfm_sysctl.debug > 0) {
  4751. psr = pfm_get_psr();
  4752. ia64_srlz_d();
  4753. seq_printf(m,
  4754. "CPU%-2d psr : 0x%lx\n"
  4755. "CPU%-2d pmc0 : 0x%lx\n",
  4756. cpu, psr,
  4757. cpu, ia64_get_pmc(0));
  4758. for (i=0; PMC_IS_LAST(i) == 0; i++) {
  4759. if (PMC_IS_COUNTING(i) == 0) continue;
  4760. seq_printf(m,
  4761. "CPU%-2d pmc%u : 0x%lx\n"
  4762. "CPU%-2d pmd%u : 0x%lx\n",
  4763. cpu, i, ia64_get_pmc(i),
  4764. cpu, i, ia64_get_pmd(i));
  4765. }
  4766. }
  4767. return 0;
  4768. }
  4769. const struct seq_operations pfm_seq_ops = {
  4770. .start = pfm_proc_start,
  4771. .next = pfm_proc_next,
  4772. .stop = pfm_proc_stop,
  4773. .show = pfm_proc_show
  4774. };
  4775. /*
  4776. * we come here as soon as local_cpu_data->pfm_syst_wide is set. this happens
  4777. * during pfm_enable() hence before pfm_start(). We cannot assume monitoring
  4778. * is active or inactive based on mode. We must rely on the value in
  4779. * local_cpu_data->pfm_syst_info
  4780. */
  4781. void
  4782. pfm_syst_wide_update_task(struct task_struct *task, unsigned long info, int is_ctxswin)
  4783. {
  4784. struct pt_regs *regs;
  4785. unsigned long dcr;
  4786. unsigned long dcr_pp;
  4787. dcr_pp = info & PFM_CPUINFO_DCR_PP ? 1 : 0;
  4788. /*
  4789. * pid 0 is guaranteed to be the idle task. There is one such task with pid 0
  4790. * on every CPU, so we can rely on the pid to identify the idle task.
  4791. */
  4792. if ((info & PFM_CPUINFO_EXCL_IDLE) == 0 || task->pid) {
  4793. regs = task_pt_regs(task);
  4794. ia64_psr(regs)->pp = is_ctxswin ? dcr_pp : 0;
  4795. return;
  4796. }
  4797. /*
  4798. * if monitoring has started
  4799. */
  4800. if (dcr_pp) {
  4801. dcr = ia64_getreg(_IA64_REG_CR_DCR);
  4802. /*
  4803. * context switching in?
  4804. */
  4805. if (is_ctxswin) {
  4806. /* mask monitoring for the idle task */
  4807. ia64_setreg(_IA64_REG_CR_DCR, dcr & ~IA64_DCR_PP);
  4808. pfm_clear_psr_pp();
  4809. ia64_srlz_i();
  4810. return;
  4811. }
  4812. /*
  4813. * context switching out
  4814. * restore monitoring for next task
  4815. *
  4816. * Due to inlining this odd if-then-else construction generates
  4817. * better code.
  4818. */
  4819. ia64_setreg(_IA64_REG_CR_DCR, dcr |IA64_DCR_PP);
  4820. pfm_set_psr_pp();
  4821. ia64_srlz_i();
  4822. }
  4823. }
  4824. #ifdef CONFIG_SMP
  4825. static void
  4826. pfm_force_cleanup(pfm_context_t *ctx, struct pt_regs *regs)
  4827. {
  4828. struct task_struct *task = ctx->ctx_task;
  4829. ia64_psr(regs)->up = 0;
  4830. ia64_psr(regs)->sp = 1;
  4831. if (GET_PMU_OWNER() == task) {
  4832. DPRINT(("cleared ownership for [%d]\n",
  4833. task_pid_nr(ctx->ctx_task)));
  4834. SET_PMU_OWNER(NULL, NULL);
  4835. }
  4836. /*
  4837. * disconnect the task from the context and vice-versa
  4838. */
  4839. PFM_SET_WORK_PENDING(task, 0);
  4840. task->thread.pfm_context = NULL;
  4841. task->thread.flags &= ~IA64_THREAD_PM_VALID;
  4842. DPRINT(("force cleanup for [%d]\n", task_pid_nr(task)));
  4843. }
  4844. /*
  4845. * in 2.6, interrupts are masked when we come here and the runqueue lock is held
  4846. */
  4847. void
  4848. pfm_save_regs(struct task_struct *task)
  4849. {
  4850. pfm_context_t *ctx;
  4851. unsigned long flags;
  4852. u64 psr;
  4853. ctx = PFM_GET_CTX(task);
  4854. if (ctx == NULL) return;
  4855. /*
  4856. * we always come here with interrupts ALREADY disabled by
  4857. * the scheduler. So we simply need to protect against concurrent
  4858. * access, not CPU concurrency.
  4859. */
  4860. flags = pfm_protect_ctx_ctxsw(ctx);
  4861. if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
  4862. struct pt_regs *regs = task_pt_regs(task);
  4863. pfm_clear_psr_up();
  4864. pfm_force_cleanup(ctx, regs);
  4865. BUG_ON(ctx->ctx_smpl_hdr);
  4866. pfm_unprotect_ctx_ctxsw(ctx, flags);
  4867. pfm_context_free(ctx);
  4868. return;
  4869. }
  4870. /*
  4871. * save current PSR: needed because we modify it
  4872. */
  4873. ia64_srlz_d();
  4874. psr = pfm_get_psr();
  4875. BUG_ON(psr & (IA64_PSR_I));
  4876. /*
  4877. * stop monitoring:
  4878. * This is the last instruction which may generate an overflow
  4879. *
  4880. * We do not need to set psr.sp because, it is irrelevant in kernel.
  4881. * It will be restored from ipsr when going back to user level
  4882. */
  4883. pfm_clear_psr_up();
  4884. /*
  4885. * keep a copy of psr.up (for reload)
  4886. */
  4887. ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
  4888. /*
  4889. * release ownership of this PMU.
  4890. * PM interrupts are masked, so nothing
  4891. * can happen.
  4892. */
  4893. SET_PMU_OWNER(NULL, NULL);
  4894. /*
  4895. * we systematically save the PMD as we have no
  4896. * guarantee we will be schedule at that same
  4897. * CPU again.
  4898. */
  4899. pfm_save_pmds(ctx->th_pmds, ctx->ctx_used_pmds[0]);
  4900. /*
  4901. * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
  4902. * we will need it on the restore path to check
  4903. * for pending overflow.
  4904. */
  4905. ctx->th_pmcs[0] = ia64_get_pmc(0);
  4906. /*
  4907. * unfreeze PMU if had pending overflows
  4908. */
  4909. if (ctx->th_pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
  4910. /*
  4911. * finally, allow context access.
  4912. * interrupts will still be masked after this call.
  4913. */
  4914. pfm_unprotect_ctx_ctxsw(ctx, flags);
  4915. }
  4916. #else /* !CONFIG_SMP */
  4917. void
  4918. pfm_save_regs(struct task_struct *task)
  4919. {
  4920. pfm_context_t *ctx;
  4921. u64 psr;
  4922. ctx = PFM_GET_CTX(task);
  4923. if (ctx == NULL) return;
  4924. /*
  4925. * save current PSR: needed because we modify it
  4926. */
  4927. psr = pfm_get_psr();
  4928. BUG_ON(psr & (IA64_PSR_I));
  4929. /*
  4930. * stop monitoring:
  4931. * This is the last instruction which may generate an overflow
  4932. *
  4933. * We do not need to set psr.sp because, it is irrelevant in kernel.
  4934. * It will be restored from ipsr when going back to user level
  4935. */
  4936. pfm_clear_psr_up();
  4937. /*
  4938. * keep a copy of psr.up (for reload)
  4939. */
  4940. ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
  4941. }
  4942. static void
  4943. pfm_lazy_save_regs (struct task_struct *task)
  4944. {
  4945. pfm_context_t *ctx;
  4946. unsigned long flags;
  4947. { u64 psr = pfm_get_psr();
  4948. BUG_ON(psr & IA64_PSR_UP);
  4949. }
  4950. ctx = PFM_GET_CTX(task);
  4951. /*
  4952. * we need to mask PMU overflow here to
  4953. * make sure that we maintain pmc0 until
  4954. * we save it. overflow interrupts are
  4955. * treated as spurious if there is no
  4956. * owner.
  4957. *
  4958. * XXX: I don't think this is necessary
  4959. */
  4960. PROTECT_CTX(ctx,flags);
  4961. /*
  4962. * release ownership of this PMU.
  4963. * must be done before we save the registers.
  4964. *
  4965. * after this call any PMU interrupt is treated
  4966. * as spurious.
  4967. */
  4968. SET_PMU_OWNER(NULL, NULL);
  4969. /*
  4970. * save all the pmds we use
  4971. */
  4972. pfm_save_pmds(ctx->th_pmds, ctx->ctx_used_pmds[0]);
  4973. /*
  4974. * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
  4975. * it is needed to check for pended overflow
  4976. * on the restore path
  4977. */
  4978. ctx->th_pmcs[0] = ia64_get_pmc(0);
  4979. /*
  4980. * unfreeze PMU if had pending overflows
  4981. */
  4982. if (ctx->th_pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
  4983. /*
  4984. * now get can unmask PMU interrupts, they will
  4985. * be treated as purely spurious and we will not
  4986. * lose any information
  4987. */
  4988. UNPROTECT_CTX(ctx,flags);
  4989. }
  4990. #endif /* CONFIG_SMP */
  4991. #ifdef CONFIG_SMP
  4992. /*
  4993. * in 2.6, interrupts are masked when we come here and the runqueue lock is held
  4994. */
  4995. void
  4996. pfm_load_regs (struct task_struct *task)
  4997. {
  4998. pfm_context_t *ctx;
  4999. unsigned long pmc_mask = 0UL, pmd_mask = 0UL;
  5000. unsigned long flags;
  5001. u64 psr, psr_up;
  5002. int need_irq_resend;
  5003. ctx = PFM_GET_CTX(task);
  5004. if (unlikely(ctx == NULL)) return;
  5005. BUG_ON(GET_PMU_OWNER());
  5006. /*
  5007. * possible on unload
  5008. */
  5009. if (unlikely((task->thread.flags & IA64_THREAD_PM_VALID) == 0)) return;
  5010. /*
  5011. * we always come here with interrupts ALREADY disabled by
  5012. * the scheduler. So we simply need to protect against concurrent
  5013. * access, not CPU concurrency.
  5014. */
  5015. flags = pfm_protect_ctx_ctxsw(ctx);
  5016. psr = pfm_get_psr();
  5017. need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
  5018. BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
  5019. BUG_ON(psr & IA64_PSR_I);
  5020. if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) {
  5021. struct pt_regs *regs = task_pt_regs(task);
  5022. BUG_ON(ctx->ctx_smpl_hdr);
  5023. pfm_force_cleanup(ctx, regs);
  5024. pfm_unprotect_ctx_ctxsw(ctx, flags);
  5025. /*
  5026. * this one (kmalloc'ed) is fine with interrupts disabled
  5027. */
  5028. pfm_context_free(ctx);
  5029. return;
  5030. }
  5031. /*
  5032. * we restore ALL the debug registers to avoid picking up
  5033. * stale state.
  5034. */
  5035. if (ctx->ctx_fl_using_dbreg) {
  5036. pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
  5037. pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
  5038. }
  5039. /*
  5040. * retrieve saved psr.up
  5041. */
  5042. psr_up = ctx->ctx_saved_psr_up;
  5043. /*
  5044. * if we were the last user of the PMU on that CPU,
  5045. * then nothing to do except restore psr
  5046. */
  5047. if (GET_LAST_CPU(ctx) == smp_processor_id() && ctx->ctx_last_activation == GET_ACTIVATION()) {
  5048. /*
  5049. * retrieve partial reload masks (due to user modifications)
  5050. */
  5051. pmc_mask = ctx->ctx_reload_pmcs[0];
  5052. pmd_mask = ctx->ctx_reload_pmds[0];
  5053. } else {
  5054. /*
  5055. * To avoid leaking information to the user level when psr.sp=0,
  5056. * we must reload ALL implemented pmds (even the ones we don't use).
  5057. * In the kernel we only allow PFM_READ_PMDS on registers which
  5058. * we initialized or requested (sampling) so there is no risk there.
  5059. */
  5060. pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
  5061. /*
  5062. * ALL accessible PMCs are systematically reloaded, unused registers
  5063. * get their default (from pfm_reset_pmu_state()) values to avoid picking
  5064. * up stale configuration.
  5065. *
  5066. * PMC0 is never in the mask. It is always restored separately.
  5067. */
  5068. pmc_mask = ctx->ctx_all_pmcs[0];
  5069. }
  5070. /*
  5071. * when context is MASKED, we will restore PMC with plm=0
  5072. * and PMD with stale information, but that's ok, nothing
  5073. * will be captured.
  5074. *
  5075. * XXX: optimize here
  5076. */
  5077. if (pmd_mask) pfm_restore_pmds(ctx->th_pmds, pmd_mask);
  5078. if (pmc_mask) pfm_restore_pmcs(ctx->th_pmcs, pmc_mask);
  5079. /*
  5080. * check for pending overflow at the time the state
  5081. * was saved.
  5082. */
  5083. if (unlikely(PMC0_HAS_OVFL(ctx->th_pmcs[0]))) {
  5084. /*
  5085. * reload pmc0 with the overflow information
  5086. * On McKinley PMU, this will trigger a PMU interrupt
  5087. */
  5088. ia64_set_pmc(0, ctx->th_pmcs[0]);
  5089. ia64_srlz_d();
  5090. ctx->th_pmcs[0] = 0UL;
  5091. /*
  5092. * will replay the PMU interrupt
  5093. */
  5094. if (need_irq_resend) ia64_resend_irq(IA64_PERFMON_VECTOR);
  5095. pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
  5096. }
  5097. /*
  5098. * we just did a reload, so we reset the partial reload fields
  5099. */
  5100. ctx->ctx_reload_pmcs[0] = 0UL;
  5101. ctx->ctx_reload_pmds[0] = 0UL;
  5102. SET_LAST_CPU(ctx, smp_processor_id());
  5103. /*
  5104. * dump activation value for this PMU
  5105. */
  5106. INC_ACTIVATION();
  5107. /*
  5108. * record current activation for this context
  5109. */
  5110. SET_ACTIVATION(ctx);
  5111. /*
  5112. * establish new ownership.
  5113. */
  5114. SET_PMU_OWNER(task, ctx);
  5115. /*
  5116. * restore the psr.up bit. measurement
  5117. * is active again.
  5118. * no PMU interrupt can happen at this point
  5119. * because we still have interrupts disabled.
  5120. */
  5121. if (likely(psr_up)) pfm_set_psr_up();
  5122. /*
  5123. * allow concurrent access to context
  5124. */
  5125. pfm_unprotect_ctx_ctxsw(ctx, flags);
  5126. }
  5127. #else /* !CONFIG_SMP */
  5128. /*
  5129. * reload PMU state for UP kernels
  5130. * in 2.5 we come here with interrupts disabled
  5131. */
  5132. void
  5133. pfm_load_regs (struct task_struct *task)
  5134. {
  5135. pfm_context_t *ctx;
  5136. struct task_struct *owner;
  5137. unsigned long pmd_mask, pmc_mask;
  5138. u64 psr, psr_up;
  5139. int need_irq_resend;
  5140. owner = GET_PMU_OWNER();
  5141. ctx = PFM_GET_CTX(task);
  5142. psr = pfm_get_psr();
  5143. BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
  5144. BUG_ON(psr & IA64_PSR_I);
  5145. /*
  5146. * we restore ALL the debug registers to avoid picking up
  5147. * stale state.
  5148. *
  5149. * This must be done even when the task is still the owner
  5150. * as the registers may have been modified via ptrace()
  5151. * (not perfmon) by the previous task.
  5152. */
  5153. if (ctx->ctx_fl_using_dbreg) {
  5154. pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
  5155. pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
  5156. }
  5157. /*
  5158. * retrieved saved psr.up
  5159. */
  5160. psr_up = ctx->ctx_saved_psr_up;
  5161. need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
  5162. /*
  5163. * short path, our state is still there, just
  5164. * need to restore psr and we go
  5165. *
  5166. * we do not touch either PMC nor PMD. the psr is not touched
  5167. * by the overflow_handler. So we are safe w.r.t. to interrupt
  5168. * concurrency even without interrupt masking.
  5169. */
  5170. if (likely(owner == task)) {
  5171. if (likely(psr_up)) pfm_set_psr_up();
  5172. return;
  5173. }
  5174. /*
  5175. * someone else is still using the PMU, first push it out and
  5176. * then we'll be able to install our stuff !
  5177. *
  5178. * Upon return, there will be no owner for the current PMU
  5179. */
  5180. if (owner) pfm_lazy_save_regs(owner);
  5181. /*
  5182. * To avoid leaking information to the user level when psr.sp=0,
  5183. * we must reload ALL implemented pmds (even the ones we don't use).
  5184. * In the kernel we only allow PFM_READ_PMDS on registers which
  5185. * we initialized or requested (sampling) so there is no risk there.
  5186. */
  5187. pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
  5188. /*
  5189. * ALL accessible PMCs are systematically reloaded, unused registers
  5190. * get their default (from pfm_reset_pmu_state()) values to avoid picking
  5191. * up stale configuration.
  5192. *
  5193. * PMC0 is never in the mask. It is always restored separately
  5194. */
  5195. pmc_mask = ctx->ctx_all_pmcs[0];
  5196. pfm_restore_pmds(ctx->th_pmds, pmd_mask);
  5197. pfm_restore_pmcs(ctx->th_pmcs, pmc_mask);
  5198. /*
  5199. * check for pending overflow at the time the state
  5200. * was saved.
  5201. */
  5202. if (unlikely(PMC0_HAS_OVFL(ctx->th_pmcs[0]))) {
  5203. /*
  5204. * reload pmc0 with the overflow information
  5205. * On McKinley PMU, this will trigger a PMU interrupt
  5206. */
  5207. ia64_set_pmc(0, ctx->th_pmcs[0]);
  5208. ia64_srlz_d();
  5209. ctx->th_pmcs[0] = 0UL;
  5210. /*
  5211. * will replay the PMU interrupt
  5212. */
  5213. if (need_irq_resend) ia64_resend_irq(IA64_PERFMON_VECTOR);
  5214. pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
  5215. }
  5216. /*
  5217. * establish new ownership.
  5218. */
  5219. SET_PMU_OWNER(task, ctx);
  5220. /*
  5221. * restore the psr.up bit. measurement
  5222. * is active again.
  5223. * no PMU interrupt can happen at this point
  5224. * because we still have interrupts disabled.
  5225. */
  5226. if (likely(psr_up)) pfm_set_psr_up();
  5227. }
  5228. #endif /* CONFIG_SMP */
  5229. /*
  5230. * this function assumes monitoring is stopped
  5231. */
  5232. static void
  5233. pfm_flush_pmds(struct task_struct *task, pfm_context_t *ctx)
  5234. {
  5235. u64 pmc0;
  5236. unsigned long mask2, val, pmd_val, ovfl_val;
  5237. int i, can_access_pmu = 0;
  5238. int is_self;
  5239. /*
  5240. * is the caller the task being monitored (or which initiated the
  5241. * session for system wide measurements)
  5242. */
  5243. is_self = ctx->ctx_task == task ? 1 : 0;
  5244. /*
  5245. * can access PMU is task is the owner of the PMU state on the current CPU
  5246. * or if we are running on the CPU bound to the context in system-wide mode
  5247. * (that is not necessarily the task the context is attached to in this mode).
  5248. * In system-wide we always have can_access_pmu true because a task running on an
  5249. * invalid processor is flagged earlier in the call stack (see pfm_stop).
  5250. */
  5251. can_access_pmu = (GET_PMU_OWNER() == task) || (ctx->ctx_fl_system && ctx->ctx_cpu == smp_processor_id());
  5252. if (can_access_pmu) {
  5253. /*
  5254. * Mark the PMU as not owned
  5255. * This will cause the interrupt handler to do nothing in case an overflow
  5256. * interrupt was in-flight
  5257. * This also guarantees that pmc0 will contain the final state
  5258. * It virtually gives us full control on overflow processing from that point
  5259. * on.
  5260. */
  5261. SET_PMU_OWNER(NULL, NULL);
  5262. DPRINT(("releasing ownership\n"));
  5263. /*
  5264. * read current overflow status:
  5265. *
  5266. * we are guaranteed to read the final stable state
  5267. */
  5268. ia64_srlz_d();
  5269. pmc0 = ia64_get_pmc(0); /* slow */
  5270. /*
  5271. * reset freeze bit, overflow status information destroyed
  5272. */
  5273. pfm_unfreeze_pmu();
  5274. } else {
  5275. pmc0 = ctx->th_pmcs[0];
  5276. /*
  5277. * clear whatever overflow status bits there were
  5278. */
  5279. ctx->th_pmcs[0] = 0;
  5280. }
  5281. ovfl_val = pmu_conf->ovfl_val;
  5282. /*
  5283. * we save all the used pmds
  5284. * we take care of overflows for counting PMDs
  5285. *
  5286. * XXX: sampling situation is not taken into account here
  5287. */
  5288. mask2 = ctx->ctx_used_pmds[0];
  5289. DPRINT(("is_self=%d ovfl_val=0x%lx mask2=0x%lx\n", is_self, ovfl_val, mask2));
  5290. for (i = 0; mask2; i++, mask2>>=1) {
  5291. /* skip non used pmds */
  5292. if ((mask2 & 0x1) == 0) continue;
  5293. /*
  5294. * can access PMU always true in system wide mode
  5295. */
  5296. val = pmd_val = can_access_pmu ? ia64_get_pmd(i) : ctx->th_pmds[i];
  5297. if (PMD_IS_COUNTING(i)) {
  5298. DPRINT(("[%d] pmd[%d] ctx_pmd=0x%lx hw_pmd=0x%lx\n",
  5299. task_pid_nr(task),
  5300. i,
  5301. ctx->ctx_pmds[i].val,
  5302. val & ovfl_val));
  5303. /*
  5304. * we rebuild the full 64 bit value of the counter
  5305. */
  5306. val = ctx->ctx_pmds[i].val + (val & ovfl_val);
  5307. /*
  5308. * now everything is in ctx_pmds[] and we need
  5309. * to clear the saved context from save_regs() such that
  5310. * pfm_read_pmds() gets the correct value
  5311. */
  5312. pmd_val = 0UL;
  5313. /*
  5314. * take care of overflow inline
  5315. */
  5316. if (pmc0 & (1UL << i)) {
  5317. val += 1 + ovfl_val;
  5318. DPRINT(("[%d] pmd[%d] overflowed\n", task_pid_nr(task), i));
  5319. }
  5320. }
  5321. DPRINT(("[%d] ctx_pmd[%d]=0x%lx pmd_val=0x%lx\n", task_pid_nr(task), i, val, pmd_val));
  5322. if (is_self) ctx->th_pmds[i] = pmd_val;
  5323. ctx->ctx_pmds[i].val = val;
  5324. }
  5325. }
  5326. static struct irqaction perfmon_irqaction = {
  5327. .handler = pfm_interrupt_handler,
  5328. .name = "perfmon"
  5329. };
  5330. static void
  5331. pfm_alt_save_pmu_state(void *data)
  5332. {
  5333. struct pt_regs *regs;
  5334. regs = task_pt_regs(current);
  5335. DPRINT(("called\n"));
  5336. /*
  5337. * should not be necessary but
  5338. * let's take not risk
  5339. */
  5340. pfm_clear_psr_up();
  5341. pfm_clear_psr_pp();
  5342. ia64_psr(regs)->pp = 0;
  5343. /*
  5344. * This call is required
  5345. * May cause a spurious interrupt on some processors
  5346. */
  5347. pfm_freeze_pmu();
  5348. ia64_srlz_d();
  5349. }
  5350. void
  5351. pfm_alt_restore_pmu_state(void *data)
  5352. {
  5353. struct pt_regs *regs;
  5354. regs = task_pt_regs(current);
  5355. DPRINT(("called\n"));
  5356. /*
  5357. * put PMU back in state expected
  5358. * by perfmon
  5359. */
  5360. pfm_clear_psr_up();
  5361. pfm_clear_psr_pp();
  5362. ia64_psr(regs)->pp = 0;
  5363. /*
  5364. * perfmon runs with PMU unfrozen at all times
  5365. */
  5366. pfm_unfreeze_pmu();
  5367. ia64_srlz_d();
  5368. }
  5369. int
  5370. pfm_install_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
  5371. {
  5372. int ret, i;
  5373. int reserve_cpu;
  5374. /* some sanity checks */
  5375. if (hdl == NULL || hdl->handler == NULL) return -EINVAL;
  5376. /* do the easy test first */
  5377. if (pfm_alt_intr_handler) return -EBUSY;
  5378. /* one at a time in the install or remove, just fail the others */
  5379. if (!spin_trylock(&pfm_alt_install_check)) {
  5380. return -EBUSY;
  5381. }
  5382. /* reserve our session */
  5383. for_each_online_cpu(reserve_cpu) {
  5384. ret = pfm_reserve_session(NULL, 1, reserve_cpu);
  5385. if (ret) goto cleanup_reserve;
  5386. }
  5387. /* save the current system wide pmu states */
  5388. ret = on_each_cpu(pfm_alt_save_pmu_state, NULL, 1);
  5389. if (ret) {
  5390. DPRINT(("on_each_cpu() failed: %d\n", ret));
  5391. goto cleanup_reserve;
  5392. }
  5393. /* officially change to the alternate interrupt handler */
  5394. pfm_alt_intr_handler = hdl;
  5395. spin_unlock(&pfm_alt_install_check);
  5396. return 0;
  5397. cleanup_reserve:
  5398. for_each_online_cpu(i) {
  5399. /* don't unreserve more than we reserved */
  5400. if (i >= reserve_cpu) break;
  5401. pfm_unreserve_session(NULL, 1, i);
  5402. }
  5403. spin_unlock(&pfm_alt_install_check);
  5404. return ret;
  5405. }
  5406. EXPORT_SYMBOL_GPL(pfm_install_alt_pmu_interrupt);
  5407. int
  5408. pfm_remove_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
  5409. {
  5410. int i;
  5411. int ret;
  5412. if (hdl == NULL) return -EINVAL;
  5413. /* cannot remove someone else's handler! */
  5414. if (pfm_alt_intr_handler != hdl) return -EINVAL;
  5415. /* one at a time in the install or remove, just fail the others */
  5416. if (!spin_trylock(&pfm_alt_install_check)) {
  5417. return -EBUSY;
  5418. }
  5419. pfm_alt_intr_handler = NULL;
  5420. ret = on_each_cpu(pfm_alt_restore_pmu_state, NULL, 1);
  5421. if (ret) {
  5422. DPRINT(("on_each_cpu() failed: %d\n", ret));
  5423. }
  5424. for_each_online_cpu(i) {
  5425. pfm_unreserve_session(NULL, 1, i);
  5426. }
  5427. spin_unlock(&pfm_alt_install_check);
  5428. return 0;
  5429. }
  5430. EXPORT_SYMBOL_GPL(pfm_remove_alt_pmu_interrupt);
  5431. /*
  5432. * perfmon initialization routine, called from the initcall() table
  5433. */
  5434. static int init_pfm_fs(void);
  5435. static int __init
  5436. pfm_probe_pmu(void)
  5437. {
  5438. pmu_config_t **p;
  5439. int family;
  5440. family = local_cpu_data->family;
  5441. p = pmu_confs;
  5442. while(*p) {
  5443. if ((*p)->probe) {
  5444. if ((*p)->probe() == 0) goto found;
  5445. } else if ((*p)->pmu_family == family || (*p)->pmu_family == 0xff) {
  5446. goto found;
  5447. }
  5448. p++;
  5449. }
  5450. return -1;
  5451. found:
  5452. pmu_conf = *p;
  5453. return 0;
  5454. }
  5455. int __init
  5456. pfm_init(void)
  5457. {
  5458. unsigned int n, n_counters, i;
  5459. printk("perfmon: version %u.%u IRQ %u\n",
  5460. PFM_VERSION_MAJ,
  5461. PFM_VERSION_MIN,
  5462. IA64_PERFMON_VECTOR);
  5463. if (pfm_probe_pmu()) {
  5464. printk(KERN_INFO "perfmon: disabled, there is no support for processor family %d\n",
  5465. local_cpu_data->family);
  5466. return -ENODEV;
  5467. }
  5468. /*
  5469. * compute the number of implemented PMD/PMC from the
  5470. * description tables
  5471. */
  5472. n = 0;
  5473. for (i=0; PMC_IS_LAST(i) == 0; i++) {
  5474. if (PMC_IS_IMPL(i) == 0) continue;
  5475. pmu_conf->impl_pmcs[i>>6] |= 1UL << (i&63);
  5476. n++;
  5477. }
  5478. pmu_conf->num_pmcs = n;
  5479. n = 0; n_counters = 0;
  5480. for (i=0; PMD_IS_LAST(i) == 0; i++) {
  5481. if (PMD_IS_IMPL(i) == 0) continue;
  5482. pmu_conf->impl_pmds[i>>6] |= 1UL << (i&63);
  5483. n++;
  5484. if (PMD_IS_COUNTING(i)) n_counters++;
  5485. }
  5486. pmu_conf->num_pmds = n;
  5487. pmu_conf->num_counters = n_counters;
  5488. /*
  5489. * sanity checks on the number of debug registers
  5490. */
  5491. if (pmu_conf->use_rr_dbregs) {
  5492. if (pmu_conf->num_ibrs > IA64_NUM_DBG_REGS) {
  5493. printk(KERN_INFO "perfmon: unsupported number of code debug registers (%u)\n", pmu_conf->num_ibrs);
  5494. pmu_conf = NULL;
  5495. return -1;
  5496. }
  5497. if (pmu_conf->num_dbrs > IA64_NUM_DBG_REGS) {
  5498. printk(KERN_INFO "perfmon: unsupported number of data debug registers (%u)\n", pmu_conf->num_ibrs);
  5499. pmu_conf = NULL;
  5500. return -1;
  5501. }
  5502. }
  5503. printk("perfmon: %s PMU detected, %u PMCs, %u PMDs, %u counters (%lu bits)\n",
  5504. pmu_conf->pmu_name,
  5505. pmu_conf->num_pmcs,
  5506. pmu_conf->num_pmds,
  5507. pmu_conf->num_counters,
  5508. ffz(pmu_conf->ovfl_val));
  5509. /* sanity check */
  5510. if (pmu_conf->num_pmds >= PFM_NUM_PMD_REGS || pmu_conf->num_pmcs >= PFM_NUM_PMC_REGS) {
  5511. printk(KERN_ERR "perfmon: not enough pmc/pmd, perfmon disabled\n");
  5512. pmu_conf = NULL;
  5513. return -1;
  5514. }
  5515. /*
  5516. * create /proc/perfmon (mostly for debugging purposes)
  5517. */
  5518. perfmon_dir = proc_create_seq("perfmon", S_IRUGO, NULL, &pfm_seq_ops);
  5519. if (perfmon_dir == NULL) {
  5520. printk(KERN_ERR "perfmon: cannot create /proc entry, perfmon disabled\n");
  5521. pmu_conf = NULL;
  5522. return -1;
  5523. }
  5524. /*
  5525. * create /proc/sys/kernel/perfmon (for debugging purposes)
  5526. */
  5527. pfm_sysctl_header = register_sysctl_table(pfm_sysctl_root);
  5528. /*
  5529. * initialize all our spinlocks
  5530. */
  5531. spin_lock_init(&pfm_sessions.pfs_lock);
  5532. spin_lock_init(&pfm_buffer_fmt_lock);
  5533. init_pfm_fs();
  5534. for(i=0; i < NR_CPUS; i++) pfm_stats[i].pfm_ovfl_intr_cycles_min = ~0UL;
  5535. return 0;
  5536. }
  5537. __initcall(pfm_init);
  5538. /*
  5539. * this function is called before pfm_init()
  5540. */
  5541. void
  5542. pfm_init_percpu (void)
  5543. {
  5544. static int first_time=1;
  5545. /*
  5546. * make sure no measurement is active
  5547. * (may inherit programmed PMCs from EFI).
  5548. */
  5549. pfm_clear_psr_pp();
  5550. pfm_clear_psr_up();
  5551. /*
  5552. * we run with the PMU not frozen at all times
  5553. */
  5554. pfm_unfreeze_pmu();
  5555. if (first_time) {
  5556. register_percpu_irq(IA64_PERFMON_VECTOR, &perfmon_irqaction);
  5557. first_time=0;
  5558. }
  5559. ia64_setreg(_IA64_REG_CR_PMV, IA64_PERFMON_VECTOR);
  5560. ia64_srlz_d();
  5561. }
  5562. /*
  5563. * used for debug purposes only
  5564. */
  5565. void
  5566. dump_pmu_state(const char *from)
  5567. {
  5568. struct task_struct *task;
  5569. struct pt_regs *regs;
  5570. pfm_context_t *ctx;
  5571. unsigned long psr, dcr, info, flags;
  5572. int i, this_cpu;
  5573. local_irq_save(flags);
  5574. this_cpu = smp_processor_id();
  5575. regs = task_pt_regs(current);
  5576. info = PFM_CPUINFO_GET();
  5577. dcr = ia64_getreg(_IA64_REG_CR_DCR);
  5578. if (info == 0 && ia64_psr(regs)->pp == 0 && (dcr & IA64_DCR_PP) == 0) {
  5579. local_irq_restore(flags);
  5580. return;
  5581. }
  5582. printk("CPU%d from %s() current [%d] iip=0x%lx %s\n",
  5583. this_cpu,
  5584. from,
  5585. task_pid_nr(current),
  5586. regs->cr_iip,
  5587. current->comm);
  5588. task = GET_PMU_OWNER();
  5589. ctx = GET_PMU_CTX();
  5590. printk("->CPU%d owner [%d] ctx=%p\n", this_cpu, task ? task_pid_nr(task) : -1, ctx);
  5591. psr = pfm_get_psr();
  5592. printk("->CPU%d pmc0=0x%lx psr.pp=%d psr.up=%d dcr.pp=%d syst_info=0x%lx user_psr.up=%d user_psr.pp=%d\n",
  5593. this_cpu,
  5594. ia64_get_pmc(0),
  5595. psr & IA64_PSR_PP ? 1 : 0,
  5596. psr & IA64_PSR_UP ? 1 : 0,
  5597. dcr & IA64_DCR_PP ? 1 : 0,
  5598. info,
  5599. ia64_psr(regs)->up,
  5600. ia64_psr(regs)->pp);
  5601. ia64_psr(regs)->up = 0;
  5602. ia64_psr(regs)->pp = 0;
  5603. for (i=1; PMC_IS_LAST(i) == 0; i++) {
  5604. if (PMC_IS_IMPL(i) == 0) continue;
  5605. printk("->CPU%d pmc[%d]=0x%lx thread_pmc[%d]=0x%lx\n", this_cpu, i, ia64_get_pmc(i), i, ctx->th_pmcs[i]);
  5606. }
  5607. for (i=1; PMD_IS_LAST(i) == 0; i++) {
  5608. if (PMD_IS_IMPL(i) == 0) continue;
  5609. printk("->CPU%d pmd[%d]=0x%lx thread_pmd[%d]=0x%lx\n", this_cpu, i, ia64_get_pmd(i), i, ctx->th_pmds[i]);
  5610. }
  5611. if (ctx) {
  5612. printk("->CPU%d ctx_state=%d vaddr=%p addr=%p fd=%d ctx_task=[%d] saved_psr_up=0x%lx\n",
  5613. this_cpu,
  5614. ctx->ctx_state,
  5615. ctx->ctx_smpl_vaddr,
  5616. ctx->ctx_smpl_hdr,
  5617. ctx->ctx_msgq_head,
  5618. ctx->ctx_msgq_tail,
  5619. ctx->ctx_saved_psr_up);
  5620. }
  5621. local_irq_restore(flags);
  5622. }
  5623. /*
  5624. * called from process.c:copy_thread(). task is new child.
  5625. */
  5626. void
  5627. pfm_inherit(struct task_struct *task, struct pt_regs *regs)
  5628. {
  5629. struct thread_struct *thread;
  5630. DPRINT(("perfmon: pfm_inherit clearing state for [%d]\n", task_pid_nr(task)));
  5631. thread = &task->thread;
  5632. /*
  5633. * cut links inherited from parent (current)
  5634. */
  5635. thread->pfm_context = NULL;
  5636. PFM_SET_WORK_PENDING(task, 0);
  5637. /*
  5638. * the psr bits are already set properly in copy_threads()
  5639. */
  5640. }
  5641. #else /* !CONFIG_PERFMON */
  5642. asmlinkage long
  5643. sys_perfmonctl (int fd, int cmd, void *arg, int count)
  5644. {
  5645. return -ENOSYS;
  5646. }
  5647. #endif /* CONFIG_PERFMON */