perfmon.c 169 KB

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