perfmon.c 169 KB

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