perfmon.c 169 KB

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