perfmon.c 166 KB

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