perfmon.c 169 KB

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