perfmon.c 169 KB

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