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