perfmon.c 167 KB

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