perfmon.c 169 KB

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