perfmon.c 168 KB

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