perfmon.c 168 KB

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